【发布时间】:2020-07-30 16:07:06
【问题描述】:
作为一个项目,我在 Python 中实现了 MinMax Tic Tac Toe AI。我进一步实现了 Alpha - Beta Pruning 并提出了这个庞大的代码。
代码:-
class TicTacToe:
game_state = [[' ',' ',' '],
[' ',' ',' '],
[' ',' ',' ']]
players = ['X','O']
def __init__(self,player_idx=0):
if(player_idx>1 or player_idx<0):
raise Exception("player index can only be 0 or 1")
self.current_player=player_idx
def check_current_state(self):
# Check if draw
draw_flag = 0
for i in range(3):
for j in range(3):
if self.game_state[i][j] is ' ':
draw_flag = 1
if draw_flag is 0:
return None, "Draw"
# Check horizontals
if (self.game_state[0][0] == self.game_state[0][1] and self.game_state[0][1] == self.game_state[0][2] and self.game_state[0][0] is not ' '):
return self.game_state[0][0], "Done"
if (self.game_state[1][0] == self.game_state[1][1] and self.game_state[1][1] == self.game_state[1][2] and self.game_state[1][0] is not ' '):
return self.game_state[1][0], "Done"
if (self.game_state[2][0] == self.game_state[2][1] and self.game_state[2][1] == self.game_state[2][2] and self.game_state[2][0] is not ' '):
return self.game_state[2][0], "Done"
# Check verticals
if (self.game_state[0][0] == self.game_state[1][0] and self.game_state[1][0] == self.game_state[2][0] and self.game_state[0][0] is not ' '):
return self.game_state[0][0], "Done"
if (self.game_state[0][1] == self.game_state[1][1] and self.game_state[1][1] == self.game_state[2][1] and self.game_state[0][1] is not ' '):
return self.game_state[0][1], "Done"
if (self.game_state[0][2] == self.game_state[1][2] and self.game_state[1][2] == self.game_state[2][2] and self.game_state[0][2] is not ' '):
return self.game_state[0][2], "Done"
# Check diagonals
if (self.game_state[0][0] == self.game_state[1][1] and self.game_state[1][1] == self.game_state[2][2] and self.game_state[0][0] is not ' '):
return self.game_state[1][1], "Done"
if (self.game_state[2][0] == self.game_state[1][1] and self.game_state[1][1] == self.game_state[0][2] and self.game_state[2][0] is not ' '):
return self.game_state[1][1], "Done"
return None, "Not Done"
def evaluate(self):
a,b=self.check_current_state()
if(a != None and b == "Done" and a == 'X'):
return 1
elif(a != None and b == "Done" and a == 'O'):
return -1
elif(a == None and b == "Draw"):
return 0
else:
return 6
def MiniMax(self,depth,isMax):
score=self.evaluate()
if(score==1):
return score;
if(score==-1):
return score;
if(score==0):
return score
if(isMax==True):
best=-100
for i in range(3):
for j in range(3):
if(self.game_state[i][j]==' '):
self.game_state[i][j]='X'
best=max(best,self.MiniMax(depth+1,False))
self.game_state[i][j]=' '
return best
elif(isMax==False):
best=100
for i in range(3):
for j in range(3):
if(self.game_state[i][j]==' '):
self.game_state[i][j]='O'
best=min(best,self.MiniMax(depth+1,True))
self.game_state[i][j]=' '
return best
def best_move(self):
best=-100
r=-1
c=-1
for i in range(3):
for j in range(3):
if(self.game_state[i][j]==' '):
self.game_state[i][j]='X'
move = self.MiniMax(0, False)
self.game_state[i][j]=' '
if(move>best):
r=i
c=j
best=move
return r,c
def play_move(self, block_num):
if self.game_state[int((block_num-1)/3)][(block_num-1)%3] is ' ' and self.current_player==1:
self.game_state[int((block_num-1)/3)][(block_num-1)%3] = self.players[self.current_player]
self.current_player=1-self.current_player
elif self.current_player==0:
i,j=self.best_move()
self.game_state[i][j] = self.players[self.current_player]
self.current_player=1-self.current_player
elif self.game_state[int((block_num-1)/3)][(block_num-1)%3] != ' ' and self.current_player==1:
block_num = int(input("Block is not empty, ya blockhead! Choose again: "))
self.play_move(block_num)
def print_board(self):
print('----------------')
print('| ' + str(self.game_state[0][0]) + ' || ' + str(self.game_state[0][1]) + ' || ' + str(self.game_state[0][2]) + ' |')
print('----------------')
print('| ' + str(self.game_state[1][0]) + ' || ' + str(self.game_state[1][1]) + ' || ' + str(self.game_state[1][2]) + ' |')
print('----------------')
print('| ' + str(self.game_state[2][0]) + ' || ' + str(self.game_state[2][1]) + ' || ' + str(self.game_state[2][2]) + ' |')
print('----------------')
def play(self):
current_state = "Not Done"
print("New Game!")
self.print_board()
winner = None
while current_state == "Not Done":
if(self.current_player==1):
block_choice = int(input(str(self.players[self.current_player]) + "s Turn! Choose where to place (1 to 9): "))
self.play_move(block_choice)
elif(self.current_player==0):
self.play_move(1)
self.print_board()
winner, current_state = self.check_current_state()
if winner is not None:
print(str(winner) + " won!")
if current_state is "Draw":
print("Draw!")
game=TicTacToe()
game.play()
我不知道为什么,但是这段代码给出了问题。
请帮帮我。
【问题讨论】:
-
嗨 Dhaval,欢迎来到 StackOverflow!当您说“此代码给出问题”时,您需要指定 what 问题。请拨打tour 并通过How to Ask 获取有关如何提出好的问题的信息,这些问题将获得有用的答案并在未来对 SO 的其他用户有用。
-
另外,请修正您的代码缩进。我试过试一试,但它太模棱两可了,我无法弄清楚你想要的缩进是什么。
-
这个问题没有重点。调试是一项重要的技能,它不包括将整个代码转储到 Stack Overflow 上。您应该自己进行足够多的调试,以至少将其缩小到特定问题,然后提出有关该问题的问题。请阅读提供minimal reproducible example 的重要性。这个代码转储远非最小。
-
对此我感到非常抱歉.. 我是 Stack Overflow 上的新手
标签: python tic-tac-toe minmax