In java create a dice game called sequences also known as straight shooter. Each player in turn rolls SIX dice and scores points for any sequence of CONSECUTIVE numbers thrown beginning with 1. In the event of two or more of the same number being rolled only one counts. However, a throw that contains three 1's cancels out player's score and they mst start from 0. A total of scores is kept and the first player to reach 100 points, wins the game. 1: 5 points 1,2 : 10 points 1,2,3 :15 points 1,2,3,4 :20 points 1,2,3,4,5 : 25 points 1,2,3,4,5,6 : 35 points 1,1,1 : cancel players total score nb: this is a non interactive game when played on computer, You will need two arrays of Die references, one for each player ( human and computer). Create a die class, game class and driver class. After each roll, print the faces of the dice and display the points for that roll along with the total accumulated points. Once a game is won by a player, print the winner's name and ask the user is he/she wants to play again. keep running total of games won and lost by each of the two players. (Here is something similar. it needs to be fixed) Die.java import java.util.Random; //Die class public class Die { private int faceUp; private final int NumberOfSides=6; //constructor public Die() { faceUp = 1; } //method to roll() public void roll() { faceUp = (int)(Math.random() * NumberOfSides) +1; } //Method to compare public void CompareTo(int value) { faceUp=value; } //Method to getFace() public int getFace() { return faceUp; } //method equals public boolean equals(int die) { die=faceUp; return true; } //Method to string public String toString() { String result = "You rolled a " + faceUp; return result; } } Sequence.java //Sequence class public class Sequence { //Data members private Die dies1, dies2; private int FirstVal, SecondVal, Tot; //constructor public Sequence () { dies1 = new Die(); dies2 = new Die(); FirstVal = 1; SecondVal = 1; Tot = FirstVal + SecondVal; } //Method to roll() public void roll () { dies1.roll(); dies2.roll(); } //Method to SumOfDice() public int SumOfDice () { Tot = firstDie() + SecondDie(); return Tot; } //Method to firstDie() public int firstDie () { return FirstVal; } //Method to SecondDie() public int SecondDie () { return SecondVal; } //Method to set FirstDieSet() public void FirstDieSet (int value) { dies1.CompareTo(value); } //Method to set SecondDieSet() public void SecondDieSet(int value) { dies2.CompareTo(value); } //Method to string() public String toString() { String result = "You rolled a " + Tot; return result; } } DieTester.java: import java.util.*; //tester class public class DieTester { //Main program public static void main(String[] args) { //Instance for class Sequence seq=new Sequence(); //Assign values int totalTurns = 0; int player2Tot = 0; int player1Tot = 0; int CompleteTurn = 1; int Wins = 100; //Scanner object Scanner inp = new Scanner( System.in); String Result; //do loop do{ if (player1Tot <= Wins && player2Tot <= Wins) { System.out.println("Your turn."); //do loop do { System.out.println("Enter 'y' or 'n' for turn."); Result = inp.next(); //Equals ignore case check if (Result.equalsIgnoreCase("y") && player1Tot <= Wins && player2Tot <= Wins) { //Calls the methods seq.roll(); seq.firstDie(); seq.SecondDie(); seq.toString(); System.out.println(seq); //Checks the sequence data if (seq.firstDie() == CompleteTurn || seq.SecondDie() == CompleteTurn){ System.out.println("You are Rolled a 1. Your turn is over."); System.out.println("'Enter 'done' for Player 2."); Result = inp.next(); } else { //Computes the results totalTurns = totalTurns + seq.SumOfDice(); player1Tot = player1Tot + seq.SumOfDice(); System.out.println("Turn Total of player1: " + totalTurns); System.out.println("Grand Total of player2: " + player1Tot); } } } //Checks the ignore case while (Result.equalsIgnoreCase("y") && player1Tot <= Wins && player2Tot <= Wins); totalTurns = 0; System.out.println(); System.out.println("Player1 Grand Total: " + player1Tot); System.out.println("Player2 GrandTotal: " + player2Tot); System.out.println(); //Assigns value int CturnEnd = 20; CompleteTurn = 1; int CompareResults = 1; //do while do { if (totalTurns <= CturnEnd && CompareResults == 1 && player1Tot <= Wins && player2Tot <= Wins) { //calls the methods seq.roll(); seq.firstDie(); seq.SecondDie(); seq.toString(); System.out.println(seq); //Compute the results if (seq.firstDie() == CompleteTurn || seq.SecondDie() == CompleteTurn) { System.out.println("Player2 rolled a 1. Their turn is over."); CompareResults = 0; } else { //Calculation totalTurns = totalTurns + seq.SumOfDice(); player2Tot = player2Tot + seq.SumOfDice(); System.out.println("Turn Total of Player2 is: " + totalTurns); System.out.println("GrandTotal of Player2 is: " + player2Tot); } } } while (totalTurns <= CturnEnd && CompareResults == 1 && player1Tot <= Wins && player2Tot <= Wins); totalTurns = 0; //Checks the player total if (player1Tot <= Wins || player2Tot <= Wins) { System.out.println(); System.out.println("Player2 GrandTotal is: " + player2Tot); System.out.println("Player1 GrandTotal is: " + player1Tot); System.out.println(); } else { System.out.println(); System.out.println(); } } } //winning result while(player1Tot <= Wins && player2Tot <= Wins); //decision result if (player1Tot >= Wins) System.out.println("You win!"); else System.out.println("You lose ): "); } }
public class PairOfDice { private int die1; // Number showing on the first die. private int die2; // Number showing on the second die. public PairOfDice() { // Constructor. Rolls the dice, so that they initially // show some random values. roll(); // Call the roll() method to roll the dice. } public void roll() { // Roll the dice by setting each of the dice to be // a random number between 1 and 6. die1 = (int)(Math.random()*6) + 1; die2 = (int)(Math.random()*6) + 1; } public int getDie1() { // Return the number showing on the first die. return die1; } public int getDie2() { // Return the number showing on the second die. return die2; } public int getTotal() { // Return the total showing on the two dice. return die1 + die2; } } // end class PairOfDice
Get Answers For Free
Most questions answered within 1 hours.