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;
19 com.codingame.game.Player gp;
22 Player(int i) { index = i; }
24 private int castlePosition;
25 public int getCastlePosition() { return castlePosition; }
26 public void setCastlePosition(int pos) { castlePosition = pos; }
28 private int multiplier;
29 public int getMultiplier() { return multiplier; }
30 public void setMultiplier(int m) { multiplier = m; }
32 class FailedToThrowStonesAndShouldHave extends Exception {}
33 class ThrewMoreStonesThanHad extends Exception {}
36 public int getStones() { return stones; }
37 public void consumeStones(int n)
38 throws ThrewMoreStonesThanHad,
39 FailedToThrowStonesAndShouldHave
42 throw new ThrewMoreStonesThanHad();
44 if (n == 0 && stones > 0) {
45 throw new FailedToThrowStonesAndShouldHave();
47 setStones(stones - n);
49 public int consumeMaxStones() {
54 public int consumeMinStones() {
56 throw new Error("Internal error: tried to consume min stones on an empty heap.");
61 public void setStones(int n) {
64 public int getOppStones() {
65 return p0.stones + p1.stones - stones;
68 public void adjustScore(int trollPosition) {
69 gp.setScore(Math.abs(castlePosition - trollPosition));
72 public int getTrollDistance() {
73 return Math.abs(castlePosition - trollPosition);
77 void init(long seed) {
78 random = new Random(seed);
79 switch (random.nextInt(4)) {
98 Properties ps = gameManager.getGameParameters();
99 String buf = ps.getProperty("roadLength");
102 int i = Integer.parseInt(buf);
103 if (i < 0 || i > 20 || (i & 1) != 0) {
104 gameManager.addToGameSummary(GameManager.formatErrorMessage("Ignoring invalid road length: " + buf));
108 gameManager.addToGameSummary("Road length overridden to " + i);
111 catch(NumberFormatException e) {
112 gameManager.addToGameSummary(GameManager.formatErrorMessage("Ill-formed road length: " + buf));
115 ps.setProperty("roadLength", new Integer(roadLength).toString());
117 buf = ps.getProperty("initialStones");
120 int i = Integer.parseInt(buf);
122 gameManager.addToGameSummary(GameManager.formatErrorMessage("Ignoring invalid initial stone count: " + buf));
126 gameManager.addToGameSummary("Initial stone count overridden to " + buf);
129 catch (NumberFormatException e) {
130 gameManager.addToGameSummary(GameManager.formatErrorMessage("Ill-formed initial stone count: " + buf));
133 ps.setProperty("initialStones", new Integer(initialStones).toString());
135 trollPosition = roadLength / 2;
138 p0.gp = gameManager.getPlayer(0);
139 p0.setCastlePosition(0);
141 p0.adjustScore(trollPosition);
142 p0.setStones(initialStones);
145 p1.gp = gameManager.getPlayer(1);
146 p1.setCastlePosition(roadLength);
147 p1.setMultiplier(-1);
148 p1.adjustScore(trollPosition);
149 p1.setStones(initialStones);
153 boolean haveWinner() {
154 if (trollPosition == 0) {
158 else if (trollPosition == roadLength) {
166 int getWinner() { return winner; }
167 int getLoser() { return 1 - winner; }
169 boolean exhausted() {
170 return p0.getStones() <= 0 && p1.getStones() <= 0;