Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here we have the classic race between the tortoise and the hare. For this projec

ID: 3691413 • Letter: H

Question

Here we have the classic race between the tortoise and the hare. For this project, you be using random-number generation to move the creatures. To make things more interesting, the animals have to race up the side of a slippery mountain, which could cause them to lose ground. In this race either animal could win or there could be a tie with no clear winner.

The animals begin at "Square 1" of 70 squares. Each of the squares represents a position the animal can hold along the racetrack. The finish line is at Square 70. When an animal wins, a winning message of your choice should be posted. For example:

Yay! The rabbit won! He hops the fastest!
Woo-hooo! Slow and steady wins the race! Congratulations, turtle!
Tie score--no winner! Want to race again?

To start the race, print a message similar to:

Bang! Off they go!

There is a clock that ticks once per second. With each tick of the clock, your program should adjust the position of the animals according to the following rules:

Animal    Move Type    Percentage of the Time   Actual Move
Tortoise Fast Plod            50%                          3 squares to the right
Tortoise Slip                   20%                           6 squares to the left
Tortoise   Slow Plod         30%                          1 squares to the right
Hare        Sleep                20%                              No move at all
Hare       Big Hop             20%                      9 squares to the right
Hare       Big Slip               10%                     12 squares to the left
Hare      Small Hop           30%                     1 square to the right
Hare      Small Slip            20%                     2 squares to the left

Keep track of the positions of the animals by using variables. If an animal slips, the lowest it can go is back to position 1. The highest it can go is to position 70 when it wins the race.

You will work with the percentages in the table above by generating a random integer current in the range
1 current 10.

For the tortoise, a "fast plod" is when 1 current 5, a "slip" when 6 current 7, or a "slow plod" 8 current 10. A similar approach would be used to set up the moves for the hare.

For each tick of the clock (each repetition of the loop), print a 70-position line showing the letter T in the tortoise’s position and the letter H in the hare’s position. If the two animals land on the same square, which may happen, the animals will bump. If this happens, print BUMP! at the current position.

After you print each line, check to see if either animal has landed on Square 70. If this happens, print a winning-type message.

It may make the simulation more interesting if you have users press any key after each iteration of the loop, so that they can see the movement of the animals.




DELIVERABLES
Your C++ source code with any header files
Your executable code
A document detailing how you will test your program
An overview document giving the name of each file submitted and its purpose, as well as any discussion you have on implementing this program

Explanation / Answer


#include <iostream>
#include <stdlib.h> //srand, rand
#include <time.h> //time
#include <stdio.h>//NULL
#include <iomanip>
#include <ctime>
using namespace std;

void haresTurn(int x, int& hare)
{
    x = rand() % 10 +1;
    int *h = &hare;
  
    if(x>=1 && x<=2)
        *h += 0;//hare is asleep no move
    if(x>=3 && x<=4)
        *h +=9;//big hop
    if(x==5)
        *h -=12;//big slip
    if(x>=6 && x<= 8)
        *h +=1;//small hop
    if(x>=9 && x <=10)
        *h -=2;//small slip
  
    else if (*h >=70)
        *h = 70;
}
void tortoisesTurn(int x, int& tortoise)
{
    x = rand() % 10 +1;
    int *t = &tortoise;
  
    if(x>=1 && x<= 5)
        *t += 3;//fast plod
    if(x>=6 && x<=7)
        *t -=6;//slip
    if(x>=8 && x<=10)
        *t += 1;//slow plod
    else if (*t >=70)
        *t = 70;
}
void positions(int *h,int *t)
{
    if ( *h == *t)
        cout << setw( *h ) << "Ouch!!!";// they landed at the same spot
  
    else if ( *h < *t)
        cout << setw( *h ) << "H" << setw( *t - *h ) << "T";
  
    else
        cout << setw( *t ) << "T"
<< setw( *h - *t ) << "H";
  
    cout << " ";
  
}

int main()
{
    srand(time(NULL));//Initializes random seed
    int randInt;
    int hare = 0;
    int tortoise = 0;
    int count = 0;
  
    cout<<"BANG !!!!! AND THEY'RE OFF !!!!!"<<endl;
  
    do{
        haresTurn(randInt, hare);
        tortoisesTurn(randInt, tortoise);
      
        positions(&hare, &tortoise);//to call the postions functions so they print out the positions like a real race
        count++;//see how many times the loop runs
    }while(hare <70 && tortoise <70 );
  
    if(hare >= 70)
        cout<<"The Hare wins "<<endl;
    if(tortoise>= 70)
        cout<<"The Tortoise Wins"<<endl;
    cout<<"The loop went "<< count<< " times"<<endl;
  
    //system("pause");
    return 0;
}


sample output

BANG !!!!!                                                                                                                                                  
AND THEY'RE OFF !!!!!                                                                                                                                       
T      H                                                                                                                                                    
T   H                                                                                                                                                       
T          H                                                                                                                                                
T        H                                                                                                                                                  
H   T                                                                                                                                                       
T   H                                                                                                                                                       
T H                                                                                                                                                         
HT                                                                                                                                                          
T      H                                                                                                                                                    
T    H                                                                                                                                                     
    T          H                                                                                                                                            
       T        H                                                                                                                                           
T              H                                                                                                                                           
T             H                                                                                                                                           
        T         H                                                                                                                                         
T               H                                                                                                                                         
   T                       H                                                                                                                                
      T                     H                                                                                                                               
       T                  H                                                                                                                                 
          T               H                                                                                                                                 
           T            H                                                                                                                                   
            T            H                                                                                                                                  
             T            H                                                                                                                                 
                T          H                                                                                                                                
               H T                                                                                                                                          
                H   T                                                                                                                                       
                 H   T                                                                                                                                      
                 H      T                                                                                                                                   
               H         T                                                                                                                                  
   H               T                                                                                                                                        
H                                     T                                                                                                                    
          H                               T                                                                                                                 
          H                                T                                                                                                                
                   H                          T                                                                                                             
                    H                            T                                                                                                          
                     H                              T                                                                                                       
                   H                                   T                                                                                                    
                 H                                        T                                                                                                 
                  H                                          T                                                                                              
                  H                                             T                                                                                           
      H                                                          T                                                                                          
    H                                                             T                                                                                         
    H                                                                T                                                                                      
The Tortoise Wins                                                                                                                                           
The loop went 72 times