为文件中特定位置的字符创建字典

我正在尝试在文件中搜索 #, M,K, O, D 的实例,并且对于每个实例,在字典中返回 {position(tuple), instance type(#, M,K, O, D)} 。我编写了一个函数来查找文件中实例的坐标,但是我不确定如何使用此信息添加和更新整个字典。这是我到目前为止所拥有的:


def init_game_information(dungeon_name="game1.txt"):

        self._dungeon=load_game(dungeon_name)

        game_dict={}

        with open("game1.txt", "r") as file:

            for line in file:

                for row, line in enumerate(self._dungeon):

                    for col, char in enumerate(line):

                        if WALL in line:

                            x=WALL

                            y=get_positions(self, WALL)

                            dict_WALL={y,x}

                        game_dict.update(dict_WALL)

                        if KEY in line:

                            x=KEY

                            y=get_positions(self, KEY)

                            KEY={y,x}

                        game_dict.update(dict_KEY)

                        if DOOR in line:

                            x=DOOR

                            y=get_positions(self, DOOR)

                            dict_DOOR={y,x}

                        game_dict.update(dict_DOOR)

                        if MOVE_INCREASE in line:

                            x=MOVE_INCREASE

                            y=get_positions(self, MOVE_INCREASE)

                            dict_MOVE={y,x}

                        game_dict.update(dict_MOVE)

                

            

def get_positions(self, entity):

    """ Returns a list of tuples containing all positions of a given Entity

         type.


    Parameters:

        entity (str): the id of an entity.


    Returns:

        )list<tuple<int, int>>): Returns a list of tuples representing the 

        positions of a given entity id.

    """

    positions = []

    for row, line in enumerate(self._dungeon):

        for col, char in enumerate(line):

            if char == entity:

                positions.append((row,col))


    return positions

这是游戏文件/应返回内容的示例:

https://img3.mukewang.com/64f6d70c00018df303120443.jpg

https://img2.mukewang.com/64f6d71c0001935c12070267.jpg

莫回无
浏览 76回答 1
1回答

喵喵时光机

您可以通过 为字典中的键设置值dictionary[key] = value。通过rowand定义一个键col并将其关联到字典:keys = [WALL, KEY, DOOR, MOVE_INCREASE]game_dict = {}with open("game1.txt", "r") as file:&nbsp; &nbsp; for row, line in enumerate(file):&nbsp; &nbsp; &nbsp; &nbsp; for col, char in enumerate(line):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if char in keys:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; game_dict[(row, col)] = char如果您有对象的类(Wall,Key,...),那么您可以执行以下操作:class_dict = { WALL : Wall, KEY : Key, DOOR : Door, MOVE_INCREASE: MoveIncrease}game_dict = {}with open("game1.txt", "r") as file:&nbsp; &nbsp; for row, line in enumerate(file):&nbsp; &nbsp; &nbsp; &nbsp; for col, char in enumerate(line):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if char in class_dict:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; game_dict[(row, col)] = class_dict[char](char)print(game_dict)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python