python 井字棋(Tic Tac Toe)
说明
用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意。另外,90%+的代码也是本人逐字逐句敲的。
minimax算法还没完全理解,所以参考了这里的代码,并作了修改。
特点
- 可以选择人人、人机、机人、机机四种对战模式之一
- 电脑玩家的AI使用了minimax算法,带apha-beta剪枝
- 电脑玩家在思考时,时时刻刻都有一个“假想敌”。以便使得minimax算法运转起来
代码
作者:hhh5460
时间:2017年6月26日
# 棋盘
class Board(object):
def __init__(self):
#self._board = '-'*9 # 坑!!
self._board = ['-' for _ in range(9)]
self._history = [] # 棋谱
# 按指定动作,放入棋子
def _move(self, action, take):
if self._board[action] == '-':
self._board[action] = take
self._history.append((action, take)) # 加入棋谱
# 撤销动作,拿走棋子
def _unmove(self, action):
self._board[action] = '-'
self._history.pop()
# 棋盘快照
def get_board_snapshot(self):
return self._board[:]
# 取棋盘上的合法走法
def get_legal_actions(self):
actions = []
for i in range(9):
if self._board[i] == '-':
actions.append(i)
return actions
# 判断走法是否合法
def is_legal_action(self, action):
return self._board[action] == '-'
# 终止检测
def teminate(self):
board = self._board
lines = [board[0:3], board[3:6], board[6:9], board[0::3], board[1::3], board[2::3], board[0::4], board[2:7:2]]
if ['X']*3 in lines or ['O']*3 in lines or '-' not in board:
return True
else:
return False
# 胜负检查
def get_winner(self):
board = self._board
lines = [board[0:3], board[3:6], board[6:9], board[0::3], board[1::3], board[2::3], board[0::4], board[2:7:2]]
if ['X']*3 in lines:
return 0
elif ['O']*3 in lines:
return 1
else:
return 2
# 打印棋盘
def print_b(self):
board = self._board
for i in range(len(board)):
print(board[i], end='')
if (i+1)%3 == 0:
print()
# 打印棋谱
def print_history(self):
print(self._history)
# 玩家
class Player(object):
'''
玩家只做两件事:思考、落子
1. 思考 --> 得到走法
2. 落子 --> 执行走法,改变棋盘
'''
def __init__(self, take='X'): # 默认执的棋子为 take = 'X'
self.take=take
def think(self, board):
pass
def move(self, board, action):
board._move(action, self.take)
# 人类玩家
class HumanPlayer(Player):
def __init__(self, take):
super().__init__(take)
def think(self, board):
while True:
action = input('Please input a num in 0-8:')
if len(action)==1 and action in '012345678' and board.is_legal_action(int(action)):
return int(action)
# 电脑玩家
class AIPlayer(Player):
def __init__(self, take):
super().__init__(take)
def think(self, board):
print('AI is thinking ...')
take = ['X','O'][self.take=='X']
player = AIPlayer(take) # 假想敌!!!
_, action = self.minimax(board, player)
#print('OK')
return action
# 极大极小法搜索,α-β剪枝
def minimax(self, board, player, depth=0) :
'''参考:https://stackoverflow.com/questions/44089757/minimax-algorithm-for-tic-tac-toe-python'''
if self.take == "O":
bestVal = -10
else:
bestVal = 10
if board.teminate() :
if board.get_winner() == 0 :
return -10 + depth, None
elif board.get_winner() == 1 :
return 10 - depth, None
elif board.get_winner() == 2 :
return 0, None
for action in board.get_legal_actions() : # 遍历合法走法
board._move(action, self.take)
val, _ = player.minimax(board, self, depth+1) # 切换到 假想敌!!!
board._unmove(action) # 撤销走法,回溯
if self.take == "O" :
if val > bestVal:
bestVal, bestAction = val, action
else :
if val < bestVal:
bestVal, bestAction = val, action
return bestVal, bestAction
# 游戏
class Game(object):
def __init__(self):
self.board = Board()
self.current_player = None
# 生成玩家
def mk_player(self, p, take='X'): # p in [0,1]
if p==0:
return HumanPlayer(take)
else:
return AIPlayer(take)
# 切换玩家
def switch_player(self, player1, player2):
if self.current_player is None:
return player1
else:
return [player1, player2][self.current_player == player1]
# 打印赢家
def print_winner(self, winner): # winner in [0,1,2]
print(['Winner is player1','Winner is player2','Draw'][winner])
# 运行游戏
def run(self):
ps = input("Please select two player's type:\n\t0.Human\n\t1.AI\nSuch as:0 0\n")
p1, p2 = [int(p) for p in ps.split(' ')]
player1, player2 = self.mk_player(p1, 'X'), self.mk_player(p2, 'O') # 先手执X,后手执O
print('\nGame start!\n')
self.board.print_b() # 显示棋盘
while True:
self.current_player = self.switch_player(player1, player2) # 切换当前玩家
action = self.current_player.think(self.board) # 当前玩家对棋盘进行思考后,得到招法
self.current_player.move(self.board, action) # 当前玩家执行招法,改变棋盘
self.board.print_b() # 显示当前棋盘
if self.board.teminate(): # 根据当前棋盘,判断棋局是否终止
winner = self.board.get_winner() # 得到赢家 0,1,2
break
self.print_winner(winner)
print('Game over!')
self.board.print_history()
if __name__ == '__main__':
Game().run()
效果图
下图是人人对战的结果

