Я пишу программу Tic Tac Toe на Python.Я сделал функции, чтобы напечатать доску, принять пользовательский ввод и фактически играть в игру (доска меняется с каждым вводом).Однако я не могу запустить свою функцию 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!»но не прерывает цикл.Однако, когда 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?