[CPPHTP7 NOTES] CH8. POINTERS(2)

(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
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值