我的问题是,在调用gameloop
中的函数check_win
之后,
即使函数的计算结果为false(我已经检查过了,所以这是
case),我将这个假值赋给循环中的gamePlaying
,
然而,游戏循环gamePlaying
条件仍然计算为true
继续前进
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
问题是,当您打算使用
elif
时,您正在使用if
语句。见docs但是,您可能希望在这些点上执行
return False
,因为这将允许函数提前退出,并且您必须担心gamePlaying
会被稍后的if
语句设置回True
相关问题 更多 >
编程相关推荐