The name of your C program file must be called animal.c, files with any other name will not be marked.
The world is a SIZE x SIZE two dimensional grid with each position represented as a coordinate (x,y). The integer values of x and y ranges from 0 to SIZE-1. The figure below depicts the world of SIZE set to 10.
0 1 2 3 4 5 6 7 8 9 0 +---+---+---+---+---+---+---+---+---+ | | | | | | | | | | 1 +---+---+---+---+---+---+---+---+---+ | | | | | | | | | | 2 +---+---+---+---+---+---+---+---+---+ | | | | | | | | | | 3 +---+---+---+---+---+---+---+---+---+ | | | | | | | | | | 4 +---+---+---+---+---+---+---+---+---+ | | | | | | | | | | 5 +---+---+---+---+---+---+---+---+---+ | | | | | | | | | | 6 +---+---+---+---+---+---+---+---+---+ | | | | | | | | | | 7 +---+---+---+---+---+---+---+---+---+ | | | | | | | | | | 8 +---+---+---+---+---+---+---+---+---+ | | | | | | | | | | 9 +---+---+---+---+---+---+---+---+---+
4 5 6 4 *---*---* | | | 5 *---O---* | | | 6 *---*---*Write a function wander that would enable the prey to move to a random position in its neighbourhood. The function prototype is given below.
void wander(int *preyX, int *preyY);Note that every random movement of the prey to a new position must be guaranteed to lie within the world.
The predator will always start off at a random position in the grid and face north, so if it is situated at (x,y), a forward move will bring it to (x,y-1). We represent the predator by its position (x,y) as well as its facing direction (deltaX, deltaY). If the predator is north facing, deltaX and deltaY would be 0 and -1 respectively. This enables the forward movement to be encoded as (x+deltaX, y+deltaY).
Write a function turnRight that modifies the (deltaX,deltaY) values according to its present facing direction to its new facing direction. The function prototype is given below.
void turnRight(int *deltaX, int *deltaY);
Write a function forward that moves the predator forward one step in the direction it is facing. The prototype of the function is given below.
void forward(int *x, int *y, int deltaX, int deltaY);Write a function distance that returns the city-block (or Manhattan) distance between the predator and the prey. This is the minimal number of steps needed for the predator to catch hold of the prey following the four major directions. For example, if the predator X is located at (1,4) and the prey O at (4,2), then the distance is 5.
0 1 2 3 4 5 0 +---+---+---+---+---+- | | | | | | 1 +---+---+---+---+---+- | | | | | | 2 +---+---+---+---O---+- | | | | | | 3 +---+---+---+---+---+- | | | | | | 4 +---X---+---+---+---+- | | | | | | 5 +---+---+---+---+---+- | | | | | |The function prototype of distance is given as follows:
int distance(int predX, int predY, int preyX, int preyY);
Repeat {} until the predator catches the prey;
- Let the prey wander to its new neighbouring location;
- Move the predator forward;
- Turn the predator if necessary;
- Print the location of the predator and the prey;
$ gcc -Wall animal.c -o animal $ ./animal Predator(3,7) --> Prey(4,6) with distance 2 Predator(3,6) --> Prey(5,5) with distance 3 Predator(3,5) --> Prey(4,4) with distance 2 Predator(3,4) --> Prey(5,3) with distance 3 Predator(3,3) --> Prey(6,3) with distance 3 Predator(3,2) --> Prey(7,2) with distance 4 Predator(3,1) --> Prey(7,1) with distance 4 Predator(3,0) --> Prey(6,0) with distance 3 Predator(3,-1) --> Prey(6,1) with distance 5 Predator(4,-1) --> Prey(5,2) with distance 4 Predator(5,-1) --> Prey(4,2) with distance 4 Predator(5,0) --> Prey(3,3) with distance 5 Predator(5,1) --> Prey(2,3) with distance 5 Predator(5,2) --> Prey(3,3) with distance 3 Predator(5,3) --> Prey(3,4) with distance 3 Predator(5,4) --> Prey(4,3) with distance 2 Predator(4,4) --> Prey(3,3) with distance 2 Predator(3,4) --> Prey(3,4) with distance 0
A total of 14 different hosts have accessed this document in the last 445 days; your host, nsrp-source.comp.nus.edu.sg, has accessed it 7 times.
If you're interested, complete statistics for this document are also available, including breakdowns by top-level domain, host name, and date.