Java dice game
Posted by Kunama at 03:35 PM on March 24, 2005.
Dice Class:
import javax.swing.JOptionPane;
public class Dice
{
// instance variables
private int sides;
private int diceNumber;
/**
* Default constructor for objects of class Dice
*/
public Dice()
{
// initialise instance variables
sides = 6;
diceNumber = 1;
}
/**
* Alternate constructor for objects of class Dice
*/
public Dice(int sides_new, int diceNumber_new)
{
sides = sides_new;
diceNumber = diceNumber_new;
}
/**
* Class methods
*/
public void setSides(int sidesIn)
{
sides = sidesIn;
}
public int getSides()
{
System.out.println("Sides on the dice: "+sides);
return sides;
}
public void setDiceNumber(int diceIn)
{
diceNumber = diceIn;
}
public int getDiceNumber()
{
System.out.println("Number of dice: "+diceNumber);
return diceNumber;
}
//method to actually roll the dice
public long[] roll(Dice d)
{
int maxUnique = d.getSides();
int maxRolls = d.getDiceNumber();
//System.out.println("maxUnique integer is: "+maxUnique);
long[] arrayRolls = new long[maxRolls];
//System.out.println("number of elements in arrayRolls is: " + arrayRolls.length);
int count = 0;
while (count < maxRolls)
{
//long randomNumber = (long)(Math.floor(Math.random() * maxUnique) + 1);
long randomNumber = (long)(Math.random()*maxUnique +1);
System.out.println(randomNumber);
arrayRolls[count] = randomNumber;
count = count + 1;
}
//JOptionPane.showMessageDialog(null, "Second element in arrayRolls is: " + arrayRolls[1]);
return arrayRolls;
}
//allow the user to change the number of dice to be rolled
public void specifyDiceNumber(Dice diceTest)
{
boolean validInput = false; //flag for input validation
int yn; //variable to hold result from showConfirmDialog
int diceIn = diceTest.getDiceNumber(); //number of dice to roll
String numberIn; //variable to hold user input
yn = JOptionPane.showConfirmDialog(null, "Would you like"
+ " to specify the number of dice to roll?", "Choose how many dice",
JOptionPane.YES_NO_OPTION);
if (yn == JOptionPane.YES_OPTION)
{
validInput = false;
while(!validInput) //while validInput is false, ask question. Pretest loop.
{
numberIn = (JOptionPane.showInputDialog(null, "How many dice do"
+ " you wish to roll?"));
try //try to parse the input. If no errors thrown, set validInput to true.
{
diceIn = Integer.parseInt(numberIn);
validInput = true;
}
catch (Exception e) //catch the error thrown if try didn't work.
{ //then set validInput to false, and re-ask.
validInput = false;
}
}
diceTest.setDiceNumber(diceIn);
}
else
{
JOptionPane.showMessageDialog(null, "The number of dice that "+
"will be rolled is " + diceTest.getDiceNumber() + "."); //roll 1 dice
}
return;
}
//allow user to specify number of sides on each dice
public void specifyDiceSides(Dice diceTest)
{
boolean validInput = false; //flag for input validation
int sidesIn = diceTest.getSides(); //number of sides per dice
int yn; //variable to hold result from showConfirmDialog
String numberIn; //variable to hold user input
yn = JOptionPane.showConfirmDialog(null, "Would you like to specify"
+ " the number of sides on each dice?", "Choose how many sides",
JOptionPane.YES_NO_OPTION);
if (yn == JOptionPane.YES_OPTION)
{
validInput = false;
while(!validInput)
{
numberIn = (JOptionPane.showInputDialog(null, "How many sides do"
+ " you want on each dice?"));
try
{
sidesIn = Integer.parseInt(numberIn);
validInput = true;
}
catch (Exception e)
{
validInput = false;
}
}
diceTest.setSides(sidesIn);
}
else
{
JOptionPane.showMessageDialog(null, "The number of sides on each" +
" dice will be " + diceTest.getSides() + "."); //use 6 sides
}
return;
}
}
/* //Generate the numbers rolled
public long generateRolls(Dice diceTest)
{
int maxUnique = diceTest.getSides();
long randomNumber = (long)(Math.floor(Math.random() * maxUnique)) + 1;
return randomNumber;
}
//Collect the rolled results into an array
public long[] collectRolls(Dice diceTest)
{
int maxIndex = diceTest.getSides();
long[] arrayRolls = new long[maxIndex+1];
int count = 0;
while (count <= maxIndex)
{
arrayRolls[count] = generateRolls(diceTest);
count++;
}
//Code from here to return is temporary. To check if the array has been filled
JOptionPane.showMessageDialog(null, arrayRolls.length + arrayRolls[0] + arrayRolls[2]);
return arrayRolls;
}*/
[break=DiceTester Class]
DiceTester Class:
import javax.swing.JOptionPane;
public class DiceTester
{
public static void main (String[] args)
{
Dice diceTest = new Dice(6, 1); //object called diceTest, of class Dice
int count = 0;
int yesno; //integer to store result of showConfirmDialog
JOptionPane.showMessageDialog(null, "Welcome to dice 'game'.");
/*post test loop to allow user to change number and sides of dice many times.
Currently re-asks if want to change. Is this good?*/
do
{
diceTest.specifyDiceNumber(diceTest);
diceTest.specifyDiceSides(diceTest);
yesno = JOptionPane.showConfirmDialog(null, "Number of dice to be rolled: " +
diceTest.getDiceNumber() + ".\nNumber of sides on each dice: " +
diceTest.getSides() + ".", "Summary", JOptionPane.YES_NO_OPTION);
} while (yesno == JOptionPane.NO_OPTION);
long arrayRolls[] = diceTest.roll(diceTest); //roll the dice
//Show what was rolled. Needs a better method other than MessageDialog.
while (count
JOptionPane.showMessageDialog(null, "Dice number " + (count+1) + ": " + arrayRolls[count] + "\n");
count++;
}
diceTest = null; //Clean-up diceTest object
System.exit(0); //Terminate - end of program
}
}
=>
Comment?