'list'对象没有属性'board'(Matrix)

时间:2016-04-12 19:44:42

标签: python python-3.x

class GameBoard(object):

    def __init__(self,blackp='X',whitep='O',counter=0):
            self.board

    def boardinit(self):
            self.board=[['+' for row in range(17)]for col in range(17)]
            def setboarder(self):
                    for hor in range(17):
                            self.board[0][hor]= board[16][hor]='--'
                    for side in range(17):
                            self.board[side][0]=board[side][16] = '|' 
                    return board
            self.board = setboarder(self.board)

我想知道如何使用OOP初始化矩阵游戏板,我发现的示例来自:https://github.com/ChenghaoQ/Pyex/blob/master/2048.py,在这种情况下,代码在哪里声明 self.field ?< / p>

1 个答案:

答案 0 :(得分:1)

在将self.board.board传递给内部函数时设置它。这是一种更好的方法,可以更快地运行并减少错误:

def boardinit(self):
    board=[['+' for row in range(17)]for col in range(17)]
    # Set border
    for hor in range(17):
        board[0][hor]= board[16][hor]='--'
    for side in range(17):
        board[side][0]=board[side][16] = '|'

    self.board = board