Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to play the pig game against the computer. At each turn, the cur

ID: 3606056 • Letter: W

Question

Write a program to play the pig game against the computer. At each turn, the current player will roll a pair of dice and accumulates points.

I'm in a beginers Java class and some if these steps are really confusing to me. Especially step 5. How do you declare boolean variables?

Write a program to play the pig game against the computer. At each turn, the current player will roll a pair of dice and accumulates points. The goal is to reach to 100 or more points before your opponent does. (For the testing purposes use 30 instead of 100 points) If, on any turn, the player rolls a 1, all the points accumulated for that round are forfeited and the control of the dice moves to the other player. If the player rolls two 1s in one turn, the player loses all the points accumulated thus far are forfeited and the control moves to the other player. The player may voluntarily turn over the control of the dice after each roll. Therefore player must decide to roll again (be a pig) and risk losing points, or relinquish control of the dice, possibly allowing the other player to win. Computer is going to flip a coin to choose the first player

Here is the list of the tasks that needs to be done

1. Describe the game by writing a method.

2. Data validation: A method that accepts a Scanner object as its parameter, prompt the user to enter “yes” or “no”. as long as the user is not entering a valid input , prompt the user again

3. Flip the coin: This method accepts a Random object and returns” head” or “tail” based on the random number that was generated

4. Roll two dices: this method accepts a Random object. Generates two random number representing one of the numbers on a dice. Returns the sum of the dices.

5. Choose a name for the computer: Come up with 10 different name for the computer. Then select a random name from the list that you created. Return the selected name.

6. Play: this method calls the other methods to play the game

Must declare the following two constants

public static final int POINTS = 40;
publinc static final into FORFEIT_Points = 20;
a. Declare all the needed variables to keep track of the scores for each player, and Boolean variables to indicate who is playing at the moment.
b. Ask the user’s name
c. Decide who start the game first by calling one of the methods you created to flip the coin.
d. Write conditional statements to switch the game between the computer and the player based on the dice rolled and overall points. Read the output and the program description to figure out the conditions. You need to use couple while loops: one loop for the human player, one loop for the computer player.
The previous two loops will be nested in another while loop to witch the game between the two players.

//declaring your variables
While (there is no winner)
{

//some codes
While (human is playing)
{

//some codes, conditional statements
}
//you may need some codes
While (computer is playing)
{

//some codes, conditional statements
}

//you may need some codes


} e. Keep playing the game until the player or the computer has 100 or more points (40 or more for the simplicity). You are not allowed to hard code any numbers.

7. Main method: Calls the method play, keep playing the game as long as there are more players.

This is my code so far. Any help would be useful thanks!

public class Pig{

static int roundTotal = 0;
public static final int POINT = 40;
public static final int FORFEIT_POINTS = 20;

public static void main(String[] args){
Scanner kb = new Scanner(System.in);
Random rand = new Random();
description();
playGame(rand, kb);
}

public static String computerChoice(Random rand){
int comp = rand.nextInt(2);
String computer = "";
switch (comp){
case 0:
computer = "heads";
break;
case 1:
computer = "tails";
break;
}
return computer;
}
public static int pairOfDice (Random rand){
int dice1 = randomDice (rand);
int dice2 = randomDice(rand);
System.out.println("dice1: " + dice1);
System.out.println("dice2: " + dice2);
int finalDice = dice1 + dice2;
return finalDice;
}

public static int randomDice(Random rand){
int randDice = rand.nextInt(5);
int dice= 0;
switch (randDice){
case 0:
dice += 1;
break;
case 1:
dice += 2;
break;
case 2:
dice += 3;
break;
case 3:
dice = 4;
break;
case 4:
dice += 5;
break;
case 5:
dice += 6;
break;
}
return dice;
}

public static String randomName(Random rand){
int randName = rand.nextInt(10);
String compName= "";
switch (randName){
case 0:
compName = "Name1";
break;
case 1:
compName = "Name2";
break;
case 2:
compName = "Name3";
break;
case 3:
compName = "Name4";
break;
case 4:
compName = "Name5";
break;
case 5:
compName = "Name6";
break;
case 6:
compName = "Name7";
break;
case 7:
compName = "Name8";
break;
case 8:
compName = "Name9";
break;
case 9:
compName = "Name10";
break;
}
return compName;
}

public static void playGame(Random rand, Scanner kb){
String computer = computerChoice(rand);
String compName = randomName(rand);
int points = 0;
System.out.println("Hi my name is " + compName);
System.out.print("What is your name? ");
String playerName = kb.next();
System.out.println("Hi " + playerName +", I am flipping the coin to determine who goes first");
System.out.println("Press any key to start the game");

int dice = pairOfDice(rand);
System.out.println(dice);
}

public static void description(){
System.out.println("***********************************************************************************");
System.out.println("***********************************************************************************");
System.out.println("letsw start the fun");
System.out.println();
}

Explanation / Answer

Import game.ComputerBrain;

Import game.Player1;

Import game.PigScannerInterface;

import game.Player2;

import game.Die;

public static final int POINTS = 40;
public static final int FORFEIT_Points = 20;

