#python
#python
Вопрос:
Я следую руководству по python и создаю игру в крестики-нолики, и после проверки того, кто выиграл, я заметил, что всякий раз, когда я проверяю diagonal_1, переменной winner является тире, когда она должна быть X, во всех остальных случаях проверка работает.
# ---- Global Variables ----
# Game Board
board = ["-", "-", "-",
"-", "-", "-",
"-", "-", "-"]
# If game is still going
game_still_going = True
# Who won or tie
winner = None
# Whose turn is it
current_player = "X"
def display_board():
print(board[0] " | " board[1] " | " board[2])
print(board[3] " | " board[4] " | " board[5])
print(board[6] " | " board[7] " | " board[8])
def play_game():
# Display initial board
display_board()
while game_still_going:
handle_turn(current_player)
check_if_game_over()
flip_player()
# The game has ended
if winner == "X" or winner == "O":
print(winner " won")
elif winner is None:
print("Tie.")
def handle_turn(player):
position = int(input("Choose a position from 1 to 9: ")) - 1
board[position] = "X"
display_board()
def check_if_game_over():
check_for_winner()
check_if_tie()
def check_for_winner():
# Set up global variable
global winner
row_winner = check_rows()
column_winner = check_columns()
diagonal_winner = check_diagonals()
if row_winner:
winner = row_winner
elif column_winner:
winner = column_winner
elif diagonal_winner:
winner = diagonal_winner
else:
winner = None
return
def check_rows():
global game_still_going
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 row_1 or row_2 or row_3:
game_still_going = False
if row_1:
return board[0]
elif row_2:
return board[3]
else:
return board[6]
def check_columns():
global game_still_going
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 column_1 or column_2 or column_3:
game_still_going = False
if column_1:
return board[0]
elif column_2:
return board[1]
else:
return board[2]
def check_diagonals():
global game_still_going
diagonal_1 = board[0] == board[4] == board[8] != "-"
diagonal_2 = board[2] == board[4] == board[6] != "-"
if diagonal_1 or diagonal_2:
game_still_going = False
if diagonal_2:
return board[2]
elif diagonal_1:
return board[0]
def check_if_tie():
return
def flip_player():
return
play_game()
Это весь код для игры. Я пытался выяснить, почему это работает не так, как предполагалось, но я не могу понять это.
Также я впервые использую Stack Overflow, поэтому извините, если мое форматирование не подходит, я все еще учусь.
Ответ №1:
Когда вы проверяете строки и столбцы, функции всегда возвращают значение, даже если победителя нет. Они ничего не должны возвращать, если нет победителя.
Вот обновленный код:
def check_rows():
global game_still_going
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 row_1 or row_2 or row_3:
game_still_going = False
if row_1:
return board[0]
elif row_2:
return board[3]
elif row_3:
return board[6]
# return nothing
def check_columns():
global game_still_going
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 column_1 or column_2 or column_3:
game_still_going = False
if column_1:
return board[0]
elif column_2:
return board[1]
elif column_3:
return board[2]
# return nothing
def check_diagonals():
global game_still_going
diagonal_1 = board[0] == board[4] == board[8] != "-"
diagonal_2 = board[2] == board[4] == board[6] != "-"
if diagonal_1 or diagonal_2:
game_still_going = False
return board[4] # diagonal always crosses center
# return nothing