所以我的TicTacToe有了这个Python代码。一切工作正常,除非没有获胜者,程序必须返回“ Tie”,并且即使董事会已满,它也继续要求X和O。我假设问题出在check_if_tie()函数中,但我无法弄清楚。
# -------Global variables--------
# If game is still going
game_still_going = True
# Who won? Or tie
winner = None
# Whose turn it is
current_player = 'X'
# The board displaying function
board = [' '] * 10
def display_board():
print(' ' + board[6] + ' | ' + board[7] + ' | ' + board[8])
print('---+-' '--+--- ')
print(' ' + board[3] + ' | ' + board[4] + ' | ' + board[5])
print('---+-' '--+--- ')
print(' ' + board[0] + ' | ' + board[1] + ' | ' + board[2])
# Checks if game is over
def check_if_game_over():
check_if_tie()
check_if_win()
# Checks if there is a winner
def check_if_win():
global winner
if check_row():
winner = check_row()
elif check_columns():
winner = check_columns()
elif check_diagonals():
winner = check_columns()
else:
winner = None
return
def check_row():
global game_still_going
row1 = board[6] == board[7] == board[8] != " "
row2 = board[3] == board[4] == board[5] != " "
row3 = board[0] == board[1] == board[2] != " "
if row1 or row2 or row3:
game_still_going = False
if row1:
return board[6]
elif row2:
return board[3]
elif row3:
return board[0]
return
def check_columns():
global game_still_going
column1 = board[6] == board[3] == board[0] != " "
column2 = board[7] == board[4] == board[1] != " "
column3 = board[8] == board[5] == board[2] != " "
if column1 or column2 or column3:
game_still_going = False
if column1:
return board[6]
elif column2:
return board[7]
elif column3:
return board[8]
return
def check_diagonals():
global game_still_going
diagonal1 = board[6] == board[4] == board[2] != " "
diagonal2 = board[0] == board[4] == board[8] != " "
if diagonal1 or diagonal2:
game_still_going = False
elif diagonal1:
return board[6]
elif diagonal2:
return board[0]
return
def check_if_tie():
global game_still_going
if ' ' not in board:
game_still_going = False
return
def flip_player():
global current_player
if current_player == 'X':
current_player = 'O'
elif current_player == 'O':
current_player = 'X'
return
# Whose turn it is to play
def handle_turn(player):
print(player + "'s turn")
position = int(input('Please write your position from 1 - 9: ')) - 1
if position not in [0,1,2,3,4,5,6,7,8,9]:
return input('Invalid position. Please write 1-9: ')
board[position] = player
display_board()
# Main gameplay function
def play_game():
global winner
# Displays initial board
display_board()
# Loop running the game
while game_still_going:
handle_turn(current_player)
flip_player()
check_if_game_over()
if winner == 'X' or winner == 'O':
print(winner + ' won.')
elif winner:
print('Tie')
play_game()
我能想到的最简单的建议是有一个变量,该变量保存剩余的可用平方数,并在每转之后将其减小。一旦达到0,如果没有胜利,那么就必须平局。
# -------Global variables--------
# If game is still going
game_still_going = True
# Who won? Or tie
winner = None
# Whose turn it is
current_player = 'X'
# The board displaying function
board = [' '] * 10
#board spaces
board_spaces = 9
def display_board():
print(' ' + board[6] + ' | ' + board[7] + ' | ' + board[8])
print('---+-' '--+--- ')
print(' ' + board[3] + ' | ' + board[4] + ' | ' + board[5])
print('---+-' '--+--- ')
print(' ' + board[0] + ' | ' + board[1] + ' | ' + board[2])
# Checks if game is over
def check_if_game_over():
check_if_win()
check_if_tie()
# Checks if there is a winner
def check_if_win():
global winner
if check_row():
winner = check_row()
elif check_columns():
winner = check_columns()
elif check_diagonals():
winner = check_columns()
else:
winner = None
return
def check_row():
global game_still_going
row1 = board[6] == board[7] == board[8] != " "
row2 = board[3] == board[4] == board[5] != " "
row3 = board[0] == board[1] == board[2] != " "
if row1 or row2 or row3:
game_still_going = False
if row1:
return board[6]
elif row2:
return board[3]
elif row3:
return board[0]
return
def check_columns():
global game_still_going
column1 = board[6] == board[3] == board[0] != " "
column2 = board[7] == board[4] == board[1] != " "
column3 = board[8] == board[5] == board[2] != " "
if column1 or column2 or column3:
game_still_going = False
if column1:
return board[6]
elif column2:
return board[7]
elif column3:
return board[8]
return
def check_diagonals():
global game_still_going
diagonal1 = board[6] == board[4] == board[2] != " "
diagonal2 = board[0] == board[4] == board[8] != " "
if diagonal1 or diagonal2:
game_still_going = False
elif diagonal1:
return board[6]
elif diagonal2:
return board[0]
return
def check_if_tie():
global game_still_going
if winner == None:
game_still_going = False
return
def flip_player():
global current_player
if current_player == 'X':
current_player = 'O'
elif current_player == 'O':
current_player = 'X'
return
# Whose turn it is to play
def handle_turn(player):
print(player + "'s turn")
position = int(input('Please write your position from 1 - 9: ')) - 1
if position not in [0,1,2,3,4,5,6,7,8,9]:
return input('Invalid position. Please write 1-9: ')
board[position] = player
display_board()
board_spaces -= 1
# Main gameplay function
def play_game():
global winner
# Displays initial board
display_board()
# Loop running the game
while game_still_going:
handle_turn(current_player)
flip_player()
check_if_game_over()
if winner == 'X' or winner == 'O':
print(winner + ' won.')
elif winner:
print('Tie')
play_game()