Skip to content

Commit

Permalink
improve fixed difficulty distribution scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
RasmusAntons committed Jun 7, 2024
1 parent dab87d6 commit 8ce1f4d
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions common/src/main/java/io/github/gaming32/bingo/game/BingoBoard.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,22 @@ private static boolean isOnSameLine(int size, int a, int b) {

private static int[] generateDifficulty(int size, BingoDifficulty difficulty, RandomSource rand) {
final int[] layout = new int[size * size];

if (difficulty.distribution() != null) {
List<Integer> scaledDistribution = difficulty.distribution().stream().map(f -> Math.round(f * size * size)).toList();
int p = 0;
for (int difficultyLevel = 0; difficultyLevel < scaledDistribution.size(); ++difficultyLevel) {
for (int i = 0; i < scaledDistribution.get(difficultyLevel) && p < layout.length; ++i)
layout[p++] = difficultyLevel;
Arrays.fill(layout, difficulty.number());
float[] cumDistribution = new float[difficulty.distribution().size()];
for (int d = 0; d < cumDistribution.length; d++) {
cumDistribution[d] = difficulty.distribution().get(d) * layout.length;
if (d > 0) {
cumDistribution[d] += cumDistribution[d - 1];
}
}
for (int i = 0; i < layout.length; ++i) {
for (int d = 0; d < cumDistribution.length; d++) {
if ((i + 1) <= cumDistribution[d]) {
layout[i] = d;
break;
}
}
}
BingoUtil.shuffle(layout, rand);
} else {
Expand Down

0 comments on commit 8ce1f4d

Please sign in to comment.