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 - 1) / 2;
57 p0 = gameManager.getPlayer(0);
58 p0.setCastlePosition(0);
60 p0.adjustScore(trollPosition);
61 p0.sendInputLine(String.format("%d %d", roadLength - 1, initialStones));
63 p1 = gameManager.getPlayer(1);
64 p1.setCastlePosition(roadLength-1);
66 p1.adjustScore(trollPosition);
67 p1.sendInputLine(String.format("%d %d", roadLength - 1, 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.castle = graphicEntityModule.createSprite()
138 .setImage("castle.png")
139 .setTint(player.getColorToken())
140 .setX(p0 ? 160 : 1920-160)
141 .setY(p0 ? 890 : 880)
145 .setScaleX(p0 ? 1 : -1);
147 player.stone = graphicEntityModule.createText()
150 .setFillColor(0x12322a)
157 private void drawTroll() {
158 troll = graphicEntityModule.createSprite()
159 .setImage("troll.png")
165 trollPositionGauge = graphicEntityModule.createText()
171 .setFillColor(0xffffff);
174 private void moveTroll() {
175 graphicEntityModule.commitEntityState(0.5, troll, trollPositionGauge);
176 int x0 = p0.castle.getX(), x1 = p1.castle.getX();
177 int y0 = p0.castle.getY(), y1 = p1.castle.getY();
178 troll.setX(x0 + trollPosition * (x1-x0) / (roadLength - 1),
180 troll.setY(y0 + trollPosition * (y1-y0) / (roadLength - 1),
183 trollPositionGauge.setX((trollPositionGauge.getX() + troll.getX()) / 2);
184 int delta = trollPosition - (roadLength - 1) / 2;
186 trollPositionGauge.setText("← " + Math.abs(delta));
188 else if (delta > 0) {
189 trollPositionGauge.setText(Math.abs(delta) + " →");
192 trollPositionGauge.setText("↔");
194 graphicEntityModule.commitEntityState(0.75, trollPositionGauge);
195 trollPositionGauge.setX(troll.getX());
199 public void gameTurn(int turn) {
201 p0.sendInputLine(String.format("%d %d %d", p0.getScore(), p0.getStones(), p1.getStones()));
202 p1.sendInputLine(String.format("%d %d %d", p1.getScore(), p1.getStones(), p0.getStones()));
204 for (Player player : gameManager.getActivePlayers()) {
209 for (Player player : gameManager.getActivePlayers()) {
211 int stones = player.getAction();
212 if (stones == 0 && player.getStones() > 0) {
213 if (random.nextInt(10) > 0) {
214 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " tried not throwing stones. Fixing that for them because I'm in a good mood today."));
218 throw new InvalidAction("tried not throwing any stone. They were then eaten by a grue.");
221 player.consumeStones(stones);
222 gameManager.addToGameSummary(String.format("%s throws %d stone%s at the troll.", player.getNicknameToken(), stones, stones == 1 ? "" : "s"));
223 delta += player.getMultiplier() * stones;
226 player.deactivate(player.getNicknameToken() + " CHEAT");
227 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " cheated. Banning account."));
231 else if (stones > 0) {
232 player.stone.setX(player.castle.getX());
233 player.stone.setY(player.castle.getY() - 100);
234 player.stone.setText(new Integer(stones).toString());
235 player.stone.setAlpha(1);
236 graphicEntityModule.commitEntityState(0, player.stone);
238 int peakX = (player.castle.getX() + troll.getX()) / 2;
240 player.stone.setX(peakX);
241 player.stone.setY(peakY, Curve.EASE_OUT);
242 graphicEntityModule.commitEntityState(0.25, player.stone,
243 player.stoneCounter);
245 player.stone.setX(troll.getX());
246 player.stone.setY(troll.getY() - 50, Curve.EASE_IN);
247 player.stone.setAlpha(0, Curve.EASE_IN);
248 graphicEntityModule.commitEntityState(0.5, player.stone);
251 catch (InvalidAction e) {
252 player.deactivate(player.getNicknameToken() + " INVALID");
253 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " " + e.getMessage()));
257 catch (NumberFormatException e) {
258 player.deactivate(player.getNicknameToken() + " ERROR");
259 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " provided malformed input!"));
263 catch (TimeoutException e) {
264 gameManager.addToGameSummary(player.getNicknameToken() + " timed out!");
265 player.deactivate(player.getNicknameToken() + " T/O");
272 gameManager.addToGameSummary("Troll walks right.");
276 else if (delta < 0) {
277 gameManager.addToGameSummary("Troll walks left.");
282 gameManager.addToGameSummary("Troll stands still.");
285 for (Player player : gameManager.getActivePlayers()) {
286 player.adjustScore(trollPosition);
287 if (trollPosition == player.getCastlePosition()) {
288 gameManager.addToGameSummary(GameManager.formatErrorMessage("Troll destroys " + player.getNicknameToken()));
293 if (p0.getStones() <= 0 && p1.getStones() <= 0) {
298 private void destroyPlayer(Player player) {
299 player.avatar.setRotation(170*Math.PI/180, Curve.ELASTIC);
301 graphicEntityModule.commitEntityState(0.5, player.castle);
302 player.castle.setX(player.castle.getX(), Curve.ELASTIC);
303 player.castle.setScaleY(-0.2, Curve.EASE_IN);
306 private void endGame() {
307 gameManager.endGame();
309 if (p0.getScore() > p1.getScore()) {
310 gameManager.addToGameSummary(GameManager.formatSuccessMessage(p0.getNicknameToken() + " wins"));
313 else if (p0.getScore() < p1.getScore()) {
314 gameManager.addToGameSummary(GameManager.formatSuccessMessage(p1.getNicknameToken() + " wins"));
317 else if (p0.getScore() < 0) {
318 gameManager.addToGameSummary(GameManager.formatErrorMessage("Everybody loses!"));
323 gameManager.addToGameSummary("Draw.");