Question: For this project you will write a complete Java program that allows two human players to play a game of Pig against each other. This
For this project you will write a complete Java program that allows two human players to play a game of Pig against each other. This program will be text-based.
The game of Pig is played between two people who take turns rolling a pair of six-sided dice. The goal is to accumulate 100 points in your bank before your opponent does. On each turn, a player may roll the dice multiple times, accumulating a running sum of turn points. After each roll, the player must choose to pass the dice to the other player, banking his/her accumulated points for that turn, or to roll again (be a pig) in an attempt to accumulate more points. However, if a one is rolled on either die, the player forfeits all points accumulated during that turn and must pass the dice to the other player. If both dice show a one (snake eyes), the player forfeits all points accumulated up to that point in the game and must pass the dice to the other player.
Sample Interaction
First players name? Player1
Second players name? Player2
[ After a couple of turns ... ]
Player1's turn. Points: 0 Bank: 25 Roll: 3 + 4 = 7 Points: 7 Bank: 25 Roll again? y Roll: 6 + 3 = 9 Points: 16 Bank: 25 Roll again? y Roll: 4 + 4 = 8 Points: 24 Bank: 25 Roll again? n Passing. Points: 0 Bank: 49
Player2's turn. Points: 0 Bank: 38 Roll: 4 + 6 = 10 Points: 10 Bank: 38 Rolling again. Roll: 5 + 2 = 7 Points: 17 Bank: 38 Rolling again. Roll: 6 + 5 = 11 Points: 28 Bank: 38 NOT rolling again. Passing. Points: 0 Bank: 66
Player1's turn. Points: 0 Bank: 49 Roll: 5 + 2 = 7 Points: 7 Bank: 49 Roll again? y Roll: 3 + 1 = 4 Points: 0 Bank: 49 LOSE TURN POINTS
Player2's turn. Points: 0 Bank: 66 Roll: 1 + 1 = 2 Points: 0 Bank: 0 LOSE ALL POINTS
Assignment Requirements The design aspects are mostly left to you. However, make sure that your solution meets the criteria outlined in the grading rubric. You must create a driver class called Pig that contains your main() method. Your main() method should not contain any significant logic, and should only be used to start the game. More specifically, create an instance of one of the classes that you have designed and call one of its methods to start the game. Consider what other classes you want to use to accomplish this. In addition, you should create your own class to represent a player, so that you can have shared state and behavior between the players. Your design should facilitate code reuse. Consider what state(data) is managed inside the player objects rather than the game itself. You should also implement a separate class to represent a single die. The design of this class as well as how the dice are managed and rolled as a pair is up to you. (You will be using Random objects to model rolling the dice).
Additionally, your solution must also meet all of the following requirements:
Before play begins, your program must prompt for and read in the players names. Think carefully about where you place this code to minimize coupling.
The first player (Player1 in the sample output provided above) always takes the first turn. At the beginning of each turn, (before any rolls during that turn), the player's name must be printed.
At a minimum, your code must accept "y" and "n" as valid responses from the players when deciding to roll again (but you are not limited to just these).
For each roll, your program must print the total value of the roll, the points earned so far for the current turn, and the current value of the player's "bank", in that order. When a player's turn ends, their current "bank" value must be printed.
All input for the program will be provided on System.in, and all output should be printed on System.out (however, you are welcome to use Scanner and/or PrintWriter objects internally to decouple classes from these choices).
The game ends when one of the players accumulates 100 points in his/her bank. Your program should terminate at this point.
Is it possible to solve the problem static private Random random = new Random(System.currentTimeMillis()); I have no idea what it does. If you can provide some comments to the below solution that would work too. The solution is provided below:
Dice.java
import java.util.Random; public class Dice { static private Random random = new Random(System.currentTimeMillis()); public int roll() { return 1 + random.nextInt(6); //return a number in range 1 - 6 } }
Player.java
import java.util.Scanner; public class Player { private String name; int bank; public Player(String name) { this.name = name; } public void roll(Dice d1, Dice d2) { boolean endTurn = false; Scanner keybd = new Scanner(System.in); String msg = ""; int dice1, dice2; int points = 0; System.out.println(name + "'s turn: Points: " + points +", Bank: "+ bank); while(!endTurn) { dice1 = d1.roll(); dice2 = d2.roll(); int t = dice1 + dice2; if(dice1 == 1 && dice2 == 1) //snake eyes.. .loose all accumulated { points = 0; bank = 0; endTurn = true; msg = "LOSE ALL POINTS"; } else if(dice1 == 1 || dice2 == 1) //one of the dice is 1, lose turn points { points = 0; endTurn = true; msg = "LOSE TURN POINTS"; } points = points + t; System.out.println("Roll: " + dice1 + " + " + dice2 + " = " + t + ", Points: " + points + ", Bank: " + bank); if(!endTurn && bank < 100) { System.out.print("Roll again (y/n)? : "); String ans = keybd.next(); if(!ans.equalsIgnoreCase("y")) { endTurn = true; bank += points; points = 0; System.out.println("Passing. Points: " + points + ", Bank: " + bank); } } else System.out.println(msg); } } public String getName() { return name; } public int getBank() { return bank; } }
Pig.java
import java.util.Scanner; public class Pig { public static void main(String[] args) { String p1name, p2name; Scanner keybd = new Scanner(System.in); System.out.print("First player's name ? "); p1name = keybd.next(); System.out.print("Second player's name ? "); p2name = keybd.next(); Player p1 = new Player(p1name); Player p2 = new Player(p2name); int turn = 0; Dice d1 = new Dice(); Dice d2 = new Dice(); while(p1.getBank() >= 100 && p2.getBank() >= 100) { if(turn == 0) p1.roll(d1, d2); else p2.roll(d1, d2); turn = 1 - turn; } if(p1.getBank() >= 100) System.out.println(p1.getName() + " wins!"); else System.out.println(p2.getName() + " wins!"); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
