/********************************
* Name: Tortoise and the Hare *
* Author: *
* Date: 11/23/2009 *
********************************/
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
// Prototypes
void MoveAnimals
(string,string,
string[],
string[],int&,int&,
int);
void DrawTrack
(int,int,
int);
void CheckWinner
(int,int,
int,bool&
);
int main
()
{
const int trackSize =
70;
// Size of the track
// Starting positions
int tortoisePos =
1;
int harePos =
1;
bool winner =
false;
// If a winner is found
// Move Sets
string tortoiseMove
[] =
{"Fast plod",
"Slip",
"Slow plod"};
string hareMove
[] =
{"Sleep",
"Big hop",
"Big slip",
"Small hop",
"Small slip"};
// Current moves
string currentTortoiseMove =
"";
string currentHareMove =
"";
SetConsoleTextAttribute
(GetStdHandle
(STD_OUTPUT_HANDLE
),
8);
// Grey
cout <<
"BANG !!!!!" << endl <<
"AND THEY'RE OFF !!!!!" << endl << endl;
// Starting message
srand
((unsigned)time
(NULL));
// Starts the random number generator
while (!winner
) // While no one has finished
{
MoveAnimals
(currentTortoiseMove, currentHareMove, tortoiseMove, hareMove, tortoisePos, harePos, trackSize
);
// Move the creature
CheckWinner
(tortoisePos, harePos, trackSize, winner
);
// Checks for a winner
}
cout << endl << endl;
SetConsoleTextAttribute
(GetStdHandle
(STD_OUTPUT_HANDLE
),
7);
// White
system
("pause");
return 0;
}
void MoveAnimals
(string CURRENT_TORTOISE_MOVE,
string CURRENT_HARE_MOVE,
string TORTOISE_MOVE
[],
string HARE_MOVE
[], int& TORTOISE_POS, int& HARE_POS,
int TRACK_SIZE
)
{
int move;
// Used for character movement
/* Determines moves */
move = rand
() %
10 +
1;
// Random number from 1 to 10
if (move >=
1 && move <=
5) // 50% Fast Plod
CURRENT_TORTOISE_MOVE = TORTOISE_MOVE
[0];
else if (move >=
6 && move <=
7) // 20% Slip
CURRENT_TORTOISE_MOVE = TORTOISE_MOVE
[1];
else if (move >=
8 && move <=
10) // 30% Slow plod
CURRENT_TORTOISE_MOVE = TORTOISE_MOVE
[2];
move = rand
() %
10 +
1;
// Random number from 1 to 10
if (move >=
1 && move <=
2) // 20% Snooze
CURRENT_HARE_MOVE = HARE_MOVE
[0];
else if (move >=
3 && move <=
4) // 20% Big hop
CURRENT_HARE_MOVE = HARE_MOVE
[1];
else if (move ==
5) // 10% Big slip
CURRENT_HARE_MOVE = HARE_MOVE
[2];
else if (move >=
6 && move <=
8) // 30% Small hop
CURRENT_HARE_MOVE = HARE_MOVE
[3];
else if (move >=
9 && move <=
10) // 20% Small slip
CURRENT_HARE_MOVE = HARE_MOVE
[4];
/* Moving the creatures */
if (CURRENT_TORTOISE_MOVE == TORTOISE_MOVE
[0]) // Moves turtle to the right 3 spaces
TORTOISE_POS +=
3;
else if (CURRENT_TORTOISE_MOVE == TORTOISE_MOVE
[1]) // Moves turtle to the left 6 spaces
TORTOISE_POS -=
6;
else if (CURRENT_TORTOISE_MOVE == TORTOISE_MOVE
[2]) // Moves turtle to the right 1 space
TORTOISE_POS +=
1;
if (CURRENT_HARE_MOVE == HARE_MOVE
[0]) // Hare is snoozing
HARE_POS = HARE_POS;
else if (CURRENT_HARE_MOVE == HARE_MOVE
[1]) // Moves hare to the right 9 spaces
HARE_POS +=
9;
else if (CURRENT_HARE_MOVE == HARE_MOVE
[2]) // Moves hare to the left 12 spaces
HARE_POS -=
12;
else if (CURRENT_HARE_MOVE == HARE_MOVE
[3]) // Moves hare to the right 1 space
HARE_POS +=
1;
else if (CURRENT_HARE_MOVE == HARE_MOVE
[4]) // Moves hare to the left 2 spaces
HARE_POS -=
2;
/* Boundaries */
if (TORTOISE_POS <
1) // Makes sure turtle doesn't go too far left
TORTOISE_POS =
1;
if (TORTOISE_POS >= TRACK_SIZE
) // Makes sure turtle doesn't go too far right
TORTOISE_POS = TRACK_SIZE;
if (HARE_POS <
1) // Makes sure bunny doesn't go too far left
HARE_POS =
1;
if (HARE_POS >= TRACK_SIZE
) // Makes sure bunny doesn't go too far left
HARE_POS = TRACK_SIZE;
DrawTrack
(TORTOISE_POS, HARE_POS, TRACK_SIZE
);
// Draws the track
}
void DrawTrack
(int TORTOISE_POS,
int HARE_POS,
int TRACK_SIZE
)
{
for (int i=
1; i <= TRACK_SIZE; i++
) // Scans through the track
{
if (HARE_POS == i && TORTOISE_POS != i
) // Prints an H if the hare is there
{
SetConsoleTextAttribute
(GetStdHandle
(STD_OUTPUT_HANDLE
),
7);
// White
cout <<
'H';
}
if (TORTOISE_POS == i && HARE_POS != i
) // Prints a T if the tortoise is there
{
SetConsoleTextAttribute
(GetStdHandle
(STD_OUTPUT_HANDLE
),
2);
// Green
cout <<
'T';
}
if (TORTOISE_POS == i && HARE_POS == i
) // Collision!
{
SetConsoleTextAttribute
(GetStdHandle
(STD_OUTPUT_HANDLE
),
6);
// Brown
cout <<
"OUCH!!!";
}
else // Open area
cout <<
' ';
}
cout << endl << endl;
}
void CheckWinner
(int TORTOISE_POS,
int HARE_POS,
int TRACK_SIZE, bool& WINNER
)
{
SetConsoleTextAttribute
(GetStdHandle
(STD_OUTPUT_HANDLE
),
8);
// Grey
if (TORTOISE_POS == TRACK_SIZE
) // Checks if tortoise is at the end
{
cout <<
"TORTOISE WINS!!! YAY!!!\n";
WINNER =
true;
}
else if (HARE_POS == TRACK_SIZE
) // Checks if hare is at the end
{
cout <<
"Hare Wins. Yuch!\n";
WINNER =
true;
}
else if (TORTOISE_POS == TRACK_SIZE && HARE_POS == TRACK_SIZE
) // If both have reached the finish line
cout <<
"It's a tie!\n";
}