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",
36 model.initialStones));
40 gameManager.getPlayer(0).view = view.p0;
41 gameManager.getPlayer(1).view = view.p1;
42 gameManager.setFrameDuration(2000);
46 public void gameTurn(int turn) {
47 // System.err.println("Starting turn " + turn);
51 boolean disqual = false;
52 boolean victory = false;
53 boolean exhausted = false;
56 for (Player player : gameManager.getActivePlayers()) {
57 Model.Player p = player.model;
59 player.sendInputLine(String.format("%d %d %d",
67 // SDK @#%^&! arbitrary sequence point: last input < first output
69 for (Player player : gameManager.getActivePlayers()) {
70 Model.Player p = player.model;
73 int stones = player.getAction();
74 if (stones == 0 && p.getStones() > 0) {
75 if (model.random.nextInt(10) > 0) {
76 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " tried not throwing stones. Fixing that for them because I'm in a good mood today."));
80 throw new InvalidAction("tried not throwing any stone. They were then eaten by a grue.");
83 p.consumeStones(stones);
84 gameManager.addToGameSummary(String.format("%s throws %d stone%s at the troll.", player.getNicknameToken(), stones, stones == 1 ? "" : "s"));
85 delta += player.model.getMultiplier() * stones;
88 player.deactivate(player.getNicknameToken() + " CHEAT");
89 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " cheated. Banning account."));
93 else if (stones > 0) {
94 player.view.animateStones(stones);
95 player.view.updateStoneCounter();
98 catch (InvalidAction e) {
99 player.deactivate(player.getNicknameToken() + " INVALID");
100 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " " + e.getMessage()));
104 catch (NumberFormatException e) {
105 player.deactivate(player.getNicknameToken() + " ERROR");
106 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " provided malformed input!"));
110 catch (TimeoutException e) {
111 gameManager.addToGameSummary(player.getNicknameToken() + " timed out!");
112 player.deactivate(player.getNicknameToken() + " T/O");
117 player.view.displayMessage(player.getMessageString());
122 gameManager.addToGameSummary("Troll walks right.");
123 model.trollPosition++;
125 else if (delta < 0) {
126 gameManager.addToGameSummary("Troll walks left.");
127 model.trollPosition--;
130 gameManager.addToGameSummary("Troll stands still.");
135 for (Player player : gameManager.getActivePlayers()) {
136 player.model.adjustScore(model.trollPosition);
139 if (model.haveWinner()) {
140 int loser = model.getLoser();
141 gameManager.addToGameSummary(GameManager.formatErrorMessage("Troll destroys " + gameManager.getPlayer(loser).getNicknameToken()) + ".");
144 else if (model.exhausted()) exhausted = true;
147 if (disqual || victory || exhausted) endGame();
150 private void endGame() {
151 gameManager.endGame();
153 Player p0 = gameManager.getPlayer(0);
154 Player p1 = gameManager.getPlayer(1);
156 int s0 = p0.getScore();
157 int s1 = p1.getScore();
160 gameManager.addToGameSummary(GameManager.formatSuccessMessage(p0.getNicknameToken() + " wins."));
164 gameManager.addToGameSummary(GameManager.formatSuccessMessage(p1.getNicknameToken() + " wins."));
168 gameManager.addToGameSummary(GameManager.formatErrorMessage("Everybody loses!"));
173 gameManager.addToGameSummary("Draw.");