Write a Python class to simulate an ecosystem containing two types of creatures,
ID: 3691672 • Letter: W
Question
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 a bear and a fish collide, however, then the fish dies (i.e., it disappearsExplanation / Answer
import random class Animal: """Animals are the inhabitants of the river""" def __init__(self, gender = None, strength = None): """Initialize animal gender gender of the animal (M, F) determines mating or fighting (default random) strength stregth of animal, determines winner of fight (default random) """ if not gender: self._gender = random.choice(['M','F']) else: self._gender = gender if not strength: self._strength = random.randint(0,9) else: self._strength = strength def get_gender(self): """Return the animals gender""" return self._gender def get_stren(self): """Return the animals strength""" return self._strength class Bear(Animal): def __init__(self, gender = None, strength = None): super().__init__(gender, strength) class Fish(Animal): def __init__(self, gender = None, strength = None): super().__init__(gender, strength) class River: """A river is in array containing animals""" def __init__(self, length): """Initialize a river with a random assortment of bears, fish, and empty cells length length of the river """ self._length = length self._contents = [] for i in range(self._length): rval = random.randint(1,3) if rval == 1: self._contents.append(Bear()) elif rval == 2: self._contents.append(Fish()) else: self._contents.append(None) def __len__(self): """Return the length of the river""" return self._length def __getitem__(self, k): """Return the contents of the kth cell in the river list""" return self._contents[k] def __setitem__(self, k, val): """Set the contents of the kth cell in the river list""" self._contents[k] = val def count_none(self): """Count the number of empty cells in the river list""" return self._contents.count(None) def add_random(self, animal): """Add animal to empty cell of river list after mating occurs""" if self.count_none() > 0: choices = [i for i, x in enumerate(self._contents) if x==None] index = random.choice(choices) self._contents[index] = animal def update_cell(self, i): """Update the cell based on rules defined above""" if self._contents[i] != None: move = random.randint(-1,1) #animal can either move forward, back, or stay in place if move != 0 and 0Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.