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,
35 gameManager.getSeed());
39 gameManager.getPlayer(0).view = view.p0;
40 gameManager.getPlayer(1).view = view.p1;
41 gameManager.setFrameDuration(2000);
44 private void disqualify(Player player, String popup, String message) {
45 player.deactivate(player.getNicknameToken() + " " + popup);
46 gameManager.addToGameSummary(GameManager.formatErrorMessage(player.getNicknameToken() + " " + message));
51 public void gameTurn(int turn) {
52 // System.err.println("Starting turn " + turn);
56 for (Player player : gameManager.getActivePlayers()) {
57 player.sendGameTurn();
59 // SDK @#%^&! arbitrary sequence point: last input < first output
61 /* Parse player actions and decide basic disqualifications.
62 * Display their optional message right now: if their action
63 * is ill-formed it could help them debug. Or shame them, at
66 boolean disqual = false;
67 for (Player player : gameManager.getActivePlayers()) {
68 player.receiveGameTurn();
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.
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 boolean victory = false;
120 boolean exhausted = false;
121 for (Player player : gameManager.getActivePlayers()) {
122 player.view.throwStones(player.stoneThrow);
123 delta += player.model.getMultiplier() * player.stoneThrow;
125 if (player.stoneThrow < 0) {
126 disqualify(player, "CHEAT", "cheated. Banning account.");
127 player.view.markCheat();
130 if (player.stoneThrow != 0) {
131 player.view.animateStones(player.stoneThrow);
132 player.view.updateStoneCounter();
136 /* If a player cheated, delta is unusable as is.
137 * (Consider the case the player on the right sent
138 * INT_MIN. INT_MIN * (-1) = INT_MIN, so that player
139 * would both glean the stones *and* push the troll away.
140 * It would be unfair to have a cheating player "win"
141 * (earn the opponent castle destruction animation) this
144 boolean cheat0 = gameManager.getPlayer(0).isActive()
145 && gameManager.getPlayer(0).stoneThrow < 0;
146 boolean cheat1 = gameManager.getPlayer(1).isActive()
147 && gameManager.getPlayer(1).stoneThrow < 0;
148 if (cheat0 && cheat1); // here we can actually keep delta's value
149 else if (cheat0) delta = -1;
150 else if (cheat1) delta = 1;
153 model.trollPosition++;
154 view.moveTroll(View.Dir.RIGHT);
156 else if (delta < 0) {
157 model.trollPosition--;
158 view.moveTroll(View.Dir.LEFT);
161 view.moveTroll(View.Dir.STILL);
165 for (Player player : gameManager.getActivePlayers()) {
166 player.model.adjustScore(model.trollPosition);
169 if (model.haveWinner()) {
170 int loser = model.getLoser();
171 gameManager.getPlayer(loser).view.destroy();
174 else if (model.exhausted()) exhausted = true;
176 if (disqual || victory || exhausted) endGame();
179 private void endGame() {
180 gameManager.endGame();
182 Player p0 = gameManager.getPlayer(0);
183 Player p1 = gameManager.getPlayer(1);
185 int s0 = p0.getScore();
186 int s1 = p1.getScore();