    private PigScannerInterface userInterface;

private ComputerBrain computer;

   private Die die1;

private Die die2;

    public Pig()

    {

       userInterface = new PigScannerInterface();

        human = new Player1();

        computer = new ComputerBrain();

        die1 = new Die();

        die2 = new Die();

        POINTS = 0;

    }

    private void calculateRoll(int rollOne, int rollTwo, Player p)

    {

        int points = rollOne + rollTwo;

        if (points == 2)

        {

            p.resetScore();

            POINTS = 0;

        }

        else if (rollOne == 1 || rollTwo == 1)

        {

            POINTS = 0;

        }

        else

        {

            POINTS += points;

        }

    }

    private boolean isWinner(Player p)

    {

        return p.getScore() >= 100 ? true : false;

    }

    public void processPlayer1Turn()

    {

        calculateRoll(die1.roll(), die2.roll(), human);

        userInterface.displayRollResults(

            die1.getFaceValue(),

            die2.getFaceValue(),

            POINTS,

            human.getScore());

        userInterface.displayRollAgainQuestion();

        if (userInterface.askRollAgain())

        {

            processPlayer1Turn();

        }

        Else

        {

            human.addPoints(POINTS);

        }

    }

    public void processComputerTurn()

    {

        calculateRoll(die1.roll(), die2.roll(), computer);

        userInterface.displayRollResults(

            die1.getFaceValue(),

            die2.getFaceValue(),

            POINTS,

            computer.getScore());

        if (computer.rollAgain(POINTS))

        {

            userInterface.displayComputerRollAgain(true);

          processComputerTurn();

        }

        else

        {

            userInterface.displayComputerRollAgain(false);

            computer.addPoints(POINTS);

        }

    }

    public void play()

    {

        userInterface.displayWelcomeMessage();

        human.setName(userInterface.askPlayer());

        while (true)

        {

            userInterface.displayTurnHeader(human.getName());

            processPlayer1Turn();

            if (isWinner(human))

            {

                userInterface.displayWinner(human.getName());

                break;

            }

            POINTS = 0;

            userInterface.displayTurnHeader("Computer");

            processComputerTurn();

            if (isWinner(computer))

            {

                userInterface.displayWinner("Computer");

                break;

            }

            POINTS = 0;

        }

    }

    public static void main(String[] args)

    {

        Pig game = new Pig();

        game.play();

    }

}

TestPig.java

import student.TestRandom;

public class TestPig extends student.TestCase

  

{

    private Pig pig;

    public TestPig()

    {

}

  

    public void setUp()

    {

        pig = new Pig();

    }

    public void testRollNoOne()

    {

        TestRandom.setNextInts(1, 1);

        setSystemIn("n");

        pig.processPlayer1Turn();

        assertEquals(

            "Roll results: 2 + 2 = 4   Points: 4   Bank: 0   " + "Roll again? ",

            systemOut().getHistory());

    }

  

    public void testComputerRollAgain()

    {

        TestRandom.setNextInts(5, 5, 5, 5,5,5);

        pig.processComputerTurn();

        assertEquals(

            "Roll results: 6 + 6 = 12 Points: 12 Bank: 0   Rolling again. " +

            "Roll results: 6 + 6 = 12 Points: 24 Bank: 0   Not rolling " +

            "again. ", systemOut().getHistory());

    }

public void testRollOneDieTwo()

    {

        TestRandom.setNextInts(1, 0);

        setSystemIn("n");

        pig.processPlayer1Turn();

        assertEquals(

            "Roll results: 2 + 1 = 3   Points: 0   Bank: 0   Roll again? ",

            systemOut().getHistory());

    }

   

