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;
29 Text trollPositionGauge;
33 switch (new Random(gameManager.getSeed()).nextInt(4)) {
52 trollPosition = (roadLength - 1) / 2;
54 p0 = gameManager.getPlayer(0);
55 p0.setCastlePosition(0);
57 p0.adjustScore(trollPosition);
58 p0.sendInputLine(String.format("%d %d", roadLength - 1, initialStones));
60 p1 = gameManager.getPlayer(1);
61 p1.setCastlePosition(roadLength-1);
63 p1.adjustScore(trollPosition);
64 p1.sendInputLine(String.format("%d %d", roadLength - 1, initialStones));
70 // result in text display, so do last:
71 p0.setStones(initialStones);
72 p1.setStones(initialStones);
74 gameManager.setFrameDuration(2000);
77 private void drawBackground() {
78 graphicEntityModule.createSprite()
79 .setImage("background.png")
83 private void drawPlayer() {
84 for (Player player : gameManager.getPlayers()) {
85 boolean p0 = player.getIndex() == 0;
86 int x = p0 ? 280 : 1920 - 280;
89 Rectangle border1 = graphicEntityModule
96 .setFillColor(player.getColorToken());
98 Rectangle border2 = graphicEntityModule
105 .setFillColor(0xffffff);
107 Text text = graphicEntityModule.createText(player.getNicknameToken())
112 .setFillColor(0x7f3f00)
115 Sprite avatar = graphicEntityModule.createSprite()
119 .setImage(player.getAvatarToken())
124 Text stoneCounter = graphicEntityModule.createText("S")
129 .setFillColor(0x7f3f00)
132 player.castle = graphicEntityModule.createSprite()
133 .setImage("castle.png")
134 .setX(p0 ? 160 : 1920-160)
135 .setY(p0 ? 890 : 880)
140 player.stone = graphicEntityModule.createText()
143 .setFillColor(0x12322a)
147 player.hud = graphicEntityModule.createGroup(border1, border2, text, avatar, stoneCounter, player.castle);
148 player.stoneCounter = stoneCounter;
152 private void drawTroll() {
153 troll = graphicEntityModule.createSprite()
154 .setImage("troll.png")
160 trollPositionGauge = graphicEntityModule.createText()
166 .setFillColor(0xffffff);
169 private void moveTroll() {
170 graphicEntityModule.commitEntityState(0.5, troll, trollPositionGauge);
171 int x0 = p0.castle.getX(), x1 = p1.castle.getX();
172 int y0 = p0.castle.getY(), y1 = p1.castle.getY();
173 troll.setX(x0 + trollPosition * (x1-x0) / (roadLength - 1),
175 troll.setY(y0 + trollPosition * (y1-y0) / (roadLength - 1),
178 trollPositionGauge.setX((trollPositionGauge.getX() + troll.getX()) / 2);
179 int delta = trollPosition - (roadLength - 1) / 2;
181 trollPositionGauge.setText("← " + Math.abs(delta));
183 else if (delta > 0) {
184 trollPositionGauge.setText(Math.abs(delta) + " →");
187 trollPositionGauge.setText("↔");
189 graphicEntityModule.commitEntityState(0.75, trollPositionGauge);
190 trollPositionGauge.setX(troll.getX());
194 public void gameTurn(int turn) {
196 p0.sendInputLine(String.format("%d %d %d", p0.getScore(), p0.getStones(), p1.getStones()));
197 p1.sendInputLine(String.format("%d %d %d", p1.getScore(), p1.getStones(), p0.getStones()));
199 for (Player player : gameManager.getActivePlayers()) {
204 for (Player player : gameManager.getActivePlayers()) {
206 final int stones = player.getAction();
207 player.consumeStones(stones);
208 gameManager.addToGameSummary(String.format("%s throws %d stone%s at the troll.", player.getNicknameToken(), stones, stones == 1 ? "" : "s"));
209 delta += player.getMultiplier() * stones;
212 player.stone.setX(player.castle.getX());
213 player.stone.setY(player.castle.getY() - 100);
214 player.stone.setText(new Integer(stones).toString());
215 player.stone.setAlpha(1);
216 graphicEntityModule.commitEntityState(0, player.stone);
218 int peakX = (player.castle.getX() + troll.getX()) / 2;
220 player.stone.setX(peakX);
221 player.stone.setY(peakY, Curve.EASE_OUT);
222 graphicEntityModule.commitEntityState(0.25, player.stone,
223 player.stoneCounter);
225 player.stone.setX(troll.getX());
226 player.stone.setY(troll.getY() - 50, Curve.EASE_IN);
227 player.stone.setAlpha(0, Curve.EASE_IN);
228 graphicEntityModule.commitEntityState(0.5, player.stone);
231 catch (InvalidAction e) {
232 player.deactivate(player.getNicknameToken() + " INVALID");
233 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " " + e.getMessage()));
237 catch (NumberFormatException e) {
238 player.deactivate(player.getNicknameToken() + " ERROR");
239 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " provided malformed input!"));
243 catch (TimeoutException e) {
244 gameManager.addToGameSummary(player.getNicknameToken() + " timed out!");
245 player.deactivate(player.getNicknameToken() + " T/O");
252 gameManager.addToGameSummary("Troll walks right.");
256 else if (delta < 0) {
257 gameManager.addToGameSummary("Troll walks left.");
262 gameManager.addToGameSummary("Troll stands still.");
265 for (Player player : gameManager.getActivePlayers()) {
266 player.adjustScore(trollPosition);
267 if (trollPosition == player.getCastlePosition()) {
268 gameManager.addToGameSummary(GameManager.formatErrorMessage("Troll destroys " + player.getNicknameToken()));
274 private void endGame() {
275 gameManager.endGame();
277 if (p0.getScore() > p1.getScore()) {
278 gameManager.addToGameSummary(GameManager.formatSuccessMessage(p0.getNicknameToken() + " wins"));
279 p1.hud.setAlpha(0.3);
281 else if (p0.getScore() < p1.getScore()) {
282 gameManager.addToGameSummary(GameManager.formatSuccessMessage(p1.getNicknameToken() + " wins"));
283 p0.hud.setAlpha(0.3);
286 gameManager.addToGameSummary(GameManager.formatErrorMessage("Everybody loses!"));
287 p0.hud.setAlpha(0.3);
288 p1.hud.setAlpha(0.3);