我被要求用Python写一个简单的井字游戏。
目前我有两个主要问题:
-
玩家符号不会从 X 更改为 O,也不会从 O 更改为 X(如 moveandturn 函数中所写(
-
即使我使用第一个选择的交易品种创建获胜条件,我的代码也无法识别获胜。请参阅代码中的 WIN 函数(
对我来说看起来还可以,除了我不确定我应该向我的 win 函数发送什么
法典:
def moveandturn(whoseturn,board):
rowloc=int(input("Player, insert the deserved row to place your symbol"))
coloc=int(input("Player, insert the deserved column to place your symbol"))
if board[rowloc][coloc] =='e':
board[rowloc][coloc] = whoseturn
else:
print("The deserved place is taken")
rowloc = int(input("Player, insert the deserved row to place your symbol"))
coloc = int(input("Player, insert the deserved column to place your symbol"))
for i in range(0,3):
print(board[i], 'n')
if whoseturn=='O':
whoseturn='X'
if whoseturn=='X':
whoseturn='O'
return whoseturn
def win(board,x,y, whoseturn):
if board[0][y] == (whoseturn) and board[1][y] == (whoseturn) and board [2][y] == (whoseturn):
return True
if board[0][y] == (whoseturn) and board[1][y] == (whoseturn) and board [2][y] == (whoseturn):
return True
if board[x][0] == (whoseturn) and board[x][1] == (whoseturn) and board [x][2] == (whoseturn):
return True
if board[x][0] == (whoseturn) and board[x][1] == (whoseturn) and board [x][2] == (whoseturn):
return True
if board[0][0] == (whoseturn) and board[1][1] == (whoseturn) and board [2][2] == (whoseturn):
return True
if board[0][0] == (whoseturn) and board[1][1] == (whoseturn) and board [2][2] == (whoseturn):
return True
if board[0][2] == (whoseturn) and board[1][1] == (whoseturn) and board [2][0] == (whoseturn):
return True
if board[0][2] == (whoseturn) and board[1][1] == (whoseturn) and board [2][0] == (whoseturn):
return True
return False
def isfull(board):
for i in range(0,3):
for j in range(0,3):
if board[i][j]=='e':
return False
return True
def main():
board = [['e','e','e']
,['e','e','e']
,['e','e','e']]
print("Welcome to the great tic tac toe game!")
print("Your board is now loading...")
for i in range(0,3):
print(board[i],'n')
player1=input("Player 1, select your symbol (X/O)")
if player1 =='O':
print('X is player 2s symbol')
player2 = 'X'
else:
print('O is player 2s symbol')
player2 = 'O'
print("Player 1 will start")
whoseturn=player1
while(not (win(board,0,0,whoseturn)) and not isfull(board)):
whoseturn=moveandturn(whoseturn,board)
moveandturn(whoseturn,board)
if not win(board,0,0,whoseturn) and isfull(board):
print("Tied")
else:
print(whoseturn,"wins")
if __name__ == '__main__':
main()
我将非常感谢您解决上述问题的任何帮助!
对于第一个问题
- 玩家符号不会从 X 更改为 O,也不会从 O 更改为 X(如 moveandturn 函数中所写(
在函数 moveandturn 中,问题是条件没有按预期工作。
if whoseturn=='O': # 1
whoseturn='X'
if whoseturn=='X': # 2
whoseturn='O'
当whoseturn == 'O'为真时,whoseturn被上面的1更改为"X"。 然后立即,whoseturn被上面的2改回"O">
解决方案是将上述条件更改为:
if whoseturn == 'X':
whoseturn = 'O'
else:
whoseturn = 'X'
或者更简单地说:
whoseturn = 'X' if whoseturn == 'O' else 'O'
代码重构
解决上述问题和代码中的其他问题(包括第二个问题(
def get_move(whoseturn, board):
# changed from moveandturn (to make the function do only one thing rather than two unrelated things, which was getting a move and switching turns)
rowloc=int(input("Player, insert the deserved row to place your symbol: "))
coloc=int(input("Player, insert the deserved column to place your symbol: "))
while True:
if not (0 <= rowloc < 3 and 0 <= coloc < 3):
print('row and column must be 0, 1, or 2')
elif board[rowloc][coloc] !='e':
print("The deserved place is taken")
else:
board[rowloc][coloc] = whoseturn
break
return rowloc, coloc
def display_board(board):
print('n'.join([' '.join(board[i]) for i in range(3)]))
def win(board, whoseturn, x, y):
""" using code from https://codereview.stackexchange.com/questions/24764/tic-tac-toe-victory-check """
# The posted code had many of the if conditions identical and always called win using row 0 and column 0 which is incorrect
#check if previous move caused a win on vertical line
if board[0][y] == board[1][y] == board [2][y] == whoseturn:
return True
#check if previous move caused a win on horizontal line
if board[x][0] == board[x][1] == board [x][2] == whoseturn:
return True
#check if previous move was on the main diagonal and caused a win
if x == y and board[0][0] == board[1][1] == board [2][2] == whoseturn:
return True
#check if previous move was on the secondary diagonal and caused a win
if x + y == 2 and board[0][2] == board[1][1] == board [2][0] == whoseturn:
return True
return False
def isfull(board):
for i in range(0,3):
for j in range(0,3):
if board[i][j]=='e':
return False
return True
def main():
board = [['e','e','e']
,['e','e','e']
,['e','e','e']]
print("Welcome to the great tic tac toe game!")
player1=input("Player 1, select your symbol (X/O): ")
if player1 =='O':
print('X is player 2s symbol')
player2 = 'X'
else:
print('O is player 2s symbol')
player2 = 'O'
print("Player 1 will start")
whoseturn=player1
while True:
# We alternative players, checking for win at each move
display_board(board)
rowloc, coloc = get_move(whoseturn, board)
if win(board,whoseturn, rowloc, coloc):
print(f'{whoseturn} wins')
break
if isfull(board):
print('Tied')
break
# cange turns
whoseturn = 'X' if whoseturn == 'O' else 'O'
if __name__ == '__main__':
main()
不查看其余代码:
if whoseturn=='O':
whoseturn='X'
if whoseturn=='X':
whoseturn='O'
return whoseturn
如果whoseturn
是"O",则将其更改为"X"。然后你立即把它改回"O",因为whoseturn=='X'
是正确的。您希望这两个选项互斥。你在想:
if whoseturn == 'O':
whoseturn = 'X'
elif whoseturn == 'X':
whoseturn = 'O'
return whoseturn