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
|
/*Simulation of a race. Both the tortoise and the hare begin at square 1 of 70.
Each square represents a possible position along the race track. The finish is square 70. You win when you reach or pass square 70.
There is a clock that ticks once per second. With each tick your program should adjust the position of the racers according to the table below.
Use variables to keep track of the positions. A racer cannot slip past square 1 into a square <= 0.
Generate the table by producing a random integer i, 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. Similarly for the hare.
Begin the race by printing :BANG !!! AND THEY'RE OFF !!!!!
For each tick of the clock, print a 70 - position line showing the letter T for the Tortoise's position and H for the hare. If the racers land on the
same square, the tortoise bites the hare and you must print OUCH !!! beginning at that position.
All positions other than the T, the H, or the OUCH !!!should be a blank.
After printing each line, whether either animal has reached or passed square 70. If so, print the winner and terminate the simulation.
If the tortoise wins, print TORTOISE WINS!!!, otherwise print HARE WINS? If both win on the same clock tick, print TIE.*/
#include "pch.h"
#include <iostream>
#include <thread>
#include <chrono>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
const int END = 70; //number of positions for the whole game
void moveTortoise(int *);
void moveHare(int *);
void positions(int *, int *);
int main()
{
int T = 1;
int H = 1;
int TIME = 0;
srand(time(0));
cout << "BANG" << endl;
cout << "AND THEY'RE OFF!!!!" << endl;
while (T != END && H != END) {
moveTortoise(&T);
moveHare(&H);
positions(&T, &H);
++TIME;
}
if (T > H) {
cout << "Tortoise Wins!" << endl;
}
else if (T < H) {
cout << "Hare Wins!" << endl;
}
else if (T == H) {
cout << "It's tie!" << endl;
}
if (T < 1 || H < 1) {
T = 1; H = 1;
}
return 0;
}
void moveTortoise(int *TT) {
int i = 1 + rand() % 10;
if (1 <= i <= 5) { //fast plod
*TT = *TT + 3;
}
else if (6 <= i <= 7) { //slip
*TT = *TT - 6;
}
else if (8 <= i <= 10) { //slow plod
*TT = *TT + 1;
}
}
void moveHare(int *HH) {
int i = 1 + rand() % 10;
if (1 <= i <= 2) { //sleep
*HH = *HH;
}
else if (3 <= i <= 4) {
*HH = *HH + 9;
}
else if (i == 5) {
*HH = *HH - 12;
}
else if(6 <= i <= 8) {
*HH = *HH + 1;
}
else if(9 <= i <= 10) {
*HH = *HH - 2;
}
}
void positions(int *TT, int *HH) {
if (*TT == *HH) {
cout << setw(*TT) << "OUCH";
}
else if (*HH < *TT)
cout << setw(*HH) << "H"
<< setw(*TT - *HH) << "T";
else
cout << setw(*TT) << "T"
<< setw(*HH - *TT) << "H";
cout << "\n";
}
|