Factor protocol and gameTurn() loop
authorJBM <jbm@codingame.com>
Sun, 24 May 2020 16:58:06 +0000 (18:58 +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/Player.java
src/main/java/com/codingame/game/Referee.java

index e8cdc57..405db66 100644 (file)
@@ -25,16 +25,37 @@ class Model {
 
         private int multiplier;
         public int getMultiplier() { return multiplier; }
-        public void setMultiplier(int m){ multiplier = m; }
+        public void setMultiplier(int m) { multiplier = m; }
+
+        class FailedToThrowStonesAndShouldHave extends Exception {}
+        class ThrewMoreStonesThanHad extends Exception {}
 
         private int stones;
         public int getStones() { return stones; }
-        public void consumeStones(int n) throws InvalidAction {
+        public void consumeStones(int n)
+            throws ThrewMoreStonesThanHad,
+                   FailedToThrowStonesAndShouldHave
+        {
             if (n > stones) {
-                throw new InvalidAction("attempted to throw more stones than they had.");
+                throw new ThrewMoreStonesThanHad();
+            }
+            if (n == 0 && stones > 0) {
+                throw new FailedToThrowStonesAndShouldHave();
             }
             setStones(stones - n);
         }
+        public int consumeMaxStones() {
+            int r = stones;
+            stones = 0;
+            return r;
+        }
+        public int consumeMinStones() {
+            if (stones < 1) {
+                throw new Error("Internal error: tried to consume min stones on an empty heap.");
+            }
+            stones--;
+            return 1;
+        }
         public void setStones(int n) {
             stones = n;
         }
index ac96489..e73482a 100644 (file)
@@ -16,27 +16,53 @@ public class Player extends AbstractMultiplayerPlayer {
     Model.Player model;
     View.Player view;
 
-    private String messageString = "";
-    public String getMessageString() { return messageString; }
-
     @Override
     public int getExpectedOutputLines() {
         return 1;
     }
 
-    static final Pattern rest = Pattern.compile(".*");
-    static final Pattern eol = Pattern.compile("\n");
-    public int getAction() throws TimeoutException, NumberFormatException {
-        Scanner s = new Scanner(getOutputs().get(0));
+    // same-typed positional parameters… a disaster waiting to happen
+    void gameInit(int roadLength, int initialStones) {
+        sendInputLine(String.format("%d %d", roadLength, initialStones));
+    }
+
+    void sendGameTurn() {
+        type = null;             //
+        stoneThrow = null;       // correctness over stability!
+        messageString = null;    //
+
+        sendInputLine(String.format("%d %d %d",
+                                    model.getTrollDistance(),
+                                    model.getStones(),
+                                    model.getOppStones()));
+        execute();
+    }
+
+    static enum Action { Throw, Timeout, Invalid }
+    Action type;
+    Integer stoneThrow;
+    String messageString;
+
+    private void reportMsg(String tag) {
+        System.err.println("Message @" + tag + ": " + messageString);
+    }
+
+    void receiveGameTurn() {
         messageString = "";
-        try {
-            int st = s.nextInt();
-            s.useDelimiter(eol);
-            if (s.hasNext(rest))
-                messageString = s.next(rest);
-            return st;
-        }
-        catch (InputMismatchException e) { throw new NumberFormatException(); }
-        catch (NoSuchElementException e) { throw new NumberFormatException(); }
+        try { messageString = getOutputs().get(0); }
+        catch (TimeoutException e) { type = Action.Timeout; return; }
+
+        Scanner s = new Scanner(messageString);
+        try { stoneThrow = s.nextInt(); }
+        catch (InputMismatchException e) { type = Action.Invalid; return; }
+        catch (NoSuchElementException e) { type = Action.Invalid; return; }
+
+        s.useDelimiter(eol);
+        if (s.hasNext(rest)) messageString = s.next(rest);
+        else messageString = "";
+        type = Action.Throw;
     }
+
+    private static final Pattern rest = Pattern.compile(".*");
+    private static final Pattern eol = Pattern.compile("\n");
 }
index 979e283..eaac5fb 100644 (file)
@@ -31,9 +31,7 @@ public class Referee extends AbstractReferee {
         gameManager.getPlayer(1).model = model.p1;
 
         for (Player p: gameManager.getPlayers()) {
-            p.sendInputLine(String.format("%d %d",
-                                          model.roadLength,
-                                          model.initialStones));
+            p.gameInit(model.roadLength, model.initialStones);
         }
 
         view.init(model);
@@ -48,76 +46,107 @@ public class Referee extends AbstractReferee {
 
         view.startTurn();
 
-        boolean disqual = false;
-        boolean victory = false;
-        boolean exhausted = false;
-
-        int delta = 0;
         for (Player player : gameManager.getActivePlayers()) {
-            Model.Player p = player.model;
-            {
-                player.sendInputLine(String.format("%d %d %d",
-                                                   p.getTrollDistance(),
-                                                   p.getStones(),
-                                                   p.getOppStones()));
-            }
-            player.execute();
+            player.sendGameTurn();
         }
-
         // SDK @#%^&! arbitrary sequence point: last input < first output
 
+        /* Parse player actions and decide basic disqualifications.
+         * Display their optional message right now: if their action
+         * is ill-formed it could help them debug.  Or shame them, at
+         * least.
+         */
+        boolean disqual = false;
         for (Player player : gameManager.getActivePlayers()) {
-            Model.Player p = player.model;
-
-            try {
-                int stones = player.getAction();
-                if (stones == 0 && p.getStones() > 0) {
+            player.receiveGameTurn();
+            switch (player.type) {
+            case Timeout:
+                gameManager.addToGameSummary(player.getNicknameToken() + " timed out!");
+                player.deactivate(player.getNicknameToken() + " T/O");
+                player.setScore(-1);
+                disqual = true;
+                break;
+            case Invalid:
+                player.deactivate(player.getNicknameToken() + " INVALID");
+                gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " provided an ill-formed action"));
+                player.setScore(-1);
+                disqual = true;
+                break;
+            case Throw:
+                try { player.model.consumeStones(player.stoneThrow); }
+                catch (Model.Player.ThrewMoreStonesThanHad e) {
                     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;
+                        gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " tried to throw more stones than they had.  I'll let it slide for this time.  (But not let them throw that much!)"));
+                        player.stoneThrow = player.model.consumeMaxStones();
                     }
                     else {
-                        throw new InvalidAction("tried not throwing any stone.  They were then eaten by a grue.");
+                        gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " tried to throw more stones than they had.  They went into debt trying to provide.  The economy tanked, recession and famine ensued; even the troll wouldn't have wanted to bash them anymore.  But that's no victory."));
+                        player.deactivate(player.getNicknameToken() + " ILLEGAL");
+                        player.setScore(-1);
+                        disqual = true;
                     }
                 }
-                p.consumeStones(stones);
-                gameManager.addToGameSummary(String.format("%s throws %d stone%s at the troll.", player.getNicknameToken(), stones, stones == 1 ? "" : "s"));
-                delta += player.model.getMultiplier() * stones;
+                catch (Model.Player.FailedToThrowStonesAndShouldHave e) {
+                    if (model.random.nextInt(10) > 0) {
+                        gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " tried not throwing any stones.  Fixing that for them because I'm in a good mood today."));
+                        player.stoneThrow = player.model.consumeMinStones();
+                    }
+                    else {
+                        gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + "tried not throwing any stones.  They were then eaten by a grue."));
+                        player.deactivate(player.getNicknameToken() + " ILLEGAL");
+                        player.setScore(-1);
+                        disqual = true;
+                    }
+                }
+                break;
+            }
+            player.view.displayMessage(player.messageString);
+        }
 