    /**

     * Tests the roll of only one on die 1 to ensure the turn points is 0.

     */

    public void testRollOneDieOne()

    {

        TestRandom.setNextInts(0, 1);

        setSystemIn("n");

        pig.processPlayer1Turn();

        assertEquals(

            "Roll results: 1 + 2 = 3   Points: 0   Bank: 0   Roll again? ",

            systemOut().getHistory());

    }

    public void testSnakeEyes()

    {

        TestRandom.setNextInts(2, 2, 2, 2, 0, 0);

        setSystemIn("n", "n", "n");

        pig.processPlayer1Turn();

        pig.processPlayer1Turn();

        pig.processPlayer1Turn();

        assertEquals(

            "Roll results: 3 + 3 = 6   Points: 6   Bank: 0   Roll again? Roll"

                + " results: 3 + 3 = 6   Points: 12 Bank: 6   Roll again? "

                + "Roll results: 1 + 1 = 2   Points: 0   Bank: 0   "

                + "Roll again? ",

            systemOut().getHistory());

    }

    public void testRollAgainYes()

    {

        TestRandom.setNextInts(2, 2, 2, 2);

        setSystemIn("y", "n");

        pig.processPlayer1Turn();

        assertEquals(

            "Roll results: 3 + 3 = 6   Points: 6   Bank: 0   Roll again? Roll"

                + " results: 3 + 3 = 6   Points: 12 Bank: 0   Roll again? ",

            systemOut().getHistory());

    }

    public void testRollAgainN()

    {

        TestRandom.setNextInts(2, 2, 2, 2);

        setSystemIn("n");

        pig.processPlayer1Turn();

        assertEquals(

            "Roll results: 3 + 3 = 6   Points: 6   Bank: 0   Roll again? ",

            systemOut().getHistory());

    }

    public void testComputerWinner() {

TestRandom.setNextInts(

        .setNextInts(1, 1, 5, 5, 1, 1 , 5, 5, ,1, 1, 1, 1, 5, 5,);

        setSystemIn("Man", "n", "n", "n", "n", "n");

        pig.play();

        assertEquals("-------Welcome to the game of Pig------- "

             + "You will play against the computer by rolling a pair of dice."

   + " The first player to 30 points wins the game.

Rules: Rolling snake eyes will result in losing all points

accumulated for the game. Rolling a one on either die results

in losing all points for the turn. Please enter your name:"

             + " Man Roll results: 2 + 2 = 4   Points: 4   Bank: 0   "

             + "Roll again? Computer Roll results: 6 + 6 = 12 "

             + "Points: 12 Bank: 0   Rolling again."

             + " Roll results: 6 + 6 = 12 Points: 24"

             + " Bank: 0   Not rolling again. Man Roll results: "

             + "2 + 2 = 4   Points: 4   Bank: 4   Roll again? Computer "

             + "Roll results: 6 + 6 = 12 Points: 12 Bank: 24 Rolling

             + "again. Roll results: 6 + 6 = 12 Points: 24 Bank: 24 Not "

             + "rolling again. Mans Roll results: 2 + 2 = 4   Points: 4"

             + "   Bank: 8   Roll again? Computer Roll results: 6 + 6 = 12"

             + " Points: 12 Bank: 48 Rolling again. Roll results: 6 + "

             + "6 = 12 Points: 24 Bank: 48 Not rolling again. Man "

             + " Rolling again. Roll results: 6 + 6 = 12 Points: 24 Bank:"

             + " 72 Not rolling again. Man Roll results: 2 + 2 = 4   "

             + "Points: 4   Bank: 16 Roll again? Computer Roll results: "

             + "6 + 6 = 12 Points: 12 Bank: 96 Rolling again. Roll "

             + "results: 6 + 6 = 12 Points: 24 Bank: 96 Not rolling again."

             + " Computer has won the game! ", systemOut().getHistory());

    }

    public void testHumanWinner()

    {

        TestRandom.setNextInts(5, 5, 5, 5, 5, 5, 5, 5, 5, 5);

        pig.play();

        assertEquals("-------Welcome to the game of Pig------- "

            + "You will play against the computer by rolling a pair of dice. "

            + "The first player to 30 points wins the game. "

            + "Rules: Rolling snake eyes will result in losing all points "

            + "accumulated for the game. Rolling a one on either die results "

            + "in losing all points for the turn. Please enter your name: "

            + "Man Roll results: 6 + 6 = 12 Points: 12 Bank: 0   "

            + "Roll again? Roll results: 6 + 6 = 12 Points: 24 Bank: 0   "

            + "Roll again? Roll results: 6 + 6 = 12 Points: 36 Bank: 0   "

            + "Roll again? Roll results: "

            + "6 + 6 = 12 Points: 48 Bank: 0   Roll again? Roll results: "

            + "6 + 6 = 12 Points: 60 Bank: 0   Roll again? "

+ "Man has won the game! ", systemOut().getHistory());

}

public void testMainMethod() {

TestRandom.setNextInts( 5, 5, 5, 5, 5, 5, 5, 5, 5, 5);

setSystemIn( "Man", "y", "y", "y", "y", "y", "n");

Pig.main(null);

assertEquals("-------Welcome to the Pig Game------- "

+ "You will play against the computer by rolling a pair of dice. "

+ "The first player to 30 points wins the game. "

+ "Rules: Rolling snake eyes will result in losing all points "

+ "accumulated for the game. Rolling a one on either die results "

+ "in losing all points for the turn. Please enter your name: "

+ "Man Roll results: 6 + 6 = 12 Points: 12 Bank: 0   "

+ "Roll again? Roll results: 6 + 6 = 12 Points: 24 Bank: 0   "

+ "Roll again? Roll results: 6 + 6 = 12 Points: 36 Bank: 0   "

+ "Roll again? Roll results: "

+ "6 + 6 = 12 Points: 48 Bank: 0   Roll again? Roll results: "

+ "6 + 6 = 12 Points: 60 Bank: 0   Roll again? "

+ "Man has won the game! ", systemOut().getHistory());

}

}

public class Pig {

