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;
20 com.codingame.game.Player gp;
34 private int castlePosition;
35 public int getCastlePosition() { return castlePosition; }
36 public void setCastlePosition(int pos) { castlePosition = pos; }
38 private int multiplier;
39 public int getMultiplier() { return multiplier; }
40 public void setMultiplier(int m) { multiplier = m; }
42 class FailedToThrowStonesAndShouldHave extends Exception {}
43 class ThrewMoreStonesThanHad extends Exception {}
46 public int getStones() { return stones; }
47 public void consumeStones(int n)
48 throws ThrewMoreStonesThanHad,
49 FailedToThrowStonesAndShouldHave
52 throw new ThrewMoreStonesThanHad();
54 if (n == 0 && stones > 0) {
55 throw new FailedToThrowStonesAndShouldHave();
57 setStones(stones - n);
59 public int consumeMaxStones() {
64 public int consumeMinStones() {
66 throw new Error("Internal error: tried to consume min stones on an empty heap.");
71 public void setStones(int n) {
74 public int getOppStones() {
75 return p0.stones + p1.stones - stones;
78 public void adjustScore(int trollPosition) {
80 gp.setScore(Math.abs(castlePosition - trollPosition));
84 public int getTrollDistance() {
85 return Math.abs(castlePosition - trollPosition);
90 seed = gameManager.getSeed();
91 random = new Random(seed);
92 switch (random.nextInt(4)) {
111 Properties ps = gameManager.getGameParameters();
112 String buf = ps.getProperty("roadLength");
115 int i = Integer.parseInt(buf);
116 if (i < 0 || i > 20 || (i & 1) != 0) {
117 gameManager.addToGameSummary(GameManager.formatErrorMessage("Ignoring invalid road length: " + buf));
121 gameManager.addToGameSummary("Road length overridden to " + i);
124 catch(NumberFormatException e) {
125 gameManager.addToGameSummary(GameManager.formatErrorMessage("Ill-formed road length: " + buf));
128 ps.setProperty("roadLength", new Integer(roadLength).toString());
130 buf = ps.getProperty("initialStones");
133 int i = Integer.parseInt(buf);
135 gameManager.addToGameSummary(GameManager.formatErrorMessage("Ignoring invalid initial stone count: " + buf));
139 gameManager.addToGameSummary("Initial stone count overridden to " + buf);
142 catch (NumberFormatException e) {
143 gameManager.addToGameSummary(GameManager.formatErrorMessage("Ill-formed initial stone count: " + buf));
146 ps.setProperty("initialStones", new Integer(initialStones).toString());
148 trollPosition = roadLength / 2;
151 p0.gp = gameManager.getPlayer(0);
152 p0.setCastlePosition(0);
154 p0.adjustScore(trollPosition);
155 p0.setStones(initialStones);
158 p1.gp = gameManager.getPlayer(1);
159 p1.setCastlePosition(roadLength);
160 p1.setMultiplier(-1);
161 p1.adjustScore(trollPosition);
162 p1.setStones(initialStones);
165 void moveTroll(int delta) {
166 trollPosition += delta;
167 if (trollPosition <= 0) {
171 if (trollPosition >= roadLength) {
172 trollPosition = roadLength;
176 p0.adjustScore(trollPosition);
177 p1.adjustScore(trollPosition);
180 private Integer winner;
181 boolean haveWinner() {
182 return winner != null;
185 int getWinner() { return winner; }
186 int getLoser() { return 1 - winner; }
188 boolean exhausted() {
189 return p0.getStones() <= 0 || p1.getStones() <= 0;