Question
please write the following program in Java

PIG is one of a family of games called jeopardy dice games, since a players main decision after each roll is whether to jeop
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program Screen Shot:

Pig.java import java.util.Random; import java.util.Scanner; public class Pig { // method to generate a random number betweenpublic static void main(String[] args) { Scanner input = new Scanner(System.in); // DEFINE THE CONSTANTS final int NUMBER_OF_System.out.println(\n----- PLAYER +(i+1)+ -----); System.out.println(Your current score is +scores[i]); // reset whenev}else { // game is still on, player i decides whether to continue or not System.out.println(Your current score is +(current

Sample Output:

PLAYER 1 Your current score is a Dice showed: 3 Your current score is 3 Enter 1 to hold or 2 to roll again 2 Dice showed: 3 Y

Program Code to Copy:

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

public class Pig {
  
   // method to generate a random number between 1 and NUMBER_OF_SIDES
   public static int rollDie() {
       final int NUMBER_OF_SIDES = 6; // number of sides of the die
       Random rand = new Random();
       return rand.nextInt(NUMBER_OF_SIDES) + 1;
   }
   // method to initialize the game scores to all zeroes
   public static int[] initializeGame(int nPlayers) {
       // ensure there are at least 2 players, else no game
       if(nPlayers<2) {
           System.out.println("Game needs at least 2 players");
           System.exit(0);
       }
       // initialize the scores to all zeros and return the scores  
       int[] scores = new int[nPlayers];
       for(int i=0; i<nPlayers;i++)
           scores[i] = 0;
       return scores;
   }

   public static void main(String[] args) {
      
       Scanner input = new Scanner(System.in);
      
       // DEFINE THE CONSTANTS
       final int NUMBER_OF_PLAYERS = 2;
       final int MAX_SCORE = 100;
      
// DEFINE THE VARIABLES
  
// ensure there are atleast 2 players then set all scores to zeros, else exit game
       int[] scores = initializeGame(NUMBER_OF_PLAYERS);

       int result; // what the die shows after rolling  

       // scores accumulated from the current turn (can be all lost if die shows 1)                 
       int currentTurnScore;

// determined when die shows 1 or when player decides to hold or roll again      
   boolean playerCanContinueToPlay;  

       boolean gameOver = false;        // whether "PIG" has been reached
       char choice;                    //decide to hold or to roll again
  
   // loop for gameOver  
   do {
           // loop for players
       for(int i=0; i<NUMBER_OF_PLAYERS; i++) {
          
           System.out.println("\n----- PLAYER "+(i+1)+" -----");
           System.out.println("Your current score is "+scores[i]);

           // reset whenever a player is starting to play their very first turn
           currentTurnScore = 0;

           // loop for turns for the current player
           do {
                 
               playerCanContinueToPlay = false; // by default until proven otherwise
              
               result = rollDie(); // player i rolls the die
               System.out.println("Dice showed: "+result);
              
               if (result==1) {
                   /* player i stops playing and maintains the held scores but
                   loses all the current scores in this turn */
                   System.out.println("Your current score is "+scores[i]);
                   playerCanContinueToPlay = false;
               }else {
                   // player i continues to play
                   currentTurnScore += result;
                   // check if game is still on
                   if(scores[i]+currentTurnScore >= MAX_SCORE) {
                       System.out.println("PIG");
                       gameOver = true;
                       playerCanContinueToPlay = false; // can't play when the game is over
                      
                   }else {
                       // game is still on, player i decides whether to continue or not
                       System.out.println("Your current score is "+(currentTurnScore+scores[i]));
                       System.out.println("Enter 1 to hold or 2 to roll again");
                       choice = input.next().charAt(0); // get input from user
                      
                       if(choice == '1') {
               //This is the only point in the entire program where original scores are updated
                           scores[i] += currentTurnScore; // hold for good.
                           playerCanContinueToPlay = false; // give up the turn
                          
                       }else if(choice == '2') {
                           // continue playing and risk all the currentTurnScore
                           playerCanContinueToPlay = true;
                          
                       }else { // incorrect choice
                           System.out.println("Invalid input");
                           System.exit(0);
                       }
                   }
                      
               }
           }while(playerCanContinueToPlay);
          
       // break the for loop so no player can play anymore because game is over
       if(gameOver)
           break;
       } // for
          
   }while(!gameOver);
   } // main  
}

------------------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERY RELATED TO THIS ANSWER,

IF YOU'RE SATISFIED, GIVE A THUMBS UP
~yc~

Add a comment
Know the answer?
Add Answer to:
please write the following program in Java PIG is one of a family of games called...
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
  • For your final project you will incorporate all your knowledge of Java that you learned in this class by programming the game of PIG. The game of Pig is a very simple jeopardy dice game in which two p...

    For your final project you will incorporate all your knowledge of Java that you learned in this class by programming the game of PIG. The game of Pig is a very simple jeopardy dice game in which two players race to reach 100 points. Each turn, a player repeatedly rolls a die until either a 1 is rolled or the player holds and scores the sum of the rolls (i.e. the turn total). At any time during a player's turn,...

  • The game of Pig is a simple two-player dice game in which the first player to...

    The game of Pig is a simple two-player dice game in which the first player to reach 100 or more points wins. Players take turns. On each turn, a player rolls a six-sided die: If the player rolls a 1, then the player gets no new points and it becomes the other player’s turn. If the player rolls 2 through 6, then he or she can either ROLL AGAIN or HOLD:      At this point, the sum of all rolls...

  • // This program creates a simulated player who takes one turn in the // Pig dice...

    // This program creates a simulated player who takes one turn in the // Pig dice game. The simulated player keeps rolling the die until // the total for the turn is 20 or greater, or until a 1 is rolled. // If a 1 is rolled, the player's score for the turn is 0. Otherwise // the player's score is the sum of the rolls for the turn. // ///////////////////////////////////////////////////////////////////// #include<iostream> #include<cstdlib> #include<time.h> using namespace std; int randNumGen(int upper,...

  • The Rules of Pig Pig is a folk jeopardy dice game with simple rules: Two players...

    The Rules of Pig Pig is a folk jeopardy dice game with simple rules: Two players race to reach 100 points. Each turn, a player repeatedly rolls a die until either a 1 (”pig”) is rolled or the player holds and scores the sum of the rolls (i.e. the turn total). At any time during a player’s turn, the player is faced with two decisions: roll If the player rolls a 1: the player scores nothing and it becomes the...

  • Hello, Could you please help me code this program in Java? It is important that all...

    Hello, Could you please help me code this program in Java? It is important that all rules are carefully followed. Using the PairOfDice class from PP 4.9, design and implement a class to play a game called Pig. In this game, the user competes against the computer. On each turn, the current player rolls a pair of dice and accumulates points. The goal is to reach 100 points before your opponent does. If, on any turn, the player rolls a...

  • Please i need helpe with this JAVA code. Write a Java program simulates the dice game...

    Please i need helpe with this JAVA code. Write a Java program simulates the dice game called GAMECrap. For this project, assume that the game is being played between two players, and that the rules are as follows: Problem Statement One of the players goes first. That player announces the size of the bet, and rolls the dice. If the player rolls a 7 or 11, it is called a natural. The player who rolled the dice wins. 2, 3...

  • Write a c++ program that simulates a million of games in craps. I am having a...

    Write a c++ program that simulates a million of games in craps. I am having a trouble getting to loop a million of times. I am trying to use a const for a million. This is the code so far. #include <iostream> #include <cstdlib>// contains prototypes for functions srand and rand #include <ctime>// contains prototype for function time #include <iomanip> using namespace std; int rollDice(); // rolls dice, calculates and displays sum void printstats(); int totroll = 0, games, point,...

  • Please write the program in python: 3. Design and implement a simulation of the game of...

    Please write the program in python: 3. Design and implement a simulation of the game of volleyball. Normal volleyball is played like racquetball, in that a team can only score points when it is serving. Games are played to 15, but must be won by at least two points. 7. Craps is a dice game played at many casinos. A player rolls a pair of normal six-sided dice. If the initial roll is 2, 3, or 12, the player loses....

  • java thank you! Pig is a traditional dice jeopardy game. Players take turns, starting with a...

    java thank you! Pig is a traditional dice jeopardy game. Players take turns, starting with a randomly-chosen player. On their turn a player rolls a single 6-sided die 1 or more times. After each roll, the player has a choice: hold Stop rolling and add the total of al numbers rolled this turn to their score, or roll Roll the die again. If the player rolls a 1, their turn ends and they score 0 for the turn. Points scored...

  • The Dice game of "Pig" can be played with the following rules. 1. Roll two six-sided dice. Add the face values...

    The Dice game of "Pig" can be played with the following rules. 1. Roll two six-sided dice. Add the face values together. 2. Choose whether to roll the dice again or pass the dice to your opponent. 3. If you pass, then you get to bank any points earned on your turn. Those points become permanent. If you roll again, then add your result to your previous score, but you run the risk of losing all points earned since your...

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