def moveBears(self) : # print 'moveBears starts...' count = 0 bearPositions = self.map.searchItems(self.map.getItems().keys(), ['Bear']) while len(bearPositions) > 0 : # 仍有未处理过的熊 randomWeight = {} for position in bearPositions : randomWeight[position] = 1 position = Auxiliary.randomSelect(randomWeight) if self.moveBear(position) : count += 1 bearPositions.remove(position) # print 'moveBears OK' return count
def moveBear(self, Position) : # print 'moveBear starts...' if self.map.checkValidity(Position) == INSIDE : # 在地图内 item = self.map.getItem(Position) if item and item.getName() == 'Bear' : # 物品是熊 positions = self.map.searchItems(self.map.getNeighbours(Position)) if len(positions) == 0 : # 相邻位置均不空闲 return False randomWeight = {} for position in positions : randomWeight[position] = 1 destination = Auxiliary.randomSelect(randomWeight) if self.map.moveItem(Position, destination) : # 移动成功 return True else : # 移动失败 return False else : return False # 物品不为熊 else : return False # 在地图外
def generateItem(self, RandomWeight) : return Class_Item(Auxiliary.randomSelect(RandomWeight))