Question

I'm trying to create an array that mimics a 12-sided die. Using the for-each statement, it will work when i do it in a single class. However I have to call on it from another class. Is there a way i can create the object(4 12-sided dice) in a public class, using the for-each statement, then call on and roll the dice in main?

package gofirst;




public class DodecaDice {
   int[] a = new int[13];


   enum dieA{1, 8, 11, 14, 19, 22, 27, 30, 35, 38, 41, 48}
   for (DieA nextDieA: DieA.values()){
        a[0++]=nextDieA;
}




}
Was it helpful?

Solution

I'm not entirely sure what it is you are trying to achieve, but if I was doing something like this I might start with a basic concept of a Die and it's core functionality...

public interface Die {
    public int getMinium();
    public int getMaximum();
    public int roll();
}

Then I would create an abstract version which filled out the most common, basic functionality, so I wouldn't need to repeat myself all the time...

public abstract class AbstractDie implements Die {

    private final int minimum;
    private final int maximum;

    public AbstractDie(int minimum, int maximum) {
        this.minimum= minimum;
        this.maximum = maximum;
    }

    @Override
    public int getMinimum() {
        return minimum;
    }

    @Override
    public int getMaximum() {
        return maximum;
    }

    @Override
    public int roll() {
        return getMinimum() + (int)(Math.random() * getMaximum());
    }

}

And then finally some implementations, for example...

public class TwelveSidedDie extends AbstractDie {

    public TwelveSidedDie() {
        super(1, 12);
    }

}

This would then allow me to use the class when ever I needed to, for example...

Die[] dice = new Die[] {new TwelveSidedDie(), new TwelveSidedDie(), new TwelveSidedDie(), new TwelveSidedDie()};
int sum = 0;
for (Die die : dice) {
    int roll = die.roll();
    System.out.println(roll);
    sum += roll;
}

System.out.println("Total: " + sum);

It should be noted that the above could also be achieved by using something like...

Die die = new TwelveSidedDie();
int rolls = 4;
for (int roll = 0; roll < rolls; roll++) {
    int result = die.roll();
    System.out.println(result);
    sum += result;
}

System.out.println("Sum: " + sum);

Which leads to the ability to write a method that can simulate a number of Die rolls...

public static int[] diceRolls(Die die, int numberOfRolls) {

    int[] results = new int[numberOfRolls];
    for (int roll = 0; roll < numberOfRolls; roll++) {
        results[roll] = die.roll();
    }

    return results;

}
public static int sumDiceRolls(Die die, int numberOfRolls) {

    int[] results = diceRolls(die, numberOfRolls);
    int sum = 0;
    for (int result : results) {
        sum += result;
    }

    return sum;

}

Which would allow you to pass in any implementation of Die and have them rolled automatically as you needed...

For example...

OTHER TIPS

I'm not sure what your wanting but maybe its something like this. Is it required for you to use 'for each'?

While I only show one dice you can make four of them in the main method... or maybe roll it four times.

import java.util.Random;

public class Dice {

   private int[] a = {1, 8, 11, 14, 19, 22, 27, 30, 35, 38, 41, 48};

   public Dice() {
       //do nothing for now
   }

   public int roll() {
       Random rand = new Random();
       int  key = rand.nextInt(12) + 1;
       return a[key];
   }


   public static void main(String args[]) {
       Dice d = new Dice();
       System.out.println(d.roll());



   }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top