为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PIPIONE 2021-12-21 10:45:40
我创建了一个基本的井字游戏,除了检查大多数 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 来说工作正常。
查看完整描述

1 回答

?
Cats萌萌

TA贡献1805条经验 获得超9个赞

更新您的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。


查看完整回答
反对 回复 2021-12-21
  • 1 回答
  • 0 关注
  • 149 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信