Background Information: - In this program we attempt to use randomization to pro
ID: 3630535 • Letter: B
Question
Background Information:
- In this program we attempt to use randomization to produce a program that has different results without the user input. In order to do this we have to use some prepackaged randomization functions. Also, we will define a few of our own functions to reduce the complexity and length of main.
- http://en.wikipedia.org/wiki/The_Tortoise_and_the_Hare
- Randomization: 5.10, pages 153 to 158.
Programming Problem Description:
This program when ran will produce the output of the race from start to end at different clock intervals (a counter). The tortoise will be represented by a‘T’ and the hare by an ‘H’ on the terminal. The width of the terminal will represent the position of the racers at each clock interval and each clockinterval will be represented on a new line. The race ends once either contender reaches position 30 or if the clock interval reaches 20 with no winner. The probability of their movement change from one clock interval to the next will be dependent on the table shown in the instructions. Clock interval 0 will represent the racers in their starting positions. The hare will start in position 0, and the tortoise will start in position 1. Starting with the first clock interval their printed position will be dependent on the result of their individual movement functions.Each contender will have a function that determines the change in their position and the function will return the integer value of their movement change. Changes may be forward, backward, or the same (positive, negative, or zero). The results of their movement changes will be governed by the following rules:
(1) Zero or Negative Position – If after the movement changes are calculated and both contenders’ position is 0 or less then adjust both contenders back to their original starting positions as they kind of fell back in place. Else,if a position change indicates that a contender has fallen behind position 0 we need to move the contender to position 0.
(2) A Tied Position - Except in the case above, if the position that the hare and the tortoise happens to be in is the same after the movement change is calculated then the hare will be “bitten” by the tortoise and the hare will move back one position, the tortoise forward one position, and an exclamation mark will be printed where they were tied.
(3) Winner – If both contender’s movement changes puts them at >= 30, then determine who would have been the farthest along and put that contender in the position 30 and the loser in position 29. If they tied at >= 30 then follow the same tie principle by placing the tortoise on position 30, the hare on 28, and an exclamation mark on 29. Otherwise, if a single contender reached >= 30, then place that contender on the position 30. After their positions are printed, print on the last line who won the race.
(4) No Contest – If after 20 clock intervals, the race has not been completed then do not print the contender’s positions at clock interval 21. Print the race end as a no contest.
Another function will be responsible for all of the calls to the printf function and thus it is essentially responsible for all of the programs output.It will consist of printing the race start, the race positions at each clock interval after 0, and the race results. The race start will consist of some header output and the race position line at clock interval 0. There are two components to the race position lines. The first is the numerical printing of the clock, the hare position, and the tortoise position. The second is the printing of blank spaces and the positions of the contenders represented by their respective characters, the potential exclamation mark and a pipe after position 30. The race end will print whether the hare won or the tortoise or whether it was a no contest.
Programming Problem Instructions:
1. main function is suggested to initialize variables, call the other 3 functions in a loop, handle the logic for rule (1), and to use an enumeration to represent constants that reflect the state of the game that printRace will return and that will control the loop iteration.
2. moveHare function ought to take no arguments and should simply return the integer value of the hare’s position change. Utilize the rand function and determine the change in position by the following probabilities. Movement Type Probability Change
Hare sleeps 60% 0
Hare sprints 10% 9
Hare hops along 20% 7
Hare slips 10% -2
3. moveTort function is the same as the previous, but with different change probabilities.
Movement Type Probability Change
Tortoise plods along 50% 2
Tortoise picks it up 30% 5
Tortoise slips 20% -2
4. printRace function ought to be the only function that calls print and it should take at least three arguments. The hare position, the tortoise position, and the clock interval. It is suggested that it should handle the logic for rules (2), (3), and (4) and that it should return a value that represents the game results (whether to continue or end). See p3template.c file for a template for this program and some given printf statements for different output parts
here is a template to help you out:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int moveHare ( void );
int moveTort ( void );
int printRace (int , int , int );
int main ( void ) {
//init vars, enum, positions, game status, clock
//srand
//loop continues as long as game status is to continue
}
int moveHare ( void ) {
//init vars
//rand
//return logic
}
int moveTort ( void ) {
//init vars
//rand
//return logic
}
int printRace (int hareP, int tortP, int clock ) {
//init vars, enum, game status, bite position
//majority of the logic occurs here if following suggestions from instructions
//Provided printf statements
printf( " Race Start! "
" C H T 0 5 10 15 20 25 30 "
"-------------------------------------------- "
" 0| 0| 1|HT | And Their Off! ");
printf("%3d|%3d|%3d|", , ,);
//printfs for race position chracters are up to your logic interpretation
printf("-------------------------------------------- No Contest! ");
printf("-------------------------------------------- Hare Wins! ");
printf("-------------------------------------------- Tort Wins! ");
}
• See sample outputs. your output should be in this EXACT formatt
PLEASE INCLUDE A LOT OF COMMENTS SO I KNOW WHATS GOIN ON
Explanation / Answer
please rate - thanks
best I can do--should get you started
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
int movetort(int);
int movehare(int);
void print(int,int,int);
int main()
{int finish=30,tort=1,hare=1,rtime=0;
srand(time(0));
printf("Race Starts! ");
printf("-C-|-H-|-T-|1 5 10 15 20 25 30 ");
do
{hare=movehare(hare);
tort=movetort(tort);
print(tort,hare,rtime);
rtime++;
}while(tort<finish&&hare<finish);
if(tort>hare )
printf(" TORTOISE WINS!!! YAY!!! ");
else if(tort<hare )
printf("Hare wins. Yuch. ");
else
printf("Would you believe IT'S A TIE!! ");
getch();
return 0;
}
void print(int t,int h,int clock)
{int i;
printf("%3d|%3d|%3d|",clock,h,t);
if(h==t)
{for(i=0;i<h;i++)
printf(" ");
printf("HIT");
}
else if(h<t)
{for(i=0;i<h;i++)
printf(" ");
printf("H");
for(i=0;i<t-h;i++)
printf(" ");
printf("T");
}
else
{for(i=0;i<t;i++)
printf(" ");
printf("T");
for(i=0;i<h-t;i++)
printf(" ");
printf("H");
}
printf(" ");
}
int movehare(int r )
{int num;
num=rand()%10;
if(num==0)
r+=9;
else if(num<3)
r+=7;
else if(num<4)
r-=2;
if(r< 1 )
r=1;
return r;
}
int movetort(int t)
{int num;
num=rand()%10;
if(num<5)
t+=2;
else if(num<8)
t+=5;
else
t-=2;
if(t<1)
t=1;
return t;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.