Dice Set Test

class Dice {

    private final int MAX = 6; // Maximum face value
    private int faceValue; // Current value on the dice
    private static final String[] numberNames = {"One", "Two", "Three", "Four", "Five", "Six"}; // Text representation

    /**
     * Constructor: Initializes the dice with a default face value of 1.
     */
    public Dice() {
        faceValue = 1;
    }

    /**
     * Rolls the dice and updates the face value.
     *
     * @return The new face value after rolling.
     */
    public int roll() {
        faceValue = (int) (Math.random() * MAX) + 1;
        return faceValue;
    }

    /**
     * Returns the face value of the dice.
     *
     * @return The current face value.
     */
    public int getFaceValue() {
        return faceValue;
    }

    /**
     * Returns a formatted string representation of the dice (e.g., "1: One").
     * Ensures uniform width for proper alignment.
     *
     * @return The formatted string representation.
     */
    @Override
    public String toString() {
        return String.format("%d: %-5s", faceValue, numberNames[faceValue - 1]);
    }
}

/**
 * The DiceSet class represents a set of dice.
 */
class DiceSet {

    private final Dice[] diceSet; // Array to store multiple dice

    /**
     * Constructor: Initializes the dice set with the specified number of dice.
     *
     * @param n The number of dice in the set.
     */
    public DiceSet(int n) {
        diceSet = new Dice[n]; // Dynamically allocate the array
        for (int i = 0; i < n; i++) {
            diceSet[i] = new Dice(); // Initialize each dice
        }
    }

    /**
     * Rolls all the dice in the set.
     */
    public void rollAll() {
        for (Dice singleDice : diceSet) {
            singleDice.roll(); // Roll each dice
        }
    }

    /**
     * Returns an array of face values for all dice.
     *
     * @return An array containing the face values.
     */
    public int[] getValues() {
        int[] values = new int[diceSet.length];
        for (int i = 0; i < diceSet.length; i++) {
            values[i] = diceSet[i].getFaceValue();
        }
        return values;
    }

    /**
     * Returns a properly aligned string representation of the dice set. Ensures
     * uniform spacing for all dice outputs.
     *
     * @return A formatted string showing all dice.
     */
    @Override
    public String toString() {
        StringBuilder result = new StringBuilder("[");
        for (int i = 0; i < diceSet.length; i++) {
            result.append(String.format("%s", diceSet[i])); // Uses formatted output from Dice.toString()
            if (i < diceSet.length - 1) {
                result.append("  "); // Uniform spacing between dice
            }
        }
        result.append("]");
        return result.toString();
    }
}

/**
 * The DiceSetTest class serves as the main driver to test the DiceSet and Dice
 * classes.
 */
public class DiceSetTest {

    public static void main(String[] args) {
        int[] sizes = {1, 2, 5, 6}; // Different sizes of dice sets to test

        for (int size : sizes) {
            System.out.println("-----------------------Rolling Results-----------------------");
            System.out.println("Testing DiceSet of size " + size + ":");
            DiceSet diceSet = new DiceSet(size); // Create a dice set

            for (int i = 1; i <= 10; i++) {
                diceSet.rollAll(); // Roll all dice
                System.out.printf("Roll %-2d | %s%n", i, diceSet); // Print the result in aligned format
            }
            System.out.println(); // Add a blank line between different sets
        }
    }
}
Scroll to Top