不同节点的列表仅生成列表中的一个节点

时间:2013-03-21 22:15:11

标签: python list nodes

我需要帮助。我的代码不会生成不同节点的列表,而是它们都是相同的,我不知道为什么。

def init(logger, playerId, numWalls, playerHomes):

"""
    Part 1 - 4

    The engine calls this function once at the beginning of the game.
    The student player module uses this function to initialize its data
    structures to the initial game state.

    Parameters
        logger: a reference to the logger object. The player model uses
            logger.write(msg) and logger.error(msg) to log diagnostic
            information.

        playerId: this player's number, from 1 to 4

        numWalls: the number of walls each player is given initially

        playerHomes: An ordered tuple of player locations
                     A location is a 2-element tuple of (row,column).
                     If any player has already been eliminated from
                     the game (rare), there will be a bool False in
                     that player's spot in the tuple instead of a
                     location.

    returns:
        a PlayerData object containing all of this player module's data
"""

class Node:
#online lecture notes for class

    """
     Node represents a node in a graph using adjacency lists.
     Node.name is a String.
     Node.neighbors is a ListOfNode.
    """


    __slots__ = ( 'name', 'neighbors' )

    def __init__(self, name):
        """
        __init__: Node * String -> None
        Constructs a node object with the given name and no neighbors.
        """
        self.name = name
        self.neighbors = []

NodeList = []
row = 0
col = 0
while row < 9:
    while col < 9:
        Node.name = (row,col)
        Node.neighbors = neigh(Node.name)
        print(str(Node))
        NodeList.append(Node)
        col = col + 1
        print(str(Node.name))
        print(str(Node.neighbors))
    col = 0
    row = row + 1
count = 0

print(NodeList[0])
print(Node.neighbors)


def neigh(node):
    name=node
    if name == (0, 0): #top left corner
        neighbors = [ None, ( 0+1, 0 ), ( 0, 0+1), None]
    elif name == (0, 8): #top right corner
        neighbors = [ None, ( 0+1, 8 ), None, (0, 8-1)]
    elif name == (8, 0): #bot left corner
        neighbors = [ (8-1, 0) , None, (8, 0+1), None]
    elif name == (8, 8): #bot right corner
        neighbors = [ (8-1, 8), None, None, (8,8-1)]
    elif name[0] < 1:
        neighbors = [ None, (name[0]+1, name[1] ), (name[0], name[1]+1 ), (name[0], name[1]-1)]
    elif name[0] > 7:
        neighbors = [ (name[0]-1, name[1] ), None, (name[0], name[1]+1 ), (name[0]-1, name[1]-1)]
    elif name[1] < 1:
        neighbors = [ (name[0]-1, name[1] ), (name[0]+1, name[1] ), (name[0], name[1]+1 ), None]
    elif name[1] > 7:
        neighbors = [ (name[0]-1, name[1] ), (name[0]+1, name[1] ), None, (name[0], name[1]-1)]
    else:
        neighbors = [ (name[0]-1, name[1] ), (name[0]+1, name[1] ), (name[0], name[1]+1 ), (name[0], name[1]-1)]

    return neighbors

init(1, 1, 1, 1)

1 个答案:

答案 0 :(得分:1)

下面:

    Node.name = (row,col)
    Node.neighbors = neigh(Node.name)

您需要创建Node的实例:

    node = Node()
    node.name = (row,col)
    node.neighbors = neigh(node.name)

并始终使用node代替Node