CS1101C Lab 3 (Even Week)

Simulating Animal Behavior

The deadline for this lab question is Friday 26 October 2007, 23:59:59 hours.

The name of your C program file must be called animal.c, files with any other name will not be marked.

Preliminary

In this lab, we shall simulate a predator-prey behavior in animals (that includes humans, of course). Each predator or prey moves around in a two-dimensional world. The prey wanders around aimlessly, while the predator targets the prey moving ever so closely to it (or him, or her).

The 2D World

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  +---+---+---+---+---+---+---+---+---+

Behavior of the Prey

We start the simulation with the prey situated in the middle of the world. Each, time the prey will only move to one of its eight neighbouring positions. For example, if prey starts off at (5,5), then it can move to any of the eight positions denoted by *. Each time, the move to one of the eight neighbour locations should be random.
   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.

Behavior of the Predator

The behavior of the predator is slightly more complex. In order to catch the prey, it follows the following simple strategy.
  1. The predator first smells for the prey to determine how far away it is.
  2. The predator then moves forward (forward is relative to the direction it is facing), and smells the prey again. If the scent is weaker (i.e. the distance between them is increased), it will make a right turn; otherwise don't do anything.
  3. Repeat steps 1 and 2 above until the predator catches the prey (i.e. the distance between them is zero).

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);

The Simulation

Complete the following main function that performs the following tasks in order.
  1. Define the SIZE of the world as a constant and set it to 10.
  2. Seed the random number generator using the current system time. You are advised to use a constant seed when developing your program.
  3. Initialize the location of the prey to be at the centre of the world.
  4. Randomly place the predator at some location within the world. Ensure that the predator faces North.
  5. Simulate the predator-prey behavior using the following algorithm:
    Repeat
    {
       
    1. Let the prey wander to its new neighbouring location;
    2. Move the predator forward;
    3. Turn the predator if necessary;
    4. Print the location of the predator and the prey;
    } until the predator catches the prey;
A sample run of the program is as follows. Note that because of the random nature of the simulation, the output need not be exact. You need to check if your program terminates with the predator catching its 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

Notes and Ponder

  1. Do not use any arrays in your program. Arrays are not necessary anyway.

  2. What do you think the prey should do in order to avoid being caught in this simulation? Think of the simplest strategy.

  3. If your program does not work as you expect (logical errors), use extra printf statements to print out all the values of your variables to aid in your debugging.

  4. Most importantly, have lots of fun programming!


This document, index.html, has been accessed 23 times since 25-Jun-24 11:57:13 +08. This is the 1st time it has been accessed today.

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.