我的问题是,在调用check_win
中的函数gameloop
之后,
即使函数评估为false(我已经检查过,所以这是
case),我将这个假值分配给循环内的gamePlaying
,
但游戏循环gamePlaying
条件仍然评估为真
并继续前进。
def check_win(): #function checks for three in a row across, down, and diagonals
for x in range (0, 3) :
y = x*3
if (board[y] == board[(y + 1)] and board[y] == board[(y + 2)]):
gamePlaying = False
else:
gamePlaying = True
if (board[x] == board[(x + 3)] and board[x] == board[(x + 6)]):
gamePlaying = False
else:
gamePlaying = True
if((board[0] == board[4] and board[0] == board[8]) or
(board[2] == board[4] and board[4] == board[6])):
gamePlaying = False
else:
gamePlaying = True
return(gamePlaying)
currentPlayer = [first_player,second_player] #creates list to iterate over turns
gamePlaying = True #bool for gameloop
while gamePlaying: #main game loop
for i in currentPlayer: #iterates over current player to switch turns
draw_board()
place_move = int(input(first_move + ' what is your move? ')) #inputs then places move for first_player
board[place_move] = first_player
gamePlaying = check_win() #should take bool output of check_win and store to cont or end gameloop
draw_board()
place_move = int(input(second_move + ' what is your move? ')) #inputs then places move for second_player
board[place_move] = second_player
gamePlaying = Check_win() #should take bool output of check_win and store to cont or end gameloop
答案 0 :(得分:0)
问题在于,当您打算使用if
时,您正在使用elif
语句。请参阅docs。
但是,您可能希望在这些点上return False
,因为这样可以让函数提前退出,而您必须担心gamePlaying
会被设置回True
后来的if
陈述。