output(gboard)
函数中运行我的exec()
函数来终止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循环?
我正在用Python编写井字游戏程序。我已经完成了打印棋盘,接受用户输入并实际玩游戏的功能(棋盘每次输入都会变化)。但是,我似乎无法运行...
ini
函数中的exec
变量与ini
函数中的outcome
变量不同(一个是局部变量,一个是全局变量)。您只需在ini
函数中将exec
声明为全局变量,即可解决此问题,如下所示:为什么我的结果函数没有突破while循环?