实例化在python对象中失败

时间:2014-11-18 13:34:21

标签: python class instance

我在python中的第一次体验有点问题。我正在做一个小游戏,比如迷宫。我创建了一个类Map,当我实例化它时,它可以工作。当我试图将同一个类实例化两次时,我遇到了一些问题。

这是代码:

from random import randint
class Map:
    """Class that generates map"""
    width = 0
    height = 0
    map = list()
    dictLine = dict()

    def __init__(self, width, height):
        """Constructor takes width and height in input"""
        self.width = width
        self.height = height
        for i in range(0, self.width * self.height):
            self.map.append('.')
        self.__defineMap()
        self.__defineEntrance()

    def __defineMap(self):
        """Defines Map in a dict"""
        index = 0
        for i in range(0,self.height):
            self.dictLine[index] = self.map[index:index + self.width]
            index = index + self.width

    def printMap(self):
        """Function that prints Wumpus World Map"""
        for i in range(0, self.width * self.height):
            if(i % self.width == 0):
                print()
            print(self.map[i], end='   ')
        print()

    def __defineEntrance(self):
        """Random entrance defined at game start"""
        state = False
        while state is False:
            randomEntrance = randint(0,(self.width * self.height)-1)
            self.map[-self.width:]
            if randomEntrance in range(0,self.width):
                #if entrance is at the first line
                self.map[randomEntrance] = 'E'
                return True
            elif randomEntrance % self.width == 0:
                #if entrance is on the left side
                self.map[randomEntrance] = 'E'
                return True
            for key in self.dictLine.keys():
                #da vedere
                if key + self.width - 1 == randomEntrance:
                    self.map[randomEntrance] = 'E'
                    return True
            l = list()
            for key in self.dictLine.keys():
                l.append(key)
            l.sort()
            l.reverse()
            if randomEntrance in range(l[0], l[0] + self.width):
                self.map[randomEntrance] = 'E'
                return True
        return False

    def reset(self):
        self.__init__(self.width, self.height)

结果如下:

>>> map = Map(6,6)
>>> map.printMap()

.   .   .   E   .   .   
.   .   .   .   .   .   
.   .   .   .   .   .   
.   .   .   .   .   .   
.   .   .   .   .   .   
.   .   .   .   .   .  

>>> map2 = Map(7,7)
>>> map2.printMap()

.   .   .   E   .   .   .   
.   .   .   .   .   .   .   
.   .   .   .   .   .   .   
.   .   .   .   .   .   .   
.   .   .   .   .   .   .   
.   .   .   .   .   .   .   
.   .   .   E   .   .   .  

我该如何解决这个问题?谢谢大家!

2 个答案:

答案 0 :(得分:1)

map是一个类属性(在类的所有实例之间共享),而不是实例属性。

如果这不是您想要的行为,请更改它以使其成为实例属性 - 也就是说,将map = list()部分移至__init__内部。您可能希望对widthheightdictLine执行相同的操作。

答案 1 :(得分:0)

嗯,首先要做的事情。

正如Max所述,您的所有变量都是作为属性而非实例创建的,因此您在一个实例中所做的任何更改都会影响其他变量(甚至那些你会创造的)。解决方案是将它们移至__init__

另外,尽量不要使用"保留"话。 map用作内置函数。也许将其更改为maze

我对您的代码做了一些快速更改。有一些改进空间,但它会很好地为您服务:

from random import randint


class Maze(object):
    """Class that generates maze"""

    def __init__(self, columns, rows):
        """Constructor takes columns and rows in input"""
        self.columns = columns
        self.rows = rows
        self.reset()

    def reset(self):
        # Building maze.
        self.__maze_cell_mapping = []
        for i in xrange(self.columns):
            for j in xrange(self.rows):
                self.__maze_cell_mapping.append((i, j))

        # Defining entrance.
        self.__entrance = None
        self.__define_entrance()

    def __define_entrance(self):
        """Random entrance defined at game start"""
        while self.__entrance is None:
            cell_id = randint(0, len(self.__maze_cell_mapping))
            if self.__is_boundary(cell_id):
                self.__entrance = self.__maze_cell_mapping[cell_id]

    def __is_boundary(self, cell_id):
        """
        :param int cell_id:
            ID of the cell to check.

        :rtype: bool
        :returns:
            ``True`` if given ``cell_id`` is at maze boundary.
        """
        column, row = self.__maze_cell_mapping[cell_id]
        if column == 0 or column == self.columns - 1:
            return True
        elif row == 0 or row == self.rows - 1:
            return True
        else:
            return False

    def print_maze(self):
        """Function that prints Wumpus World Maze"""
        result = [
            ['.'] * self.columns
            for _i in xrange(self.rows)
        ]

        column, row = self.__entrance
        result[column][row] = 'E'

        for row in result:
            print('   '.join(row))

希望它有所帮助!