Hi all,
I'm learning C++ from a textbook & am stumped for ideas with ways to finish this program.
I need to know:
1) If there's a way to slow program speed so that the race progress is updated per second ( rather than at processor speed ).
2) A method for clearing the text so that updated race progress is printed on a fresh 'page' ( I hope that makes sense ).
3) If anybody can see anything I might have missed from my program.
Here's the code:
Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int finish = 69;
void moveHare( int *, int[] ); // to move the hare
void moveTortoise( int *, int[] ); // to move the tortoise
void displayRace( const int *, const int *, const int[] ); // to display the race
int main()
{
srand( time( 0 ) );
int mountain[ 70 ] = { NULL }; // 70 steps to the top of the mountain
int tortoisePos = 0;
int harePos = 0;
cout << "BANG !!!!!" << endl << "AND THEY'RE OFF !!!!!" << endl << endl;
while ( tortoisePos < finish && harePos < finish )
{
moveHare( &harePos, mountain );
moveTortoise( &tortoisePos, mountain );
displayRace( &tortoisePos, &harePos, mountain );
cout << endl;
} // end while
} // end main
// to display the race
void displayRace( const int *tortoisePos, const int *harePos, const int mountain[] )
{
for ( int i = 0; i < finish; i++ )
{
if ( mountain[ i ] == 0 )
{
cout << ".";
}
else if ( mountain[ i ] == 1 )
{
cout << "T";
}
else if ( mountain[ i ] == 2 )
{
cout << "H";
} // end if
} // end for
} // end function displayRace
// to move the tortoise
void moveTortoise( int *tortoisePosPtr, int mountain[] )
{
mountain[ *tortoisePosPtr ] = 0;
int move = 1 + rand() % 10;
switch ( move )
{
case 1: // fast plod
case 2:
case 3:
case 4:
case 5: *tortoisePosPtr += 3;
break;
case 6: // slip
case 7: if ( *tortoisePosPtr -= 6 < 0 )
{
*tortoisePosPtr = 0;
}
else
{
*tortoisePosPtr -= 6;
} // end if
break;
case 8: // slow plod
case 9:
case 10: *tortoisePosPtr += 1;
break;
} // end switch
mountain[ *tortoisePosPtr ] = 1;
} // end function moveTortoise
// to move the hare
void moveHare( int *harePosPtr, int mountain[] )
{
mountain[ *harePosPtr ] = 0;
int move = 1 + rand() % 10;
switch ( move )
{
case 1: // sleep
case 2:
break;
case 3: // big hop
case 4: *harePosPtr += 9;
break;
case 5: if ( *harePosPtr -= 12 < 0 ) // big slip
{
*harePosPtr = 0;
}
else
{
*harePosPtr -= 12;
} // end if
break;
case 6: // small hop
case 7:
case 8: *harePosPtr += 1;
break;
case 9: // small slip
case 10: if ( *harePosPtr -= 2 < 0 )
{
*harePosPtr = 0;
}
else
{
*harePosPtr -= 2;
} // end if
break;
} // end switch
mountain[ *harePosPtr ] = 2;
} // end function moveHare