Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this exercise, your team will be developing a program that simulates an ecos

ID: 3682544 • Letter: F

Question

For this exercise, your team will be developing a program that simulates an ecosystem. The user will enter the number of each type of organism (you check that this entry is reasonable), and the program randomly distributes these organisms on 12 by 12 grid (more than one organism can occupy a square). At each turn of the game (as triggered by the use clicking a button), the organisms, which have the properties identified below, check to see whether they will take certain actions, which are also specified below. Each organism has: Grass - each unit of grass can produce a new unit of grass every two turns (that is every other turn) - it reproduces in its own square or in an adjacent square (which direction it spreads, including its own square, is random) - it never ages - it only dies through a fire or being eaten - you can have a maximum of ten units of vegetable material (that is, grass and trees combined) on any given square - in the event of a fire, any unit of grass has a 75% chance of dying. Trees - each tree can produce a new tree every five turns - it reproduces in its own square or in an adjacent square - it never ages - it only dies through a fire - you can have a maximum of ten units of vegetable material (that is, grass and trees combined) on any given square - if a tree wants to spread in a particular direction, it can eliminate the grass that is in that square if necessary to avoid violating the ten vegetable unit limitation - in the event of a fire, any unit of grass has a 60% chance of dying. Deer - deer can reproduce once every four turns and they must be in perfect health (note, to simplify coding, we've made deer able to asexually reproduce) - deer are not affected by fire - each deer has health (up to a maximum of eight points - the deer starts with eight) based on eating; each turn that the deer eats (which it does whenever it can), its health goes up by one (until the maximum); if the deer has nothing to eat, its health goes down to eat - during any given move, first the deer should eat, then the deer may move (the deer moves to whatever square adjoining it has the most food, unless the square it is on has the most food); if there is no food in its own square or an adjacent square, the deer will move randomly - deer die once their health reaches zero. When the program starts up, the position and how long since reproduction must be random (otherwise everything reproduces in lockstep). Note: Work on this assignment incrementally (get the basics first, if you can get grass to grow, you are at least half way to finishing). We will be looking to see that you've demonstrated mastery of the concepts in the book (to demonstrate all the concepts, you need to define all of the species, but you can do this without demonstrating complete mastery of the rules.

Explanation / 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   strength 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 Deer(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:

    def __init__(self, length):

        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 0 <= i + move < self._length:

                if self._contents[i + move] == None:

                    self._contents[i + move] = self._contents[i]

                    self._contents[i] = None

                elif type(self._contents[i]) == type(self._contents[i+move]):

                    if self._contents[i].get_gender() != self._contents[i+move].get_gender():

                        #two animals of the same type and different gender mate

                        if type(self._contents[i]) == Bear:

                            self.add_random(Bear())

                        else:

                            self.add_random(Fish())

                    else: #two animals of the same type and gender fight

                        if self._contents[i].get_stren() > self._contents[i+move].get_stren():

                            self._contents[i+move] = self._contents[i]

                        self._contents[i] = None

                else:

                    #bear always kills fish if they encounter eachother

                    if type(self._contents[i]) == Bear:

                        self._contents[i + move] = self._contents[i]

                    self._contents[i] = None

    def update_river(self):

        """Update each cell in the river"""

        for i in range(len(self._contents)):

            self.update_cell(i)

    def print_river(self):

        """Print the river contents in human readable form

           Each cell displays the animal type, strength, and gender between two pipes

           Example: male bear with strength 8    |B8M|

                    female fish with strength 0 |F0F|

        """

        s = '|'

        for x in self._contents:

            if x:

                if type(x) == Bear:

                    s += 'B'

                elif type(x) == Fish:

                    s += 'F'

                s += str(x.get_stren())

                s += x.get_gender()

            else:

                s += '   '

            s += '|'

        print(s)

if __name__ == "__main__":

    river = River(10)

    for i in range(10):

        print("Year ", i)

        river.print_river()

        river.update_river()

python oop python-3.x

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote