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;
31 Text trollPositionGauge;
35 random = new Random(gameManager.getSeed());
36 switch (random.nextInt(4)) {
55 trollPosition = roadLength / 2;
57 p0 = gameManager.getPlayer(0);
58 p0.setCastlePosition(0);
60 p0.adjustScore(trollPosition);
61 p0.sendInputLine(String.format("%d %d", roadLength, initialStones));
63 p1 = gameManager.getPlayer(1);
64 p1.setCastlePosition(roadLength-1);
66 p1.adjustScore(trollPosition);
67 p1.sendInputLine(String.format("%d %d", roadLength, initialStones));
73 // result in text display, so do last:
74 p0.setStones(initialStones);
75 p1.setStones(initialStones);
77 gameManager.setFrameDuration(2000);
80 private void drawBackground() {
81 graphicEntityModule.createSprite()
82 .setImage("background.png")
86 private void drawPlayer() {
87 for (Player player : gameManager.getPlayers()) {
88 boolean p0 = player.getIndex() == 0;
89 int x = p0 ? 280 : 1920 - 280;
92 Rectangle border1 = graphicEntityModule
99 .setFillColor(player.getColorToken());
101 Rectangle border2 = graphicEntityModule
108 .setFillColor(0xffffff);
110 Sprite avatarSprite = graphicEntityModule.createSprite()
114 .setImage(player.getAvatarToken())
119 player.avatar = graphicEntityModule.createGroup(border1, border2, avatarSprite);
121 Text text = graphicEntityModule.createText(player.getNicknameToken())
126 .setFillColor(0x7f3f00)
129 player.stoneCounter = graphicEntityModule.createText("S")
134 .setFillColor(0x7f3f00)
137 player.message = graphicEntityModule.createText()
138 .setX(p0 ? 15 : 1920-15)
142 .setStrokeColor(0x000000)
143 .setFillColor(0xffbf7f)
144 .setAnchorX(p0 ? 0 : 1)
147 player.castle = graphicEntityModule.createSprite()
148 .setImage("castle.png")
149 .setTint(player.getColorToken())
150 .setX(p0 ? 160 : 1920-160)
151 .setY(p0 ? 890 : 880)
155 .setScaleX(p0 ? 1 : -1);
157 player.stone = graphicEntityModule.createText()
160 .setFillColor(0x12322a)
166 private void drawTroll() {
167 troll = graphicEntityModule.createSprite()
168 .setImage("troll.png")
174 trollPositionGauge = graphicEntityModule.createText()
180 .setFillColor(0xffffff);
183 private void moveTroll() {
184 graphicEntityModule.commitEntityState(0.5, troll, trollPositionGauge);
185 int x0 = p0.castle.getX(), x1 = p1.castle.getX();
186 int y0 = p0.castle.getY(), y1 = p1.castle.getY();
187 troll.setX(x0 + trollPosition * (x1-x0) / roadLength,
189 troll.setY(y0 + trollPosition * (y1-y0) / roadLength,
192 trollPositionGauge.setX((trollPositionGauge.getX() + troll.getX()) / 2);
193 int delta = trollPosition - roadLength / 2;
195 trollPositionGauge.setText("← " + Math.abs(delta));
197 else if (delta > 0) {
198 trollPositionGauge.setText(Math.abs(delta) + " →");
201 trollPositionGauge.setText("↔");
203 graphicEntityModule.commitEntityState(0.75, trollPositionGauge);
204 trollPositionGauge.setX(troll.getX());
208 public void gameTurn(int turn) {
210 p0.sendInputLine(String.format("%d %d %d", p0.getScore(), p0.getStones(), p1.getStones()));
211 p1.sendInputLine(String.format("%d %d %d", p1.getScore(), p1.getStones(), p0.getStones()));
213 for (Player player : gameManager.getActivePlayers()) {
218 for (Player player : gameManager.getActivePlayers()) {
220 int stones = player.getAction();
221 if (stones == 0 && player.getStones() > 0) {
222 if (random.nextInt(10) > 0) {
223 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " tried not throwing stones. Fixing that for them because I'm in a good mood today."));
227 throw new InvalidAction("tried not throwing any stone. They were then eaten by a grue.");
230 player.consumeStones(stones);
231 gameManager.addToGameSummary(String.format("%s throws %d stone%s at the troll.", player.getNicknameToken(), stones, stones == 1 ? "" : "s"));
232 delta += player.getMultiplier() * stones;
235 player.deactivate(player.getNicknameToken() + " CHEAT");
236 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " cheated. Banning account."));
240 else if (stones > 0) {
241 player.stone.setX(player.castle.getX());
242 player.stone.setY(player.castle.getY() - 100);
243 player.stone.setText(new Integer(stones).toString());
244 player.stone.setAlpha(1);
245 graphicEntityModule.commitEntityState(0, player.stone);
247 int peakX = (player.castle.getX() + troll.getX()) / 2;
249 player.stone.setX(peakX);
250 player.stone.setY(peakY, Curve.EASE_OUT);
251 graphicEntityModule.commitEntityState(0.25, player.stone,
252 player.stoneCounter);
254 player.stone.setX(troll.getX());
255 player.stone.setY(troll.getY() - 50, Curve.EASE_IN);
256 player.stone.setAlpha(0, Curve.EASE_IN);
257 graphicEntityModule.commitEntityState(0.5, player.stone);
260 catch (InvalidAction e) {
261 player.deactivate(player.getNicknameToken() + " INVALID");
262 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " " + e.getMessage()));
266 catch (NumberFormatException e) {
267 player.deactivate(player.getNicknameToken() + " ERROR");
268 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " provided malformed input!"));
272 catch (TimeoutException e) {
273 gameManager.addToGameSummary(player.getNicknameToken() + " timed out!");
274 player.deactivate(player.getNicknameToken() + " T/O");
280 .setText(player.getMessageString());
281 // .setAnchorX(/*player == p0 ? 0 : */ 1);
282 graphicEntityModule.commitEntityState(0, player.message);
286 gameManager.addToGameSummary("Troll walks right.");
290 else if (delta < 0) {
291 gameManager.addToGameSummary("Troll walks left.");
296 gameManager.addToGameSummary("Troll stands still.");
299 for (Player player : gameManager.getActivePlayers()) {
300 player.adjustScore(trollPosition);
301 if (trollPosition == player.getCastlePosition()) {
302 gameManager.addToGameSummary(GameManager.formatErrorMessage("Troll destroys " + player.getNicknameToken()));
307 if (p0.getStones() <= 0 && p1.getStones() <= 0) {
312 private void destroyPlayer(Player player) {
313 player.avatar.setRotation(170*Math.PI/180, Curve.ELASTIC);
315 graphicEntityModule.commitEntityState(0.5, player.castle);
316 player.castle.setX(player.castle.getX(), Curve.ELASTIC);
317 player.castle.setScaleY(-0.2, Curve.EASE_IN);
320 private void endGame() {
321 if (! gameManager.isGameEnd()) {
322 gameManager.endGame();
324 if (p0.getScore() > p1.getScore()) {
325 gameManager.addToGameSummary(GameManager.formatSuccessMessage(p0.getNicknameToken() + " wins"));
328 else if (p0.getScore() < p1.getScore()) {
329 gameManager.addToGameSummary(GameManager.formatSuccessMessage(p1.getNicknameToken() + " wins"));
332 else if (p0.getScore() < 0) {
333 gameManager.addToGameSummary(GameManager.formatErrorMessage("Everybody loses!"));
338 gameManager.addToGameSummary("Draw.");