我正在创建一个名为Loaded Dice的Dice类的子类,它将使骰子滚动加载并按一定百分比,看起来有点像;
public LoadedDice (int anumSides,int loadVal, double loadPct)
但是我不知道从哪里开始,我已经有了一些想法来创建一个ArrayList
并让它拥有更多的一个数字,然后其他人从那里随机挑选但是那里&# 39;那必须是一个更干净的方式来做那么对吗?
public class Dice
{
// --------------------------------------------
// Instance Variables
// --------------------------------------------
private ArrayList<Die> dieArray;
// ------------------------------------------------
// Constructors
// -----------------------------------------------
/**
* Default Constructor creates 2 6 sided dice
*/
public Dice ()
{
dieArray = new ArrayList<Die>();
dieArray.add(new Die());
dieArray.add(new Die());
}
/**
* Takes an ArrayList of die objects to use
*/
public Dice (ArrayList<Die> someDice)
{
dieArray.addAll(someDice);
}
/**
* Takes in 1 int (# of dice), which creates that number of 6-sided
* dice
*/
public Dice (int numberOfDice)
{
dieArray = new ArrayList<Die>();
for (int i = 0; i < numberOfDice; i++)
{
dieArray.add(new Die());
}
}
/**
* Takes in 2 ints (# of dice, # of sides per die) which creates that
* number of dice with that number of sides per die
*/
public Dice (int numberOfDice, int numberOfSides)
{
dieArray = new ArrayList<Die>();
for (int i = 0; i < numberOfDice; i++)
{
dieArray.add(new Die(numberOfSides));
}
}
// -------------------------------------------------
// Class Methods
// -------------------------------------------------
/**
* ContainsDice: takes 0 parameters, and returns a string value
* representing all of the dice
*/
public String ContainsDice ()
{
String dieString = new String();
for (int i = 0; i < dieArray.size(); i++)
{
dieString += (dieArray.get(i) + " ");
}
return dieString;
}
/**
* Roll: takes 0 parameters, and returns an int value that is the sum
* of all the dice rolled
*/
public int Roll ()
{
int value = 0;
for (int i = 0; i < dieArray.size(); i++)
{
value = value + dieArray.get(i).roll();
}
return value;
}
/**
* Add: Takes a Die object as an input parameter and adds it to the
* current Dice
*/
public void Add (Die newDie)
{
dieArray.add(newDie);
}
}
此外,所有Dice类都来自我创建的模具类。如果需要,我也可以提供。