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

我创建了一个基本的井字游戏,除了检查大多数 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")


我想让游戏检查你是否连续得到 X 三次。相反,游戏不会为 X 案例注册它,但对于 O 来说工作正常。


PIPIONE
浏览 150回答 1
1回答

Cats萌萌

更新您的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。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python