1 package com.codingame.game;
3 import java.util.Random;
4 import java.util.Properties;
6 import com.codingame.gameengine.core.GameManager;
7 import com.codingame.gameengine.core.MultiplayerGameManager;
8 import com.google.inject.Inject;
11 @Inject private MultiplayerGameManager<com.codingame.game.Player> gameManager;
12 @Inject private LeagueManager league;
21 com.codingame.game.Player gp;
35 private int castlePosition;
36 public int getCastlePosition() { return castlePosition; }
37 public void setCastlePosition(int pos) { castlePosition = pos; }
39 private int multiplier;
40 public int getMultiplier() { return multiplier; }
41 public void setMultiplier(int m) { multiplier = m; }
43 class FailedToThrowStonesAndShouldHave extends Exception {}
44 class ThrewMoreStonesThanHad extends Exception {}
47 public int getStones() { return stones; }
48 public void consumeStones(int n)
49 throws ThrewMoreStonesThanHad,
50 FailedToThrowStonesAndShouldHave
53 throw new ThrewMoreStonesThanHad();
55 if (n == 0 && stones > 0) {
56 throw new FailedToThrowStonesAndShouldHave();
58 setStones(stones - n);
60 public int consumeMaxStones() {
65 public int consumeMinStones() {
67 throw new Error("Internal error: tried to consume min stones on an empty heap.");
72 public void setStones(int n) {
75 public int getOppStones() {
76 return p0.stones + p1.stones - stones;
79 public void adjustScore(int trollPosition) {
81 gp.setScore(Math.abs(castlePosition - trollPosition));
85 public int getTrollDistance() {
86 return Math.abs(castlePosition - trollPosition);
91 seed = gameManager.getSeed();
92 random = new Random(seed);
94 switch(league.mapLevel) {
100 int i = random.nextInt(4);
121 roadLength = 2 * (3 + random.nextInt(7-3+1));
122 initialStones = 15 + random.nextInt(50-15+1);
126 Properties ps = gameManager.getGameParameters();
127 String buf = ps.getProperty("roadLength");
130 int i = Integer.parseInt(buf);
131 if (i < 0 || i > 20 || (i & 1) != 0) {
132 gameManager.addToGameSummary(GameManager.formatErrorMessage("Ignoring invalid road length: " + buf));
136 gameManager.addToGameSummary("Road length overridden to " + i);
139 catch(NumberFormatException e) {
140 gameManager.addToGameSummary(GameManager.formatErrorMessage("Ill-formed road length: " + buf));
143 ps.setProperty("roadLength", new Integer(roadLength).toString());
145 buf = ps.getProperty("initialStones");
148 int i = Integer.parseInt(buf);
150 gameManager.addToGameSummary(GameManager.formatErrorMessage("Ignoring invalid initial stone count: " + buf));
154 gameManager.addToGameSummary("Initial stone count overridden to " + buf);
157 catch (NumberFormatException e) {
158 gameManager.addToGameSummary(GameManager.formatErrorMessage("Ill-formed initial stone count: " + buf));
161 ps.setProperty("initialStones", new Integer(initialStones).toString());
163 trollPosition = roadLength / 2;
166 p0.gp = gameManager.getPlayer(0);
167 p0.setCastlePosition(0);
169 p0.adjustScore(trollPosition);
170 p0.setStones(initialStones);
173 p1.gp = gameManager.getPlayer(1);
174 p1.setCastlePosition(roadLength);
175 p1.setMultiplier(-1);
176 p1.adjustScore(trollPosition);
177 p1.setStones(initialStones);
180 void moveTroll(int delta) {
181 trollPosition += delta;
182 if (trollPosition <= 0) {
186 if (trollPosition >= roadLength) {
187 trollPosition = roadLength;
191 p0.adjustScore(trollPosition);
192 p1.adjustScore(trollPosition);
195 private Integer winner;
196 boolean haveWinner() {
197 return winner != null;
200 int getWinner() { return winner; }
201 int getLoser() { return 1 - winner; }
203 boolean exhausted() {
204 return p0.getStones() <= 0 || p1.getStones() <= 0;