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 @Inject private View view;
26 Model model = new Model();
33 model.init(gameManager.getSeed());
35 p0 = gameManager.getPlayer(0);
36 p0.setCastlePosition(0);
38 p0.adjustScore(model.trollPosition);
39 p0.sendInputLine(String.format("%d %d", model.roadLength, model.initialStones));
41 p1 = gameManager.getPlayer(1);
42 p1.setCastlePosition(model.roadLength);
44 p1.adjustScore(model.trollPosition);
45 p1.sendInputLine(String.format("%d %d", model.roadLength, model.initialStones));
50 // result in text display, so do last:
51 p0.setStones(model.initialStones);
52 p1.setStones(model.initialStones);
54 gameManager.setFrameDuration(2000); // XXX
57 private void drawPlayer() {
58 for (Player player : gameManager.getPlayers()) {
59 boolean p0 = player.getIndex() == 0;
60 int x = p0 ? 280 : 1920 - 280;
63 Rectangle border1 = graphicEntityModule
70 .setFillColor(player.getColorToken());
72 Rectangle border2 = graphicEntityModule
79 .setFillColor(0xffffff);
81 Sprite avatarSprite = graphicEntityModule.createSprite()
85 .setImage(player.getAvatarToken())
90 player.avatar = graphicEntityModule.createGroup(border1, border2, avatarSprite);
92 Text text = graphicEntityModule.createText(player.getNicknameToken())
97 .setFillColor(0x7f3f00)
100 player.stoneCounter = graphicEntityModule.createText("S")
105 .setFillColor(0x7f3f00)
108 player.message = graphicEntityModule.createText()
109 .setX(p0 ? 15 : 1920-15)
113 .setStrokeColor(0x000000)
114 .setFillColor(0xffbf7f)
115 .setAnchorX(p0 ? 0 : 1)
118 player.castle = graphicEntityModule.createSprite()
119 .setImage("castle.png")
120 .setTint(player.getColorToken())
121 .setX(p0 ? 160 : 1920-160)
122 .setY(p0 ? 890 : 880)
126 .setScaleX(p0 ? 1 : -1);
128 player.stone = graphicEntityModule.createText()
131 .setFillColor(0x12322a)
137 private void moveTroll() {
138 graphicEntityModule.commitEntityState(0.5, view.troll, view.trollPositionGauge);
139 int x0 = p0.castle.getX(), x1 = p1.castle.getX();
140 int y0 = p0.castle.getY(), y1 = p1.castle.getY();
141 view.troll.setX(x0 + model.trollPosition * (x1-x0) / model.roadLength,
143 view.troll.setY(y0 + model.trollPosition * (y1-y0) / model.roadLength,
146 view.trollPositionGauge.setX((view.trollPositionGauge.getX() + view.troll.getX()) / 2);
147 int delta = model.trollPosition - model.roadLength / 2;
149 view.trollPositionGauge.setText("← " + Math.abs(delta));
151 else if (delta > 0) {
152 view.trollPositionGauge.setText(Math.abs(delta) + " →");
155 view.trollPositionGauge.setText("↔");
157 graphicEntityModule.commitEntityState(0.75, view.trollPositionGauge);
158 view.trollPositionGauge.setX(view.troll.getX());
162 public void gameTurn(int turn) {
164 p0.sendInputLine(String.format("%d %d %d", p0.getScore(), p0.getStones(), p1.getStones()));
165 p1.sendInputLine(String.format("%d %d %d", p1.getScore(), p1.getStones(), p0.getStones()));
167 for (Player player : gameManager.getActivePlayers()) {
172 for (Player player : gameManager.getActivePlayers()) {
174 int stones = player.getAction();
175 if (stones == 0 && player.getStones() > 0) {
176 if (model.random.nextInt(10) > 0) {
177 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " tried not throwing stones. Fixing that for them because I'm in a good mood today."));
181 throw new InvalidAction("tried not throwing any stone. They were then eaten by a grue.");
184 player.consumeStones(stones);
185 gameManager.addToGameSummary(String.format("%s throws %d stone%s at the troll.", player.getNicknameToken(), stones, stones == 1 ? "" : "s"));
186 delta += player.getMultiplier() * stones;
189 player.deactivate(player.getNicknameToken() + " CHEAT");
190 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " cheated. Banning account."));
194 else if (stones > 0) {
195 player.stone.setX(player.castle.getX());
196 player.stone.setY(player.castle.getY() - 100);
197 player.stone.setText(new Integer(stones).toString());
198 player.stone.setAlpha(1);
199 graphicEntityModule.commitEntityState(0, player.stone);
201 int peakX = (player.castle.getX() + view.troll.getX()) / 2;
203 player.stone.setX(peakX);
204 player.stone.setY(peakY, Curve.EASE_OUT);
205 graphicEntityModule.commitEntityState(0.25, player.stone,
206 player.stoneCounter);
208 player.stone.setX(view.troll.getX());
209 player.stone.setY(view.troll.getY() - 50, Curve.EASE_IN);
210 player.stone.setAlpha(0, Curve.EASE_IN);
211 graphicEntityModule.commitEntityState(0.5, player.stone);
214 catch (InvalidAction e) {
215 player.deactivate(player.getNicknameToken() + " INVALID");
216 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " " + e.getMessage()));
220 catch (NumberFormatException e) {
221 player.deactivate(player.getNicknameToken() + " ERROR");
222 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " provided malformed input!"));
226 catch (TimeoutException e) {
227 gameManager.addToGameSummary(player.getNicknameToken() + " timed out!");
228 player.deactivate(player.getNicknameToken() + " T/O");
234 .setText(player.getMessageString());
235 // .setAnchorX(/*player == p0 ? 0 : */ 1);
236 graphicEntityModule.commitEntityState(0, player.message);
240 gameManager.addToGameSummary("Troll walks right.");
241 model.trollPosition++;
244 else if (delta < 0) {
245 gameManager.addToGameSummary("Troll walks left.");
246 model.trollPosition--;
250 gameManager.addToGameSummary("Troll stands still.");
253 for (Player player : gameManager.getActivePlayers()) {
254 player.adjustScore(model.trollPosition);
255 if (model.trollPosition == player.getCastlePosition()) {
256 gameManager.addToGameSummary(GameManager.formatErrorMessage("Troll destroys " + player.getNicknameToken()));
261 if (p0.getStones() <= 0 && p1.getStones() <= 0) {
266 private void destroyPlayer(Player player) {
267 player.avatar.setRotation(170*Math.PI/180, Curve.ELASTIC);
269 graphicEntityModule.commitEntityState(0.5, player.castle);
270 player.castle.setX(player.castle.getX(), Curve.ELASTIC);
271 player.castle.setScaleY(-0.2, Curve.EASE_IN);
274 private void endGame() {
275 if (! gameManager.isGameEnd()) {
276 gameManager.endGame();
278 if (p0.getScore() > p1.getScore()) {
279 gameManager.addToGameSummary(GameManager.formatSuccessMessage(p0.getNicknameToken() + " wins"));
282 else if (p0.getScore() < p1.getScore()) {
283 gameManager.addToGameSummary(GameManager.formatSuccessMessage(p1.getNicknameToken() + " wins"));
286 else if (p0.getScore() < 0) {
287 gameManager.addToGameSummary(GameManager.formatErrorMessage("Everybody loses!"));
292 gameManager.addToGameSummary("Draw.");