    private PigScannerInterface userInterface;

private Player1 human;

private ComputerBrain computer;

   private Die die1;

private Die die2;

    public Pig()

    {

       userInterface = new PigScannerInterface();

        human = new Player1();

        computer = new ComputerBrain();

        die1 = new Die();

        die2 = new Die();

        POINTS = 0;

    }

    private void calculateRoll(int rollOne, int rollTwo, Player p)

    {

        int points = rollOne + rollTwo;

        if (points == 2)

        {

            p.resetScore();

            POINTS = 0;

        }

        else if (rollOne == 1 || rollTwo == 1)

        {

            POINTS = 0;

        }

        else

        {

            POINTS += points;

        }

    }

    private boolean isWinner(Player p)

    {

        return p.getScore() >= 100 ? true : false;

    }

    public void processPlayer1Turn()

    {

        calculateRoll(die1.roll(), die2.roll(), human);

        userInterface.displayRollResults(

            die1.getFaceValue(),

            die2.getFaceValue(),

            POINTS,

            human.getScore());

        userInterface.displayRollAgainQuestion();

        if (userInterface.askRollAgain())

        {

            processPlayer1Turn();

        }

        Else

        {

            human.addPoints(POINTS);

        }

    }

    public void processComputerTurn()

    {

        calculateRoll(die1.roll(), die2.roll(), computer);

        userInterface.displayRollResults(

            die1.getFaceValue(),

            die2.getFaceValue(),

            POINTS,

            computer.getScore());

        if (computer.rollAgain(POINTS))

        {

            userInterface.displayComputerRollAgain(true);

          processComputerTurn();

        }

        else

        {

            userInterface.displayComputerRollAgain(false);

            computer.addPoints(POINTS);

        }

    }

    public void play()

    {

        userInterface.displayWelcomeMessage();

        human.setName(userInterface.askPlayer());

        while (true)

        {

            userInterface.displayTurnHeader(human.getName());

            processPlayer1Turn();

            if (isWinner(human))

            {

                userInterface.displayWinner(human.getName());

                break;

            }

            POINTS = 0;

            userInterface.displayTurnHeader("Computer");

            processComputerTurn();

            if (isWinner(computer))

            {

                userInterface.displayWinner("Computer");

                break;

            }

            POINTS = 0;

        }

    }

    public static void main(String[] args)

    {

        Pig game = new Pig();

        game.play();

    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote