Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a java program for the dice game Pig for 2 players. Game Rules The rules to the (dice) game of Pig: You need 2

Write a java program for the dice game Pig for 2 players. Game Rules The rules to the (dice) game of Pig: You need 2 dice. a. The players each take turns rolling two die. b. A player scores the sum of the two dice thrown (unless the roll contains a 1): If a single number 1 is thrown on either die, the score for that whole turn is lost (referred to as Pigged Out). A 1 on both dice is scored as 25. c. During a single turn, a player may roll the dice as many times as they desire. The score for a single turn is the sum of the individual scores for each dice roll. d. The first player to reach the goal score wins unless a player scores higher subsequently in the same round. Therefore, everyone in the game must have the same number of turns. Execution and User Input Program is interactive with the user(s) and will take in the following information:

the sample input / output sessions for details; we describe them again here emphasizing input. The program will prompt for the number of points the game will be played to (1-100). Once a valid goal score has been entered the game will roll the dice for the first player by randomly selecting the valid values 1 thru 6 for each of the two dice. The score will be displayed and the player will be prompted to continue their turn by rolling the die again (unless they have pigged out by rolling a single 1). The player will have the option of continuing or ending the turn by entering y for Yes. When Player 1 completes their turn, Player 2 will engage the exact same way. At the conclusion of both players turns the total score for each will be compared to the goal score to determine if the game has ended. If the goal score has not been reached, then the game continues with both players taking their turn in sequence. Once one or both of the players reaches the goal score the game ends and the results are displayed. The game may end in a tie. At the conclusion of a game, the program will prompt whether a new game shall be played: y for Yes. Requirements Generally, user input must not be case sensitive. The project must consist of the following classes. o Class EntryPoint: contains the main method and instantiation of the GameController class; this class has been provided. o Class GameController: Manages the flow of 2-player game play. The players must have the option to play as many games as desired. The user must supply a valid maximum score. After the goal score is reached by at least one player, the winner and winning score must be displayed. Will show total scores after each round of rolls. o Class PigDice: Holds the state of a set of dice for a player. Scoring must be correct for all combinations of dice. o Class Die: Represents a standard, fair, 6-sided die. Simulates a die being rolled. The face value of the die can be observed without rolling. Specifications Die class Provides all functionality for random number generation and access of the rolled face value. Must provide an overloaded constructor that, for testing purposes, takes a random number seed. For testing and submission, use 5 and 10 as seeds to the random number generator. EntryPoint class Contains the main method. It declares and allocates a GameController object and invokes the play method. GameController class Method play contains the central game control logic. Use of additional private methods must be used for modularization. o For example, since the play method controls the game logic, a private method takeTurn (which takes the keyboard Scanner and a PigDice object) should call a method that takes a turn for a single player. o A second private method would maintain functionality to acquire the valid goal score. PigDice Class This class (1) maintains the Player Pig score for the round, (2) maintains and saves the total Pig score for a player, and (3) provides functionality related to rolling and scoring die. The following methods discuss that functionality in more detail. Method currentTotal returns the current total integer score for a game of Pig. Method currentRound returns the current integer score for a round. Method rollDice simulates rolling two die. Method evaluate computes the rolled score and adds to the current round score. Method lastRoll acquires a formatted string (consistent with the sample session output) describing the values of the last dice roll. Method save will (1) add the current round score to the total score and (2) clear the current round score, and (3) return the complete round score. Method piggedOut returns true / false whether the last roll contained a 1 and ended the roll. Additional private methods used for modularization: o Method singleOneRolled returns true / false whether one of the two die resulted in a 1. o Method doubleOnesRolled returns true / false whether both die rolled a 1.

Here are the skeletons for this.

import java.util.Random;

//

// class to manage the value of a single simulated die

//

public class Die

{

private int _pips = 1;

private final int _MAX_PIPS = 6;

private Random _randNum;

// constructor that will create a Random class and generate a random start value.

public Die()

{

// TODO

}

//

// constructor that will create a Random class, set the seed of the RNG,

// and generate a random start value.

//

public Die(int seed)

{

// TODO

}

//

// accessor to return the current value of the die.

//

public int faceValue()

{

// TODO

}

//

// mutator to randomly change the value of the die.

//

public int roll()

{

// TODO

}

}

.

public class EntryPoint

{

public static void main(String[] args)

{

//

// start up

//

GameController gc = new GameController();

gc.play();

}

}

..

import java.util.Scanner;

public class GameController

{

// central method to start and manage game play

public void play()

{

// TODO

}

//

// Returns the initial max score (loops until a value between 1 <= score <= 100 is entered)

//

private int getInitialMax(Scanner kb)

{

// TODO

}

//

// method for managing a single session of rolling dice

//

private void takeTurn(Scanner kb, PigDice pd)

{

String response;

boolean keepRolling = true;

do

{

// Roll the dice

pd.rollDice();

// Report the result

System.out.println(pd.lastRoll() + " scored " + pd.evaluate() + " points.");

// Did the player pig out?

if (pd.piggedOut())

{

System.out.println("You pigged out this turn.");

}

else

{

//

// Roll again; see if the user wants to roll again to add to total or pass and keep current points

//

System.out.println("Your current roll is " + pd.currentRound() + " points. Keep rolling? Respond (Y/N) only.");

if (!yesResponse(kb))

{

keepRolling = false;

int roundScore = pd.save();

System.out.printf("Your total for the round was %d and your total score is %d. ", roundScore, pd.currentTotal());

}

}

} while (!pd.piggedOut() && keepRolling);

}

//

// Returns true if the user enters a 'y' or 'Y'

//

final String _YES = "Y";

public boolean yesResponse(Scanner kb)

{

return kb.nextLine().substring(0, 1).toUpperCase().equals(_YES);

}

}

.

// this class manages the state of the dice and the scoring

public class PigDice

{

// keep track of total and round scores as well as the two dice.

private int _totalScore = 0;

private int _roundScore = 0;

private Die _die1;

private Die _die2;

public PigDice()

{

// TODO

}

// accessor for total score

public int currentTotal()

{

return _totalScore;

}

// accessor for this round score

public int currentRound()

{

return _roundScore;

}

// accessor to see if the user has rolled a single "1" and loses turn

public boolean piggedOut()

{

// TODO

}

// mutator that simulates rolling two dice and evaluating the resulting score

public void rollDice()

{

// Roll the die

_die1.roll();

_die2.roll();

}

// accessor for a formatted string of what the last roll looked like

public String lastRoll()

{

return "D1 (" + _die1.faceValue() + "), D2 (" + _die2.faceValue() + ")";

}

public int evaluate()

{

// TODO

}

private boolean singleOneRolled()

{

// TODO

}

private boolean doubleOnesRolled()

{

// TODO

}

//

// mutator to end a round and keep the add this round to the total

// also returns the total value of the round and resets the round total for next time

//

public int save()

{

// TODO

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions