Python CS50AI Tictactoe函数的一个参数为None,原因是什么?
我正在尝试为CS50AI的第一个任务创建一个tictactoe AI。我得到了你将在下面看到的错误。这意味着结果函数中的操作以某种方式为Python CS50AI Tictactoe函数的一个参数为None,原因是什么?,python,cs50,Python,Cs50,我正在尝试为CS50AI的第一个任务创建一个tictactoe AI。我得到了你将在下面看到的错误。这意味着结果函数中的操作以某种方式为None。但问题是我只需点击一下鼠标,怎么可能是N one my函数给出以下错误: Traceback (most recent call last): File "c:\Users\ahmet\Desktop\tictactoe\runner.py", line 116, in <module> board
None
。但问题是我只需点击一下鼠标,怎么可能是N one
my函数给出以下错误:
Traceback (most recent call last):
File "c:\Users\ahmet\Desktop\tictactoe\runner.py", line 116, in <module>
board = ttt.result(board, move)
File "c:\Users\ahmet\Desktop\tictactoe\tictactoe.py", line 69, in result
raise Exception("Invalid action")
Exception: Invalid action
我找到了答案:这是因为我的
minimax
函数。除非有一个胜利的举动,否则它将一无所获。我忘了给它的其余部分编码了。谢谢你的帮助。board=ttt.result(board,move)
显然不是你的代码的一部分,但是move
已经是None
,所以问题不在于你共享的代码,而在于你没有共享的代码。。。
"""
Tic Tac Toe Player
"""
import math
X = "X"
O = "O"
EMPTY = None
def initial_state():
"""
Returns starting state of the board.
"""
return [[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY]]
def initial_stateh():
return [[X, X, X],
[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY]]
def initial_statetie():
return [[O, X, O],
[X, O, X],
[X, X, O]]
def initial_stateboş():
return [[EMPTY, O, O],
[X, O, EMPTY],
[X, X, EMPTY]]
def player(board):
numx = 0
numo = 0
for i in range(3):
for j in range(3):
if board[i][j] == X:
numx = numx + 1
if board[i][j] == O:
numo = numo + 1
if numx == numo:
return X
elif numx > numo:
return O
def actions(board):
possiblemoves = set()
for i in range(3):
for j in range(3):
if board[i][j] == EMPTY:
possiblemoves.add((i,j))
return possiblemoves
def result(board, action):
"""
Returns the board that results from making move (i, j) on the board.
"""
if action is None:
raise Exception("Invalid action")
copyboard = [row[:] for row in board]
if copyboard[ action[0] ][ action[1] ] is EMPTY:
#print(action[0],action[1])
copyboard[ action[0] ][ action[1] ] = player(board)
return copyboard
else:
raise Exception("Move is not possible")
def horizontal(board):
for x in range(3):
if (board[x][0] == board[x][1] and board[x][1] == board[x][2]) and board[x][0] != None:
pl = board[x][0]
return pl
return None
def vertical(board):
for y in range(3):
if (board[0][y] == board[1][y] and board[1][y] == board[2][y]) and board[1][y] != None:
pl = board[1][y]
return pl
return None
def diagonally(board):
if (board[0][0] == board[1][1] and board[1][1]==board[2][2]) and board[0][0] != None:
return board[0][0]
if (board[0][2] == board[1][1] and board[1][1]==board[2][0]) and board[0][2] != None:
return board[0][2]
else:
return None
def winner(board):
"""
Returns the winner of the game, if there is one.
"""
if vertical(board) != None :
return vertical(board)
if horizontal(board) != None :
return horizontal(board)
if diagonally(board) != None :
return diagonally(board)
else:
return None
def arethereanyspace(board):
space = 0
for row in board:
for item in row:
if item == None:
space +=1
else:
return space
def tie(board):
if winner(board) == None and arethereanyspace(board) == None:
return True
else:
return False
def terminal(board):
"""
Returns True if game is over, False otherwise.
"""
if tie(board) == True :
return True
if winner(board) != None:
return True
else:
return False
def utility(board):
"""
Returns 1 if X has won the game, -1 if O has won, 0 otherwise.
"""
if winner(board) == X:
return 1
if winner(board) == O:
return -1
if tie(board):
return 0
def minimax(board):
"""
Returns the optimal action for the current player on the board.
"""
if terminal(board) == True: #if the game has ended
return None
for move in actions(board):
if winner(result(board, move)) != None:#if the move is a winning move, do it!
return move
i = move[0]
j = move[1]
if j+1 <= 2 and board[i][j+1] == player(board): #check horizontal
return move
if j-1 >= 0 and board[i][j-1] == player(board): #check horizontal
return move
if i+1 <= 2 and board[i+1][j] == player(board): #check vertical
return move
if i-1 >= 0 and board[i-1][j] == player(board): #check vertical
return move
if (j+1<=2) and (i+1<= 2) and board[i+1][j+1] == player(board): #checking the diag
return move
if (i-1 >= 0) and (j-1 >= 0) and board[i-1][j-1] == player(board): #checking the diag
return move
if (i-1 >= 0) and (j+1 <= 2) and board[i-1][j+1] == player(board):
return move
if (i+1 <= 2) and (j-1 >= 0) and board[i+1][j-1] == player(board):
return move