This is a simulation of a predator-prey relationship between foxes and rabbits. A simple model of a predator-prey relationship might have, say, 100 prey and 10 predators distributed across a plain; the prey would then be eliminated according to the proximity of a predator and the chances of the predator catching the prey. For example, if a prey were no more than 2 spaces from a predator then the latter would claim its meal, otherwise the prey would survive to the next round. A more sophisticated version might base the chance of being eaten on some probability value such as 0.3.
This model is more sophisticated. A field is divided into a grid of 50x50. A grid square may contsin a fox or a rabbit or nothing. There is a superclass called Animal from which the Fox and Rabbit classes are derived. The constructor for Animal sets age to 0 and alive to true. It includes a method setDead(), which removes the object from the simulation. It includes three other methods to set and get the age of an animal and to get a Field. The age and hunger of each fox is taken into account and the fox may die of either. Foxes and Rabbits can breed into adjacent cells, as long as they are not already occupied. Foxes and rabbits can only breed when they reach their breeding age.
This is the world for the simulation.
What are the dimensions of the field? How is a field created at run-time?
How is a field populated with foxes and rabbits?
How many more rabbits than foxes are there?
How would you change the balance of foxes and rabbits?
What are the images for the fox and rabbit? What are their dimensions?
What type of value does randomAdjacentLocation return?
What range of values is generated by getRandomNumber in randomAdjacentLocation ?
Explain what is going on in the if..else block.
What do the methods freeAdjacentLocation and adjacentLocations do?
Check out Plotter on the Helper classes page. What parameters does the constructor for a plotter require?
What is a location?
This is a parent class for Fox and Rabbit. What word is used to describe the link between Animal and the other two classes?
Explain the function of the Animal class in relation to Fox and Rabbit. Comment on all the functions/methods in Animal.
What does 'super()' do in Fox?
Where does the value of randomAge come from? What are the origins of a fox whose randomAge is true and one whose randomAge is false? (Field creates animals with age > 0, newly bred foxes have age = 0.)
What is the role of foodLevel?
What determines whether a fox will live? What would make a fox die?
Where is the function isAlive() located?
What determines whether a fox can breed?
What determines how many cubs a fox will have from the maximum possible?
What locations will new foxes (cubs) occupy?
How does a fox find out if there is a rabbit nearby?
At what age does a fox die?
Explain the meaning of 'List rabbits' and 'rabbits(0)'.
Explain what the getNeighbours() function does. How many cells does this call examine?
What type of data does findFood return?
What does findFood tell Fox.act() about the surrounding squares?
Where is setDead located? Why is it here instead of in Fox and Rabbit?
What does the fox do if it finds no rabbits in the adjacent squares?
What does the fox do if it does find a rabbit in the adjacent squares?
Describe and explain the differences between the Fox and Rabbit classes.
Use the Animated Actor video to help you with this exercise.
Create an animated actor. Download the AnimatedActor class from the Helper Classes page and paste the code into a new class under actor. Create a sequence of images with a base name such as 'stickman', a file suffix such as 'jpg' (.png allows transparency) and a sequence of number after the base name (0, 1, 2...).
Use the Running, jumping and falling video to help you with this exercise.
Create a game with a main character and platforms. Write code so that the main character can move left and right. Add code to make the character fall. Add code to make the character jump. Add code to make the code stop if the character falls to the bottom of the screen. Game play should involve the character moving from one platform to another.