Question

**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 is 20, then start with 20 straws. However, if the random integer is 17 (which is 1 more than a multiple of 4), start with 18.

On a user’s turn, your program should prompt for, read in, and process the user’s input (which should be 1, 2, or 3—the number of straws to pick up).

On the program’s turn, it should determine its move by generating a random number between 1 and 3. At the end of each game, your program should display

Do you want to play another game?

It should then read in the user’s yes/no response, and proceed accordingly. Use a do­while loop to control the repetition of games.

If either a user’s move or the program’s move is greater than the number of straws, then all the remaining straws should be picked up, resulting in a loss. For example, if there are 2 straws left, and the user’s move is 3, the remaining 2 straws should be picked up, causing the user to lose.

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

Java Program:

import java.util.Random;

import java.util.Scanner;

public class StrawsGame {

       public static void main(String[] args)

       {

              Random rand = new Random(); //random number generator

              Scanner sc = new Scanner(System.in); //scanner object

             

              String choice; //to hold user's choice

              //loop till user wants to quit

              do

              {

                     int straws = rand.nextInt(11) + 10; //generate straws 10-20

                     //if straws are 1 more than a multiple of 4

                     if(straws%4 == 1)

                           straws = straws + 1; //add 1

                     System.out.println("Total straws are "+straws); //print straws

                     int user, program; //to hold straws picked

                     String winner = "Program"; //assume winner is program

                     //loop till strwas becomes 0

                     while(straws > 0)

                     {

                           System.out.println("\n"+straws+" straws are remaining."); //strwas remaining

                           System.out.print("Enter straws to pick: "); //ask user to pick

                           user = sc.nextInt(); //get value

                           sc.nextLine(); //ignore \n

                           //if more than straws

                           if(straws <= user)

                           {     

                                  straws = 0; //set straws to 0

                                  winner = "Program"; //declare Program as winner

                           }

                           //otherwise subtract user choice

                           else

                                  straws = straws - user;

                           program = rand.nextInt(3) + 1; //programs choice

                           //if more than straws

                           if(straws <= program)

                           {     

                                  program = straws; //set value to straws

                                  straws = 0; //set straws to 0

                                  winner = "User"; //declare user as winner

                           }

                           //otherwise remove program's choice

                           else

                                  straws = straws - program;

                           //print strwas picked by program

                           System.out.println("Program picked "+program+" straws.");

                          

                     }

                     //print winner

                     System.out.println("\nWinner is "+ winner);

                     System.out.print("\nDo you want to continue?(yes/no)"); //ask to continue or not

                     choice = sc.nextLine(); //get choice

                     choice = choice.toLowerCase();

              }while("yes".equals(choice) || "y".equals(choice));

             

              sc.close();

       }

}

OUTPUT:

Total straws are 12 12 straws are remaining Enter straws to pick: 3 Program picked 1 straws 8 straws are remaining Enter stra

Add a comment
Know the answer?
Add Answer to:
**IN JAVA** Make a Game -Game Strategy The program and the user alternate turns, picking up...
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
  • 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....

  • JAVA PROGRAM: Guessing Game Objective The student will write an individualized program that utilizes conditional flow...

    JAVA PROGRAM: Guessing Game Objective The student will write an individualized program that utilizes conditional flow of control structures in Java. Specifications For this assignment, you will write a program that guesses a number chosen by your user. Your program will prompt the user to pick a number from 1 to 10. The program asks the user yes or no questions, and the guesses the user’s number. When the program starts up, it outputs a prompt asking the user to...

  • For this assignment, you will write a program that guesses a number chosen by your user....

    For this assignment, you will write a program that guesses a number chosen by your user. Your program will prompt the user to pick a number from 1 to 10. The program asks the user yes or no questions, and the guesses the user’s number. When the program starts up, it outputs a prompt asking the user to guess a number from 1 to 10. It then proceeds to ask a series of questions requiring a yes or no answer....

  • Modular program mathlab: Write a program in Matlab that would continuously ask the user for an...

    Modular program mathlab: Write a program in Matlab that would continuously ask the user for an input between 1 and 6, which would represent the result of rolling a die. The program would then generate a random integer between 1 and 6 and compare its value to the value entered by user. If the user’s die is larger, it should display, “You got it” If the user’s die is smaller, it should display, “Nice try” If the results are the...

  • IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors...

    IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet....

  • Write a Java application program that plays a number guessing game with the user. In the...

    Write a Java application program that plays a number guessing game with the user. In the starter code that you are given to help you begin the project, you will find the following lines of code: Random generator = args.length == 0 ? new Random() :                    new Random(Integer.parseInt(args[0])); int secret = generator.nextInt(100); You must keep these lines of code in your program. If you delete these lines, or if you change thse lines in any way, then your program...

  • (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against...

    (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. Don’t display the computer’s choice yet. The...

  • Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a...

    Write a Java program to play the game Tic-Tac-Toe. Start off with a human playing a human, so each player makes their own moves. Follow the design below, creating the methods indicated and invoking them in the main program. Use a char array of size 9 as the board; initialize with the characters 0 to 8 so that it starts out looking something like the board on the left. 0|1|2 3|4|5 6|7|8 and then as moves are entered the board...

  • In C... Write a program to simulate a pick-5 lottery game. Your program must generate and...

    In C... Write a program to simulate a pick-5 lottery game. Your program must generate and store 5 distinct random numbers between 1 and 9 (inclusive) in an array. The program prompts the user for: an integer random seed five distinct integers between 1 and 9 (which are stored in another array) The program then compares the two arrays to determine if they are identical. If the two arrays are identical, then the user wins the game. otherwise the program...

  • Write a C++ program that simulates playing the Powerball game. The program will generate random numbers...

    Write a C++ program that simulates playing the Powerball game. The program will generate random numbers to simulate the lottery terminal generated numbers for white balls and red Powerball. The user will have the option to self-pick the numbers for the balls or let the computer randomly generate them. Set the Grand Prize to $1,000,000,000.00 in the program. Project Specifications Input for this project: Game mode choice – self pick or auto pick Five numbers between 1 and 69 for...

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