Early game termination
[troll.git] / src / main / java / com / codingame / game / Model.java
index 00701d1..6eb0546 100644 (file)
@@ -66,7 +66,9 @@ class Model {
         }
 
         public void adjustScore(int trollPosition) {
-            gp.setScore(Math.abs(castlePosition - trollPosition));
+            if (gp.isActive()) {
+                gp.setScore(Math.abs(castlePosition - trollPosition));
+            }
         }
 
         public int getTrollDistance() {
@@ -149,24 +151,30 @@ class Model {
         p1.setStones(initialStones);
     }
 
-    private int winner;
-    boolean haveWinner() {
-        if (trollPosition == 0) {
+    void moveTroll(int delta) {
+        trollPosition += delta;
+        if (trollPosition <= 0) {
+            trollPosition = 0;
             winner = 1;
-            return true;
         }
-        else if (trollPosition == roadLength) {
+        if (trollPosition >= roadLength) {
+            trollPosition = roadLength;
             winner = 0;
-            return true;
-        }
-        else {
-            return false;
         }
+
+        p0.adjustScore(trollPosition);
+        p1.adjustScore(trollPosition);
     }
+
+    private Integer winner;
+    boolean haveWinner() {
+        return winner != null;
+    }
+
     int getWinner() { return winner; }
     int getLoser() { return 1 - winner; }
 
     boolean exhausted() {
-        return p0.getStones() <= 0 && p1.getStones() <= 0;
+        return p0.getStones() <= 0 || p1.getStones() <= 0;
     }
 }