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.gameInit(model.roadLength, model.initialStones);
38 gameManager.getPlayer(0).view = view.p0;
39 gameManager.getPlayer(1).view = view.p1;
40 gameManager.setFrameDuration(2000);
43 private void disqualify(Player player, String popup, String message) {
44 player.deactivate(player.getNicknameToken() + " " + popup);
45 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " " + message));
50 public void gameTurn(int turn) {
51 // System.err.println("Starting turn " + turn);
55 for (Player player : gameManager.getActivePlayers()) {
56 player.sendGameTurn();
58 // SDK @#%^&! arbitrary sequence point: last input < first output
60 /* Parse player actions and decide basic disqualifications.
61 * Display their optional message right now: if their action
62 * is ill-formed it could help them debug. Or shame them, at
65 boolean disqual = false;
66 for (Player player : gameManager.getActivePlayers()) {
67 player.receiveGameTurn();
68 switch (player.type) {
70 disqualify(player, "T/O", "timed out!");
74 disqualify(player, "INVALID", "provided an ill-formed action");
78 try { player.model.consumeStones(player.stoneThrow); }
79 catch (Model.Player.ThrewMoreStonesThanHad e) {
80 if (model.random.nextInt(10) > 0) {
81 player.view.threwMoreStonesThanHad();
82 player.stoneThrow = player.model.consumeMaxStones();
85 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 catch (Model.Player.FailedToThrowStonesAndShouldHave e) {
90 if (model.random.nextInt(10) > 0) {
91 player.view.failedToThrowStonesAndShouldHave();
92 player.stoneThrow = player.model.consumeMinStones();
95 disqualify(player, "ILLEGAL", "tried not throwing any stones. They were then eaten by a grue.");
101 player.view.displayMessage(player.messageString);
104 /* Update game model and view.
106 * As a special case, the "cheater" (sending out negative
107 * stones) handling is deferred here because we need to update
108 * its view for it to be funny. In the end, they're still
111 * Gather other game end scenarios (actual victory or stone
115 boolean victory = false;
116 boolean exhausted = false;
118 for (Player player : gameManager.getActivePlayers()) {
119 player.view.throwStones(player.stoneThrow);
120 delta += player.model.getMultiplier() * player.stoneThrow;
122 if (player.stoneThrow < 0) {
123 disqualify(player, "CHEAT", "cheated. Banning account.");
126 if (player.stoneThrow != 0) {
127 player.view.animateStones(player.stoneThrow);
128 player.view.updateStoneCounter();
132 /* If a player cheated, delta is unusable as is.
133 * (Consider the case the player on the right sent
134 * INT_MIN. INT_MIN * (-1) = INT_MIN, so that player
135 * would both glean the stones *and* push the troll away.
136 * It would be unfair to have a cheating player "win"
137 * (earn the opponent castle destruction animation) this
140 boolean cheat0 = gameManager.getPlayer(0).stoneThrow < 0;
141 boolean cheat1 = gameManager.getPlayer(1).stoneThrow < 0;
142 if (cheat0 && cheat1); // here we can actually keep delta's value
143 else if (cheat0) delta = -1;
144 else if (cheat1) delta = 1;
147 model.trollPosition++;
148 view.moveTroll(View.Dir.RIGHT);
150 else if (delta < 0) {
151 model.trollPosition--;
152 view.moveTroll(View.Dir.LEFT);
155 view.moveTroll(View.Dir.STILL);
159 for (Player player : gameManager.getActivePlayers()) {
160 player.model.adjustScore(model.trollPosition);
163 if (model.haveWinner()) {
164 int loser = model.getLoser();
165 gameManager.getPlayer(loser).view.destroy();
168 else if (model.exhausted()) exhausted = true;
171 if (disqual || victory || exhausted) endGame();
174 private void endGame() {
175 gameManager.endGame();
177 Player p0 = gameManager.getPlayer(0);
178 Player p1 = gameManager.getPlayer(1);
180 int s0 = p0.getScore();
181 int s1 = p1.getScore();