0% found this document useful (0 votes)
5 views3 pages

Oop Enonce

This document outlines a project to simulate an ocean environment with fish and sharks, detailing the rules of movement, reproduction, and energy management for each species. It describes the structure of the classes involved, including Animal, Fish, Shark, and Ocean, along with methods for initializing the ocean, handling animal movements, and managing the simulation ticks. The document emphasizes the importance of maintaining the grid structure and provides guidance on implementing the simulation effectively.

Uploaded by

c
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Oop Enonce

This document outlines a project to simulate an ocean environment with fish and sharks, detailing the rules of movement, reproduction, and energy management for each species. It describes the structure of the classes involved, including Animal, Fish, Shark, and Ocean, along with methods for initializing the ocean, handling animal movements, and managing the simulation ticks. The document emphasizes the importance of maintaining the grid structure and provides guidance on implementing the simulation effectively.

Uploaded by

c
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

fish and sharks

1 Introduction
The aim of this project is to emulate the evolution of a small ocean, with fish and sharks living in it.

Here are the mains rules :


— the time moves in ”ticks”. At each ”tick” every animal moves if possible
— animals can move in 4 directions : left, right, above, below
— if an animal leaves the grid it re-enters on the other side (it is not blocked or disappears)
— fish move at random if an empty space is available
— sharks move on a random adjacent fish if there is one, or at random if there are only empty spaces
— an animal with no available cell to move to stays where it is
— after a certain time being alive an animal can reproduce
— sharks have energy which decreases naturally but increases when they eat fish
— a shark with no energy dies
The rules will be detailed in their respectives functions.

Classes include a class Animal, with two subclasses, Fish and Shark, and an Ocean class.

The Fish and Shark classes includes a num class variable that you can use as an easy identifier : if animal is a fish
or a shark object, animal.num will returns either 1 or 2.

A support file (OOPAssignment_support) is provided, and includes a method called show in the Ocean class to
display the animation showing the evolution of the forest. You cannot use it before the last function (tick) is
written.
A __str__ method is provided for the Ocean class for small tests, but it migth not be very useful to debug your
functions. Learn to read the error messages.

In all of the assignment, a position refers to a couple of integers. (Either a list or a tuple, with two values, the
abscissa and ordinate.)

2 First steps
The ocean is represented by a matrix, where each cell can have 3 values : either None, or an instance of the Fish
or Shark classes. For ease of visualisation, the provided __str__ method displays 0, 1, and 2’s, but it important
to remember that cells do not hold integers. You cannot deviate from this without breaking the animation.
The Ocean class also features an attribute holding a list of all the animals in it, that will be useful for the tick
method at the end.
1. Write the initialize method. It takes no argument other than self and spawns fish and sharks in the ocean.
For each cell in the grid, there is a 10% chance for a shark to spawn, and a 30% chance for a fish to spawn. If
not animal spawns, the cell stays empty. Two animals can’t exist at the same time in a cell. For each animal
spawned you need to create a new instance, add it to the grid, add it to the list of animals of the ocean.
You can use rd.randint(a,b) to get random integer between a and b included.

1
CPES2 OOP : Home assignment

You can change the value at the end when testing.


2. Write the surroundings method. It takes self and a position and returns a dictionnary with 3 associations,
for the keys 0, 1, and 2. It explores the four cells adjacent to the position given in the parameters, and returns
for each type of cell (None, Fish, Shark) a list of two elements : the number of adjacent cells of that type, and
the list of the positions of those cells.
Remember that if the cell is on the edge, the adjacent position is on the other side. The operator % might be
useful.

Example : If we take a look at the small ocean below (let’s call it sea) :

| 0 0 1 |
| 2 2 1 |
| 0 1 1 |

sea.surroundings((1,1)) should return :


