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

Turtle and Hare Race In this problem you will recreate the classic race of the t

ID: 3798204 • Letter: T

Question


Turtle and Hare Race In this problem you will recreate the classic race of the turtle and the hare. You will use random number generation and method development to simulate this event. Our contenders begin the race at position 1. Their destination, i.e. the finish line, is at position 75. The first contender to reach or pass position 75 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 and slip. They try to make up for those occasional slips by fast plods (the turtle) and big hops (the hare). Our hare is so sure of his advantage over the turtle that he takes short naps every now and then. In each step of the race the position of the animals should be adjusted according to the following rules: Animal Move type Percentage of the time Actual move Turtle Fast plod 37% 4 squares forward Slip 35% 2 5 squares backward Slow plod 28% 2 squares forward Hare Sleep 18% No move Big hop 15% 10 squares forward Big slip 15% 12 square backward Small hop 35% 1 square forward Small slip 17% 2 squares backward Use variables to keep track of the positions of the animals(i.e., position numbers are 1 - 75). Start each animal at position 1 (i.e., "starting gate"). If an animal slips the back to square 1, move the animal back to square 1. If the animal advances past the finish line, set the position to square 75. For each the race (i.e., each repetition of a loop), print a line showing the letter T in the position of the H the position of the hare. All positions other than the T or the H should be blank. After each line is printed, test if either animal has reached or passed square 75. If so, print the winner and terminate the simulation. Otherwise, the race continues. Your program should implement and use the following methods: public int moveTurtle (int pos, int finishLine) public int moveHare (int pos, int finishLine) public void printCurrentPositions (int turtlePos, int harePos) The first two functions take in the current position of the turtle/hare and, based on randomly generated number, compute the new position following the rules in the table above. The third function takes positions of both animals and produces the "image" on the screen reflecting the current state of the race. You may also do text mode, using T for turtle and H for hare. The main method needs to repeatedly call these three methods until one of the animals reaches the finish line (at position 75). Hand in your program listing (source code with javadoc tags and generated html file) and test runs to show that your program works.

Explanation / Answer

#include <stdlib.h>
#include <time.h>
int main()
{
int i;
int i2;
int hare = 0;
int turtle = 0;
int finish = 1;
int race;
printf("The race between the turtle and the Hare! On your mark, get set, Bang !!! ");
printf("And they are off! ");
srand(time(NULL));
while ( turtle != RACE_END && hare != RACE_END )
{
public int moveTurtle(int pos, int finishline);
public int moveHare(int pos, int finishline);
public void printCurrentPositions(int turtlepos,int harepos);
++timer;
}

for( i = 0; i < 75; i++)
{
if(i == hare)
{
printf("H");
}
else if (i == turtle)
{
printf("T");
}

else
{
printf("- ");

}
}
printf(" ");
  
if(hare == turtle)
{
int k;
for(k = 0; k < turtle-1; k++)
{
printf(" ");
}
printf("Ouch! ");
}

}

if( hare >= 75)
{
printf("Hare wins. Yuch. ");
race = 0;
}
if(turtle >= 75)
{
printf(" turtle WINS!!! YAY!!!");
race = 0;
}
if (hare >= 75 && turtle >= 75)
{
printf("It's a tie. ");
race = 0;
}
}
return 0;
}
  
  
  
public int moveTurtle(int pos, int finishline)
{
int i=rand()%10 +1;
if(37%<= i)
pos += 4;
else if (35% <= i)
pos -= 5;
else (28%<= i)
pos += 2;
if ( finishline < 1 )
pos = 1;
else if ( finishline > RACE_END )
pos = RACE_END;
}



public int moveHare(int pos, int finishline)
{
int i2=rand()%10 +1;
if (18%<= i2)
pos =0;
else if (15% <= i2)
pos += 10;
else if(15%<= i2)
pos -= 12;
else if (35% <= i2)
pos += 1;
else (17%<= i2)
pos -= 2;
finishline=pos
if ( finishline < 1 )
pos = 1;
else if ( finishline > RACE_END )
pos = RACE_END;
}
}

public void printCurrentPositions(int *turtlepos,int *harepos);
{
if ( turtlepos == harepos )
printf( "%d ",*harepos );
else if ( harepos < turtlepos )
printf("%d ",*harepos );
else
printf("%d ",*turtlepos );
  
}


  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote