Question: Modify the 13 eggs problem from Lab 3. Write a function that makes the selection for the Tortoise. The prototype for the function is: int
Modify the 13 eggs problem from Lab 3.
Write a function that makes the selection for the Tortoise.
The prototype for the function is: int tortoisePick(int numberEggs, int harePicked)
There is a strategy that you can use so that the Tortoise always wins.
When the function is called, if numberEggs is equal to 13,
the tortoise is making the first selection. If
numberEggs is not equal to 13 , the hare has
made a selection and the hares selection was passed
as the second parameter, harePicked.
The return value is the number of eggs that the Tortoise selects.
Hint: After the hare picks, if the tortoise makes a selection so that the sum of
both picks is 4, then the tortoise will always be the winner, since 12 is evenly
divisible by 4.
Here is the code from my original program:
#include
using namespace::std;
// Use of a flag to control the game
// The program plays the role of the "Judge" for the game
// - program askes for a valid selection
// - program determines if the game is over
// - program declares the winner
void main()
{
bool gameOver = false; // flag
int numberEggs = 13;
int player = 1; // 1 for Tortoise , 2 for Hare
int selected;
while (!gameOver)
{
cout << "Enter your selection, "
<< (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)
{
numberEggs -= selected;
if (numberEggs > 0)
{
(player ? (player == 1 ? player = 2 : player = 1) : player = 1);
}
else
{
gameOver = true;
}
}
else // not a valid selection
{
cout << "Not a valid egg selection, try again ";
}
} // end while
cout << "The winner is:" << (player == 1 ? "Tortoise" : "Hare") << endl;
return;
/* Output: The winner is: Tortoise
Output 2: The winner is: Hare
Output 3: The winner is: Hare */
} // end main
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
