Skip to content

Instantly share code, notes, and snippets.

@valery1707
Created January 13, 2017 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valery1707/cd30b3b01423581d7d2835b903159cc5 to your computer and use it in GitHub Desktop.
Save valery1707/cd30b3b01423581d7d2835b903159cc5 to your computer and use it in GitHub Desktop.
Temir.Dice created by valery1707 - https://repl.it/FIal/0
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.stream.IntStream;
class Main {
public static void main(String[] args) {
Dice dice = new RandomDice(6);
System.out.println("dice6.roll() = " + dice.roll());
System.out.println("dice6.roll() = " + dice.roll());
System.out.println("dice6.roll(2) = " + dice.roll(2));
System.out.println("dice6.roll(6) = " + dice.roll(6));
Dice history = new DiceWithHistory(new RandomDice(6));
history.roll(2);
history.roll(2);
history.roll(5);
System.out.println("history = " + history);
history = new DiceWithHistory(new FrozenDice(6, 5));
history.roll(2);
history.roll(2);
history.roll(5);
System.out.println("frozen = " + history);
}
public interface Dice {
Roll roll();
Roll roll(int count);
}
public static class RandomDice implements Dice {
private final int max;
private final Random generator = new Random();
public RandomDice(int max) {
this.max = max;
}
@Override
public Roll roll() {
return roll(1);
}
@Override
public Roll roll(int count) {
//А вот так можно на Java8
// return new Roll(generator
// .ints(count, 0, max)
// .map(v -> v + 1)
// .toArray()
// );
int[] values = new int[count];
for (int i = 0; i < count; i++) {
values[i] = rollImpl();
}
return new Roll(values);
}
protected int rollImpl() {
return generator.nextInt(max) + 1;
}
}
public static class Roll {
private final int count;
private final int[] values;
private final int min;
private final int max;
private final double ave;
private final int sum;
public Roll(int[] values) {
count = values.length;
this.values = values;
//Тут куски Java8 - влом писать самому
min = IntStream.of(values).min().getAsInt();
max = IntStream.of(values).max().getAsInt();
ave = IntStream.of(values).average().getAsDouble();
sum = IntStream.of(values).sum();
}
@Override
public String toString() {
return "Roll{" +
"count=" + count +
", values=" + Arrays.toString(values) +
", min=" + min +
", max=" + max +
", ave=" + ave +
", sum=" + sum +
'}';
}
}
public static class DiceWithHistory implements Dice {
private final RandomDice dice;
private final List<Roll> history = new ArrayList<>();
public DiceWithHistory(RandomDice dice) {
this.dice = dice;
}
@Override
public Roll roll() {
Roll roll = dice.roll();
history.add(roll);
return roll;
}
@Override
public Roll roll(int count) {
Roll roll = dice.roll(count);
history.add(roll);
return roll;
}
@Override
public String toString() {
return "DiceWithHistory{" +
"history=" + history +
'}';
}
}
public static class FrozenDice extends RandomDice {
private final int value;
public FrozenDice(int max, int value) {
super(max);
this.value = value;
}
@Override
protected int rollImpl() {
return value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment