我该如何解决这个游戏,以便在中间一栏填满时不会显示并列?

问题描述 投票:-2回答:1

你好,我是编码的新手,我做了这个井字游戏,但是每当我玩它并用X或O填充中间栏时,它告诉我这是平局,而不是告诉我获胜者是X或O。如何解决这个问题,以便如果X填满所有中间列或X填满所有中间列,它不会说这是平局,但会告诉我谁是赢家?

#Game Board
board = ["-","-","-",
         "-","-","-",
         "-","-","-",]


#Game still going
game_still_going = True

#No winner yet
winner = None 

#firt player turn is X
current_player = "X"


#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 single turn of player
    handle_turn(current_player)


    #check if the game has ended
    check_if_game_over()


    #Flip to the other player
    flip_player()


  #The game has ended
  if winner == "X" or winner == "O":
    print(winner + " WON!!")
  elif winner == None:
    print("Tie!")


#Display Board
def display_board():
  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")


def handle_turn(player):

  print("")
  print(player + "'s turn")
  position = input("Choose a position from 1 to 9: ")


  valid = False
  while not valid:


    while position not in ["1", "2", "3", "4", "5", "6", "7", "8", "9"]:
      position = input("Invalid input. Choose a position from 1 to 9: ")


    position = int(position) - 1


    if board[position] == "-":
      valid = True
    else:
      print("You cant go there. Go Again.")


  board[position] = player


  display_board()


def check_if_game_over():
  check_for_winner()
  check_for_tie()


def check_for_winner():
  global winner
  # check for rows
  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



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]
  if row_2:
    return board[3]
  if row_3:
    return board[6]
  else:
    return None


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]
 if column_1:
  return board[1]
 if column_1:
  return board[2]
 else:
   return None


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_1:
   return board[0]
  if diagonal_2:
   return board[2]
  else:
    return None


def check_for_tie():
  global game_still_going
  if "-" not in board:
    game_still_going = False
    return True
  else:
    return False



def flip_player():
  global current_player
  if current_player == "X":
    current_player = "O"
  elif current_player == "O":
    current_player = "X"



play_game()
python tic-tac-toe
1个回答
0
投票

[如果您查看def check_columns():,则在“ if”语句中重复column_1,并且不使用column_2column_3(如mkrieger1所评论)。可能只是剪切和粘贴错误。

[当玩家完成第2列或第3列时,您的代码会给出相同的错误“平局”。

© www.soinside.com 2019 - 2024. All rights reserved.