Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <random>
- #include <string>
- #include <ctime>
- using namespace std;
- void runRace ();
- void printTrack(int, int);
- void moveTortoise(int*);
- void moveHare(int*);
- void printResults(int, int);
- int main()
- {
- srand(time(0));
- bool isRacing{true};
- while(isRacing) {
- runRace();
- cout << "Race Again? 1 for Yes, 0 for No";
- cin >> isRacing;
- }
- }
- void runRace(){
- int tortoise{1};
- int hare{1};
- while((tortoise <= 70) && (hare <= 70)) {
- printTrack(tortoise, hare);
- cout << endl;
- if (tortoise == 70) {
- tortoise = 71;
- }
- else moveTortoise(&tortoise);
- if (hare == 70) {
- hare = 71;
- }
- else moveHare(&hare);
- }
- printResults(tortoise, hare);
- }
- void printTrack(int tortoise, int hare){
- for(int i{1}; i <= 71; i++) {
- bool ouch{false};
- if ((i == hare) && (tortoise == hare && tortoise + hare != 2)){
- cout << "OUCH!!!";
- ouch = true;
- i += 4;
- }
- if (!ouch){
- if(i == tortoise){
- cout << 'T';
- }
- if(i == hare){
- cout << 'H';
- }
- if (i < 70) cout << '-';
- }
- }
- }
- void moveTortoise(int* tortPtr) {
- int x{1 + rand() % 10};
- if (x <= 5){ *tortPtr += 3; }
- else if (x <= 7){ *tortPtr -= 6; }
- else if (x <= 10){ *tortPtr += 1; }
- if (*tortPtr < 1){ *tortPtr = 1; }
- if (*tortPtr > 70) { *tortPtr = 70; }
- }
- void moveHare(int* harePtr) {
- int x{1 + rand() % 10};
- if (x >= 2 && x <= 4){ *harePtr += 9; }
- else if (x <= 5){ *harePtr -= 12; }
- else if (x <= 8){ *harePtr += 1; }
- else *harePtr -= 2;
- if (*harePtr < 1){ *harePtr = 1; }
- if (*harePtr > 70){ *harePtr = 70; }
- }
- void printResults(int tortoise, int hare){
- if (hare == tortoise) {
- cout << "It's a Tie!" << endl;
- }
- else if (tortoise >= 70) {
- cout << "Tortoise Wins!!!" << endl;
- }
- else if (hare >= 70) {
- cout << "Hare Wins. Yuch." << endl;
- }
- }
Add Comment
Please, Sign In to add comment