在井字游戏代码中寻求布尔运算符帮助的初学者
Beginner looking for assistance with Boolean operators in tac tac toe code
我最近开始学习 python,并尝试了一个 Tic Tac Toe 项目,我在其中观看了一段视频。尽管非常努力地遵循并参考了代码,但我已经到了程序在一轮后终止的地步。我已经将我的代码与项目代码进行了比较,我认为问题出在我代码的“检查是否获胜”部分,每次比较都会将“game_still_going”变量转换为 false,从而终止游戏。
除了复制和粘贴指南代码的各个部分之外,我已经检查并重试了一个多小时的代码。
任何有关布尔部分的帮助将不胜感激。
board = ["_", "_", "_",
"_", "_", "_",
"_", "_", "_"]
game_still_going = True
winner = None
current_player = "X"
def display_board():
print(board[0] + " | " + board[1] + " | " + board[2])
print(board[3] + " | " + board[4] + " | " + board[5])
print(board[6] + " | " + board[7] + " | " + board[8])
def handle_turn(player):
print(player + "'s turn.")
position = input("Choose a position, from 1-9: ")
invalid = True
while invalid == True:
while position not in ["1", "2", "3", "4", "5", "6", "7", "8", "9"]:
position = input("Choose a position from 1-9")
position = int(position) - 1
if board[position] == "_":
invalid = False
else:
print("You can't go there! Go again.")
board[position] = player
display_board()
def check_for_winner():
global winner
global game_still_going
# setup global variables
# check rows
row_winner = check_rows()
# check columns
column_winner = check_columns()
# check diagonals
diagonals_winner = check_diagonals()
if row_winner:
winner = row_winner
elif column_winner:
winner = column_winner
elif diagonals_winner:
winner = diagonals_winner
else:
winner = None
return
def check_rows():
global game_still_going
row_1 = board[0] == board[1] == board[2] != ["_"]
row_2 = board[3] == board[4] == board[5] != ["_"]
row_3 = board[6] == board[7] == board[8] != ["_"]
if row_1:
return board[0]
elif row_2:
return board[3]
elif row_3:
return board[6]
else:
return None
def check_columns():
global game_still_going
column_1 = board[0] == board[3] == board[6] != ["_"]
column_2 = board[1] == board[4] == board[7] != ["_"]
column_3 = board[2] == board[5] == board[8] != ["_"]
if column_1 or column_2 or column_3:
game_still_going = False
if column_1:
return board[0]
elif column_2:
return board[1]
elif column_3:
return board[2]
else:
return None
def check_diagonals():
global game_still_going
diagonals_1 = board[0] == board[4] == board[8] != ["_"]
diagonals_2 = board[2] == board[4] == board[6] != ["_"]
if diagonals_1:
game_still_going = False
elif diagonals_2:
game_still_going = False
if diagonals_1:
return board[0]
elif diagonals_2:
return board[2]
else:
return None
def check_for_tie():
global game_still_going
if "_" not in board:
game_still_going = False
return True
else:
return False
def flip_player():
global current_player
if current_player == "X":
current_player = "O"
elif current_player == "O":
current_player = "X"
return
def check_if_game_over():
check_for_winner()
check_for_tie()
def play_game():
display_board()
while game_still_going:
handle_turn(current_player)
check_if_game_over()
flip_player()
print(game_still_going)
if winner == "X" or winner == "O":
print(winner + 'won!')
elif winner == None:
print("Tie!")
play_game()
board[n]
是字符串,不是列表。所以这个:
column_1 = board[0] == board[3] == board[6] != ["_"]
column_2 = board[1] == board[4] == board[7] != ["_"]
column_3 = board[2] == board[5] == board[8] != ["_"]
应该是:
column_1 = board[0] == board[3] == board[6] != "_"
column_2 = board[1] == board[4] == board[7] != "_"
column_3 = board[2] == board[5] == board[8] != "_"
同check_diagonals
。我还没有检查这是否是唯一的问题。
我最近开始学习 python,并尝试了一个 Tic Tac Toe 项目,我在其中观看了一段视频。尽管非常努力地遵循并参考了代码,但我已经到了程序在一轮后终止的地步。我已经将我的代码与项目代码进行了比较,我认为问题出在我代码的“检查是否获胜”部分,每次比较都会将“game_still_going”变量转换为 false,从而终止游戏。
除了复制和粘贴指南代码的各个部分之外,我已经检查并重试了一个多小时的代码。
任何有关布尔部分的帮助将不胜感激。
board = ["_", "_", "_",
"_", "_", "_",
"_", "_", "_"]
game_still_going = True
winner = None
current_player = "X"
def display_board():
print(board[0] + " | " + board[1] + " | " + board[2])
print(board[3] + " | " + board[4] + " | " + board[5])
print(board[6] + " | " + board[7] + " | " + board[8])
def handle_turn(player):
print(player + "'s turn.")
position = input("Choose a position, from 1-9: ")
invalid = True
while invalid == True:
while position not in ["1", "2", "3", "4", "5", "6", "7", "8", "9"]:
position = input("Choose a position from 1-9")
position = int(position) - 1
if board[position] == "_":
invalid = False
else:
print("You can't go there! Go again.")
board[position] = player
display_board()
def check_for_winner():
global winner
global game_still_going
# setup global variables
# check rows
row_winner = check_rows()
# check columns
column_winner = check_columns()
# check diagonals
diagonals_winner = check_diagonals()
if row_winner:
winner = row_winner
elif column_winner:
winner = column_winner
elif diagonals_winner:
winner = diagonals_winner
else:
winner = None
return
def check_rows():
global game_still_going
row_1 = board[0] == board[1] == board[2] != ["_"]
row_2 = board[3] == board[4] == board[5] != ["_"]
row_3 = board[6] == board[7] == board[8] != ["_"]
if row_1:
return board[0]
elif row_2:
return board[3]
elif row_3:
return board[6]
else:
return None
def check_columns():
global game_still_going
column_1 = board[0] == board[3] == board[6] != ["_"]
column_2 = board[1] == board[4] == board[7] != ["_"]
column_3 = board[2] == board[5] == board[8] != ["_"]
if column_1 or column_2 or column_3:
game_still_going = False
if column_1:
return board[0]
elif column_2:
return board[1]
elif column_3:
return board[2]
else:
return None
def check_diagonals():
global game_still_going
diagonals_1 = board[0] == board[4] == board[8] != ["_"]
diagonals_2 = board[2] == board[4] == board[6] != ["_"]
if diagonals_1:
game_still_going = False
elif diagonals_2:
game_still_going = False
if diagonals_1:
return board[0]
elif diagonals_2:
return board[2]
else:
return None
def check_for_tie():
global game_still_going
if "_" not in board:
game_still_going = False
return True
else:
return False
def flip_player():
global current_player
if current_player == "X":
current_player = "O"
elif current_player == "O":
current_player = "X"
return
def check_if_game_over():
check_for_winner()
check_for_tie()
def play_game():
display_board()
while game_still_going:
handle_turn(current_player)
check_if_game_over()
flip_player()
print(game_still_going)
if winner == "X" or winner == "O":
print(winner + 'won!')
elif winner == None:
print("Tie!")
play_game()
board[n]
是字符串,不是列表。所以这个:
column_1 = board[0] == board[3] == board[6] != ["_"]
column_2 = board[1] == board[4] == board[7] != ["_"]
column_3 = board[2] == board[5] == board[8] != ["_"]
应该是:
column_1 = board[0] == board[3] == board[6] != "_"
column_2 = board[1] == board[4] == board[7] != "_"
column_3 = board[2] == board[5] == board[8] != "_"
同check_diagonals
。我还没有检查这是否是唯一的问题。