python 井字棋(Tic Tac Toe)的更多相关文章
- [CareerCup] 17.2 Tic Tac Toe 井字棋游戏
17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...
- LeetCode 5275. 找出井字棋的获胜者 Find Winner on a Tic Tac Toe Game
地址 https://www.acwing.com/solution/LeetCode/content/6670/ 题目描述A 和 B 在一个 3 x 3 的网格上玩井字棋. 井字棋游戏的规则如下: ...
- Principle of Computing (Python)学习笔记(7) DFS Search + Tic Tac Toe use MiniMax Stratedy
1. Trees Tree is a recursive structure. 1.1 math nodes https://class.coursera.org/principlescomputin ...
- python 游戏(井字棋)
1. 游戏思路和流程图 实现功能,现实生活中的井字棋玩法 游戏流程图 2. 使用模块和游戏提示 import random def game_info(): print('欢迎来到井字棋游戏') pr ...
- POJ 2361 Tic Tac Toe
题目:给定一个3*3的矩阵,是一个井字过三关游戏.开始为X先走,问你这个是不是一个合法的游戏.也就是,现在这种情况,能不能出现.如果有人赢了,那应该立即停止.那么可以知道X的步数和O的步数应该满足x= ...
- [LeetCode] 348. Design Tic-Tac-Toe 设计井字棋游戏
Design a Tic-tac-toe game that is played between two players on a n x n grid. You may assume the fol ...
- 井字棋(Tic-Tac-Toe)
井字棋介绍:https://en.wikipedia.org/wiki/Tic-tac-toe 井字棋简单,但是获胜策略却和直觉不同,四角比中间重要性要高,而且先手有很大的获胜概率获胜(先手胜:91, ...
- python3 井字棋 GUI - 人机对战、机器对战 (threading、tkinter库)
python3 井字棋 GUI - 人机对战.机器对战 功能 GUI界面 人机对战(可选择机器先走) 机器对战(50局) 流程图 内核 棋盘 [0][1][2] [3][4][5] [6][7][8] ...
- [LeetCode] Design Tic-Tac-Toe 设计井字棋游戏
Design a Tic-tac-toe game that is played between two players on a n x n grid. You may assume the fol ...
随机推荐
- 【Redis】Redis学习(三) Redis 主从模式详解
不管任何程序,只运行一个实例都是不可靠的,一旦因为网络原因导致所在机器不可达,或者所在服务器挂掉,那么这个程序将不能对外提供服务了,Redis也是一样的.不过Redis的主从并不是解决这个问题的,一些 ...
- 前端构建工具Gulp使用总结
1.安装准备 1.1 Node.js安装 在安装Gulp之前首先的安装Node.js, 安装教程详见Node.js 安装配置 1.2 npm安装 在安装node的时候会自动安装npm模块管理器,详见n ...
- idea总是编译启动报错
使用多环境配置时候,总是会出现莫名其妙的启动报错.主要是没有多环境配置的参数,挺奇怪的,因为这个问题时现时不现.又没有什么具体规律,一直找不到原因.今天一个偶然的机会,发现会不会是这个原因?
- sh: ./bin/my_print_defaults: /lib/ld-linux.so.2: bad ELF interpreter: 没有那个文件或目录 FATAL ERROR: Neither host 'kvm' nor 'localhost' could be looked up with ./bin/resolveip Please configure the 'hostname'
初始化数据库报错: sh: ./bin/my_print_defaults: /lib/ld-linux.so.2: bad ELF interpreter: 没有那个文件或目录FATAL ERROR ...
- SQL Server如何附加只有mdf的数据库文件
有时候SQL Server意外断电会导致SQL Server的ldf日志文件丢失或者损坏,这个时候你如果直接附加mdf文件到SQL Server会失败,这里提供一个方法可以还原只有mdf的数据库文件, ...
- 光杆mdf文件的导入
场景,准备学习SSAS的时候,按照教程在微软下载了示例数据库AdventureWorksDW2012,下载来才发现只有一个mdf文件. 正好今天群里有位兄弟也碰到差不多的问题,客户数据库里的ldf文件 ...
- Linux中 /proc/[pid] 目录各文件简析
Linux 内核提供了一种通过 proc 文件系统,在运行时访问内核内部数据结构.改变内核设置的机制.proc 文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以文件系统的方式为访问系 ...
- excel如何冻结首行或首列及首行首列同时冻结
冻结首行方法: 首先选择首行,在菜单栏选择视图菜单,再选择冻结窗格下拉三角里的冻结首行即可. 效果如下:拖动垂直滚动条 冻结首列方法: 首先选择首列,在菜单栏选择视图菜单,再选择冻结窗格下拉三角里的冻 ...
- Windows窗体数据抓取详解
最近在客户项目上刚好遇到一个问题,项目需求是要获取某台机床的实时状态,问题点刚好就在于该机床不是传统意义上的数控机床,也不是PLC控制器,只有一个上传下载程序文件的应用程序,上面刚好有几个按钮可以大概 ...
- Hyper-v虚拟化
1.打开Hyper-V管理器,新建虚拟机 2.点击下一步,继续设置 3.设置虚拟机名称和存储位置,点击选择打钩,自定义路径 4.设置虚拟机运行内存 5.设置虚拟机设置好的网络 6.创建虚拟磁盘 7创建 ...