Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 皮提塔克酒店_Python_Tic Tac Toe - Fatal编程技术网

Python 皮提塔克酒店

Python 皮提塔克酒店,python,tic-tac-toe,Python,Tic Tac Toe,在纸笔游戏Tic tac toe中,两名玩家轮流在一块3x3正方形的棋盘上标记“X”和“O”。在垂直、水平或对角条纹上连续标记3个“X”或“O”的玩家获胜。编写一个函数,用于确定tic-tac-toe游戏的结果 例子 >>> tictactoe([('X', ' ', 'O'), (' ', 'O', 'O'), ('X', 'X', 'X') ]) "'X' wins (horizontal)." >>

在纸笔游戏Tic tac toe中,两名玩家轮流在一块3x3正方形的棋盘上标记“X”和“O”。在垂直、水平或对角条纹上连续标记3个“X”或“O”的玩家获胜。编写一个函数,用于确定tic-tac-toe游戏的结果

例子

>>> tictactoe([('X', ' ', 'O'), 
               (' ', 'O', 'O'), 
               ('X', 'X', 'X') ])
"'X' wins (horizontal)."
>>> tictactoe([('X', 'O', 'X'), 
...            ('O', 'X', 'O'), 
...            ('O', 'X', 'O') ])
'Draw.'
>>> tictactoe([('X', 'O', 'O'), 
...            ('X', 'O', ' '), 
...            ('O', 'X', ' ') ])
"'O' wins (diagonal)."
>>> tictactoe([('X', 'O', 'X'), 
...            ('O', 'O', 'X'), 
...            ('O', 'X', 'X') ])
"'X' wins (vertical)."




def tictactoe(moves):
for r in range(len(moves)):
    for c in range(len(moves[r])):      
        if moves[0][c]==moves[1][c]==moves[2][c]:
            a="'%s' wins (%s)."%((moves[0][c]),'vertical')
        elif moves[r][0]==moves[r][1]==moves[r][2]:
            a="'%s' wins (%s)."%((moves[r][0]),'horizontal')
        elif moves[0][0]==moves[1][1]==moves[2][2]:
            a="'%s' wins (%s)."%((moves[0][0]),'diagonal')
        elif moves[0][2]==moves[1][1]==moves[2][0]:
            a="'%s' wins (%s)."%((moves[0][2]),'diagonal')
        else:
            a='Draw.'
print(a)
我写了这样的代码,但我的范围不起作用(我想)。因为,它将r和c的值取为3,而不是0,1,2,3。那么,有人能帮我吗?
谢谢

当玩家获胜时,你的循环不会退出。我想试试这样的东西:

def tictactoe_state(moves):
  for r in range(len(moves)):
    for c in range(len(moves[r])):
      if moves[0][c] == moves[1][c] == moves[2][c]:
        return "'%s' wins (%s)." % (moves[0][c], 'vertical')
      elif moves[r][0] == moves[r][1] == moves[r][2]:
        return "'%s' wins (%s)." % (moves[r][0], 'horizontal')
      elif moves[0][0] == moves[1][1] == moves[2][2]:
        return "'%s' wins (%s)." % (moves[0][0], 'diagonal')
      elif moves[0][2] == moves[1][1] == moves[2][0]:
        return "'%s' wins (%s)." % (moves[0][2], 'diagonal')

  # You still have to make sure the game isn't a draw.
  # To do that, see if there are any blank squares.

  return 'Still playing'
另外,我将把检查对角线的
if
语句移出循环。它们不依赖于
r
c

试试这个

def tictactoe(moves):
    for r in range(len(moves)):
        for c in range(len(moves[r])):      
            if moves[0][c]==moves[1][c]==moves[2][c]:
                return "\'%s\' wins (%s)." % ((moves[0][c]),'vertical')
            elif moves[r][0]==moves[r][1]==moves[r][2]:
                return "\'%s\' wins (%s)."%((moves[r][0]),'horizontal')
            elif moves[0][0]==moves[1][1]==moves[2][2]:
                return "\'%s\' wins (%s)."%((moves[0][0]),'diagonal')
            elif moves[0][2]==moves[1][1]==moves[2][0]:
                return "\'%s\' wins (%s)."%((moves[0][2]),'diagonal')
    return 'Draw.'

错误是什么?而且,您的缩进是错误的。for需要比def缩进得更远。请解释为什么这样做。这可以防止在未完全理解的情况下复制和粘贴。