-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtortoise_hare.cpp
117 lines (89 loc) · 3.24 KB
/
tortoise_hare.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// tortoise and hare race
// the point of using pass by reference with pointer arguments in this example is to directly modify variable values in main()
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
// function prototypes
void startAnnouncement();
void clockTick(float);
void moveTortoise(int * const);
void moveHare(int * const);
void printRace(const int * const, const int * const);
void printWinner(const int *const, const int *const);
const int end = 70; // global variable
int main() // ******************* MAIN ***********************
{
int tortoise = 1;
int hare = 1;
int speed = 1;
srand(time(0));
cout << "Choose speed of the race (1-slow, 15-fast): ";
cin >> speed;
cout << endl;
startAnnouncement(); // print announcement for the start of the run
while( tortoise < end && hare < end) // ********** LOOP ****************
{
clockTick(speed); // clockTick creates one second delay.
moveTortoise(&tortoise);
moveHare(&hare);
printRace(&tortoise, &hare); // display the moving letters
printWinner(&tortoise, &hare); // display the winner
} // ************************ END LOOP *****************************
} // ****************** END MAIN *********************
void startAnnouncement()
{ cout << "BANG !!!!!\n" << "AND THEY'RE OFF !!!!!\n\n"; }
void moveTortoise(int *const tortoisePtr)
{
int randomT = 1 + rand() % 10;
if(1 <= randomT && randomT <= 5)
*tortoisePtr = *tortoisePtr + 3;
else if(6 <= randomT && randomT <= 7)
*tortoisePtr = *tortoisePtr - 6;
else //if(8 <= randomT && randomT <= 10)
*tortoisePtr = *tortoisePtr + 1;
if(*tortoisePtr < 1)
*tortoisePtr = 1;
}
void moveHare(int *const harePtr)
{
int randomH = 1 + rand() % 10;
if(1 <= randomH && randomH <= 2)
*harePtr = *harePtr; // sleep -- do nothing, retain previous position
else if(3 <= randomH && randomH <= 4)
*harePtr = *harePtr + 9;
else if(randomH == 5)
*harePtr = *harePtr - 12;
else if(6 <= randomH && randomH <= 8)
*harePtr = *harePtr + 1; // small hop -- move hare 1 square to the right
else //if(9 <= randomH && randomH <= 10)
*harePtr = *harePtr - 2; // small slip -- move hare 2 squares to the left
if(*harePtr < 1)
*harePtr = 1;
}
void clockTick(float s)
{
clock_t second; // generate a variable 'second' that holds 1 second
second = clock() + (1/s)*CLOCKS_PER_SEC; // change step if you want a different delay (floats also allowed)
while(clock() < second) {}; // empty loop
}
void printRace(const int *const tPtr, const int *const hPtr)
{
if ( *tPtr == *hPtr )
cout << setw( *tPtr ) << "OUCH!!!";
else if ( *hPtr < *tPtr )
cout << setw( *hPtr ) << "H" << setw( *tPtr - *hPtr ) << "T";
else
cout << setw( *tPtr ) << "T" << setw( *hPtr - *tPtr ) << "H";
cout << '\n';
}
void printWinner(const int *const tPtr, const int *const hPtr)
{
if( *hPtr >= end && *tPtr < end )
cout << "Hare wins. Yach.\n";
if( *tPtr >= end && *hPtr < end )
cout << "TORTOISE WINS!!! YAY!!!\n";
if( *tPtr >= end && *hPtr >= end )
cout << "A tie, but TORTOISE WINS!!! YAY!!!\n";
}