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;
29 private int castlePosition;
30 public int getCastlePosition() { return castlePosition; }
31 public void setCastlePosition(int pos) { castlePosition = pos; }
33 private int multiplier;
34 public int getMultiplier() { return multiplier; }
35 public void setMultiplier(int m) { multiplier = m; }
37 class FailedToThrowStonesAndShouldHave extends Exception {}
38 class ThrewMoreStonesThanHad extends Exception {}
41 public int getStones() { return stones; }
42 public void consumeStones(int n)
43 throws ThrewMoreStonesThanHad,
44 FailedToThrowStonesAndShouldHave
47 throw new ThrewMoreStonesThanHad();
49 if (n == 0 && stones > 0) {
50 throw new FailedToThrowStonesAndShouldHave();
52 setStones(stones - n);
54 public int consumeMaxStones() {
59 public int consumeMinStones() {
61 throw new Error("Internal error: tried to consume min stones on an empty heap.");
66 public void setStones(int n) {
69 public int getOppStones() {
70 return p0.stones + p1.stones - stones;
73 public void adjustScore(int trollPosition) {
75 gp.setScore(Math.abs(castlePosition - trollPosition));
79 public int getTrollDistance() {
80 return Math.abs(castlePosition - trollPosition);
85 seed = gameManager.getSeed();
86 random = new Random(seed);
87 switch (random.nextInt(4)) {
106 Properties ps = gameManager.getGameParameters();
107 String buf = ps.getProperty("roadLength");
110 int i = Integer.parseInt(buf);
111 if (i < 0 || i > 20 || (i & 1) != 0) {
112 gameManager.addToGameSummary(GameManager.formatErrorMessage("Ignoring invalid road length: " + buf));
116 gameManager.addToGameSummary("Road length overridden to " + i);
119 catch(NumberFormatException e) {
120 gameManager.addToGameSummary(GameManager.formatErrorMessage("Ill-formed road length: " + buf));
123 ps.setProperty("roadLength", new Integer(roadLength).toString());
125 buf = ps.getProperty("initialStones");
128 int i = Integer.parseInt(buf);
130 gameManager.addToGameSummary(GameManager.formatErrorMessage("Ignoring invalid initial stone count: " + buf));
134 gameManager.addToGameSummary("Initial stone count overridden to " + buf);
137 catch (NumberFormatException e) {
138 gameManager.addToGameSummary(GameManager.formatErrorMessage("Ill-formed initial stone count: " + buf));
141 ps.setProperty("initialStones", new Integer(initialStones).toString());
143 trollPosition = roadLength / 2;
146 p0.gp = gameManager.getPlayer(0);
147 p0.setCastlePosition(0);
149 p0.adjustScore(trollPosition);
150 p0.setStones(initialStones);
153 p1.gp = gameManager.getPlayer(1);
154 p1.setCastlePosition(roadLength);
155 p1.setMultiplier(-1);
156 p1.adjustScore(trollPosition);
157 p1.setStones(initialStones);
160 void moveTroll(int delta) {
161 trollPosition += delta;
162 if (trollPosition <= 0) {
166 if (trollPosition >= roadLength) {
167 trollPosition = roadLength;
171 p0.adjustScore(trollPosition);
172 p1.adjustScore(trollPosition);
175 private Integer winner;
176 boolean haveWinner() {
177 return winner != null;
180 int getWinner() { return winner; }
181 int getLoser() { return 1 - winner; }
183 boolean exhausted() {
184 return p0.getStones() <= 0 || p1.getStones() <= 0;