Question

Pig is a traditional dice jeopardy game. Players take turns, starting with a randomly-chosen player. On their turn a player r

Pig PigGame WINNING SCORE int 100 fread-only) +main(StringD): void + addPlayers(theGame : PigGame, stdin: Scanner): void +add

Human players have names, and decide whether to hold or not based on user input. 3 AI players are named Bot #0, Bot #1, e

Sample Run ow many players? 3 1) Humarn 2) Dumb AI 3) Threshold AI Bob rolled a 2 Hold? [y/n] Bob rolled a 6 Hold? [y/n] Name

java

thank you!

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

Java code for Pig Game:

import static java.lang.System.*;
import java.util.*;

public class PigDiceGame {

public static Scanner scan = new Scanner (in);
public static Random generator = new Random ();

public static int p1Score = 0;
public static int p2Score = 0;
public static int counter;

public static void main(String args[]){
    out.println("Welcome to Pig, the two-player dice game!");
    out.println("Player 1 go first");
    p1Mechanics ();
}

public static int Roll() {
    int Low = 1;
    int High = 7;
    int Roll = generator.nextInt (High - Low) + Low;
    return Roll;
}

public static void p1Mechanics() {
    counter = 0;
    out.println("Do you want to play this turn? (Y/N)");
    String answer = scan.nextLine ();
    answer = answer.replaceAll (" ", "") ;
    answer = answer.toUpperCase ();
    if (answer.equals("N")) {
        out.println ("Player 1 skipped their turn.");
        out.println ("Player 2's turn to roll");
        p2Mechanics ();
    }
    int Roll = Roll();
    out.println ("You rolled a " + Roll);
    while (p1Score + counter < 100) {
        while (Roll != 1) {
            counter += Roll;
            out.println ("Do you want to roll more? (Y/N)");
            answer = scan.nextLine ();
            answer = answer.replaceAll (" ", "") ;
            answer = answer.toUpperCase ();
            while(!(answer.equals("Y")) && !(answer.equals("N"))){
                out.println("Please enter only Y/N:");
                answer = scan.nextLine ();
                answer = answer.replaceAll (" ", "") ;
                answer = answer.toUpperCase ();
            }
            if (answer.equals("N")) {
                p1Score += counter;
                out.println ("Player 1 ended their turn.");
                out.println ("P1 vs P2: " + p1Score + "-" + p2Score);
                out.println ("Player 2's turn to roll");
                p2Mechanics ();
            }
            Roll = Roll ();
            out.println ("You rolled a " + Roll);

        }
        if (Roll == 1) {
            counter = 0;
        }
        out.println ("Player 1 rolled a '1'.");
        out.println ("P1 vs P2: " + p1Score + "-" + p2Score);
        out.println ("Player 2's turn to roll");
        p2Mechanics ();
    }
    out.println("Player 1's score reached 100!");
    out.println("P1 wins by " + (p1Score - p2Score) + " points!");
    exit (0);
}

public static void p2Mechanics() {
    counter = 0;
    out.println("Do you want to play this turn? (Y/N)");
    String answer = scan.nextLine ();
    answer = answer.replaceAll (" ", "") ;
    answer = answer.toUpperCase ();
    if (answer.equals("N")) {
        out.println ("Player 2 skipped their turn.");
        out.println ("Player 1's turn to roll");
        p1Mechanics ();
    }
    int Roll = Roll();
    out.println ("You rolled a " + Roll);
    maxScore: while (p2Score < 100) {
        while (Roll != 1) {
            counter += Roll;
            out.println ("Do you want to roll more? (Y/N)");
            answer = scan.nextLine ();
            answer = answer.replaceAll (" ", "") ;
            answer = answer.toUpperCase ();
            while(!(answer.equals("Y")) && !(answer.equals("N"))){
                out.println("Please enter only Y/N:");
                answer = scan.nextLine ();
                answer = answer.replaceAll (" ", "") ;
                answer = answer.toUpperCase ();
            }
            if (answer.equals("N")) {
                p2Score += counter;
                out.println ("Player 2 ended their turn.");
                out.println ("P1 vs P2: " + p1Score + "-" + p2Score);
                out.println ("Player 1's turn to roll");
                p1Mechanics ();
            }
            Roll = Roll ();
            out.println ("You rolled a " + Roll);

        }
        if (Roll == 1) {
            counter = 0;
        }
        out.println ("Player 2 rolled a '1'.");
        out.println ("P1 vs P2: " + p1Score + "-" + p2Score);
        out.println ("Player 1's turn to roll");
        p1Mechanics ();
    }
    out.println("Player 2's score reached 100!");
    out.println("P2 wins by " + (p2Score - p1Score) + " points!");
    exit (0);
}

}

Thank you,

Don't forget to upvote this answer. :)

Add a comment
Know the answer?
Add Answer to:
java thank you! Pig is a traditional dice jeopardy game. Players take turns, starting with a...
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
  • 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...

  • 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,...

  • Java Listed below is code to play a guessing game. In the game, two players attempt...

    Java Listed below is code to play a guessing game. In the game, two players attempt to guess a number. Your task is to extend the program with objects that represent either a human player or a computer player. boolean checkForWin (int guess, int answer) { System.out.println("You guessed" + guess +"."); if (answer == guess) { System.out.println( "You're right! You win!") ; return true; } else if (answer < guess) System.out.println ("Your guess is too high.") ; else System.out.println ("Your...

  • 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,...

  • 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...

  • 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...

  • 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...

  • This is my code for my game called Reversi, I need to you to make the...

    This is my code for my game called Reversi, I need to you to make the Tester program that will run and complete the game. Below is my code, please add comments and Javadoc. Thank you. public class Cell { // Displays 'B' for the black disk player. public static final char BLACK = 'B'; // Displays 'W' for the white disk player. public static final char WHITE = 'W'; // Displays '*' for the possible moves available. public static...

  • In a game of Tic Tac Toe, two players take turns making an available cell in...

    In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...

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