Hey guys, I am making a hare and turtle simulation.
It works perfectly well, but I want the game to run for 10 seconds, and after every second display one of the array positions of each animal.
The theory I came up with did work and it calculates all the moves within 10 seconds and then prints out the final array showing all moves made during the game. I would like it to do this:
#After first second passed - show array with only the first move
#After second sec passed - update array with new move but keeping original
The other theory I tried was placing a big loop around the whole code, including the final array, but all that did was print the final output 10 times.
This is my full code:
Code:
// function prototypes
void setUp ( char[][ 50 ] );
void startingPoint ( char*, char*, char[][ 50 ] );
void playGame ( char*, char*, char[][ 50 ] );
const std::string displayTitleText();
const std::string startRaceText();
const std::string announceWinnerTurtle();
const std::string announceWinnerHare();
void showBoard ( char[][ 50 ] );
// main function - driver //////////////////////////////////////////////////////
//
int main ( void )
{
srand((unsigned)time(0));
const int MAX_ROWS = 50;
const int MAX_COLS = 50;
char playerBoard[ MAX_ROWS ][ MAX_COLS ] = {{ 0, 0 }};
setUp ( playerBoard );
std::cin.get(); // freeze console output window
return 0; // return value from int main
}
void setUp ( char board[][ 50 ] )
{
char turtle[] = "T";
char hare[] = "H";
startingPoint ( turtle, hare, board );
}
void startingPoint ( char *tut, char *hre, char board[][ 50 ] )
{
board[ 0 ][ 0 ] = *tut;
board[ 0 ][ 1 ] = *hre;
playGame ( tut, hre, board );
}
void playGame ( char *tut, char *hre, char board[][ 50 ] )
{
std::cout << "\t" << displayTitleText();
Sleep( 1500 );
std::cout << std::endl << startRaceText();
std::cout << "\n\n";
for ( int i = 10; i > 0; )
{
int xCorrTurt = rand() % 50 + 1;
int yCorrTurt = rand() % 50 + 1;
int xCorrHare = rand() % 50 + 1;
int yCorrHare = rand() % 50 + 1;
board[ xCorrTurt ][ yCorrTurt ] = *tut;
board[ xCorrHare ][ yCorrHare ] = *hre;
if ( *tut == board[ 50 ][ 50 ] )
{
std::cout << "\n" << announceWinnerTurtle();
}
if ( *hre == board[ 50 ][ 50 ] )
{
std::cout << "\n" << announceWinnerHare();
}
Sleep( 1000 );
i--;
}
showBoard ( board );
}
const std::string displayTitleText()
{
std::string str = "Welcome to the Tortoise and the Hare!";
return str;
}
const std::string startRaceText()
{
std::string str = "And..... THEY ARE OFF!";
return str;
}
const std::string announceWinnerTurtle()
{
std::string str = "OMG! THE TURTLE WAS THE WINNER! WOOOOO!!";
return str;
}
const std::string announceWinnerHare()
{
std::string str = "OH WELL, PREDICATABLE THAT THE HARE WOULD WIN...";
return str;
}
void showBoard ( char board[][ 50 ] )
{
for ( int i = 0; i < 50; i++ )
{
for ( int j = 0; j < 50; j++ )
std::cout << board[ i ][ j ];
std::cout << std::endl;
}
}
As it stands, it does work but I wanted to improve it so it looks better
when it runs. I appreiciate any help guys.