Question: In Java Game Rules This dice game is played with three standard 6 or 8 sided dice. Bets are placed on possible combinations that can

In Java

Game Rules

This dice game is played with three standard 6 or 8 sided dice. Bets are placed on possible combinations that can appear on the dice. A game begins with 10 credits. Each roll costs one credit. A payout increases the number of credits by one if that outcomes appears.

The various outcomes are described below:

Large total value of the three dice is over ten

Three of a kind the face value of all dice is identical

Two of a kind - the face value of two dice is identical

Step 1: Create a New IntelliJ Project

Step 2: Class Definition: SixDie

Rather than writing your own Die class, we are providing a completed class for you. Download SixDie.java to the folder of your project. It should compile with no errors. Do not make any changes to this code. You only need to use the following methods, but you are encouraged to read through the source code to see how it works.

Step 3: Class Definition: DiceCup

Create a new class called DiceCup. Implement each of the following methods. All must be complete before the game can be played.

Important Note:

We are providing a JUnit class to test your project. Exact spelling is required in the DiceCup class for the class name and all the method headers. Do not change the method headers in any way.

Instance Variables

A class should contain several instance variables. Provide appropriate names and data types for each of the private instance variables:

  • an ArrayList of three MyDie objects.
  • credit balance (integer)

Constructors

  • public DiceCup ( )
    • instantiate and populate the ArrayList
      • instantiate the arraylist of three SixDie or EightDie objects
      • use a loop to assign them to the elements of the arraylist
    • initialize the credit to 10 (a game begins with 10 credits)

Accessor Methods

  • public int getCredits ( ) - return the current credit balance.
  • public ArrayList getDice()- return the arraylist of the MyDie objects.
  • public void roll()- call the die object method to generate a new random number for each die in the arraylist
  • public int getTotal()- return the total addition of the values in all of the dice
  • public void checkTriplets()- Check if all three dice have an identical value and give one credit if true
  • public void checkDoubles()- Check if you have two of a kind in the arraylist and give one credit if true
  • public void checkLarge()- Check if the sum of all the dice has a value of 10 or more and give one credit if true
  • public void updateCredits() - Calls on all the checks and removes one credit per play. Only allow to execute if you have enough credits.
  • public boolean enoughCredits ( ) return true if the player has enough credits to play this round, return false otherwise.
  • public void testRoll(int [] values) this method will roll the dice until the desired values entered as the array of 3 integer values have been rolled. This makes the testing a lot easier because you can control what numbers are rolled.
    • If there are enough credits to play, this method has to do the following:
      • Repeatedly roll each die in the array of dice until the desired value is obtained.
      • For example, if the method is called like this:

game.testRoll(new int [] {6,3,3});

  • The ArrayList should have those value in each die in that order.
  • Make sure that you decrease the value of the credits by one in this method as well.

GameManager Class

The GameManager will have to control and access the container class, DiceCup.java.

Instance Variables

The Game Manager should only have one instance variable. It will be an instace of the container class

  • a DiceCup object

Constructors

  • public GameManager ( int num )
    • instantiate a DiceCup object
      • The number in the constructor determines if you are using a SixDie or an EightDie

Accessor Methods

  • public String toString()- return the representation of the Game
  • public DiceCup getDice()- return the DiceCup Object

Main Method

  • Create an instance of GameManager
  • Take input from user to see if you want a six or eight die
  • Take input from user in a loop to see if you want to roll or stop
  • Provide a print statement with Credits Before bet, Total Sum of Dice, and Credits After Bet.

SixDie class

import java.util.*; public class SixDie extends MyDie implements Comparable { public SixDie() { // set default values myValue = (int) (Math.random()*6)+1; rand = new Random(); } public void roll () { myValue = rand.nextInt(6) + 1; } public int getValue() { return myValue; } // set the random number generator seed for testing public void setSeed(int seed) { rand.setSeed(seed); } // allows dice to be compared if necessary public int compareTo(Object o) { SixDie d = (SixDie) o; return getValue() - d.getValue(); } }

MyDie class

import java.util.Random; public abstract class MyDie { protected int myValue; protected Random rand; public MyDie(){ myValue = -1; } abstract public void roll (); abstract public int getValue(); abstract public void setSeed(int seed); abstract public int compareTo(Object o); }

MyDiceGameTest.java

import static org.junit.Assert.*; import org.junit.*; /******************************************* * The test class for Chuck * * @author Resendiz * @version February 2021 ******************************************/ public class MyDiceGameTest { /****************************************************** * Test initial values of the constructor *****************************************************/ @Test public void testConstructor() { GameManager game = new GameManager(6); int credits = game.getDice().getCredits(); // confirm there is only one copy of Title 1 Assert.assertEquals("Game should start with 10 credits", credits, 10); } /****************************************************** * Test Large *****************************************************/ @Test public void testLarge() { // confirm large is rolled GameManager game = new GameManager(6); int credits = game.getDice().getCredits(); game.getDice().testRoll(new int[]{6, 4, 5}); game.getDice().checkLarge(); Assert.assertEquals("Dice => (6,4,5): credits should stay the same", game.getDice().getCredits(), credits); // confirm large is not rolled GameManager game2 = new GameManager(6); credits = game2.getDice().getCredits(); game2.getDice().testRoll(new int[]{1, 2, 3}); game2.getDice().checkLarge(); Assert.assertEquals("Dice => (1,2,3): credits should decrease by one", game2.getDice().getCredits(), credits - 1); } /****************************************************** * Test Doubles *****************************************************/ @Test public void testDoubles() { // confirm doubles is rolled. 1st Combo GameManager game = new GameManager(6); int credits = game.getDice().getCredits(); game.getDice().testRoll(new int[]{1, 1, 2}); game.getDice().checkDoubles(); Assert.assertEquals("Dice => (1,1,2): credits should stay the same", game.getDice().getCredits(), credits); // confirm doubles is rolled. 2nd Combo GameManager game2 = new GameManager(6); int credits2 = game2.getDice().getCredits(); game2.getDice().testRoll(new int[]{2, 1, 1}); game2.getDice().checkDoubles(); Assert.assertEquals("Dice => (1,1,1): credits should stay the same", game2.getDice().getCredits(), credits2); } /****************************************************** * Test Combination of Points Awarded *****************************************************/ @Test public void testCombinations(){ // confirm large and double are called GameManager game = new GameManager(6); int credits = game.getDice().getCredits(); game.getDice().testRoll(new int[]{6, 6, 3}); game.getDice().checkDoubles(); game.getDice().checkLarge(); Assert.assertEquals("Dice => (6,6,3): credits should increase by one", game.getDice().getCredits(), credits + 1); // confirm large, triplets and doubles are given points GameManager game3 = new GameManager(6); credits = game3.getDice().getCredits(); game3.getDice().testRoll(new int[]{4, 4, 4}); game3.getDice().checkLarge(); game3.getDice().checkTriplets(); game3.getDice().checkDoubles(); Assert.assertEquals("Dice => (4,4,4): credits should increased by two", game3.getDice().getCredits(), credits + 2); // confirm triplets and doubles are called but not large GameManager game4 = new GameManager(6); credits = game4.getDice().getCredits(); game4.getDice().testRoll(new int[]{3, 3, 3}); game4.getDice().checkLarge(); game4.getDice().checkTriplets(); game4.getDice().checkDoubles(); Assert.assertEquals("Dice => (3,3,3): credits should increase by one", game4.getDice().getCredits(), credits+1); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!