提问人:Amin Askari 提问时间:8/11/2023 更新时间:8/11/2023 访问量:33
我的井字游戏有问题
i have a problem with my tic tac toe python game
问:
问题是在向获胜者展示之后,它并没有逃脱循环。它要求玩家说出他想把X放在哪里。如果你们能帮我解决这个问题,我将不胜感激。 我希望它开始游戏。可玩,回合制,当有人获胜时,会显示获胜者并询问他们是否想再次玩
import random
try:
print("Welcome to Tic Tac Toe \n======================")
Running = 1
while Running == 1:
possible_numbers = [1,2,3,4,5,6,7,8,9]
game_board = [["1","2","3"],["4","5","6"],["7","8","9"]]
def print_game_board():
print("+---+---+---+")
print(f"| {game_board[0][0]} | {game_board[0][1]} | {game_board[0][2]} |")
print("+---+---+---+")
print(f"| {game_board[1][0]} | {game_board[1][1]} | {game_board[1][2]} |")
print("+---+---+---+")
print(f"| {game_board[2][0]} | {game_board[2][1]} | {game_board[2][2]} |")
print("+---+---+---+")
GG = False
def check_for_winner():
if game_board[0][0] == game_board[0][1] == game_board[0][2]:
print(f"{game_board[0][0]} has won the game")
GG = True
elif game_board[1][0] == game_board[1][1] == game_board[1][2]:
print(f"{game_board[1][0]} has won the game")
GG = True
elif game_board[2][0] == game_board[2][1] == game_board[2][2]:
print(f"{game_board[2][0]} has won the game")
GG = True
elif game_board[0][0] == game_board[1][0] == game_board[2][0]:
print(f"{game_board[0][0]} has won the game")
GG = True
elif game_board[0][1] == game_board[1][1] == game_board[2][1]:
print(f"{game_board[0][1]} has won the game")
GG = True
elif game_board[0][2] == game_board[1][2] == game_board[2][2]:
print(f"{game_board[0][2]} has won the game")
GG = True
elif game_board[0][0] == game_board[1][1] == game_board[2][2]:
print(f"{game_board[0][0]} has won the game")
GG = True
elif game_board[0][2] == game_board[1][1] == game_board[2][0]:
print(f"{game_board[0][2]} has won the game")
GG = True
print_game_board()
print("You are going to play as X")
turn = 0
while G
G == False:
if (turn % 2) == 0 :
user_input = int(input("What number do you wanna put your X? 1-9 "))
possible_numbers.remove(user_input)
if user_input == 1:
game_board[0][0] = "X"
elif user_input == 2:
game_board[0][1] = "X"
elif user_input == 3:
game_board[0][2] = "X"
elif user_input == 4:
game_board[1][0] = "X"
elif user_input == 5:
game_board[1][1] = "X"
elif user_input == 6:
game_board[1][2] = "X"
elif user_input == 7:
game_board[2][0] = "X"
elif user_input == 8:
game_board[2][1] = "X"
elif user_input == 9:
game_board[2][2] = "X"
turn += 1
print_game_board()
check_for_winner()
if (turn % 2) == 1 :
print("Now it's computer turn")
comp_choice = random.choice(possible_numbers)
possible_numbers.remove(comp_choice)
if comp_choice == 1:
game_board[0][0] = "O"
elif comp_choice == 2:
game_board[0][1] = "O"
elif comp_choice == 3:
game_board[0][2] = "O"
elif comp_choice == 4:
game_board[1][0] = "O"
elif comp_choice == 5:
game_board[1][1] = "O"
elif comp_choice == 6:
game_board[1][2] = "O"
elif comp_choice == 7:
game_board[2][0] = "O"
elif comp_choice == 8:
game_board[2][1] = "O"
elif comp_choice == 9:
game_board[2][2] = "O"
print("Computer has played its turn")
print_game_board()
check_for_winner()
turn += 1
wanna_play = input("do you wanna play another round? yes/no ").lower()
if wanna_play == "yes":
Running = 1
else:
print("Thanks for playing")
quit()
except (ValueError, TypeError):
quit()
答:
1赞
Kraigolas
8/11/2023
#1
这里有很多代码,但你有一个范围问题。我会把它做得小得多,这样你就可以看到问题而不会混乱。请考虑以下代码:
def outer_function():
GG = False
def inner_function():
GG = True
print(GG)
outer_function() # prints `False`
它将打印,因为没有在外部函数中设置为,它正在阴影它。False
GG = True
GG
True
相反,请尝试一个值:return
def outer_function():
GG = False
def inner_function():
return True
GG = inner_function()
print(GG)
outer_function() # prints `True`
因此,如果有赢家,如果没有赢家,应该返回。调用此函数时,请编写:check_for_winner
True
False
GG = check_for_winner()
您可以进一步简化这一点。你有:
while GG == False:
...
但是,如果你进行上述更改,你可以写:
while not check_for_winner():
...
或者,您可以将此函数重命名为is_winner
while not is_winner():
...
评论