Need some hints please!

I am working on an assignment in which I wrote the function tortoisePick(int )and need to find a way to be able to have the player chose if they want to go first. The computer is the tortoise and should pick a number (if I wrote the function correctly. How would I go about selecting a person? Please just hints, I would really like to figure this out on my own.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66


#include <iostream>
using namespace::std;
int tortoisePick(int numberOfEggs);

//    Use of a flag to control the game
//    The program plays the role of the "Judge" for the game
//         -  program asks for a valid selection
//         -  program determines if the game is over
//         -  program declares the winner (last one to take eggs)

void main()
{
	bool gameOver = false;   // flag
	int  numberEggs = 13;
	int  player = 1;         //  1 for Tortoise  ,   2 for Hare
	int  selected;
	
	while( !gameOver )
	{
		
		cout << "Number of eggs remaining is " << numberEggs << endl;
		cout << "Enter your selection (1, 2 or 3) " 
			<< (player==1 ? "Tortoise":"Hare") << " ";
		cin >> selected;
		
		// If move is legal: 1 to 3 eggs and no more than numEggs remaining
		if ( selected >=1 && selected <= 3 && selected <= numberEggs )		// student supplies code
		{    
			// adjust numberEggs		// student supplies code
			numberEggs -=selected;

			if ( numberEggs > 0)	// student supplies code
			{
				// change  players		//  student supplies code
				player= (player==1) ? 2 : 1;
			}
			else
			{
				gameOver = true;
			}
		}
		else    // not a valid selection
		{
			cout <<"Not a valid egg selection, try again\n";
		}
	 } // end while
														//declare the winner  (Tortoise or Hare)	// student supplies code
	cout<< "The Winner: "<< ( player==1 ? "tortoise" : "hare") << endl;

	return;
} // end main


int tortoisePick(int numberOfEggs)
{
	int result;

	if(  (numberOfEggs %4) !=0 )
		result = (numberOfEggs%4);
	else
		result = 1;

return result;
}
Hello,

when do you call the function "tortoisePick" ??
And when should you call it?

greetings
Last edited on
fluppe,
Finally got it! For some reason I had a hard time visualizing where the function needed to be called. On line 11, where I switch players, is there a better way to have player 1 and 2 switch? Or is this the most efficient?
Thanks again!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
void main()
{
bool gameOver = false;   // flag
int  numberEggs = 13;
int  player = 0;         //  1 for Tortoise  ,   2 for Hare
int  selected;
cout << "Would you like to go first or second? " << endl;

cin >> player;

if (player==1)
player = 2;
else
player = 1;


while( !gameOver )
{

cout << "Number of eggs remaining is " << numberEggs << endl;
if (player == 2){

cout << "Enter your selection (1, 2 or 3) " 
<< (player==1 ? "Tortoise":"Hare") << " ";
cin >> selected;
}
else{
selected = tortoisePick(numberEggs);
cout << "The computer picked: " <<  selected << endl;
}
Last edited on
Topic archived. No new replies allowed.