我的代码中有错误
def getEntry(player, entries)
def getCoord(player, dimension)
文件“ main.py”,第136行def getEntry(玩家,条目)^SyntaxError:语法无效
这是一个数字的井字游戏
非常感谢!
class NumTicTacToe:
def __init__(self):
'''
Initializes an empty Numerical Tic Tac Toe board.
Inputs: none
Returns: None
'''
self.board = [] # list of lists, where each internal list represents a row
self.size = 3 # number of columns and rows of board
# populate the empty squares in board with 0
for i in range(self.size):
row = []
for j in range(self.size):
row.append(0)
self.board.append(row)
def drawBoard(self):
'''
Displays the current state of the board, formatted with column and row
indices shown.
Inputs: none
Returns: None
'''
grid_row = 0
grid = [['0 ', ' ', ' | ', ' ', ' | ' , ' ' , ' '], ['1 ', ' ', ' | ', ' ', ' | ' , ' ' , ' '], ['2 ', ' ', ' | ', ' ', ' | ' , ' ' , ' ']]
print(' 0 1 2 ')
for row in self.board:
grid_col = 1
for col in row:
if col != 0:
grid[grid_row][grid_col] = str(col)
grid_col += 2
print(''.join(grid[grid_row]))
grid_row += 1
if grid_row < 3:
print(' -----------')
def squareIsEmpty(self, row, col):
'''
Checks if a given square is empty, or if it already contains a number
greater than 0.
Inputs:
row (int) - row index of square to check
col (int) - column index of square to check
Returns: True if square is empty; False otherwise
'''
if self.board[row][col] == 0:
return True
else:
return False
def update(self, row, col, num):
'''
Assigns the integer, num, to the board at the provided row and column,
but only if that square is empty.
Inputs:
row (int) - row index of square to update
col (int) - column index of square to update
num (int) - entry to place in square
Returns: True if attempted update was successful; False otherwise
'''
if self.squareIsEmpty(row,col):
self.board[row][col] = num
return True
else:
return False
def boardFull(self):
'''
Checks if the board has any remaining empty squares.
Inputs: none
Returns: True if the board has no empty squares (full); False otherwise
'''
flag = True
for row in self.board:
for col in row:
if col == 0:
flag = False
return flag
def isWinner(self):
'''
Checks whether the current player has just made a winning move. In order
to win, the player must have just completed a line (of 3 squares) that
adds up to 15. That line can be horizontal, vertical, or diagonal.
Inputs: none
Returns: True if current player has won with their most recent move;
False otherwise
'''
total = 0
total2 = 0
for index in range(0, self.size):
total = total + self.board[index][index]
total2 = total2 + self.board[index][self.size - 1 - index]
if total == 15 or total2 == 15:
return True
else:
total = 0
total2 = 0
for col_index in range(0,self.size):
for row_index in range(0,self.size):
total = total + self.board[row_index][col_index]
if total == 15:
return True
else:
total = 0
for row_index in range(0, self.size):
for col_index in range (0, self.size):
total = total + self.board[row_index][col_index]
if total == 15:
return True
else:
total = 0
return False
def getEntry(player, entries)
def getCoord(player, dimension)
if __name__ == "__main__":
# TEST EACH METHOD THOROUGHLY HERE
# suggested tests are provided as comments, but more tests may be required
# start by creating empty board and checking the contents of the board attribute
myBoard = NumTicTacToe()
print('Contents of board attribute when object first created:')
print(myBoard.board)
# does the empty board display properly?
myBoard.drawBoard()
# assign a number to an empty square and display
# try to assign a number to a non-empty square. What happens?
# check if the board has a winner. Should there be a winner after only 1 entry?
# check if the board is full. Should it be full after only 1 entry?
# add values to the board so that any line adds up to 15. Display
# check if the board has a winner
# check if the board is full
# write additional tests, as needed
def
用于定义功能。通常,代码应如下所示: