Algorithm Tic-Tac-Toe游戏使用Minimax算法的实现不适用于python
我为tic-tac-toe实现了Minimax算法。我不明白为什么计算机不能玩一个完美的游戏。帮助我,我是数据结构的新手。 谢谢你的帮助。 所有使用的功能都已提及,其功能也已提及Algorithm Tic-Tac-Toe游戏使用Minimax算法的实现不适用于python,algorithm,python-3.x,tree,artificial-intelligence,minimax,Algorithm,Python 3.x,Tree,Artificial Intelligence,Minimax,我为tic-tac-toe实现了Minimax算法。我不明白为什么计算机不能玩一个完美的游戏。帮助我,我是数据结构的新手。 谢谢你的帮助。 所有使用的功能都已提及,其功能也已提及 ''' My initial List is _ _ _ _ _ _ _ _ _ functions.py file is imported var = "X" or "0" (if user chooses X then var is 0 and vice versa as
'''
My initial List is
_ _ _
_ _ _
_ _ _
functions.py file is imported
var = "X" or "0" (if user chooses X then var is 0 and vice versa as user
can choose that if user wants to play as X or 0)
makemove() --> for computer to make move
functions.main_list --> 2D 3X3 list in which game is being stored
functions.placeinpt(p,var) --> places var(X or 0) at position p
functions.possibleMoves(List) --> This checks possible positions in the list
and returns an array containing possible
positions eg:['1','2','5']
functions.showlist(List) --> Displays the list on the screen
functions.wincases(List,var) --> Check if var (X or 0) is
winning in the given list
(Check if list contains any
3 in a row winning combination
of variable
eg: if var = "X" is at (1,2,3) or (4,5,6) position and so on
|1 2 3|
|4 5 6| # Here 1 2 ...9 are the positions
|7 8 9|
'''
import functions
import math
def makemove(game_state):
functions.placeInput(minimax(game_state),game_state)
def minimax(game_state): # Returns best move
if functions.winCases(functions.main_list,"X"):
return 1
elif functions.winCases(functions.main_list, "0"):
return -1
if functions.possibleMoves(functions.main_list) == []:
return 0
moves = functions.possibleMoves(functions.main_list)
functions.showlist(functions.main_list)
print("")
bestscore = - math.inf
for move in moves:
clone = functions.placeInput(int(move),game_state)
score = min_play(game_state)
if score> bestscore:
bestmove = int(move)
bestscore = score
functions.placeInput(int(move),"_")
return bestmove
def min_play(game_state): # Returns best score
if functions.winCases(functions.main_list,"X"):
return 1
elif functions.winCases(functions.main_list, "0"):
return -1
if functions.possibleMoves(functions.main_list) == []:
return 0
moves = functions.possibleMoves(functions.main_list)
functions.showlist(functions.main_list)
print("")
bestscore = math.inf
for move in moves:
functions.placeInput(int(move), game_state)
if game_state == "X":
game_state = "0"
else:
game_state = "X"
score = max_play(game_state)
if score < bestscore:
bestscore = score
bestmove = move
functions.placeInput(int(move),"_") # To empty the position when going back up in tree we move so that game state doesnt changes
return bestscore
def max_play(game_state): # Returns best score
if functions.winCases(functions.main_list,"X"):
return 1
elif functions.winCases(functions.main_list, "0"):
return -1
if functions.possibleMoves(functions.main_list) == []:
return 0
moves = functions.possibleMoves(functions.main_list)
functions.showlist(functions.main_list)
print("")
bestscore = - math.inf
for move in moves:
functions.placeInput(int(move), game_state)
if game_state == "X":
game_state = "0"
else:
game_state = "X"
score = min_play(game_state)
if score > bestscore:
bestscore = score
bestmove = move
functions.placeInput(int(move),"_")
return bestscore
我遵循的教程
谢谢你