{0 : [1,[(0,1)]], 1 : [2,[(1,2),(2,1)]], 2 : [1,[(1,0)]]}
et sea.surroundings((1,2)) should return :
{0 : [0,[]], 1 : [2,[(0,2),(2,2)]], 2 : [2,[(1,0),(1,1)]]}

3 The Animals
Now let’s take a look at our animals. We have the Animal class which holds all the methods, and the Fish and
Shark subclasses which will serve to define class variables.
For our animals we want to memorize their position, how long before they reproduce, and wether or not they are
alive. (At the end of each tick, all the dead animals will be removed).
3. Write the __init__ method for the Animal class. It takes the abscissa and ordinate of the animal as argument
and creates 3 attributes : the position, the reproduction value initialized at 0, and wether or not it’s alive as a
boolean.
4. Write the reproduce method. It takes as argument the ocean, and a position, and spawns an a new animal of
the same subclass at the indicated position. Don’t forget to add the new animal to the list of animals of the
ocean, and to reset the reproduction value of the parent.
There are two main ways to get the class of the animal in order to know which animal to spawn : you can use
the type function which return the class of the object, or you can use the num class variable of the animal.
5. Write the move method. It takes the ocean, a dictionary representing the surroundings of the animal, and a cell
type (0, 1, or 2), and moves the animal to a random adjacent cell of the specified type. (For now we assume
such a cell exists, its existence will be checked in another function).
Be careful, there a lot of things to check in this function :
— make sure to empty the cell were the animal was before moving
— if there is an animal in the cell where the animal is moving (for example if a shark eats a fish) mark that
animal as dead
— update the cell where the animal has moved, and change the position of the animal in its attributes
— if the reproduction value of the animal exceed the reproduction threshold of its specie, it reproduces in
its former cell
6. Add two class variables to the Shark class : a starting energy, and a maximum energy (with values 3 and 6
respectively, you will be able to change them once you’re done).
7. Write the __init__ method for the Shark class to add an energy attribute to sharks, with a starting value
equal to the starting energy class variable.
(The Fish class does not have a __init__ method, because it has no additional attributes compared to the
Animal class. Therefore it uses the constructor from its parent class.)

Lycée Chateaubriand 2/3


CPES2 OOP : Home assignment

4 Tying everything together


Now we have almost everything ready. We only need the tick method. The ticks simulates the passage of time.
Since we can’t make all the animals move at the same time (well, we could, but it would be long and complicated),
we will make them move one by one, using the list of animals attribute of the ocean.
To avoid moving them always in the same order, I added a line at the beggining of the tick method that randomly
changes the order of the list.
8. A quick method we need is the removeDeads method. IT change the value of the attribute animals of the ocean
to the same list, without the dead animals. This is where the attribute you’ve specified earlier comes into play.
9. The last function is the tick method. It is not very complicated but you need to be thorough. After the list
of animal has been shuffled, you need to loop through it, and for each animal, if it isn’t dead (because animals
can die during the execution of tick), you need to :
— increase its reproduction value,
— make it move correctly depending on its specie - if a shark eats a fish it gets +3 energy, but cannot go
beyond the max energy variable -, then, wether or not it ate a fish,
— decrease its energy if it’s a shark, and if its energy reaches 0, it dies (make sure to empty the grid at that
position).
Then, after all the animals have moved, remove the dead ones.
After this question is complete, you can start the simulation.

With the initial values for proportions, reproduction rate, energy, neither species should disappear. I have run
simulation of up to 1000 ticks (with the base 50 × 50 grid), but if you want to increase the length of the simulation
from the starting 100 ticks, go up in steps, as Python needs to memorize the grid for each tick before creating the
animation, meaning going for too many ticks risks crashing Python or your computer.
You can also use the function LV (for Lotka-Volterra, a prey-predator model) to plot the number of fish and sharks
through the simulation. And you can also plot the number of sharks as a ”function” of the number of fish. This is
what i got on one of my simulations with 500 ticks.

Lycée Chateaubriand 3/3

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy