Question

In: Computer Science

In this game, two players sit in front of a pile of 100 stones. They take...

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.

Solutions

Expert Solution

​
#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.


Related Solutions

Two players take turns taking sticks from a pile of 16 sticks. Each player can take...
Two players take turns taking sticks from a pile of 16 sticks. Each player can take at most 3 sticks and at least 1 stick at each turn. Whoever takes the final stick wins the game. Describe in words the optimal strategy for each player. Is there a first-mover advantage in this game? Is there a second-mover advantage?
This is a sequential game with two players A and B. In this game a dime...
This is a sequential game with two players A and B. In this game a dime is put on the table. A can take it or pass. If A takes a dime, the game ends; if A passes, then B can take 2 dimes or pass; if B takes 2 dimes, the game ends; if B passes, then A can take 3 dimes or pass; and so on until a choice of a dollar. This process is shown in the...
Consider a game in which two players, Fred and Barney, take turns removing matchsticks from a...
Consider a game in which two players, Fred and Barney, take turns removing matchsticks from a pile. They start with 21 matchsticks, and Fred goes first. On each turn, each player may remove either one, two, or three matchsticks. The player to remove the last matchstick wins the game. (a) Suppose there are only 5 matchsticks left, and it is Fred’s turn. What move should Fred make to guarantee himself victory? Explain your reasoning. (b) Suppose there are 10 matchsticks...
Suppose two players take turns playing another version of the parlor game discussed in class on...
Suppose two players take turns playing another version of the parlor game discussed in class on Tuesday. In this version, players can say any whole number between 1 and 10. The first person to get the running total to 25 wins. Do you want to go first or second? Figure out the optimal strategy for this game. Show your work. (b) (5 points) ECN-322 only for this part: Suppose two players take turns playing another version of the parlor game...
1. Consider the following game. There are two piles of matches and two players. The game...
1. Consider the following game. There are two piles of matches and two players. The game starts with Player 1 and thereafter the players take turns. When it is a player's turn, she can remove any number of matches from either pile. Each player is required to remove some number of matches if either pile has matches remaining, and can only remove matches from one pile at a time. Whichever player removes the last match wins the game. Winning gives...
There are 21 pennies on a table between two players. The two players take turns removing...
There are 21 pennies on a table between two players. The two players take turns removing either 1, 2 or 3 pennies at a time. The player who takes the last penny loses. Use backward induction to come up with a strategy that the player who takes the second turn in the game can use to guarantee that she wins the game.
Is it possible for a game with two players, two choices, and no mixed strategies to...
Is it possible for a game with two players, two choices, and no mixed strategies to have more than two Nash equilibria?
This game is meant for two or more players. In the game, each player starts out...
This game is meant for two or more players. In the game, each player starts out with 50 points, as each player takes a turn rolling the dice; the amount generated by the dice is subtracted from the player’s points. The first player with exactly one point remaining wins. If a player’s remaining points minus the amount generated by the dice results in a value less than one, then the amount should be added to the player’s points. (As an...
There are 100 coins in a jar. Two players take turns removing anywhere from 1-10 coins...
There are 100 coins in a jar. Two players take turns removing anywhere from 1-10 coins from the jar. The player who empties the jar by removing the remaining coin(s) wins the game. To guarantee that you win the game, would you choose to move first or second, and what strategy would you follow?
Consider a following zero-sum game of two players a) Reduce the initial game to a 2x2...
Consider a following zero-sum game of two players a) Reduce the initial game to a 2x2 game. Eliminate only strictly dominated strategies. In the obtained 2x2 game name Player 1’s (Row player’s) strategies “Up” and “Down” and Player 2’s (Column player’s) strategies “Left” and “Right”. b) Find all Nash equilibria of the 2x2 game (both in pure and mixed strategies) ALL ANSWERS MUST BE EXPLAINED. 2 0 1 -1 1 0 1 2 3 1 2 0
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT