All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.oliviercailloux.g421.RandomDiceRoller Maven / Gradle / Ivy

The newest version!
package io.github.oliviercailloux.g421;

import static com.google.common.base.Preconditions.checkState;

import com.google.common.collect.ImmutableList;
import java.util.Random;

public class RandomDiceRoller implements DiceRoller {

  private final Random random;
  private ImmutableList rolled;

  public RandomDiceRoller() {
    random = new Random();
    rolled = null;
  }

  @Override
  public void roll() {
    rolled = random.ints(1, 7).limit(3).boxed().collect(ImmutableList.toImmutableList());
  }

  @Override
  public int first() throws IllegalStateException {
    return ith(0);
  }

  @Override
  public int second() throws IllegalStateException {
    return ith(1);
  }

  @Override
  public int third() throws IllegalStateException {
    return ith(2);
  }

  private int ith(int i) {
    checkState(rolled != null);
    return rolled.get(i);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy