1 package com.codingame.game;
3 import java.util.ArrayList;
4 import java.util.Collections;
6 import java.util.Random;
8 import com.codingame.gameengine.core.AbstractPlayer.TimeoutException;
9 import com.codingame.gameengine.core.AbstractReferee;
10 import com.codingame.gameengine.core.GameManager;
11 import com.codingame.gameengine.core.MultiplayerGameManager;
12 import com.codingame.gameengine.module.entities.GraphicEntityModule;
13 import com.codingame.gameengine.module.entities.Rectangle;
14 import com.codingame.gameengine.module.entities.Sprite;
15 import com.codingame.gameengine.module.entities.Text;
16 import com.codingame.gameengine.module.entities.Curve;
17 import com.google.inject.Inject;
18 import com.google.inject.Provider;
20 public class Referee extends AbstractReferee {
21 @Inject private MultiplayerGameManager<Player> gameManager;
22 @Inject private GraphicEntityModule graphicEntityModule;
24 Model model = new Model();
30 View view = new View();
34 model.random = new Random(gameManager.getSeed());
35 switch (model.random.nextInt(4)) {
38 model.initialStones = 15;
42 model.initialStones = 30;
45 model.roadLength = 14;
46 model.initialStones = 30;
49 model.roadLength = 14;
50 model.initialStones = 50;
54 model.trollPosition = model.roadLength / 2;
56 p0 = gameManager.getPlayer(0);
57 p0.setCastlePosition(0);
59 p0.adjustScore(model.trollPosition);
60 p0.sendInputLine(String.format("%d %d", model.roadLength, model.initialStones));
62 p1 = gameManager.getPlayer(1);
63 p1.setCastlePosition(model.roadLength);
65 p1.adjustScore(model.trollPosition);
66 p1.sendInputLine(String.format("%d %d", model.roadLength, model.initialStones));
72 // result in text display, so do last:
73 p0.setStones(model.initialStones);
74 p1.setStones(model.initialStones);
76 gameManager.setFrameDuration(2000);
79 private void drawBackground() {
80 graphicEntityModule.createSprite()
81 .setImage("background.png")
85 private void drawPlayer() {
86 for (Player player : gameManager.getPlayers()) {
87 boolean p0 = player.getIndex() == 0;
88 int x = p0 ? 280 : 1920 - 280;
91 Rectangle border1 = graphicEntityModule
98 .setFillColor(player.getColorToken());
100 Rectangle border2 = graphicEntityModule
107 .setFillColor(0xffffff);
109 Sprite avatarSprite = graphicEntityModule.createSprite()
113 .setImage(player.getAvatarToken())
118 player.avatar = graphicEntityModule.createGroup(border1, border2, avatarSprite);
120 Text text = graphicEntityModule.createText(player.getNicknameToken())
125 .setFillColor(0x7f3f00)
128 player.stoneCounter = graphicEntityModule.createText("S")
133 .setFillColor(0x7f3f00)
136 player.message = graphicEntityModule.createText()
137 .setX(p0 ? 15 : 1920-15)
141 .setStrokeColor(0x000000)
142 .setFillColor(0xffbf7f)
143 .setAnchorX(p0 ? 0 : 1)
146 player.castle = graphicEntityModule.createSprite()
147 .setImage("castle.png")
148 .setTint(player.getColorToken())
149 .setX(p0 ? 160 : 1920-160)
150 .setY(p0 ? 890 : 880)
154 .setScaleX(p0 ? 1 : -1);
156 player.stone = graphicEntityModule.createText()
159 .setFillColor(0x12322a)
165 private void drawTroll() {
166 view.troll = graphicEntityModule.createSprite()
167 .setImage("troll.png")
173 view.trollPositionGauge = graphicEntityModule.createText()
179 .setFillColor(0xffffff);
182 private void moveTroll() {
183 graphicEntityModule.commitEntityState(0.5, view.troll, view.trollPositionGauge);
184 int x0 = p0.castle.getX(), x1 = p1.castle.getX();
185 int y0 = p0.castle.getY(), y1 = p1.castle.getY();
186 view.troll.setX(x0 + model.trollPosition * (x1-x0) / model.roadLength,
188 view.troll.setY(y0 + model.trollPosition * (y1-y0) / model.roadLength,
191 view.trollPositionGauge.setX((view.trollPositionGauge.getX() + view.troll.getX()) / 2);
192 int delta = model.trollPosition - model.roadLength / 2;
194 view.trollPositionGauge.setText("← " + Math.abs(delta));
196 else if (delta > 0) {
197 view.trollPositionGauge.setText(Math.abs(delta) + " →");
200 view.trollPositionGauge.setText("↔");
202 graphicEntityModule.commitEntityState(0.75, view.trollPositionGauge);
203 view.trollPositionGauge.setX(view.troll.getX());
207 public void gameTurn(int turn) {
209 p0.sendInputLine(String.format("%d %d %d", p0.getScore(), p0.getStones(), p1.getStones()));
210 p1.sendInputLine(String.format("%d %d %d", p1.getScore(), p1.getStones(), p0.getStones()));
212 for (Player player : gameManager.getActivePlayers()) {
217 for (Player player : gameManager.getActivePlayers()) {
219 int stones = player.getAction();
220 if (stones == 0 && player.getStones() > 0) {
221 if (view.random.nextInt(10) > 0) {
222 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " tried not throwing stones. Fixing that for them because I'm in a good mood today."));
226 throw new InvalidAction("tried not throwing any stone. They were then eaten by a grue.");
229 player.consumeStones(stones);
230 gameManager.addToGameSummary(String.format("%s throws %d stone%s at the troll.", player.getNicknameToken(), stones, stones == 1 ? "" : "s"));
231 delta += player.getMultiplier() * stones;
234 player.deactivate(player.getNicknameToken() + " CHEAT");
235 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " cheated. Banning account."));
239 else if (stones > 0) {
240 player.stone.setX(player.castle.getX());
241 player.stone.setY(player.castle.getY() - 100);
242 player.stone.setText(new Integer(stones).toString());
243 player.stone.setAlpha(1);
244 graphicEntityModule.commitEntityState(0, player.stone);
246 int peakX = (player.castle.getX() + view.troll.getX()) / 2;
248 player.stone.setX(peakX);
249 player.stone.setY(peakY, Curve.EASE_OUT);
250 graphicEntityModule.commitEntityState(0.25, player.stone,
251 player.stoneCounter);
253 player.stone.setX(view.troll.getX());
254 player.stone.setY(view.troll.getY() - 50, Curve.EASE_IN);
255 player.stone.setAlpha(0, Curve.EASE_IN);
256 graphicEntityModule.commitEntityState(0.5, player.stone);
259 catch (InvalidAction e) {
260 player.deactivate(player.getNicknameToken() + " INVALID");
261 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " " + e.getMessage()));
265 catch (NumberFormatException e) {
266 player.deactivate(player.getNicknameToken() + " ERROR");
267 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " provided malformed input!"));
271 catch (TimeoutException e) {
272 gameManager.addToGameSummary(player.getNicknameToken() + " timed out!");
273 player.deactivate(player.getNicknameToken() + " T/O");
279 .setText(player.getMessageString());
280 // .setAnchorX(/*player == p0 ? 0 : */ 1);
281 graphicEntityModule.commitEntityState(0, player.message);
285 gameManager.addToGameSummary("Troll walks right.");
286 model.trollPosition++;
289 else if (delta < 0) {
290 gameManager.addToGameSummary("Troll walks left.");
291 model.trollPosition--;
295 gameManager.addToGameSummary("Troll stands still.");
298 for (Player player : gameManager.getActivePlayers()) {
299 player.adjustScore(model.trollPosition);
300 if (model.trollPosition == player.getCastlePosition()) {
301 gameManager.addToGameSummary(GameManager.formatErrorMessage("Troll destroys " + player.getNicknameToken()));
306 if (p0.getStones() <= 0 && p1.getStones() <= 0) {
311 private void destroyPlayer(Player player) {
312 player.avatar.setRotation(170*Math.PI/180, Curve.ELASTIC);
314 graphicEntityModule.commitEntityState(0.5, player.castle);
315 player.castle.setX(player.castle.getX(), Curve.ELASTIC);
316 player.castle.setScaleY(-0.2, Curve.EASE_IN);
319 private void endGame() {
320 if (! gameManager.isGameEnd()) {
321 gameManager.endGame();
323 if (p0.getScore() > p1.getScore()) {
324 gameManager.addToGameSummary(GameManager.formatSuccessMessage(p0.getNicknameToken() + " wins"));
327 else if (p0.getScore() < p1.getScore()) {
328 gameManager.addToGameSummary(GameManager.formatSuccessMessage(p1.getNicknameToken() + " wins"));
331 else if (p0.getScore() < 0) {
332 gameManager.addToGameSummary(GameManager.formatErrorMessage("Everybody loses!"));
337 gameManager.addToGameSummary("Draw.");