Search code examples
javaarraysarraylistdice

Roll a collection of die in Array and ArrayList java


Having a bit of a problem here...

• I created an array and a separate ArrayList object that can store 10 Die objects.

• I created Die objects and add them to the array, and then created Die objects and add them to the the ArrayList object.

• I display all the elements of both the array and ArrayList.

Now I am trying to do 2 things:

• roll each of the Die objects in both the array and ArrayList

• calculate the total value of each collection by adding up the points in each die, and determine which is greater and display those results

Any thought on how I could implement those 2 things listed above?

Dice Object

public class Dice {

String name;
int[] values;

        // constructor 
        public Dice(int faces,String name){
            int[] values =new int[faces];
            for (int i=0;i<faces;i++){
                values[i]=i+1;
            }
            this.name=name;
            this.values=values;
        }
}

**

import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {

        final int SIZE = 10;
        Dice[] diceList = new Dice[SIZE];
        ArrayList<Dice> diceList2 = new ArrayList<Dice>();

        //create 10 die

        for (int i=0;i<SIZE;i++){
            String name = "dice"+ Integer.toString(i+1);
            Dice dice = new Dice(6,name);
            diceList2.add(dice);
            System.out.println(dice.name);


        }
        System.out.println();

        for (int i = 0; i<SIZE; i++) {
            String name = "dice"+ Integer.toString(i+1);
            Dice dice = new Dice(6,name);
            diceList[i] = dice;
            System.out.println(dice.name);


        }
    }
}

Solution

  • To roll the die I would assume you would use something like Random().

    public class Dice {
        private String name;
        private int[] values;
        private int rolledValue;
        private java.util.Random random = new java.util.Random();
    
        // constructor 
        public Dice(int faces,String name){
            int[] values =new int[faces];
            for (int i=0;i<faces;i++){
                values[i]=i+1;
            }
            this.name=name;
            this.values=values;
        }
    
        public void rollDie() {
            rolledValue = values[random.nextInt(values.length)];
        }
    
        public int getRolledValue() {
            return rolledValue;
        }
    }
    

    then looping over the array or ArrayList is trivial: (mind you I'd use only 1 die myself and simply call the same die to roll several times.

    int total = 0;
    for (int i = 0; i < diceList.length; ++i) {
         diceList[i].rollDie();
         total+= diceList[i].getRolledValue();
    }
    
    int total2 = 0;
    for (Dice die : diceList2) {
        die.rollDie();
        total2 += die.getRolledValue();
    }
    

    However as I said I'd use the dice class differently:

    public class Dice {
        private final int[] faces;
        private java.util.Random random = new java.util.Random();
        public Dice(final int[] faces) {
            this.faces = faces;
        }
        public int getNextRoll() {
            return faces[random.nextInt(faces.length)];
        }
    }
    

    And roll them like:

    Dice d6 = new Dice(new int[] { 1, 2, 3, 4, 5, 6 };
    Dice d4 = new Dice(new int[] { 1, 2, 3, 4},
    int total1 = 0;
    int total2 = 0;
    for (int i = 0; i < 10; ++i) {
        total1 += d6.getNextRoll();
        total2 += d4.getNextRoll();
    }
    

    But I admit this doesn't help you play with arrays very much :)