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.game.GodModeManager;
11 import com.google.inject.Inject;
12 import com.google.inject.Provider;
14 public class Referee extends AbstractReferee {
15 @Inject private GameManager gameManager;
16 @Inject private GodModeManager gm;
18 @Inject private View view;
19 @Inject private Model model;
21 boolean disqual = false;
27 gameManager.getPlayer(0).model = model.p0;
28 gameManager.getPlayer(1).model = model.p1;
30 for (Player p: gameManager.getPlayers()) {
31 p.gameInit(model.roadLength, model.initialStones,
32 model.seed, gm.getSalt());
36 gameManager.getPlayer(0).view = view.p0;
37 gameManager.getPlayer(1).view = view.p1;
40 private void disqualify(Player player, String popup, String message) {
41 player.deactivate(player.getNicknameToken() + " " + popup);
42 player.view.disqualify(message);
47 public void gameTurn(int turn) {
48 // System.err.println("Starting turn " + turn);
52 // Did I mention I hate Java? It didn't *have* to be this ugly!
53 if (disqual) { endGame(); return; }
54 if (model.exhausted()) { finishStones(); return ;}
55 if (model.haveWinner()) { endGame(); return; }
57 for (Player player : gameManager.getActivePlayers()) {
58 player.sendGameTurn();
60 // SDK @#%^&! arbitrary sequence point: last input < first output
62 /* Parse player actions and decide basic disqualifications.
63 * Display their optional message right now: if their action
64 * is ill-formed it could help them debug. Or shame them, at
67 for (Player player : gameManager.getActivePlayers()) {
68 player.receiveGameTurn(); gm.transcend(player);
69 switch (player.type) {
71 disqualify(player, "T/O", "timed out!");
72 player.view.markTimeout();
76 disqualify(player, "INVALID", "provided an ill-formed action");
77 player.view.markIllegal();
81 try { player.model.consumeStones(player.stoneThrow); }
82 catch (Model.Player.ThrewMoreStonesThanHad e) {
83 if (model.random.nextInt(10) > 0) {
84 player.view.threwMoreStonesThanHad();
85 player.stoneThrow = player.model.consumeMaxStones();
88 disqualify(player, "ILLEGAL", "tried to throw more stones than they had. They went into debt trying to provide. The economy tanked, recession and famine ensued; even the troll wouldn't have wanted to bash them anymore. But that's no victory.");
89 player.view.markIllegal();
93 catch (Model.Player.FailedToThrowStonesAndShouldHave e) {
94 if (model.random.nextInt(10) > 0) {
95 player.view.failedToThrowStonesAndShouldHave();
96 player.stoneThrow = player.model.consumeMinStones();
99 disqualify(player, "ILLEGAL", "tried not throwing any stones. They were then eaten by a grue.");
105 player.view.displayMessage(player.messageString);
108 /* Update game model and view, stones' part.
110 * As a special case, the "cheater" (sending out negative
111 * stones) handling is deferred here because we need to update
112 * its view for it to be funny. In the end, they're still
115 * Gather other game end scenarios (actual victory or stone
119 gm.update(gameManager.getPlayers());
120 for (Player player : gameManager.getActivePlayers()) {
121 player.view.throwStones(player.stoneThrow);
122 delta += player.model.getMultiplier() * player.stoneThrow;
124 if (player.stoneThrow < 0) {
125 disqualify(player, "CHEAT", "cheated. Banning account.");
126 player.view.markCheat();
129 if (player.stoneThrow != 0) {
130 player.view.animateStones(player.stoneThrow);
131 player.view.updateStoneCounter();
135 /* Update game model and view, troll part.
137 * If a player cheated, delta is unusable as is.
138 * (Consider the case the player on the right sent
139 * INT_MIN. INT_MIN * (-1) = INT_MIN, so that player
140 * would both glean the stones *and* push the troll away.
141 * It would be unfair to have a cheating player "win"
142 * (earn the opponent castle destruction animation) this
145 boolean cheat0 = gameManager.getPlayer(0).isActive()
146 && gameManager.getPlayer(0).stoneThrow < 0;
147 boolean cheat1 = gameManager.getPlayer(1).isActive()
148 && gameManager.getPlayer(1).stoneThrow < 0;
149 if (cheat0 && cheat1); // here we can actually keep delta's value
150 else if (cheat0) delta = -1;
151 else if (cheat1) delta = 1;
155 view.moveTroll(View.Dir.RIGHT);
157 else if (delta < 0) {
159 view.moveTroll(View.Dir.LEFT);
162 view.moveTroll(View.Dir.STILL);
167 // XXX very similar to main turn pendant
168 private void finishStones() {
169 boolean noStones = true;
171 for (Player player : gameManager.getActivePlayers()) {
172 if (model.haveWinner() && player.getIndex() == model.getLoser())
174 player.stoneThrow = player.model.getStones();
175 player.model.setStones(0);
176 delta += player.stoneThrow * player.model.getMultiplier();
177 player.view.throwStones(player.stoneThrow);
178 if (player.stoneThrow != 0) {
180 player.view.animateStones(player.stoneThrow);
181 player.view.updateStoneCounter();
184 if (noStones) { endGame(); return; }
185 model.moveTroll(delta);
189 private void endGame() {
190 gameManager.endGame();
192 if (model.haveWinner()) {
193 int loser = model.getLoser();
194 gameManager.getPlayer(loser).view.defeat();
197 Player p0 = gameManager.getPlayer(0);
198 Player p1 = gameManager.getPlayer(1);
200 int s0 = p0.getScore();
201 int s1 = p1.getScore();