我不知道赢家的错误在哪里,它不识别,但它仍然识别出平局,请帮助,我仍然是初学者,谢谢。 我已经在荧屏上主演了3个小时,仍然无法解决这个问题,而且,我也在论坛上看过,但什么也没找到
#------全局变量------
# Will hold our game board data
board = ["-", "-", "-",
"-", "-", "-",
"-", "-", "-",]
#If game is still going
game_still_going = True
#Tell us who won
winner = None
#Tell us who goes first, x goes first
current_player = "X"
#---------------FUNCTIONS---------------
#Play a game of tic tac toe
def play_game():
#Display initial board
display_board()
#While the game is still going
while game_still_going:
# Handle a turn
handle_turn(current_player)
# Check if the game is over
check_if_game_over()
# Flip to the other player
flip_player()
# Since the game is over, print the winner or tie
if winner == "X" or winner == "O":
print(winner + " won.")
elif winner == None:
print("Tie.")
# Display the game board to the screen
def display_board():
print("\n")
print(board[0] + " | " + board[1] + " | " + board[2] + " 1 | 2 | 3")
print(board[3] + " | " + board[4] + " | " + board[5] + " 4 | 5 | 6")
print(board[6] + " | " + board[7] + " | " + board[8] + " 7 | 8 | 9")
print("\n")
#Handle a single turn of an arbitrary player
def handle_turn(player):
#get position from player
print(player + "'s turn. ")
position = input("Choose a position from 1-9: ")
print()
# Whatever the user inputs, make sure it is a valid input, and the spot is open
valid = False
while not valid:
#Make sure the input is correct
while position not in ["1", "2", "3", "4", "5", "6", "7", "8", "9"]:
position = input("Choose a position from 1-9: ")
# Get correct index in our board list
position = int(position) - 1
# Then also make sure the spot is available on the board
if board[position] == "-":
valid = True
else:
print("You can't go there, go again. ")
print()
# Put the game piece on the board
board[position] = player
# Show the game board
display_board()
# Check if the game is over
def check_if_game_over():
check_for_winner
check_for_tie()
#Check if someone won the game
def check_for_winner():
# Set global variable
global winner
# Check if there was a winner anywhere
row_winner = check_rows()
column_winner = check_columns()
diagonal_winner = check_diagonals()
#Get the winner
if row_winner:
winner = row_winner
elif column_winner:
winner = column_winner
elif diagonal_winner:
winner = diagonal_winner
else:
winner = None
#Looking for winner in rows
def check_rows():
#Set up global variables
global game_still_going
#Checking if the rows got the same value and are not empty
row_1 = board[0] == board[1] == board[2] != "-"
row_2 = board[3] == board[4] == board[5] != "-"
row_3 = board[6] == board[7] == board[8] != "-"
#If any row does have a match, flag that there is a win
if row_1 or row_2 or row_3:
game_still_going = False
#return the winner X or O
if row_1:
return board[0]
elif row_2:
return board[3]
elif row_3:
return board[6]
else:
return None
#Looking for winner in columns
def check_columns():
#Set up global variables
global game_still_going
#Checking if the column got the same value and are not empty
column_1 = board[0] == board[3] == board[6] != "-"
column_2 = board[1] == board[4] == board[7] != "-"
column_3 = board[2] == board[5] == board[8] != "-"
#If any column does have a match, flag that there is a win
if column_1 or column_2 or column_3:
game_still_going = False
#return the winner X or O
if column_1:
return board[0]
elif column_2:
return board[1]
elif column_3:
return board[2]
# Or return None if there was no winner
else:
return None
#Looking for a winner in diagonals
def check_diagonals():
#Set up global variables
global game_still_going
#Checking if the diagonal got the same value and are not empty
diagonal_1 = board[0] == board[4] == board[8] != "-"
diagonal_2 = board[2] == board[4] == board[6] != "-"
#If any diagonal does have a match, flag that there is a win
if diagonal_1 or diagonal_2:
game_still_going = False
#return the winner X or O
if diagonal_1:
return board[0]
elif diagonal_2:
return board[2]
else:
return None
#Looking if there's a tie
def check_for_tie():
#Global variable
global game_still_going
#if the board is full
if "-" not in board:
game_still_going = False
# Else there is no tie
else:
return False
#Changing players time a time
def flip_player():
#Global variable we need
global current_player
#If the current player was x, then change it to O
if current_player == "X":
current_player = "O"
elif current_player == "O":
current_player = "X"
#--------Start the application----------
play_game()
在
check_if_game_over
函数中,没有正确调用check_for_winner
函数。您需要在后面添加括号才能正确调用它在大多数函数中,您遗漏了全局[variable],我发现了一些遗漏的变量,请逐行参阅下面的代码:
有关全球的更多信息,请阅读本手册: https://www.google.com/url?sa=t&source=web&rct=j&url=https://www.geeksforgeeks.org/global-keyword-in-python/amp/&ved=2ahUKEwi2gbeo_fDoAhVo7XMBHZvjAHIQFjAPegQICxAy&usg=AOvVaw0fpoalAG4dROAIz3PlXcQo&cf=1
相关问题 更多 >
编程相关推荐