Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.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_List_Nodes - Fatal编程技术网

Python 不同节点的列表只生成一个节点的列表

Python 不同节点的列表只生成一个节点的列表,python,list,nodes,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 d

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

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)
def init(记录器、playerId、numWalls、playerHomes):
"""
第1-4部分
引擎在游戏开始时调用此函数一次。
学生播放器模块使用此函数初始化其数据
结构到初始游戏状态。
参数
logger:对logger对象的引用。播放器模型使用
logger.write(消息)和logger.error(消息)到日志诊断
信息。
玩家ID:这个玩家的号码,从1到4
numWalls:最初给出每个玩家的墙数
玩家之家:玩家位置的有序元组
位置是(行、列)的2元素元组。
如果有任何球员已经被淘汰出局
这场比赛(罕见),将有一个布尔假
那个玩家在元组中的位置而不是
地点。
返回:
包含此播放器模块所有数据的PlayerData对象
"""
类节点:
#在线课堂讲稿
"""
节点使用邻接列表表示图形中的节点。
Node.name是一个字符串。
Node.neights是一个ListOfNode。
"""
__插槽(名称、邻居)
定义初始化(self,name):
"""
__初始化:节点*字符串->无
构造具有给定名称且没有邻居的节点对象。
"""
self.name=名称
self.neights=[]
节点列表=[]
行=0
col=0
当行<9时:
而col<9:
Node.name=(行、列)
Node.neights=neigh(Node.name)
打印(str(节点))
NodeList.append(节点)
col=col+1
打印(str(Node.name))
打印(str(Node.neights))
col=0
行=行+1
计数=0
打印(节点列表[0])
打印(节点.邻居)
def neigh(节点):
名称=节点
如果名称==(0,0):#左上角
邻居=[无,(0+1,0),(0,0+1),无]
elif名称==(0,8):#右上角
邻居=[无,(0+1,8),无,(0,8-1)]
elif名称==(8,0):#机器人左角
邻居=[(8-1,0),无,(8,0+1),无]
elif名称==(8,8):#机器人右角
邻居=[(8-1,8),无,无,(8,8-1)]
elif名称[0]<1:
邻居=[None,(名称[0]+1,名称[1]),(名称[0],名称[1]+1),(名称[0],名称[1]-1)]
elif名称[0]>7:
邻居=[(名称[0]-1,名称[1]),无,(名称[0],名称[1]+1),(名称[0]-1,名称[1]-1)]
elif名称[1]<1:
邻居=[(名称[0]-1,名称[1]),(名称[0]+1,名称[1]),(名称[0],名称[1]+1),无]
elif名称[1]>7:
邻居=[(名称[0]-1,名称[1]),(名称[0]+1,名称[1]),无,(名称[0],名称[1]-1)]
其他:
邻居=[(名称[0]-1,名称[1]),(名称[0]+1,名称[1]),(名称[0],名称[1]+1),(名称[0],名称[1]-1)]
回乡
初始(1,1,1,1)
此处:

    Node.name = (row,col)
    Node.neighbors = neigh(Node.name)
您需要创建
节点的实例

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

并使用
node
而不是
node
贯穿始终。

您能解释什么不起作用吗?此代码生成一个节点列表,该列表仅为node.name(8,8),我需要列表中的所有编号(8,8)。此部分没有将所有正确的节点添加到列表中。我不知道为什么。while col<9:Node.name=(row,col)Node.neigh=neigh(Node.name)print(str(Node))NodeList.append(Node)您对
行和
列使用了哪些while语句?我建议对带有
range()
函数的
语句使用
。示例:
对于范围(10)中的行:
这将迭代变量
从0到9,而无需每次手动重置初始值,并在每个循环中递增值。添加Node()的实例似乎无法解决此问题。。。然后我又一次不确定是否应该在Node()的括号中抛出一些内容