python井字棋ai,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()
效果圖
下圖是人人對戰的結果
[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?的網格上玩井字棋. 井字棋游戲的規則如下: ...
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 ...
隨機推薦
ODCA最佳實踐翻譯:Architecting Cloud-Aware Applications (一)
Architecting Cloud-Aware Applications ** ODCA(Open Data Center Alliance)最佳實踐 ** MagicBowen(e.bowen.w ...
H.264中NAL、Slice與frame意思及相互關系
H.264中NAL.Slice與frame意思及相互關系 NAL nal_unit_type中的1(非IDR圖像的編碼條帶).2(編碼條帶數據分割塊A).3(編碼條帶數據分割塊B).4(編碼條帶數據分 ...
c++ 類名和enum時重復時要在類名前加class::
c++ 類名和enum時重復時要在類名前加class:: 一些不好的習慣都是用小寫,但又沒有區分開token,看看代碼再說,下面的代碼是我在測試polymorphism時寫的一部分,怎么也查不出,最后 ...
理解python的元類
看了一篇文檔,借鑒一下!寫下自己對python元類的理解,歡迎各位大神給出意見. 我的理解就是 type用來創建元類,元類用來創建類,類用來創建實例 這樣一想,是不是可以認為元類創建類的過程等同于類創 ...
js流程語句
一.跳轉語句1.break; 終止整個循環,不再進行判斷2.continue; 終止本次循環,接著去判斷是否執行下次循環 二.選擇(判斷)結構1.if 如果? ? ? ? ? ? ? ?if(條件1) ...
C#比較兩個由基本數據類型構成的object類型
/// /// 比較查詢條件 /// public class ModelExtensions { ///
python之打印日志logging
import logging # 簡單打印日志舉例 logging.basicConfig(level=logging.DEBUG) # 設置日志級別,WARN logging.warning('Wa ...
8.0-uC/OS-III臨界段
1.臨界段 (臨界段代碼,也叫臨界區,是指那些必須完整連續運行,不可被打斷的代碼段) 鎖調度器,可以執行ISR,開啟調度器不可執行ISR: (1).臨界段代碼,也稱作臨界域,是一段不可分割的代碼. u ...
Luogu 2279 [HNOI2003]消防局的設立 - 貪心
Description 給定一棵樹形圖, 建若干個消防站, 消防站能夠覆蓋到距離不超過2的點, 求最少需要建幾個消防站才能覆蓋所有點 Solution 從深度最深的點開始, 在它的爺爺節點上建, 每建 ...
總結
以上是生活随笔為你收集整理的python井字棋ai,python 井字棋(Tic Tac Toe)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2021年9月Github优秀项目推荐
- 下一篇: 3D引擎Axiom的选择与学习