Facade the SDK's GameManager out
[troll.git] / src / main / java / com / codingame / game / Model.java
index 405db66..108b5a6 100644 (file)
@@ -1,12 +1,15 @@
 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;
 
 class Model {
     @Inject private MultiplayerGameManager<com.codingame.game.Player> gameManager;
+    long seed;
     Random random;
     int roadLength;
     int initialStones;
@@ -16,8 +19,12 @@ class Model {
     class Player {
         com.codingame.game.Player gp;
         int index;
+        boolean hit;
 
-        Player(int i) { index = i; }
+        Player(int i) {
+            index = i;
+            hit = false;
+        }
 
         private int castlePosition;
         public int getCastlePosition() { return castlePosition; }
@@ -64,7 +71,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() {
@@ -72,7 +81,8 @@ class Model {
         }
     }
 
-    void init(long seed) {
+    void init() {
+        seed = gameManager.getSeed();
         random = new Random(seed);
         switch (random.nextInt(4)) {
         case 0:
@@ -93,6 +103,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);
@@ -110,24 +157,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;
     }
 }