【问题标题】:Tic-Tac-Toe TypeError: 'NoneType' object has no attribute '__getitem__'井字游戏类型错误:“NoneType”对象没有属性“__getitem__”
【发布时间】:2013-04-21 13:20:07
【问题描述】:

我正在编写井字游戏。我已经完成了大部分程序。但我不断收到以下错误,我不明白我做错了什么。我尝试过不同的格式。

Traceback (most recent call last):
  File "C:/Users/Akshay Sastry/Documents/CS 303E/Tic-Tac-Toe.py", line 66, in <module>
    main()
  File "C:/Users/Akshay Sastry/Documents/CS 303E/Tic-Tac-Toe.py", line 3, in main
    while isWinner(board) == 0 and movesLeft(board) == True:
  File "C:/Users/Akshay Sastry/Documents/CS 303E/Tic-Tac-Toe.py", line 20, in isWinner
    if (b[0][0]=='x') and (b[0][0]==b[0][1]==b[0][2]):
TypeError: 'NoneType' object has no attribute '__getitem__'

这是我的代码:

def main():
    board = makeBoard()
    while isWinner(board) == 0 and movesLeft(board) == True:
        printBoard(board)
        p1row, p1col = input("Enter a row and column for x: ")
        board[p1row][p1col] = 'x'
        if isWinner(board) == 0 and movesLeft(board) == True:
            printBoard(board)
            p2row, p2col = input("Enter a row and column for o: ")
            board[p2row][p2col] = 'o'
    if isWinner(board) != 0:
        print isWinner(board), 'won!'
    else:
        print 'Tie game.'

def makeBoard():
    board = [['*','*','*'],['*','*','*'],['*','*','*']]

def isWinner(b):
    if (b[0][0]=='x') and (b[0][0]==b[0][1]==b[0][2]):
        return 'x'
    elif (b[1][0]=='x') and (b[1][0]==b[1][1]==b[1][2]):
        return 'x'
    elif (b[2][0]=='x') and (b[2][0]==b[1][1]==b[2][2]):
        return 'x'
    elif (b[0][0]=='x') and (b[0][0]==b[1][0]==b[2][0]):
        return 'x'
    elif (b[0][1]=='x') and (b[0][1]==b[1][1]==b[2][1]):
        return 'x'
    elif (b[0][2]=='x') and (b[0][2]==b[1][2]==b[2][2]):
        return 'x'
    elif (b[0][0]=='x') and (b[0][0]==b[1][1]==b[2][2]):
        return 'x'
    elif (b[0][2]=='x') and (b[0][2]==b[1][1]==b[2][0]):
        return 'x'
    elif (b[0][0]=='o') and (b[0][0]==b[0][1]==b[0][2]):
        return 'o'
    elif (b[1][0]=='o') and (b[1][0]==b[1][1]==b[1][2]):
        return 'o'
    elif (b[2][0]=='o') and (b[2][0]==b[1][1]==b[2][2]):
        return 'o'
    elif (b[0][0]=='o') and (b[0][0]==b[1][0]==b[2][0]):
        return 'o'
    elif (b[0][1]=='o') and (b[0][1]==b[1][1]==b[2][1]):
        return 'o'
    elif (b[0][2]=='o') and (b[0][2]==b[1][2]==b[2][2]):
        return 'o'
    elif (b[0][0]=='o') and (b[0][0]==b[1][1]==b[2][2]):
        return 'o'
    elif (b[0][2]=='o') and (b[0][2]==b[1][1]==b[2][0]):
        return 'o'
    else:
        return 0

def printBoard(board):
    for i in range(3):
        for j in range(3):
            print board[i][j],
        print

def movesLeft(board):
    if board[0].count("*") != 0 or board[1].count("*") != 0 or board[2].count("*") != 0:
        return True
    else:
        return False
main()

【问题讨论】:

    标签: python-2.7


    【解决方案1】:

    您的makeBoard() 函数返回None。你应该这样做:

    def makeBoard():
        return [['*','*','*'],['*','*','*'],['*','*','*']]
    

    【讨论】:

    • 哇!不能相信我错过了。谢谢!
    【解决方案2】:

    您的isWinner 函数可以缩小 3 倍,如下所示

    def isWinner(b):
        for i in range(3):
            if (b[i][0] != '*') and (b[i][0]==b[i][1]==b[i][2]): # all rows
                return b[i][0]
            if (b[0][i] != '*') and (b[0][i]==b[1][i]==b[2][i]): # all cols
                return b[0][i]
        if (b[0][0] != '*') and (b[0][0]==b[1][1]==b[2][2]): # tl-br diag
            return b[0][0]
        elif (b[0][2] != '*') and (b[0][2]==b[1][1]==b[2][0]): # bl-tr diag
            return b[0][2]
        else:
            return 0
    

    但是,对于像 connect 4 这样的较大板,您将遍历板上的所有点并编写一个方法,在循环中检查每个方向上的任意距离,而不是对一行可以的每个位置进行硬编码进去吧。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-10
      • 2014-08-14
      • 1970-01-01
      • 2012-12-04
      • 2015-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多