Make game parameters overridable through game settings
authorJBM <jbm@codingame.com>
Fri, 29 May 2020 12:54:25 +0000 (14:54 +0200)
committerJBM <jbm@codingame.com>
Fri, 29 May 2020 12:54:25 +0000 (14:54 +0200)
PLAN.org
config/statement_en.html
src/main/java/com/codingame/game/Model.java
src/test/java/Main.java

index d8d1402..11f0892 100644 (file)
--- a/PLAN.org
+++ b/PLAN.org
@@ -46,6 +46,6 @@ That one's probably never going to be DONE ^^'
 - [ ] troll pants colors
 * TODO Troll quotes
 - pain au chocolat ou chocolatine
-* TODO initial model parametrization
+* DONE initial model parametrization
 * TODO view parameterization?
 * TODO troll races
index 7ba83d1..428577f 100644 (file)
    <div class="statement-section statement-expertrules">
      <h2>
        <span class="icon icon-expertrules">&nbsp;</span>
-       <span>Maps</span>
+       <span>Expert rules</span>
      </h2>
      <div class="statement-expert-rules-content">
        <p>
          is <strong>subject to change without notice</strong>.  Why do
          you think they're provided in the game input?
        </p>
+       <p>
+         You can also override them via game
+         settings <tt>roadLength</tt> and <tt>initialStones</tt>.
+       </p>
      </div>
    </div>
    <div class="statement-section statement-protocol">
      <p>
        This draft's last change is:
        <strong>
-         the troll can speak.
+         game parameters can be overridden.
        </strong>
      </p>
    </div>
index 405db66..46841db 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;
 
@@ -93,6 +95,40 @@ 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));
+            }
+        }
+        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));
+            }
+        }
+
         trollPosition = roadLength / 2;
 
         p0 = new Player(0);
index 79bfaed..8de5bd7 100644 (file)
@@ -1,9 +1,15 @@
+import java.util.Properties;
+
 import com.codingame.gameengine.runner.MultiplayerGameRunner;
 
 public class Main {
     public static void main(String[] args) {
+        Properties props = new Properties();
+        props.setProperty("seed", "3");
         
         MultiplayerGameRunner gameRunner = new MultiplayerGameRunner();
+        gameRunner.setGameParameters(props);
+
         gameRunner.addAgent(Player1.class);
         gameRunner.addAgent(PlayerRand.class);