Sunday, October 7, 2012

Tortoise and the Hare Simulation

Tortoise and the Hare Race


Language: C

Purpose:

A Simulation of the famous Race. I had to print out the position of the Tortoise (T) and the Hare (H) at every "tic" (each iteration of a loop). I decided to just use an array and replace the indexes where the Tortoise and the Hare were position on the "race track." Here are the guidelines our professor gave us:


Problem Statement
(Simulation: The Tortoise and the Hare) In this problem, you’ll recreate one of the truly great moments in history, namely the classic race of the tortoise and the hare. You’ll use random number generation to develop a simulation of this memorable event.
Our contenders begin the race at "square 1" of 70 squares. Each square represents a possible position along the race course. The finish line is at square 70. The first contender to reach or pass square 70 is rewarded with a pail of fresh carrots and lettuce. The course weaves its way up the side of a slippery mountain, so occasionally the contenders lose ground. There is a clock that ticks once per second (one pass through the calculation loop). With each tick of the clock, your program should adjust the position of the animals according to the rules below. Use variables to keep track of the positions of the animals (i.e., position numbers are 1–70).
Start each animal at position 1 (i.e., the "starting gate"). If an animal slips left before square 1, move the animal back to square 1. Generate the percentages in the preceding table by producing a random integer, i, in the range
1 ≤ i ≤10. For the tortoise, perform a ・fast plod・ when 1 ≤ i ≤5, a ・slip・ when 6 ≤ i ≤7, or a ・slow plod・ when 8 ≤ i ≤10. Use a similar technique to move the hare.


WINS!!! YAY!!! If the hare wins, print Hare wins. Yuch. If both animals win on the same tick of the clock, you may want to favor the turtle (the "underdog"), or you may want to print It's a tie. If neither animal wins, perform the loop again to simulate the next tick of the clock. When you are ready to run your program, assemble a group of fans to watch the race. You'll be amazed at how involved your audience gets!

Use at least two subroutines (functions) from moving the tortoise and the hare with the form:

void moveTortoise( int *turtlePtr ); //header of function for moving the tortoise

Start of a Sample Session (1. T fast, H sleeps; 2. T fast, H sleeps; 3. T fast, H big hop; 4. T slow, H big slip; … )

ON YOUR MARK, GET SET

BANG !!!!

AND THEY'RE OFF !!!!

H T

H       T

         OUCH!!!

          H      T

 


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//Prototypes
void moveTortoise( int *turtlePtr );
void moveHare( int *rabbitPtr );

//Global Variables
int harePosition = 0;
int tortPosition = 0;
int *turtlePtr = &tortPosition;
int *rabbitPtr = &harePosition;

 void main(void)
{
    //Local Variables
    int race = 1;
    size_t i;
    srand ( time(NULL) );

    //Start the Race
    puts("ON YOUR MARK, GET SET\nBANG !!!!!\nAND THEY'RE OFF!!!!!\n");
    puts("H T");

    //Race Loop: every Iteration is one "tic."
    while(race == 1){

        moveTortoise(turtlePtr);
        moveHare(rabbitPtr);
        
        if(*rabbitPtr < 0){
            *rabbitPtr = 0;
        }
        else if(*turtlePtr < 2){
            *turtlePtr = 2;
        }
        else if(*rabbitPtr == *turtlePtr){
            int k;
                for(k = 0; k < *rabbitPtr-1; k++){
                printf(" ");
                }
            printf("Ouch!\n");
        }
        
    //Print the Status of the Race.
        for( i = 0; i < 70; i++){
        
            if(i == *rabbitPtr){
            printf("H");
            }
            else if (i == *turtlePtr){
                printf("T");
            }
            else{
                    printf(" ");
            }
        }
        printf("\n");

    //Win Conditions
        if( *rabbitPtr >= 70){
            puts("\nHare wins. Yuch.");
            race = 0;
            }
        if(*turtlePtr >= 70){
            puts("\nTORTOISE WINS!!! YAY!!!");
            race = 0;
            }
         if(*rabbitPtr >= 70 && *turtlePtr >= 70){
            puts("\nIt's a tie.");
            race = 0;
            }
    }
}//Function main

 /* Takes a psuedo-random value and then based on that value increments or decrements
  * the *turtlePtr (tortPosition) value.
  *
  * @param *turtlePtr  - Position of the animal. 
  */
void moveTortoise( int *turtlePtr ){
    
     int value  = rand() % 10 + 1;

    switch(value){
    case 1 :
    case 2 :
    case 3 :
    case 4 :
    case 5 :
        *turtlePtr += 3;
        break;
    case 6 :
    case 7 :
        *turtlePtr -= 6;
        break;
    case 8 :
    case 9 :
    case 10 :
        *turtlePtr += 1;
        break;
    }//end Switch
}
 /* Takes a psuedo-random value and then based on that value increments or decrements
  * the *rabbitPtr (harePosition) value.
  *
  * @param *rabbitPtr  - Position of the animal. 
  */
void moveHare( int *rabbitPtr ){
    
     int value  = rand() % 10 + 1;

    switch(value){
    case 1 :
    case 2 :
        *rabbitPtr += 0;
        break;
    case 3 :
    case 4 :
        *rabbitPtr += 9;
        break;
    case 5 :
        *rabbitPtr -= 12;
        break;
    case 6 :
    case 7 :
    case 8 :
        *rabbitPtr += 1;
        break;
    case 9 :
    case 10 :
        *rabbitPtr -= 2;
        break;
    }//end Switch

}





4 comments:

  1. Sorry Folks, This is my second and updated attempt at this project. The first time around I was using an array and the Stack seemed to get hung up on that. This now works great!

    ReplyDelete
  2. Buddy could you please give it in python

    ReplyDelete