|
我在写个小游戏的时候碰到了问题,请大侠帮忙看看。
Game类如果用第一个__init__方法执行,如果修改g1的cells[0],g2也发生变化
如果用第二个__init__方法执行就没事,不知道问题出在哪
class Cell:
pos=(9, 9)
value=-1
def __init__(self, value, pos):
self.value, self.pos = value, pos
def __lt__(self, cell):
if self.value<cell.value: return True
return True if self.value==cell.value and self.pos<cell.pos else False
class Game:
cells=[]
step=-1
father=None
#### 1
def __init__(self, ti='001021043100122133233301321343'):
self.cells.clear()
cellCount=int(len(ti)/3)
for i in range(cellCount):
self.cells.append(Cell(int(ti[i*3+2]), (int(ti[i*3]), int(ti[i*3+1]))))
#### 2
def __init__(self, ti='001021043100122133233301321343'):
self.cells.clear()
cellCount=int(len(ti)/3)
self.cells=[ Cell(int(ti[i*3+2]), (int(ti[i*3]), int(ti[i*3+1]))) for i in range(int(len(ti)/3)) ]
self.cells.sort()
g1=Game()
g2=Game()
|
|