У меня проблема с моим кодом. Я работаю над игрой ti c ta c toe с минимаксным алгоритмом. При этой функции результата (используя метод deepcopy для возврата доски, которая получается в результате перемещения на доске, я получаю ошибку типа «Non Type», как показано ниже:
Traceback (most recent call last):
File "runner.py", line 116, in <module>
board = ttt.result(board, move)
File "D:\projects\AI with Py\pset0\tictactoe\tictactoe.py", line 71, in result
if result_board[action[0]][action[1]] != EMPTY:
TypeError: 'NoneType' object is not subscriptable
Я попытался отладить свой VSCode, но Я не очень понимаю, что там происходит.
import math
from copy import deepcopy
from random import randint
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 player(board):
"""
Returns player who has the next turn on a board.
"""
# start counting the number of X and O
number_of_X = 0
number_of_O = 0
for row in board:
for cell in row:
if cell == X:
number_of_O += 1
elif cell == O:
number_of_X += 1
# if X goes first, number of X > number of O, then it's O's turn
# if then O goes after, number of O == number of X and if not terminal state,
# then it's X's turn
if number_of_X > number_of_O:
return O
elif number_of_O == number_of_X and not terminal(board):
return X
else:
return None
def actions(board):
"""
Returns set of all possible actions (i, j) available on the board.
"""
# create a set of all possible moves
possible_moves = set()
# all the possible move in 3x3 board
for i in range(3):
for j in range(3):
# if cell empty, then can make a move at that cell
if board[i][j] == EMPTY:
possible_moves.add((i, j))
# else, return actions
return possible_moves
def result(board, action):
"""
Returns the board that results from making move (i, j) on the board.
"""
result_board = deepcopy(board)
if result_board[action[0]][action[1]] != EMPTY:
raise Exception("Invalid Move")
result_board[action[0]][action[1]] = player(board)
return result_board