#include <iostream>
usingnamespace::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 .
#include <iostream>
usingnamespace 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;
}