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;
25 @Inject private Model model;
29 model.init(gameManager.getSeed());
30 gameManager.getPlayer(0).model = model.p0;
31 gameManager.getPlayer(1).model = model.p1;
33 for (Player p : gameManager.getPlayers()) {
34 p.sendInputLine(String.format("%d %d", model.roadLength, model.initialStones));
38 gameManager.getPlayer(0).view = view.p0;
39 gameManager.getPlayer(1).view = view.p1;
41 gameManager.setFrameDuration(2000); // XXX
45 public void gameTurn(int turn) {
46 // System.err.println("Starting turn " + turn);
48 boolean disqual = false;
49 boolean victory = false;
50 boolean exhausted = false;
53 for (Player player : gameManager.getActivePlayers()) {
54 Model.Player p = player.model;
56 int trollDistance = p.getTrollDistance();
57 int stones = p.getStones();
58 int oppStones = p.getOppStones();
59 player.sendInputLine(String.format("%d %d %d", trollDistance, stones, oppStones));
64 for (Player player : gameManager.getActivePlayers()) {
65 Model.Player p = player.model;
68 int stones = player.getAction();
69 if (stones == 0 && p.getStones() > 0) {
70 if (model.random.nextInt(10) > 0) {
71 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " tried not throwing stones. Fixing that for them because I'm in a good mood today."));
75 throw new InvalidAction("tried not throwing any stone. They were then eaten by a grue.");
78 p.consumeStones(stones);
79 gameManager.addToGameSummary(String.format("%s throws %d stone%s at the troll.", player.getNicknameToken(), stones, stones == 1 ? "" : "s"));
80 delta += player.model.getMultiplier() * stones;
83 player.deactivate(player.getNicknameToken() + " CHEAT");
84 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " cheated. Banning account."));
88 else if (stones > 0) {
89 player.view.animateStones(stones);
92 catch (InvalidAction e) {
93 player.deactivate(player.getNicknameToken() + " INVALID");
94 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " " + e.getMessage()));
98 catch (NumberFormatException e) {
99 player.deactivate(player.getNicknameToken() + " ERROR");
100 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " provided malformed input!"));
104 catch (TimeoutException e) {
105 gameManager.addToGameSummary(player.getNicknameToken() + " timed out!");
106 player.deactivate(player.getNicknameToken() + " T/O");
111 player.view.displayMessage(player.getMessageString());
116 gameManager.addToGameSummary("Troll walks right.");
117 model.trollPosition++;
119 else if (delta < 0) {
120 gameManager.addToGameSummary("Troll walks left.");
121 model.trollPosition--;
124 gameManager.addToGameSummary("Troll stands still.");
129 for (Player player : gameManager.getActivePlayers()) {
130 player.model.adjustScore(model.trollPosition);
133 if (model.haveWinner()) {
134 int winner = model.getWinner();
135 gameManager.addToGameSummary(GameManager.formatErrorMessage("Troll destroys " + gameManager.getPlayer(winner).getNicknameToken()));
138 else if (model.exhausted()) exhausted = true;
141 if (disqual || victory || exhausted) endGame();
144 private void endGame() {
145 gameManager.endGame();
147 Player p0 = gameManager.getPlayer(0);
148 Player p1 = gameManager.getPlayer(1);
150 int s0 = p0.getScore();
151 int s1 = p1.getScore();
154 gameManager.addToGameSummary(GameManager.formatSuccessMessage(p0.getNicknameToken() + " wins"));
158 gameManager.addToGameSummary(GameManager.formatSuccessMessage(p1.getNicknameToken() + " wins"));
162 gameManager.addToGameSummary(GameManager.formatErrorMessage("Everybody loses!"));
167 gameManager.addToGameSummary("Draw.");