python - 返回 true 的函数应该返回 false 吗?Python方法
问题描述
class ClassicTicTacToe:
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 empty string
for i in range(self.size):
row = []
for j in range(self.size):
row.append(' ')
self.board.append(row)
#print(self.board)
def drawBoard(self):
l1=' 0 1 2'
l2=' 0 %s | %s | %s' % (self.board[0][0],self.board[0][1],self.board[0][2])
l3=' '+'-'*11
l4=' 1 %s | %s | %s' % (self.board[1][0],self.board[1][1],self.board[1][2])
l5=l3
l6=' 2 %s | %s | %s' % (self.board[2][0],self.board[2][1],self.board[2][2])
print(l1,l2,l3,l4,l5,l6,sep='\n')
'''
Displays the current state of the board, formatted with column and row
indicies shown.
Inputs: none
Returns: None
'''
def squareIsEmpty(self, row, col):
if self.board[row][col]==' ':
return True
else:
return False
'''
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
'''
# mark is a string for the users input of x or o
def update(self, row, col, mark):
if self.squareIsEmpty(row,col)==True:
self.board[row][col]=mark.lower()
return True
if self.squareIsEmpty(row,col)==False:
return False
'''
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 (string) - entry to place in square
Returns: True if attempted update was successful; False otherwise
'''
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
'''
nonempty_list=[]
# TO DO: delete pass and complete method
for i in self.board:
# alternatively this could be range(len(self.board))
for j in range(0,3):
if i[j]!=' ':
nonempty_list.append(i[j])
if len(nonempty_list)<self.size**2:
return False
return True
def isWinner(self):
for i in range(len(self.board)):
if self.board[i][0]+self.board[i][1]+self.board[i][2]==(3*'x') or (3*'o'):
#return self.board[i][0]+self.board[i][1]+self.board[i][2]
return True
if self.board[0][i]+self.board[1][i]+self.board[2][i]==(3*'x') or (3*'o'):
#return self.board[0][i]+self.board[1][i]+self.board[2][i]
return True
if self.board[0][0]+self.board[2][2]+self.board[1][1]==(3*'x') or (3*'o'):
#return self.board[0][0]+self.board[2][2]+self.board[1][1]
return True
if self.board[0][2]+self.board[2][0]+self.board[1][1]==(3*'x') or (3*'o'):
#return self.board[0][2]+self.board[2][0]+self.board[1][1]
return True
return False
def isNum(self):
if self.choice=='num':
return False
else:
return True
'''
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
'''
if __name__ == "__main__":
new_game=ClassicTicTacToe()
new_game.update(1,1,'x')
new_game.update(2,0,'x')
new_game.update(0,0,'x')
new_game.drawBoard()
print(new_game.isWinner())
创建带有 X 和 O 的经典井字游戏版本。isWin 方法检查我们是否有任何获胜条件(连续 3 个 x 或 o),如果有,则返回 true,否则返回 false。我已经设置了一个场景,它应该返回 False 但它返回 True 任何人都可以找到原因吗?我不确定问题是什么
解决方案
推荐阅读
- powershell - Powershell Test-NetConnection“[域]的名称解析失败”错误
- java - 找不到 libarcore
- c# - 将 JsonString 列表转换为类对象
- android - 为什么 Intent.ACTION_SENDTO 将应用程序作为选项包含在内
- javascript - Cakephp:使用javascript时,按钮的名称不包含在帖子中
- python - 为什么调用我的函数会引发 NameError?
- python - 提高从 pandas 列中提取信息的速度
- sql - 需要帮助从字符串中提取子字符串直到特定长度之前的最后一个空格
- angular - 如何使用 *ngFor 渲染二维数组
- html - URL - 使用 R 进行网页抓取