-                if (stones < 0) {
+        /* Update game model and view.
+         *
+         * As a special case, the "cheater" (sending out negative
+         * stones) handling is deferred here because we need to update
+         * its view for it to be funny.  In the end, they're still
+         * disqualified.
+         *
+         * Gather other game end scenarios (actual victory or stone
+         * exhaustion).
+         */
+        int delta = 0;
+        boolean victory = false;
+        boolean exhausted = false;
+        if (! disqual) {
+            for (Player player : gameManager.getActivePlayers()) {
+                gameManager.addToGameSummary(String.format("%s throws %d stone%s at the troll.", player.getNicknameToken(), player.stoneThrow, player.stoneThrow == 1 ? "" : "s"));
+                delta += player.model.getMultiplier() * player.stoneThrow;
+
+                if (player.stoneThrow < 0) {
                     player.deactivate(player.getNicknameToken() + " CHEAT");
                     gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " cheated.  Banning account."));
                     player.setScore(-1);
                     disqual = true;
                 }
-                else if (stones > 0) {
-                    player.view.animateStones(stones);
+                else if (player.stoneThrow > 0) {
+                    player.view.animateStones(player.stoneThrow);
                     player.view.updateStoneCounter();
                 }
             }
-            catch (InvalidAction e) {
-                player.deactivate(player.getNicknameToken() + " INVALID");
-                gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " " + e.getMessage()));
-                player.setScore(-1);
-                disqual = true;
-            }
-            catch (NumberFormatException e) {
-                player.deactivate(player.getNicknameToken() + " ERROR");
-                gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " provided malformed input!"));
-                player.setScore(-1);
-                disqual = true;
-            }
-            catch (TimeoutException e) {
-                gameManager.addToGameSummary(player.getNicknameToken() + " timed out!");
-                player.deactivate(player.getNicknameToken() + " T/O");
-                player.setScore(-1);
-                disqual = true;
-            }
 
-            player.view.displayMessage(player.getMessageString());
-        }
+            /* If a player cheated, delta is unusable as is.
+             * (Consider the case the player on the right sent
+             * INT_MIN.  INT_MIN * (-1) = INT_MIN, so that player
+             * would both glean the stones *and* push the troll away.
+             * It would be unfair to have a cheating player "win"
+             * (earn the opponent castle destruction animation) this
+             * way.
+             */
+            boolean cheat0 = gameManager.getPlayer(0).stoneThrow < 0;
+            boolean cheat1 = gameManager.getPlayer(1).stoneThrow < 0;
+            if (cheat0 && cheat1); // here we can actually keep delta's value
+            else if (cheat0) delta = -1;
+            else if (cheat1) delta =  1;
 
-        if (! disqual) {
             if (delta > 0) {
                 gameManager.addToGameSummary("Troll walks right.");
                 model.trollPosition++;