gpt4 book ai didi

python - 如何使用另一个函数终止函数中的 while 循环。 (井字棋游戏)

转载 作者:行者123 更新时间:2023-12-01 07:12:25 25 4
gpt4 key购买 nike

我正在用 Python 编写一个 Tic Tac Toe 程序。我已经制作了打印棋盘、接受用户输入并实际玩游戏的功能(棋盘会随着每次输入而变化)。但是,我似乎无法在主 exec() 函数中运行我的 output(gboard) 函数来终止 while True: 循环。代码如下:

def board(gboard):
print("\n"*10)
print(gboard[7]+'|'+gboard[8]+'|'+gboard[9])
print(gboard[4]+'|'+gboard[5]+'|'+gboard[6])
print(gboard[1]+'|'+gboard[2]+'|'+gboard[3])


def outcome(gboard):
global ini
if(gboard[1] == 'X') and (gboard[2] == 'X') and gboard[3] == 'X':
print('Winner is X!')
ini = 0
return ini



def exec():
move_counter = 0
ini = 1
winner = 'X'
outcomes = ['X','O']

print("Welcome to Tic Tac Toe!")
print("Here's how the game works:\nEach number on the keypad represents a place on the table.")
print("For example the number 1 stands for the box in column 1 row 3, while number 6 stands for column 3 row 2.")
print("You will tell the computer where you wish to play using this system!")
print("\n"*2+"Here is a visual representation of the board and the key numbers: ")
print('7|8|9')
print('4|5|6')
print('1|2|3')
print('If you wish to play X in the middle, you tell the computer to place X in position 5. Here is how a blank board will evolve:')
print(' | | ')
print(' |X| ')
print(' | | ')
print("So. Let's play!")

player2 = None
player1 = input('Player 1, would you like to be X or O(represents the letter)?: ')
player1 = player1.upper()
if(player1 == 'X'):
player2 = 'O'
else:
player2 = 'X'
print(f"Ok. Player 1 is {player1} and so that means that Player 2 is {player2}")


gboard = ['#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ']
while True:
move = int(input('Player 1, where would you like to play?: '))
gboard[move] = player1
move_counter += 1
board(gboard)
outcome(gboard)
if(ini == 0) or move_counter == 9:
break

move2 = int(input('Player 2, where would you like to play?: '))
gboard[move2] = player2
move_counter += 1
board(gboard)
outcome(gboard)

if(ini == 0) or move_counter == 9:
break





print('DONE!')
exec()

在该代码中,如果底行全是 X,我只输入了一个获胜条件 - 这就是我用来测试程序的条件。当我运行该程序并输入带有 X 的底行时,它会打印“Winner is X!”但不会打破 while 循环。然而,当 move_counter 达到 9 时,它会中断 while 循环。下面是执行过程:

  Welcome to Tic Tac Toe!
Here's how the game works:
Each number on the keypad represents a place on the table.
For example the number 1 stands for the box in column 1 row 3, while number 6 stands for column 3 row 2.
You will tell the computer where you wish to play using this system!


Here is a visual representation of the board and the key numbers:
7|8|9
4|5|6
1|2|3
If you wish to play X in the middle, you tell the computer to place X in position 5. Here is how a blank board will evolve:
| |
|X|
| |
So. Let's play!
Player 1, would you like to be X or O(represents the letter)?: X
Ok. Player 1 is X and so that means that Player 2 is O
Player 1, where would you like to play?: 1
| |
| |
X| |
Player 2, where would you like to play?: 4
| |
O| |
X| |
Player 1, where would you like to play?: 2
| |
O| |
X|X|
Player 2, where would you like to play?: 5
| |
O|O|
X|X|
Player 1, where would you like to play?: 3
| |
O|O|
X|X|X
Winner is X!
Player 2, where would you like to play?:

我不希望程序在获胜者确定后询问玩家 2 他们想在哪里玩。为什么我的结果函数没有跳出 while 循环?

最佳答案

您的 exec 函数中的 ini 变量似乎与 outcome 中的 ini 变量不同函数(一种是局部变量,一种是全局变量)。您可以通过将 exec 函数中的 ini 声明为全局变量来解决此问题,如下所示:

def exec():
move_counter = 0
global ini <------- here
ini = 1
winner = 'X'
outcomes = ['X','O']

祝你好运!

关于python - 如何使用另一个函数终止函数中的 while 循环。 (井字棋游戏),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58143375/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com