由于某些原因,当条件满足时,while循环没有中断。while循环应该检查玩家的输入,以填充tic-tac趾板,直到变量“win”为真。你知道吗
一旦电路板反映了tic-tac-toe的一个获胜条件,它就会将变量“win”赋值为True,并反过来跳出循环。你知道吗
出于某种原因,循环没有中断,但是变量“win”的读数仍然是真的。你知道吗
有人能解释为什么循环没有中断吗?我尝试将while循环的条件重写为“while win==False”,但这似乎也不能解决问题。你知道吗
我已经包括了一些我正在使用的函数,并在旁边用注释解释了一些更简单的函数。你知道吗
我正在使用复制所有这些都是在网上完成的,而不是在本地机器上的程序上,所以我认为这可能也是问题的一部分。你知道吗
import os
board = ["#"," "," "," "," "," "," "," "," "," "]
def determine_win(marker):
# Winning Patterns:
# (1,2,3), (4,5,6), (7,8,9), (1,4,7), (2,5,8), (3,6,9), (3,5,7), (1,5,9)
if board[1]== board[2]==board[3]==marker:
return True
elif board[4]== board[5]==board[6]==marker:
return True
elif board[7]== board[8]==board[9]==marker:
return True
elif board[1]== board[4]==board[7]==marker:
return True
elif board[2]== board[5]==board[8]==marker:
return True
elif board[3]== board[6]==board[9]==marker:
return True
elif board[3]== board[5]==board[7]==marker:
return True
elif board[1]== board[5]==board[9]==marker:
return True
else:
return False
player1 = xo() # A Function that takes user input either "X" or O"
if player1 == "X":
player2 = "O"
else:
player2 = "X"
win = False
while not win:
display_board(board) # display_baord(board) takes the list "board" and uses it as input to display the tic tac toe board to the screen.
print("\nPlayer 1")
board[player_turn()] = player1
win = determine_win(player1)
print(win) # used to verify if win is changing
input() # used to pause the screen for troubleshooting
display_board(board)
print("\nPlayer 2")
board[player_turn()] = player2
win = determine_win(player2)
print(win) # used to verify if win is changing
input() # used to pause the screen for troubleshooting
print("Win Declared")
正如注释所说,原因是
while
只在完成循环的整个迭代时检查win
的条件。我更喜欢以下方法使代码更整洁:用这样的东西。这与@jasonharper和@Idlehands的回答相同
相关问题 更多 >
编程相关推荐