Split some code to view
authorJBM <jbm@codingame.com>
Thu, 21 May 2020 22:30:39 +0000 (00:30 +0200)
committerJBM <jbm@codingame.com>
Wed, 27 May 2020 14:53:54 +0000 (16:53 +0200)
src/main/java/com/codingame/game/Model.java
src/main/java/com/codingame/game/Referee.java
src/main/java/com/codingame/game/View.java

index b98de7d..b310fdd 100644 (file)
@@ -7,4 +7,28 @@ class Model {
     int roadLength;
     int initialStones;
     int 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;
+    }
 }
index f087503..edcafe2 100644 (file)
@@ -21,37 +21,16 @@ public class Referee extends AbstractReferee {
     @Inject private MultiplayerGameManager<Player> gameManager;
     @Inject private GraphicEntityModule graphicEntityModule;
 
+    @Inject private View view;
+
     Model model = new Model();
 
     // Hybrid :-(
     Player p0, p1;
 
-    // View
-    View view = new View();
-    
     @Override
     public void init() {
-        model.random = new Random(gameManager.getSeed());
-        switch (model.random.nextInt(4)) {
-        case 0:
-            model.roadLength = 6;
-            model.initialStones = 15;
-            break;
-        case 1:
-            model.roadLength = 6;
-            model.initialStones = 30;
-            break;
-        case 2:
-            model.roadLength = 14;
-            model.initialStones = 30;
-            break;
-        case 3:
-            model.roadLength = 14;
-            model.initialStones = 50;
-            break;
-        }
-
-        model.trollPosition = model.roadLength / 2;
+        model.init(gameManager.getSeed());
 
         p0 = gameManager.getPlayer(0);
         p0.setCastlePosition(0);
@@ -65,21 +44,14 @@ public class Referee extends AbstractReferee {
         p1.adjustScore(model.trollPosition);
         p1.sendInputLine(String.format("%d %d", model.roadLength, model.initialStones));
 
-        drawBackground();
+        view.init();
         drawPlayer();
-        drawTroll();
 
         // result in text display, so do last:
         p0.setStones(model.initialStones);
         p1.setStones(model.initialStones);
 
-        gameManager.setFrameDuration(2000);
-    }
-
-    private void drawBackground() {
-        graphicEntityModule.createSprite()
-                .setImage("background.png")
-                .setAnchor(0);
+        gameManager.setFrameDuration(2000); // XXX
     }
 
     private void drawPlayer() {
@@ -162,23 +134,6 @@ public class Referee extends AbstractReferee {
         }
     }
 
-    private void drawTroll() {
-        view.troll = graphicEntityModule.createSprite()
-            .setImage("troll.png")
-            .setAnchorX(0.5)
-            .setAnchorY(1)
-            .setX(1920/2)
-            .setY(880)
-            .setZIndex(2);
-        view.trollPositionGauge = graphicEntityModule.createText()
-            .setZIndex(2)
-            .setAnchor(0.5)
-            .setFontSize(40)
-            .setX(1980/2)
-            .setY(980)
-            .setFillColor(0xffffff);
-    }
-
     private void moveTroll() {
         graphicEntityModule.commitEntityState(0.5, view.troll, view.trollPositionGauge);
         int x0 = p0.castle.getX(), x1 = p1.castle.getX();
@@ -218,7 +173,7 @@ public class Referee extends AbstractReferee {
             try {
                 int stones = player.getAction();
                 if (stones == 0 && player.getStones() > 0) {
-                    if (view.random.nextInt(10) > 0) {
+                    if (model.random.nextInt(10) > 0) {
                         gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " tried not throwing stones.  Fixing that for them because I'm in a good mood today."));
                         stones = 1;
                     }
index 34894ac..38dcac1 100644 (file)
@@ -2,11 +2,44 @@ package com.codingame.game;
 
 import java.util.Random;
 
+import com.codingame.gameengine.module.entities.GraphicEntityModule;
 import com.codingame.gameengine.module.entities.Sprite;
 import com.codingame.gameengine.module.entities.Text;
+import com.google.inject.Inject;
 
 class View {
+    @Inject private GraphicEntityModule graphicEntityModule;
+
     Random random = new Random();
     Sprite troll;
     Text trollPositionGauge;
+
+    void init() {
+        drawBackground();
+        drawTroll();
+    }
+
+    private void drawBackground() {
+        graphicEntityModule.createSprite()
+                .setImage("background.png")
+                .setAnchor(0);
+    }
+
+    private void drawTroll() {
+        troll = graphicEntityModule.createSprite()
+            .setImage("troll.png")
+            .setAnchorX(0.5)
+            .setAnchorY(1)
+            .setX(1920/2)
+            .setY(880)
+            .setZIndex(2);
+        trollPositionGauge = graphicEntityModule.createText()
+            .setZIndex(2)
+            .setAnchor(0.5)
+            .setFontSize(40)
+            .setX(1980/2)
+            .setY(980)
+            .setFillColor(0xffffff);
+    }
+
 }