EggGame function help

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
#include <iostream>
using namespace::std;





int tortoisePick(int numberEggs, int harePicked)
void main()
{
	bool gameOver = false;            // false means game is NOT over yet
	int numberEggs = 13;
	int player = 1;                         // 1 means Tortoise --- 2 means Hare
	int eggs_selected;
	while (!gameOver)
	{
		cout << "Enter your selection " << (player == 1 ? "Tortoise" : "Hare") << "... ";
		cin >> eggs_selected;

		if (eggs_selected >= 1 && eggs_selected <= 3 && eggs_selected <= numberEggs)         // condition - "selected" must stay between 1 and 3 and not exceed "numberEggs"  remaining
		{
			numberEggs = numberEggs - eggs_selected;// subtract the eggs selected from the number of eggs left

			if (numberEggs>0)       // condition - there are still eggs left
				(player ? (player == 1 ? player = 2 : player = 1) : player = 1);// change player to either Hare or Tortoise (USE TERTIARY OPERATOR)
				
			else                           // ie...no more eggs
			{
				gameOver = true;             //Game-over status changes
			} //END ELSE
		}


		else                              // USER MADE INVALID SELECTION
		{
			cout << "Not a valid egg selection, try again\n";
		}     //END OUTER IF

		cout << " The number of eggs left is " << numberEggs << endl; //Output the number of eggs left
	} // ENDWHILE BODY

	cout << "The winner is " << (player == 1 ? "Tortoise" : "Hare") << endl;// Output the current value of player...ie.the winner...USE TERTIARY OPERATOR


	return;

} // ENDMAIN



int tortoisePick(int numberEggs, int harePicked)
{
	int result;

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

	return result;
}







So with this program I am trying to get a function that will have the tortoise automatically pick its selection. I am having trouble getting the function to work, and not entirely sure I have it written correctly to start with.
"There is a strategy that you can use so that the Tortoise always wins. When the tortoisePick function is called,
if numberEggs is equal to 13, it is the first time it is called so the function will return 1, otherwise it will
return (4 minus the hare's selection.)"
In the quotations is the assignment outline. Any help would be appreciated.
I am trying to get a function that will have the tortoise automatically pick its selection


Are you looking to implement a strategy for an AI, or should the tortoise pick randomly? I also suggest you purge your code of unhelpful comments like //ENDMAIN and //ENDWHILE BODY .
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
#include <iostream>
using namespace std; //correct this line

int tortoisePick(int numberEggs, int harePicked);
int main()
{
	bool gameOver = false;            // false means game is NOT over yet
	int numberEggs = 13;
	int player = 1;                         // 1 means Tortoise --- 2 means Hare
	int eggs_selected=0;           //First tortoise starts so hare picked nothing
	while (!gameOver)
	{
		if(player==1) //let tortoisePick play for tortoise
		eggs_selected = tortoisePick(numberEggs, eggs_selected);
		else
		{
		cout << "Enter your selection Hare... ";
		cin >> eggs_selected;
		}
		if (eggs_selected >= 1 && eggs_selected <= 3 && eggs_selected <= numberEggs)         // condition - "selected" must stay between 1 and 3 and not exceed "numberEggs"  remaining
		{
			numberEggs = numberEggs - eggs_selected;// subtract the eggs selected from the number of eggs left

			if (numberEggs>0)       // condition - there are still eggs left
				(player ? (player == 1 ? player = 2 : player = 1) : player = 1);// change player to either Hare or Tortoise (USE TERTIARY OPERATOR)
				
			else                           // ie...no more eggs
			{
				gameOver = true;             //Game-over status changes
			} //END ELSE
		}


		else                              // USER MADE INVALID SELECTION
		{
			cout << "Not a valid egg selection, try again\n";
		}     //END OUTER IF

		cout << " The number of eggs left is " << numberEggs << endl; //Output the number of eggs left
	} // ENDWHILE BODY

	cout << "The winner is " << (player == 1 ? "Tortoise" : "Hare") << endl;// Output the current value of player...ie.the winner...USE TERTIARY OPERATOR


} // ENDMAIN

int tortoisePick(int numberEggs, int harePicked)
{
	int result;

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

	return result;
}
Last edited on
Yes I am trying to have the tortoise automatically pick their selection based on using the function.
Topic archived. No new replies allowed.