Ok the assigment is as follows:
Re-create the classic Tortoise and Hare race. A clock ticks every second (loop) and a random number is generated for each animal that then determines whether they move left or right and how many spaces. Each cylcle of the loop it shows the position of the tortoise and the hare on a 70 space line using H and T with blanks between. Once either animal reaches square 70, the race is over and it should say "Tortoise won" or "Hare won" etc.
Read the full problem hereI have the program figured out but for some reason when the Hare wins it says "The tortoise won", and vice versa when the tortoise wins. I could cheat and just change the output statements so they print properly but I'd like to know what I'm doing wrong.
Here is the code:
CODE
#include <iostream>
using std::cout;
using std::endl;
#include <cstdlib>
#include <ctime>
#include <iomanip>
using std::setw;
// set a constant for race end
const int RACE_END = 70;
// Function prototype for moveTortoise
void moveTortoise (int *);
// Function prototype for moveHare
void moveHare(int *);
// Function prototype for showPosition
void showPosition(int *,int *);
int main()
{
int tortoise = 1;
int hare = 1;
int timer = 0;
srand(time(0));
cout << "BANG !!!!" << endl;
cout << "AND THEY’RE OFF !!!!" << endl;
cout << endl;
// While the race isn't over, keep getting moves and showing positions
while (tortoise != RACE_END && hare != RACE_END)
{
// get the tortoises move
moveTortoise(&tortoise);
// get the hares move
moveHare(&hare);
// show their positions with function and keep track of time
showPosition(&tortoise,&hare);
++timer;
} // end while
// determine winner
if (tortoise > hare)
{
cout << endl;
cout << "TORTOISE WINS!!! YAY!!!" << endl;
}
else if (hare > tortoise)
{
cout << "Hare wins. Yuch." << endl;
}
else if (hare == tortoise)
{
cout << "It's a tie, but we'll say the Tortoise won!" << endl;
}
cout << "TIME ELAPSED = " << timer << " seconds" << endl;
cout << endl;
system("pause");
return 0;
} // end main
// function definition for moveTortoise
void moveTortoise(int *tortoisePtr)
{
int x = 1 + rand() % 10;
// determine which move to make
if (x >= 1 && x <= 5) // fast plod
*tortoisePtr += 3;
else if (x == 6 || x == 7) // slip
*tortoisePtr -= 6;
else // slow plod
++(*tortoisePtr);
if (*tortoisePtr < 1)
*tortoisePtr = 1;
else if (*tortoisePtr > RACE_END)
*tortoisePtr = RACE_END;
} // end function moveTortoise
// function definition for moveHare
void moveHare(int *harePtr)
{
int y = 1 + rand() % 10;
// determine which move to make
if (y == 1 || y == 2) // 20% chance to sleep
{
*harePtr += 0; // move zero spaces
}
else if (y == 3 || y == 4) // 20% chance to do big hop
{
*harePtr += 9; // move 9 squares right
}
else if (y == 5) // 10% chance to big slip
{
*harePtr -= 12; // move 12 squares left
}
else if (y >= 6 && y <= 8) // 30% chance to small hop
{
*harePtr += 1; // move 1 square right
}
else if (y == 9 || y == 10) // 20% chance to small slip
{
*harePtr -= 2; // move 2 squares left
}
// Test if hare is before beginning of race
// and don't let it go outside array bounds
if (*harePtr < 1)
{
*harePtr = 1;
}
else if (*harePtr > RACE_END)
*harePtr = RACE_END;
} // end function moveHare
// function definition for showPosition
void showPosition(int *bunnyPtr,int *snapperPtr)
{
if (*bunnyPtr == *snapperPtr)
cout << setw( *bunnyPtr ) << "OUCH!!!";
else if (*bunnyPtr < *snapperPtr)
cout << setw( *bunnyPtr ) << "H"
<< setw( *snapperPtr - *bunnyPtr) << "T";
else
cout << setw(*snapperPtr) << "T"
<< setw(*bunnyPtr - *snapperPtr) << "H";
cout << endl;
} // end function showPosition
This post has been edited by Smarf: 15 Oct, 2007 - 01:24 PM