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));
39 gameManager.getPlayer(0).view = view.p0;
40 gameManager.getPlayer(1).view = view.p1;
42 gameManager.setFrameDuration(2000); // XXX
46 public void gameTurn(int turn) {
47 // System.err.println("Starting turn " + turn);
49 boolean disqual = false;
50 boolean victory = false;
51 boolean exhausted = false;
54 for (Player player : gameManager.getActivePlayers()) {
55 Model.Player p = player.model;
57 int trollDistance = p.getTrollDistance();
58 int stones = p.getStones();
59 int oppStones = p.getOppStones();
60 player.sendInputLine(String.format("%d %d %d", trollDistance, stones, oppStones));
65 // SDK @#%^&! arbitrary sequence point: last input < first output
67 for (Player player : gameManager.getActivePlayers()) {
68 Model.Player p = player.model;
71 int stones = player.getAction();
72 if (stones == 0 && p.getStones() > 0) {
73 if (model.random.nextInt(10) > 0) {
74 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " tried not throwing stones. Fixing that for them because I'm in a good mood today."));
78 throw new InvalidAction("tried not throwing any stone. They were then eaten by a grue.");
81 p.consumeStones(stones);
82 gameManager.addToGameSummary(String.format("%s throws %d stone%s at the troll.", player.getNicknameToken(), stones, stones == 1 ? "" : "s"));
83 delta += player.model.getMultiplier() * stones;
86 player.deactivate(player.getNicknameToken() + " CHEAT");
87 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " cheated. Banning account."));
91 else if (stones > 0) {
92 player.view.animateStones(stones);
93 player.view.updateStoneCounter();
96 catch (InvalidAction e) {
97 player.deactivate(player.getNicknameToken() + " INVALID");
98 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " " + e.getMessage()));
102 catch (NumberFormatException e) {
103 player.deactivate(player.getNicknameToken() + " ERROR");
104 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " provided malformed input!"));
108 catch (TimeoutException e) {
109 gameManager.addToGameSummary(player.getNicknameToken() + " timed out!");
110 player.deactivate(player.getNicknameToken() + " T/O");
115 player.view.displayMessage(player.getMessageString());
120 gameManager.addToGameSummary("Troll walks right.");
121 model.trollPosition++;
123 else if (delta < 0) {
124 gameManager.addToGameSummary("Troll walks left.");
125 model.trollPosition--;
128 gameManager.addToGameSummary("Troll stands still.");
133 for (Player player : gameManager.getActivePlayers()) {
134 player.model.adjustScore(model.trollPosition);
137 if (model.haveWinner()) {
138 int winner = model.getWinner();
139 gameManager.addToGameSummary(GameManager.formatErrorMessage("Troll destroys " + gameManager.getPlayer(winner).getNicknameToken()));
142 else if (model.exhausted()) exhausted = true;
145 if (disqual || victory || exhausted) endGame();
148 private void endGame() {
149 gameManager.endGame();
151 Player p0 = gameManager.getPlayer(0);
152 Player p1 = gameManager.getPlayer(1);
154 int s0 = p0.getScore();
155 int s1 = p1.getScore();
158 gameManager.addToGameSummary(GameManager.formatSuccessMessage(p0.getNicknameToken() + " wins"));
162 gameManager.addToGameSummary(GameManager.formatSuccessMessage(p1.getNicknameToken() + " wins"));
166 gameManager.addToGameSummary(GameManager.formatErrorMessage("Everybody loses!"));
171 gameManager.addToGameSummary("Draw.");