井字游戏中发生错误,但没有错误输出

时间:2019-03-28 20:26:57

标签: python

我已经创建了一个基本的Tic TAC Toe游戏,除了检查大多数X获胜情况外,其他一切似乎都可以正常工作。 O在所有情况下都能正常工作,但X不能。我不明白怎么了。

我试图将获胜条件合并为一组,但这没有用。我没有看到任何错误。

def def_board():
  global board
  empty = "| |"
  board = {'1':empty, '2':empty, '3':empty, '4':empty, '5':empty, '6':empty, '7':empty, '8':empty, '9':empty,}
  print("Board:")
  print('|1|2|3|\n|4|5|6|\n|7|8|9|')

def board_check_x():
  board_check_board = ["null", board['1'], board['2'], board['3'] ,board['4'],board['5'],board['6'],board['7'],board['8'],board['9']]
  if "|X|" == board_check_board[1] and "|X|" == board_check_board[2] and "|X|" == board_check_board[3]:
    player_win("2")
  if "|X|" == (board_check_board[4] and "|X|" == board_check_board[5] and "|X|" == board_check_board[6]):
    player_win("2")
  if "|X|" == (board_check_board[7] and "|X|" == board_check_board[8] and "|X|" == board_check_board[9]):
    player_win("2")
  if "|X|" == (board_check_board[1] and "|X|" == board_check_board[4] and "|X|" == board_check_board[7]):
    player_win("2")
  if "|X|" == (board_check_board[2] and "|X|" == board_check_board[5] and "|X|" == board_check_board[8]):
    player_win("2")
  if "|X|" == (board_check_board[3] and "|X|" == board_check_board[6] and "|X|" == board_check_board[9]):
    player_win("2")
  if "|X|" == (board_check_board[1] and "|X|" == board_check_board[5] and "|X|" == board_check_board[9]):
    player_win("2")
  if "|X|" == (board_check_board[3] and "|X|" == board_check_board[5] and "|X|" == board_check_board[7]):
    player_win("2")

def board_check_o():
  board_check_board = ["null", board['1'], board['2'], board['3'] ,board['4'],board['5'],board['6'],board['7'],board['8'],board['9']]
  if "|O|" == board_check_board[1] and "|O|" == board_check_board[2] and "|O|" == board_check_board[3]:
    player_win("1")
  elif "|O|" == board_check_board[4] and "|O|" == board_check_board[5] and "|O|" == board_check_board[6]:
    player_win("1")
  elif "|O|" == board_check_board[7] and "|O|" == board_check_board[8] and "|O|" == board_check_board[9]:
    player_win("1")
  elif "|O|" == board_check_board[1] and "|O|" == board_check_board[4] and "|O|" == board_check_board[7]:
    player_win("1")
  elif "|O|" == board_check_board[2] and "|O|" == board_check_board[5] and "|O|" == board_check_board[8]:
    player_win("1")
  elif "|O|" == board_check_board[3] and "|O|" == board_check_board[6] and "|O|" == board_check_board[9]:
    player_win("1")
  elif "|O|" == board_check_board[1] and "|O|" == board_check_board[5] and "|O|" == board_check_board[9]:
    player_win("1")
  elif "|O|" == board_check_board[3] and "|O|" == board_check_board[5] and "|O|" == board_check_board[7]:
    player_win("1")

我希望游戏检查一下您是否连续三次获得X。而是不为X案例注册游戏,但为O正常运行。

1 个答案:

答案 0 :(得分:0)

更新您的board_check_x函数。

尝试一下:

def board_check_x():
  board_check_board = ["null", board['1'], board['2'], board['3'] ,board['4'],board['5'],board['6'],board['7'],board['8'],board['9']]
  if "|X|" == board_check_board[1] and "|X|" == board_check_board[2] and "|X|" == board_check_board[3]:
    player_win("2")
  elif "|X|" == board_check_board[4] and "|X|" == board_check_board[5] and "|X|" == board_check_board[6]:
    player_win("2")
  elif "|X|" == board_check_board[7] and "|X|" == board_check_board[8] and "|X|" == board_check_board[9]:
    player_win("2")
  elif "|X|" == board_check_board[1] and "|X|" == board_check_board[4] and "|X|" == board_check_board[7]:
    player_win("2")
  elif "|X|" == board_check_board[2] and "|X|" == board_check_board[5] and "|X|" == board_check_board[8]:
    player_win("2")
  elif "|X|" == board_check_board[3] and "|X|" == board_check_board[6] and "|X|" == board_check_board[9]:
    player_win("2")
  elif "|X|" == board_check_board[1] and "|X|" == board_check_board[5] and "|X|" == board_check_board[9]:
    player_win("2")
  elif "|X|" == board_check_board[3] and "|X|" == board_check_board[5] and "|X|" == board_check_board[7]:
    player_win("2")

如果您的o工作,这应该适用于x。我只是将您的o函数重构为在我的编辑器中使用两个快速正则表达式来查找x。