Question

Write a Game for a picking stones being played between a user and computer. Rules for...

Write a Game for a picking stones being played between a user and computer. Rules for the game are: • There are total 21 stones. • User will start picking the stones. • User cannot pick more than 3 or remaining stones (whichever is less). • After the user picks the stones, computer does its picking. • Computer cannot pick more than 3 or remaining stones (whichever is less). • Repeat the picking (User and Computer) if stones are available. • Whoever is forced to pick up the last stone loses the game. • Your program should ensure that the computer always wins.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PickingStoneGame.java

import java.util.Random;
import java.util.Scanner;

public class PickingStoneGame {
   public static void main(String[] args) {
       // Random class object to generate random integer
       Random random=new Random();
       // Scanner class object to get the input from user
       Scanner input=new Scanner(System.in);
       System.out.println("*** Welcome to Picking Stone Game ***");
       // declare local variables
       int userPick;
       int computerPick;
       int stones=21;
       while(true) {
           // print remaining stones
           System.out.println("No of stones remaining: "+stones);
           System.out.print("User picks: ");
           userPick=input.nextInt(); // get the user pick
           // check for user picked valid number of picks or not
           if(userPick>3) {
               System.out.println("Invalid pick, kindly pick between 1 to 3 stones.");
               continue;
           }
           if(userPick>stones) {
               System.out.println("Invalid pick, kindly pick less than or equal to remaing stones.");
               continue;
           }
           // subtract stones user picked from total stones
           stones-=userPick;
           // check user picked last stone or not
           if(stones==0) {
               System.out.println("*** You Lost the Game ***");
               break;
           }
           while(true) {
               // randomly generate the computer's pick
               computerPick=random.nextInt(2)+1;
               if(computerPick>stones)
                   continue;
               System.out.println("Computer picks: "+computerPick);
               break;
           }
           // subtract stones computer picked from total stones
           stones-=computerPick;
           // check computer picked last stone or not
           if(stones==0) {
               System.out.println("*** Congratulations.. You Won the Game ***");
               break;
           }
       }
       input.close(); // close Scanner object
   }
}

Sample run

Add a comment
Know the answer?
Add Answer to:
Write a Game for a picking stones being played between a user and computer. Rules for...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider the following game, called ‘Picking stones’. There are three players, A, B and C, who...

    Consider the following game, called ‘Picking stones’. There are three players, A, B and C, who have four stones set in front of them. The rules of the game are as follows. A moves first and takes one or two stones. B moves next and takes one or two stones. Then, if there are any stones left, C moves and takes one or two stones. Finally, A picks up the last stone, if there is one left. Whoever picks up...

  • **IN JAVA** Make a Game -Game Strategy The program and the user alternate turns, picking up...

    **IN JAVA** Make a Game -Game Strategy The program and the user alternate turns, picking up one, two, or three straws on each turn. The program goes first. The player to pick up the last straw loses. -Game process Your program should start by generating and displaying a random integer (which represents the number of straws) between 10 and 20 inclusive. If this number is 1 more than a multiple of 4, add 1. For example, if the random integer...

  • C++ Part 2: Rock Paper Scissors Game Write a program that lets the user play this...

    C++ Part 2: Rock Paper Scissors Game Write a program that lets the user play this game against the computer. The program should work as follows: When the program begins, a random number between 1 and 3 is generated. If the number is 1, the computer has chosen rock. If the number is 2, the computer has chosen paper. If the number is 3, the computer has chosen scissors. Don't display the computer's choice yet. Use a menu to display...

  • PYTHON Stuck on this problem: Write a program for playing a game with the computer. First,...

    PYTHON Stuck on this problem: Write a program for playing a game with the computer. First, generate a random integer in the range of 6 through 9. This will be the computer’s pick. Then generate three random integers for the human player. These random integers are in the range of 1 through 10. If any of these three random integers is greater than the computer’s pick, the human player wins the game. Otherwise, the human player loses. You must use...

  • PYTHON Write a program for playing a game with the computer. First, generate a random integer...

    PYTHON Write a program for playing a game with the computer. First, generate a random integer in the range of 6 through 9. This will be the computer’s pick. Then generate three random integers for the human player. These random integers are in the range of 1 through 10. If any of these three random integers is greater than the computer’s pick, the human player wins the game. Otherwise, the human player loses. You must use a for loop to...

  • Using Dr Java Objective: Create a game of video poker with a graphical user interface (GUI)....

    Using Dr Java Objective: Create a game of video poker with a graphical user interface (GUI). The player should start with $100, and then the system deals out 5 playing cards. After the player sees the cards they are then asked to wager $10, $20, $50, or $100. Next the user picks which cards they wish to throw away, and then the system deals more cards in their place. Once this has concluded money are awarded by these criteria: Nothing...

  • This program should be in c++. Rock Paper Scissors: This game is played by children and...

    This program should be in c++. Rock Paper Scissors: This game is played by children and adults and is popular all over the world. Apart from being a game played to pass time, the game is usually played in situations where something has to be chosen. It is similar in that way to other games like flipping the coin, throwing dice or drawing straws. There is no room for cheating or for knowing what the other person is going to...

  • Monshimout is a game from the Cheyenne people and played by women. It could be played...

    Monshimout is a game from the Cheyenne people and played by women. It could be played by two or more players, and if played by more than two, then the players divided into two equal teams. Game equipment consisted of five plum stones and a basket made of woven grass or willow twigs. The basket measured 3-4 inches deep, 8 inches across at the top, and almost 1/2 inch thick. The plum stones were left plain on one side, but...

  • Write a program that plays the popular scissor-rock-paper game. (A scissor can cut a paper, a...

    Write a program that plays the popular scissor-rock-paper game. (A scissor can cut a paper, a rock can knock a scissor, and a paper can wrap a rock.) The program randomly generates a number 0, 1, or 2 representing scissor, rock, and paper. The program prompts the user to enter a number 0, 1, or 2 and displays a message indicating whether the user or the computer wins, loses, or draws. Here are sample runs: Enter your selection: scissor (0),...

  • In python language Write a program that lets the user play the game of Rock, Paper,...

    In python language Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: 1. When the program begins, a random number in the range of 1 and 3 is generated. 1 = Computer has chosen Rock 2 = Computer has chosen Paper 3 = Computer has chosen Scissors (Dont display the computer's choice yet) 2. The user enters his or her choice of “Rock”, “Paper", “Scissors" at...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT