Major model/view split. Compiles and runs, but viewer is sub-par.
[troll.git] / src / main / java / com / codingame / game / Model.java
index b98de7d..4cd1280 100644 (file)
@@ -2,9 +2,110 @@ package com.codingame.game;
 
 import java.util.Random;
 
+import com.codingame.gameengine.core.MultiplayerGameManager;
+import com.google.inject.Inject;
+
 class Model {
+    @Inject private MultiplayerGameManager<com.codingame.game.Player> gameManager;
     Random random;
     int roadLength;
     int initialStones;
     int trollPosition;
+    Player p0, p1;
+
+    class Player {
+        com.codingame.game.Player gp;
+        int index;
+
+        Player(int i) { index = i; }
+
+        private int castlePosition;
+        public int getCastlePosition() { return castlePosition; }
+        public void setCastlePosition(int pos) { castlePosition = pos; }
+
+        private int multiplier;
+        public int getMultiplier() { return multiplier; }
+        public void setMultiplier(int m){ multiplier = m; }
+
+        private int stones;
+        public int getStones() { return stones; }
+        public void consumeStones(int n) throws InvalidAction {
+            if (n > stones) {
+                throw new InvalidAction("attempted to throw more stones than they had.");
+            }
+            setStones(stones - n);
+        }
+        public void setStones(int n) {
+            stones = n;
+        }
+        public int getOppStones() {
+            return p0.stones + p1.stones - stones;
+        }
+
+        public void adjustScore(int trollPosition) {
+            gp.setScore(Math.abs(castlePosition - trollPosition));
+        }
+
+        public int getTrollDistance() {
+            return Math.abs(castlePosition - trollPosition);
+        }
+    }
+
+    void init(long seed) {
+        random = new Random(seed);
+        switch (random.nextInt(4)) {
+        case 0:
+            roadLength = 6;
+            initialStones = 15;
+            break;
+        case 1:
+            roadLength = 6;
+            initialStones = 30;
+            break;
+        case 2:
+            roadLength = 14;
+            initialStones = 30;
+            break;
+        case 3:
+            roadLength = 14;
+            initialStones = 50;
+            break;
+        }
+
+        trollPosition = roadLength / 2;
+
+        p0 = new Player(0);
+        p0.gp = gameManager.getPlayer(0);
+        p0.setCastlePosition(0);
+        p0.setMultiplier(1);
+        p0.adjustScore(trollPosition);
+        p0.setStones(initialStones);
+
+        p1 = new Player(1);
+        p1.gp = gameManager.getPlayer(1);
+        p1.setCastlePosition(roadLength);
+        p1.setMultiplier(-1);
+        p1.adjustScore(trollPosition);
+        p1.setStones(initialStones);
+    }
+
+    private int winner;
+    boolean haveWinner() {
+        if (trollPosition == 0) {
+            winner = 1;
+            return true;
+        }
+        else if (trollPosition == roadLength) {
+            winner = 0;
+            return true;
+        }
+        else {
+            return false;
+        }
+    }
+    int getWinner() { return winner; }
+
+    boolean exhausted() {
+        return p0.getStones() <= 0 && p1.getStones() <= 0;
+    }
 }