Beispiel #1
0
class GameBasic:
    def __init__(self, info, lx, ly, max_age, split_energy, is_debug=False):
        self.universe = Universe(info, lx, ly)
        self.spotties = list()
        self.population = dict()
        self.population_tot = 0
        self.max_age = max_age
        self.split_energy = split_energy
        self._is_debug = is_debug

    def add_spotties_random(self, new_pop, info):
        pop0 = self.spotties.__len__()
        for n in range(0, new_pop):
            self.spotties.append(Spotty(info, self.max_age, self.split_energy))
            self.spotties[n + pop0].enter_map_random(
                self.universe.map['tribe'])
            self.universe.put_universe(self.spotties[n + pop0])

    def add_spotty_positions(self, info, positions):
        new_pop = len(positions)
        pop0 = self.spotties.__len__()
        for n in range(0, new_pop):
            self.spotties.append(Spotty(info, self.max_age, self.split_energy))
            self.spotties[n + pop0].enter_map_position(positions[n])
            self.universe.put_universe(self.spotties[n + pop0])

    def spotty_split_random(self, nth):
        # split the nth spotty
        neighbor1 = self.universe.read_map(self.spotties[nth], 1)
        movement = np.nonzero(neighbor1['tribe'] == 0)
        rand_pos = np.random.randint(0, len(movement[1]), 1)
        movement = movement[1][rand_pos[0]] + 1
        new_pos = find_position_from_movement(self.spotties[nth].position,
                                              movement)
        new = spotty_copy(self.spotties[nth])
        new.enter_map_position(new_pos)
        self.universe.put_universe(new)
        self.spotties.append(new)
        self.spotties[nth].energy = 0
        self.spotties[nth].age += 1

    def spotty_move(self, nth, decision):
        self.universe.delete_universe(self.spotties[nth])
        self.spotties[nth].move(decision)
        self.universe.put_universe(self.spotties[nth])
        self.spotties[nth].age += 1

    def spotty_die(self, nth):
        self.universe.delete_universe(self.spotties[nth])
        self.spotties.__delitem__(nth)

    def update_population_info(self):
        self.population_tot = self.spotties.__len__()
        self.population = dict()
        for s in self.spotties:
            for p in s.info:
                if p in self.population:
                    self.population[p] += 1
                else:
                    self.population[p] = 1