我目前正在为Tic Tac Toe游戏编写脚本,但遇到了一个问题。当我的获胜声明有效时,立即宣布一个玩家是否有一个,而我的平局却没有。当填充最后一个将导致平局的空插槽时,它的行为就好像它已经进行了普通移动一样,并且仅在后续移动中才声明平局。为了澄清起见,董事会是列表清单。 我的代码如下。
class TicTacToe:
"""Defines a Tic Tac Toe game"""
def get_current_state(self):
"""Returns the current state of the Tic-Tac-Toe game"""
return self._current_state
def __init__(self):
"""Initiates a new TicTacToe game with a board and current state"""
self._board = [["", "", ""], ["", "", ""], ["", "", ""]]
self._current_state = "UNFINISHED"
def make_move(self, row, column, player):
"""Places the users move on the board"""
# Checks if any legal moves allowed and if so, places player on board
if self._board[0][column] != player and self._board[1][column]\
!= player and self._board[2][column] != player or \
self._board[row][0] != player and self._board[row][1] != player\
and self._board[row][2] != player or self._board[0][0] != player\
and self._board[1][1] != player and self._board[2][2] != player\
or self._board[0][2] != player and self._board[2][0] != player\
and self._board[1][1] != player and self._current_state == "UNFINISHED":
self._board[row][column] = player
# Checks for vertical wins and updates _current_state
if self._board[0][column] == player and self._board[1][column] == player \
and self._board[2][column] == player:
self._current_state = player.upper() + "_WON"
return True
# Checks for horizontal wins and updates _current_state
elif self._board[row][0] == player and self._board[row][1] == player \
and self._board[row][2] == player:
self._current_state = player.upper() + "_WON"
return True
# Checks for diagonal wins and updates _current_state
elif self._board[0][0] == player and self._board[1][1] == player\
and self._board[2][2] == player or self._board[0][2] == player\
and self._board[2][0] == player and self._board[1][1] == player:
self._current_state = player.upper() + "_WON"
return True
# Checks if the board is full with no wins and declares game a draw
elif "" not in self._board and self._current_state == "UNFINISHED":
self._current_state = "DRAW"
return True
return True
else:
return False
答案 0 :(得分:0)
我对二维列表(在列表中称为列表)有相同的问题。仅检查“”是否在整个板子内部是行不通的,因为“板子”列表中有很多列表。我建议编写代码来检查板上的每个瓷砖是否被覆盖。