summaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorJoffrey BION <joffrey.bion@gmail.com>2016-12-08 01:56:32 +0100
committerJoffrey BION <joffrey.bion@gmail.com>2016-12-08 02:11:44 +0100
commit58393dae90326f9f9b29092f2dea22633414c30f (patch)
tree3d1eec4454a0deffb5c994b6fb88abc0d824e2d4 /src/main/java
parentFirst draft of new game initialization (diff)
downloadseven-wonders-58393dae90326f9f9b29092f2dea22633414c30f.tar.gz
seven-wonders-58393dae90326f9f9b29092f2dea22633414c30f.tar.bz2
seven-wonders-58393dae90326f9f9b29092f2dea22633414c30f.zip
Add science points computation logic and tests
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/boards/Science.java40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/main/java/org/luxons/sevenwonders/game/boards/Science.java b/src/main/java/org/luxons/sevenwonders/game/boards/Science.java
index 2eb0117a..3b08ce8f 100644
--- a/src/main/java/org/luxons/sevenwonders/game/boards/Science.java
+++ b/src/main/java/org/luxons/sevenwonders/game/boards/Science.java
@@ -1,5 +1,6 @@
package org.luxons.sevenwonders.game.boards;
+import java.util.Arrays;
import java.util.EnumMap;
import java.util.Map;
@@ -17,8 +18,47 @@ public class Science {
jokers += quantity;
}
+ public int getJokers() {
+ return jokers;
+ }
+
public void addAll(Science science) {
science.quantities.forEach(this::add);
jokers += science.jokers;
}
+
+ public int getQuantity(ScienceType type) {
+ return quantities.get(type);
+ }
+
+ public int computePoints() {
+ Integer[] values = quantities.values().toArray(new Integer[quantities.size()]);
+ return computePoints(values, jokers);
+ }
+
+ private static int computePoints(Integer[] values, int jokers) {
+ if (jokers == 0) {
+ return computePointsNoJoker(values);
+ }
+ int maxPoints = 0;
+ for (int i = 0; i < values.length; i++) {
+ values[i]++;
+ maxPoints = Math.max(maxPoints, computePoints(values, jokers - 1));
+ values[i]--;
+ }
+ return maxPoints;
+ }
+
+ private static int computePointsNoJoker(Integer[] values) {
+ int independentSquaresSum = Arrays.stream(values).mapToInt(i -> i * i).sum();
+ int nbGroupsOfAll = Arrays.stream(values).mapToInt(i -> i).min().orElse(0);
+ return independentSquaresSum + nbGroupsOfAll * 7;
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder sb = new StringBuilder("Science{");
+ quantities.forEach((type, count) -> sb.append(type).append("=").append(count).append(" "));
+ return sb.append("*=").append(jokers).toString();
+ }
}
bgstack15