In: Computer Science
In this game, two players sit in front of a pile of 100 stones. They take turns, each
removing between 1 and 5 stones (assuming there are at least 5 stones left in the pile).
The person who removes the last stone(s) wins.
Write a program to play this game. This may seem tricky, so break it down into parts.
Like many programs, we have to use nested loops (one loop inside another).
In the outermost loop, we want to keep playing until we are out of stones.
Inside that, we want to keep alternating players. You have the option of either writing
two blocks of code, or keeping a variable that tracks the current player. The second way
is slightly trickier since we haven't learned lists yet, but it's definitely do-able!
Finally, we might want to have an innermost loop that checks if the user's input is valid.
Is it a number? Is it a valid number (e.g. between 1 and 5)? Are there enough stones in
the pile to take off this many? If any of these answers are no, we should tell the user
and re-ask them the question.
So, the basic outline of the program should be something like this:
TOTAL = 100
MAX = 5
pile = TOTAL // all stones are in the pile to start
while (pile>=0){
while (palyer1_turn){
[ask player 1]
[check player 1's input... is it valid?]
[same as above for player 2]
Note how the important numbers 100 and 5 are stored in a single variable at the top.
This is good practice -- it allows you to easily change the constants of a program. For
example, for testing, you may want to start with only 15 or 20 stones.
Be careful with the validity checks. Specifically, we want to keep asking player 1 for their
choice as long as their answer is not valid, BUT we want to make sure we ask them at
least ONCE. So, for example, we will want to keep a variable that tracks whether their
answer is valid, and set it to false initially.
#include<iostream>
#include<algorithm>
using namespace std;
int pileGame(int max, int pile)
{
int pl1, pl2;
while(pile>0)
{
int flag=1;
while(flag)
{
cout<<"Player 1 Enter a valid number from 1 to "<<min(5,pile)<<endl;
cin>>pl1;
if((pl1>0 && pl1<=5) && pile>=pl1)
{
pile=pile-pl1;
if(pile<=0)
{
cout<<"Winner Player 1";
break;
}
else
{
cout<<"Stones left "<<pile<<endl;
}
flag=0;
}
else
{
cout<<"It was not a valid number. Please try again."<<endl;
}
}
flag =1;
while(flag && pile>0)
{
cout<<"Player 2 Enter a valid number from 1 to "<<min(5,pile)<<endl;
cin>>pl2;
if((pl2>0 && pl2<=5) && pile>=pl2)
{
pile=pile-pl2;
if(pile<=0)
{
cout<<"Winner Player 2";
break;
}
else
{
cout<<"Stones left "<<pile<<endl;
}
flag=0;
}
else
{
cout<<"It was not a valid number. Please try again."<<endl;
}
}
}
return 0;
}
int main()
{
int max=5;
int pile = 100;
pileGame(max, pile);
}
In this code, we have used a pile of 100 stones. (pile=100) and a max variable(max=100). We defined a function pileGame() where we are taking Max and Pile as arguments. In this function, we defined two players pl1 and pl2. We have used a loop to check the condition for number of stones in pile. In this loop we have used another loop to check a valid input from player. If the value entered by player is valid (1 to 5) then we proceed further and show the remaining number of stones present in pile. After complete chance of player 1 the turn moves to player 2 and conditions are checked. In the end whoever plays the last turn to get the last stones out is the Winner.
Attaching a output snapshot to get a better unterstanding: In the second output image it is visible that in case of invalid input it'll ask again for entering valid input. So, it'll ask for valid inputs untill a valid input is entered.