Write a Python class to simulate an ecosystem containing two types of creatures, bears…
- User
Write a Python class to simulate an ecosystem containing two types of creatures, bears and fish. The ecosystem consists of a river, which is modeled as a relatively large list. Each element of the list should be a Bear object, a Fish object, or None. In each time step, based on a random process, each animal either attempts to move into an adjacent list location or stay where it is. If two animals of the same type are about to collide in the same cell, then they stay where they are, but they create a new instance of that type of animal, which is placed in a random empty (i.e., previously None) location in the list (if there is no empty location in the list, the newly generated animal will be discarded). If a bear and a fish collide, however, then the fish dies (i.e., it disappears). Write an initializer for the ecosystem class, the initializer should allow the user to assign the initial values of the river length, the number of fishes and the number of bears. Before the simulation, fishes and bears should be allocated randomly into the river. The ecosystem class should also contain a simulation() method, which will simulate the next N steps of the random moving process. N should be inputted by the user. In each step of your simulation, all animals in the river should try to take some random moves. In each step of your simulation, the animals should take actions one by one. The animals on the left will take action first. The newly generated animals will NOT take actions in the current step.
- Assistant
Here's the implementation of the
Ecosystemclass that simulates the ecosystem containing bears and fish:import random class Bear: def __str__(self): return "B" class Fish: def __str__(self): return "F" class Ecosystem: def __init__(self, river_length, num_fishes, num_bears): self.river = [None] * river_length self.num_fishes = num_fishes self.num_bears = num_bears self.allocate_animals(Fish, num_fishes) self.allocate_animals(Bear, num_bears) def allocate_animals(self, animal_type, num_animals): for _ in range(num_animals): index = random.randint(0, len(self.river) - 1) while self.river[index] is not None: index = random.randint(0, len(self.river) - 1) self.river[index] = animal_type() def simulate(self, num_steps): for _ in range(num_steps): for i in range(len(self.river)): if self.river[i] is not None: self.move_animal(i) def move_animal(self, index): movement = random.randint(-1, 1) new_index = index + movement if new_index < 0 or new_index >= len(self.river): return if self.river[new_index] is None: self.river[new_index] = self.river[index] self.river[index] = None else: if type(self.river[new_index]) == type(self.river[index]): self.create_new_animal() else: if type(self.river[new_index]) == Fish: self.river[new_index] = self.river[index] else: self.river[index] = None def create_new_animal(self): empty_indices = [i for i in range(len(self.river)) if self.river[i] is None] if empty_indices: index = random.choice(empty_indices) self.river[index] = type(self.river[index])() def __str__(self): return ' '.join(str(animal) if animal is not None else '-' for animal in self.river)