From 20c63dac92d830a4c61f6d17ecfb8e7e0d04e58d Mon Sep 17 00:00:00 2001 From: JBM Date: Fri, 22 May 2020 00:08:19 +0200 Subject: [PATCH 1/1] Split referee data to model and view --- src/main/java/com/codingame/game/Model.java | 10 +++ src/main/java/com/codingame/game/Referee.java | 87 +++++++++---------- src/main/java/com/codingame/game/View.java | 12 +++ 3 files changed, 65 insertions(+), 44 deletions(-) create mode 100644 src/main/java/com/codingame/game/Model.java create mode 100644 src/main/java/com/codingame/game/View.java diff --git a/src/main/java/com/codingame/game/Model.java b/src/main/java/com/codingame/game/Model.java new file mode 100644 index 0000000..b98de7d --- /dev/null +++ b/src/main/java/com/codingame/game/Model.java @@ -0,0 +1,10 @@ +package com.codingame.game; + +import java.util.Random; + +class Model { + Random random; + int roadLength; + int initialStones; + int trollPosition; +} diff --git a/src/main/java/com/codingame/game/Referee.java b/src/main/java/com/codingame/game/Referee.java index 685b458..f087503 100644 --- a/src/main/java/com/codingame/game/Referee.java +++ b/src/main/java/com/codingame/game/Referee.java @@ -21,58 +21,57 @@ public class Referee extends AbstractReferee { @Inject private MultiplayerGameManager gameManager; @Inject private GraphicEntityModule graphicEntityModule; - Random random; + Model model = new Model(); - int roadLength; - int initialStones; - int trollPosition; + // Hybrid :-( Player p0, p1; - Sprite troll; - Text trollPositionGauge; + + // View + View view = new View(); @Override public void init() { - random = new Random(gameManager.getSeed()); - switch (random.nextInt(4)) { + model.random = new Random(gameManager.getSeed()); + switch (model.random.nextInt(4)) { case 0: - roadLength = 6; - initialStones = 15; + model.roadLength = 6; + model.initialStones = 15; break; case 1: - roadLength = 6; - initialStones = 30; + model.roadLength = 6; + model.initialStones = 30; break; case 2: - roadLength = 14; - initialStones = 30; + model.roadLength = 14; + model.initialStones = 30; break; case 3: - roadLength = 14; - initialStones = 50; + model.roadLength = 14; + model.initialStones = 50; break; } - trollPosition = roadLength / 2; + model.trollPosition = model.roadLength / 2; p0 = gameManager.getPlayer(0); p0.setCastlePosition(0); p0.setMultiplier(1); - p0.adjustScore(trollPosition); - p0.sendInputLine(String.format("%d %d", roadLength, initialStones)); + p0.adjustScore(model.trollPosition); + p0.sendInputLine(String.format("%d %d", model.roadLength, model.initialStones)); p1 = gameManager.getPlayer(1); - p1.setCastlePosition(roadLength); + p1.setCastlePosition(model.roadLength); p1.setMultiplier(-1); - p1.adjustScore(trollPosition); - p1.sendInputLine(String.format("%d %d", roadLength, initialStones)); + p1.adjustScore(model.trollPosition); + p1.sendInputLine(String.format("%d %d", model.roadLength, model.initialStones)); drawBackground(); drawPlayer(); drawTroll(); // result in text display, so do last: - p0.setStones(initialStones); - p1.setStones(initialStones); + p0.setStones(model.initialStones); + p1.setStones(model.initialStones); gameManager.setFrameDuration(2000); } @@ -164,14 +163,14 @@ public class Referee extends AbstractReferee { } private void drawTroll() { - troll = graphicEntityModule.createSprite() + view.troll = graphicEntityModule.createSprite() .setImage("troll.png") .setAnchorX(0.5) .setAnchorY(1) .setX(1920/2) .setY(880) .setZIndex(2); - trollPositionGauge = graphicEntityModule.createText() + view.trollPositionGauge = graphicEntityModule.createText() .setZIndex(2) .setAnchor(0.5) .setFontSize(40) @@ -181,27 +180,27 @@ public class Referee extends AbstractReferee { } private void moveTroll() { - graphicEntityModule.commitEntityState(0.5, troll, trollPositionGauge); + graphicEntityModule.commitEntityState(0.5, view.troll, view.trollPositionGauge); int x0 = p0.castle.getX(), x1 = p1.castle.getX(); int y0 = p0.castle.getY(), y1 = p1.castle.getY(); - troll.setX(x0 + trollPosition * (x1-x0) / roadLength, + view.troll.setX(x0 + model.trollPosition * (x1-x0) / model.roadLength, Curve.ELASTIC); - troll.setY(y0 + trollPosition * (y1-y0) / roadLength, + view.troll.setY(y0 + model.trollPosition * (y1-y0) / model.roadLength, Curve.ELASTIC); - trollPositionGauge.setX((trollPositionGauge.getX() + troll.getX()) / 2); - int delta = trollPosition - roadLength / 2; + view.trollPositionGauge.setX((view.trollPositionGauge.getX() + view.troll.getX()) / 2); + int delta = model.trollPosition - model.roadLength / 2; if (delta < 0) { - trollPositionGauge.setText("← " + Math.abs(delta)); + view.trollPositionGauge.setText("← " + Math.abs(delta)); } else if (delta > 0) { - trollPositionGauge.setText(Math.abs(delta) + " →"); + view.trollPositionGauge.setText(Math.abs(delta) + " →"); } else { - trollPositionGauge.setText("↔"); + view.trollPositionGauge.setText("↔"); } - graphicEntityModule.commitEntityState(0.75, trollPositionGauge); - trollPositionGauge.setX(troll.getX()); + graphicEntityModule.commitEntityState(0.75, view.trollPositionGauge); + view.trollPositionGauge.setX(view.troll.getX()); } @Override @@ -219,7 +218,7 @@ public class Referee extends AbstractReferee { try { int stones = player.getAction(); if (stones == 0 && player.getStones() > 0) { - if (random.nextInt(10) > 0) { + if (view.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; } @@ -244,15 +243,15 @@ public class Referee extends AbstractReferee { player.stone.setAlpha(1); graphicEntityModule.commitEntityState(0, player.stone); - int peakX = (player.castle.getX() + troll.getX()) / 2; + int peakX = (player.castle.getX() + view.troll.getX()) / 2; int peakY = 540; player.stone.setX(peakX); player.stone.setY(peakY, Curve.EASE_OUT); graphicEntityModule.commitEntityState(0.25, player.stone, player.stoneCounter); - player.stone.setX(troll.getX()); - player.stone.setY(troll.getY() - 50, Curve.EASE_IN); + player.stone.setX(view.troll.getX()); + player.stone.setY(view.troll.getY() - 50, Curve.EASE_IN); player.stone.setAlpha(0, Curve.EASE_IN); graphicEntityModule.commitEntityState(0.5, player.stone); } @@ -284,12 +283,12 @@ public class Referee extends AbstractReferee { if (delta > 0) { gameManager.addToGameSummary("Troll walks right."); - trollPosition++; + model.trollPosition++; moveTroll(); } else if (delta < 0) { gameManager.addToGameSummary("Troll walks left."); - trollPosition--; + model.trollPosition--; moveTroll(); } else { @@ -297,8 +296,8 @@ public class Referee extends AbstractReferee { } for (Player player : gameManager.getActivePlayers()) { - player.adjustScore(trollPosition); - if (trollPosition == player.getCastlePosition()) { + player.adjustScore(model.trollPosition); + if (model.trollPosition == player.getCastlePosition()) { gameManager.addToGameSummary(GameManager.formatErrorMessage("Troll destroys " + player.getNicknameToken())); endGame(); } diff --git a/src/main/java/com/codingame/game/View.java b/src/main/java/com/codingame/game/View.java new file mode 100644 index 0000000..34894ac --- /dev/null +++ b/src/main/java/com/codingame/game/View.java @@ -0,0 +1,12 @@ +package com.codingame.game; + +import java.util.Random; + +import com.codingame.gameengine.module.entities.Sprite; +import com.codingame.gameengine.module.entities.Text; + +class View { + Random random = new Random(); + Sprite troll; + Text trollPositionGauge; +} -- 2.30.2