(Exercise 8.12) In this exercise, I am asked to write a program to simulate the classic race of the tortoise and the hare. Random numbers are used to stimulate the probability of their actions in the race.
In my implementation, I use a int array to represent the race course, where each element contain an integer to indicate a position in the race course. Then I use twoint pointers to point at the positions where the animals are at. To pass the locations to the functions, I use twoint ** pointers to store the address of the position pointers, so that I can change the animals' location by changing*(posPtr).
I use this complicated approach to demonstrate the use of pointers between functions. Actually, one can use ints to represent the position of the animals and pass them to function by reference. The program would be simpler.
Note that const is used very often to ensure the principle of least privilege. Chance of geting potential error due to pointer manipulation is minimized.
Here comes the code:
#include <iostream>
#include <iomanip>
#include <cstdlib> // for rand() and srand()
#include <ctime> // for time()
using namespace std;
void race( int course[], int ** const harePosPtr, int ** const tortoisePosPtr, const int distance );
bool showStatus( const int t, int ** const harePosPtr, int ** const tortoisePosPtr, const int distance );
void moveTortoise( int course[], int ** const posPtr, const int distance );
void moveHare( int course[], int ** const posPtr, const int distance );
int main()
{
const int numOfPosition = 70; // the number of possible position along the race course
int raceCourse[numOfPosition];
int * harePos = &raceCourse[0]; // hare is at the starting point
int * tortoisePos = &raceCourse[0]; // tortoise is at the starting point
srand(time(0)); // provide seed for the random generator
// initialize raceCourse
for( int i=0; i<numOfPosition; i++ )
{
raceCourse[i] = i+1; //position 1...70
}
race( raceCourse, &harePos, &tortoisePos, numOfPosition );
return 0;
}
void race( int course[], int ** const harePosPtr, int ** const tortoisePosPtr, const int distance )
{
int t = 0; // tick of the clock
bool hasEnded = false;
cout << "BANG !!!!!" << endl;
cout << "AND THEY'RE OFF !!!!!" << endl;
cout << setfill('-') << setw(70) << '-' << endl;
showStatus( t, harePosPtr, tortoisePosPtr, distance );
while( !hasEnded )
{
moveTortoise( course, tortoisePosPtr, distance );
moveHare( course, harePosPtr, distance );
hasEnded = showStatus( ++t, harePosPtr, tortoisePosPtr, distance );
}
}
void moveTortoise( int course[], int ** const posPtr, const int distance )
{
int prob = rand()%10 + 1; // 1...10
int tempPos = **posPtr; // value of the element pointed by *posPtr, position x
if( prob <= 5 ) // 50%
{
tempPos += 3;
if( tempPos <= distance ) *posPtr += 3; // to the right by 3
}
else if( prob <= 7 ) // 20%
{
tempPos -= 6;
if( tempPos > 0 ) *posPtr -= 6; // to the left by 6
else *posPtr = &course[0]; // back to the starting point
}
else // 30%
{
*posPtr += 1; // to the right by 1
}
}
void moveHare( int course[], int ** const posPtr, const int distance )
{
int prob = rand()%10 + 1; // 1...10
int tempPos = **posPtr; // value of the element pointed by *posPtr, position x
if( prob <= 2 ) // 20% sleep
{
// sleep
}
else if( prob <= 4 ) // 20% big hop
{
tempPos += 9;
if( tempPos <= distance ) *posPtr += 9; // to the right by 9
}
else if( prob == 5 ) // 10% big slip
{
tempPos -= 12;
if( tempPos > 0 ) *posPtr -= 12; // to the left by 12
else *posPtr = &course[0];
}
else if( prob <= 8 ) // 30% small hop
{
*posPtr += 1; // to the right by 1
}
else // 20% small slip
{
tempPos -= 2;
if( tempPos > 0 ) *posPtr -= 2; // to the left by 2
else *posPtr = &course[0];
}
}
bool showStatus( const int t, int ** const harePosPtr, int ** const tortoisePosPtr, const int distance )
{
bool isSamePos = ( *harePosPtr == *tortoisePosPtr ); // point to same position
bool hareFinish = ( **harePosPtr == distance );
bool tortoiseFinish = ( **tortoisePosPtr == distance );
for( int i=1; i<=distance; i++ ) // print location of hare
{
if( **harePosPtr == i )
{
if( isSamePos && t!=0 && !hareFinish && !tortoiseFinish )
{
cout << "OUCH!!!"; // the tortoise bite the hare
i+=6;
}
else
{
cout << 'H';
}
}
else cout << ' ';
}
cout << endl;
for( int i=1; i<=distance; i++ ) // print location of tortoise
{
if( **tortoisePosPtr == i )
{
if( isSamePos && t!=0 && !hareFinish && !tortoiseFinish )
{
cout << "OUCH!!!"; // the tortoise bite the hare
i+=6;
}
else
{
cout << 'T';
}
}
else cout << ' ';
}
cout << endl;
if( hareFinish || tortoiseFinish )
{
cout << setfill('-') << setw(70) << '-' << endl;
if( hareFinish && tortoiseFinish )
{
cout << "It's tie." << endl;
}
if( hareFinish )
{
cout << "Hare wins. Yuch." << endl;
}
else
{
cout << "TORTOISE WINS!!! YAY!!!" << endl;
}
return true; // true when complete
}
return false; // continue to race
}

本文介绍了一个使用C++实现的经典龟兔赛跑模拟程序。通过随机数模拟比赛中的不确定性,利用指针来更新和展示比赛中兔子和乌龟的位置变化。程序采用复杂的设计来演示指针在函数间的应用。
2467

被折叠的 条评论
为什么被折叠?



