Trolls can be of different races
[troll.git] / src / main / java / com / codingame / game / Model.java
index 4cd1280..00701d1 100644 (file)
@@ -1,7 +1,9 @@
 package com.codingame.game;
 
 import java.util.Random;
+import java.util.Properties;
 
+import com.codingame.gameengine.core.GameManager;
 import com.codingame.gameengine.core.MultiplayerGameManager;
 import com.google.inject.Inject;
 
@@ -25,16 +27,37 @@ class Model {
 
         private int multiplier;
         public int getMultiplier() { return multiplier; }
-        public void setMultiplier(int m){ multiplier = m; }
+        public void setMultiplier(int m) { multiplier = m; }
+
+        class FailedToThrowStonesAndShouldHave extends Exception {}
+        class ThrewMoreStonesThanHad extends Exception {}
 
         private int stones;
         public int getStones() { return stones; }
-        public void consumeStones(int n) throws InvalidAction {
+        public void consumeStones(int n)
+            throws ThrewMoreStonesThanHad,
+                   FailedToThrowStonesAndShouldHave
+        {
             if (n > stones) {
-                throw new InvalidAction("attempted to throw more stones than they had.");
+                throw new ThrewMoreStonesThanHad();
+            }
+            if (n == 0 && stones > 0) {
+                throw new FailedToThrowStonesAndShouldHave();
             }
             setStones(stones - n);
         }
+        public int consumeMaxStones() {
+            int r = stones;
+            stones = 0;
+            return r;
+        }
+        public int consumeMinStones() {
+            if (stones < 1) {
+                throw new Error("Internal error: tried to consume min stones on an empty heap.");
+            }
+            stones--;
+            return 1;
+        }
         public void setStones(int n) {
             stones = n;
         }
@@ -72,6 +95,43 @@ class Model {
             break;
         }
 
+        Properties ps = gameManager.getGameParameters();
+        String buf = ps.getProperty("roadLength");
+        if (buf != null) {
+            try {
+                int i = Integer.parseInt(buf);
+                if (i < 0 || i > 20 || (i & 1) != 0) {
+                    gameManager.addToGameSummary(GameManager.formatErrorMessage("Ignoring invalid road length: " + buf));
+                }
+                else {
+                    roadLength = i;
+                    gameManager.addToGameSummary("Road length overridden to " + i);
+                }
+            }
+            catch(NumberFormatException e) {
+                gameManager.addToGameSummary(GameManager.formatErrorMessage("Ill-formed road length: " + buf));
+            }
+        }
+        ps.setProperty("roadLength", new Integer(roadLength).toString());
+
+        buf = ps.getProperty("initialStones");
+        if (buf != null) {
+            try {
+                int i = Integer.parseInt(buf);
+                if (i > 50) {
+                    gameManager.addToGameSummary(GameManager.formatErrorMessage("Ignoring invalid initial stone count: " + buf));
+                }
+                else {
+                    initialStones = i;
+                    gameManager.addToGameSummary("Initial stone count overridden to " + buf);
+                }
+            }
+            catch (NumberFormatException e) {
+                gameManager.addToGameSummary(GameManager.formatErrorMessage("Ill-formed initial stone count: " + buf));
+            }
+        }
+        ps.setProperty("initialStones", new Integer(initialStones).toString());
+
         trollPosition = roadLength / 2;
 
         p0 = new Player(0);
@@ -104,6 +164,7 @@ class Model {
         }
     }
     int getWinner() { return winner; }
+    int getLoser() { return 1 - winner; }
 
     boolean exhausted() {
         return p0.getStones() <= 0 && p1.getStones() <= 0;