package rdices;///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.util.Scanner;
import rdices.Die;
import rdices.PairOfDice;
public class DiceRollerApp {
/*
* *Author: Piano Hagens
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//Display a welcome messages
System.out.println("Welcome to the Dice Roller");
System.out.println();
Scanner sc = new Scanner(System.in); //create a scanner to capture user input
String choice = "y";
System.out.println("Roll the dice? (y/n): "); //Ask the user want to play?
//create a loop so to designed play, if y to play, if n to exit the loop
while (choice.equalsIgnoreCase("y")){
choice = sc.nextLine();
System.out.println();
int numberOfRolledCount;//initial the time of been rolled.
numberOfRolledCount = 0;
//create another loop to design play 1 die or 2 dices
while (choice.equalsIgnoreCase("y")){//this nested loop is to call out Die Class, PairOfDice class, stay, or exit this nested loop
numberOfRolledCount++;//initial the time of rollCount
//Let user design to play 1 die or 2 dice
System.out.println("Want to roll one die or two dices? Enter a number 1 or 2: ");
int roll1DieOr2Dice = Integer.parseInt(sc.nextLine());
System.out.println();
System.out.println("Roll " + numberOfRolledCount + ":");
if (roll1DieOr2Dice == 1){ //if one die
Die d1 = new Die();//call out Die class Object for rolling 1 Die
d1.roll();
int rollValue1Die = d1.getValue();//create a local variable
if (rollValue1Die == 1)//show result if rolled one die
System.out.println("Rolled One Die, roll value is: " + rollValue1Die + " ->Circle!" + "\n");
else if (rollValue1Die == 3)
System.out.println("Rolled One Die, roll value is: " + rollValue1Die + " ->Triangle!" + "\n");
else if (rollValue1Die == 4)
System.out.println("Rolled One Die, roll value is: " + rollValue1Die + " ->Square!" + "\n");
else{//other value then 1,3,4.
System.out.println("Rolled One Die, roll value is: " + rollValue1Die + "\n");
}
}else if (roll1DieOr2Dice == 2){//call out PairOfDice class Object for rolling 2 Dices
PairOfDice d2 = new PairOfDice(); //call out PairOfDice class object.
d2.getValue1(); d2.getValue2(); d2.getSum();
//create variable to hold the Objects, total 3 Objects needs from PairOfDice Class
int rollVal1 = d2.getValue1(); int rollVal2 = d2.getValue2(); int sumValue2 = d2.getSum();
//show result if rolled 2 dices
if (rollVal1 == 2 && rollVal2 == 5)//2-5,Craps
System.out.println(rollVal1 + "\n" + rollVal2 + "\n" + "Craps!\n" + "Rolled Two Dices, Total Value: " + sumValue2);
else if (rollVal1 == 1 && rollVal2 == 1)//1-1, Snake eyes!
System.out.println(rollVal1 + "\n" + rollVal2 + "\n" + "Snake eyes!\n" + "Rolled Two Dices, Total Value: " + sumValue2);
else if (rollVal1 == 6 && rollVal2 == 6)//6-6 Box cars!
System.out.println(rollVal1 + "\n" + rollVal2 + "\n" + "Box cars!\n" + "Rolled Two Dices, Total Value: " + sumValue2);
else{//other value but 2-5, 1-1, and 6-6
System.out.println(rollVal1 + "\n" + rollVal2 + "\n" + "Rolled Two Dices, Total Value: " + sumValue2);
}}
System.out.println("\nRoll again? (y/n): "); //see if the user want to play again
choice = sc.nextLine();
System.out.println();
}}
//close scanner and Bye
sc.close();
System.out.println("Bye");
}}
package rdices;
public class Die {
/*Create a class named Die to store the data about each die. T
* his class should contain these constructors and methods:
public Die() // default to a six-sided die
public Die(int sides) // allow a variable number of sides
public void roll()
public int getValue()
*/
//create instance variable
private int dieFaces;
private int rollOneDieValue;
public Die(){
//default constructor here to call the roll() method to roll
// default to a six-sided die sides = 6;
dieFaces = 6;
} public Die(int side){//ordinary constructors
// allow a variable number of sides if (side > 0 && side <= 6)
dieFaces = side;
} public void roll(){ //create roll method
rollOneDieValue = (int) (Math.random() * dieFaces) + 1; } public int getValue(){ return rollOneDieValue; }
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package rdices;
public class PairOfDice {
/*Create a class named PairOfDice to store two dice. This class should contain two
* instance variables of the Die type, an instance variable that holds the sum of
* the two dice, and these constructors and methods:
public PairOfDice() // default to six-sided dice
public PairOfDice(int sides) // allow a variable number of sides
public void roll()
public int getValue1() // get value of die1
public int getValue2() // get value of die2
public int getSum() // get the sum of both dice
*/
//create instance variable
private int dieFaces;
private int getValue1; // Instance variable for number showing on the first die.
private int getValue2; // Instance variable for number showing on the second die.
private int sum; // Instance variable to hold sum of two dice
public PairOfDice(){//default constructor here to call the roll() method to roll
// default to a six-sided die sides = 6;
dieFaces = 6;
}
public PairOfDice(int side){
// allow a variable number of sides
if (side > 0 && side <= 6)
dieFaces = side;
}
public void roll(){ //create roll method
getValue1 = (int)(Math.random()*dieFaces) + 1;
getValue2 = (int)(Math.random()*dieFaces) + 1;
}
// get value of die1
public int getValue1(){
this.getValue1 = (int)(Math.random()*dieFaces) + 1;
return getValue1;
}
// get value of die2
public int getValue2(){
this.getValue2 = (int)(Math.random()*dieFaces) + 1;
return getValue2;
}
// get the sum of both dice
public int getSum(){
sum = getValue1 + getValue2;
return sum;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Works Great!
ReplyDelete