FIX: IT belongs in a MODEL! trolls-have-no-master
authorJBM <jbm@codingame.com>
Tue, 25 Aug 2020 16:16:44 +0000 (18:16 +0200)
committerJBM <jbm@codingame.com>
Tue, 25 Aug 2020 18:04:31 +0000 (20:04 +0200)
(authored months ago, had remained WIP in my work directory
for "some" reason)

src/main/java/com/codingame/game/Model.java
src/main/java/com/codingame/game/Referee.java

index 96140f5..beff100 100644 (file)
@@ -192,6 +192,14 @@ class Model {
         p1.adjustScore(trollPosition);
     }
 
+    boolean FIX_IT() {
+        switch (league.fixLevel) {
+           case SOMETIMES: return random.nextInt(10) > 0;
+           case NEVER: return false;
+           default: throw new JavaLimitationError();
+        }
+    }
+
     private Integer winner;
     boolean haveWinner() {
         return winner != null;
index 7a718e9..8f5fc97 100644 (file)
@@ -81,17 +81,7 @@ public class Referee extends AbstractReferee {
             case Throw:
                 try { player.model.consumeStones(player.stoneThrow); }
                 catch (Model.Player.ThrewMoreStonesThanHad e) {
-                    boolean FIX_IT;
-                    switch (league.fixLevel) {
-                    case SOMETIMES:
-                        FIX_IT = model.random.nextInt(10) > 0;
-                        break;
-                    case NEVER:
-                        FIX_IT = false;
-                        break;
-                    default: throw new JavaLimitationError();
-                    }
-                    if (FIX_IT) {
+                    if (model.FIX_IT()) {
                         player.view.threwMoreStonesThanHad();
                         player.stoneThrow = player.model.consumeMaxStones();
                     }
@@ -102,17 +92,7 @@ public class Referee extends AbstractReferee {
                     }
                 }
                 catch (Model.Player.FailedToThrowStonesAndShouldHave e) {
-                    boolean FIX_IT;
-                    switch (league.fixLevel) {
-                    case SOMETIMES:
-                        FIX_IT = model.random.nextInt(10) > 0;
-                        break;
-                    case NEVER:
-                        FIX_IT = false;
-                        break;
-                    default: throw new JavaLimitationError();
-                    }
-                    if (FIX_IT) {
+                    if (model.FIX_IT()) {
                         player.view.failedToThrowStonesAndShouldHave();
                         player.stoneThrow = player.model.consumeMinStones();
                     }