1 package com.codingame.game;
 
   3 import java.util.Random;
 
   5 import com.codingame.gameengine.core.MultiplayerGameManager;
 
   6 import com.google.inject.Inject;
 
   9     @Inject private MultiplayerGameManager<com.codingame.game.Player> gameManager;
 
  17         com.codingame.game.Player gp;
 
  20         Player(int i) { index = i; }
 
  22         private int castlePosition;
 
  23         public int getCastlePosition() { return castlePosition; }
 
  24         public void setCastlePosition(int pos) { castlePosition = pos; }
 
  26         private int multiplier;
 
  27         public int getMultiplier() { return multiplier; }
 
  28         public void setMultiplier(int m) { multiplier = m; }
 
  30         class FailedToThrowStonesAndShouldHave extends Exception {}
 
  31         class ThrewMoreStonesThanHad extends Exception {}
 
  34         public int getStones() { return stones; }
 
  35         public void consumeStones(int n)
 
  36             throws ThrewMoreStonesThanHad,
 
  37                    FailedToThrowStonesAndShouldHave
 
  40                 throw new ThrewMoreStonesThanHad();
 
  42             if (n == 0 && stones > 0) {
 
  43                 throw new FailedToThrowStonesAndShouldHave();
 
  45             setStones(stones - n);
 
  47         public int consumeMaxStones() {
 
  52         public int consumeMinStones() {
 
  54                 throw new Error("Internal error: tried to consume min stones on an empty heap.");
 
  59         public void setStones(int n) {
 
  62         public int getOppStones() {
 
  63             return p0.stones + p1.stones - stones;
 
  66         public void adjustScore(int trollPosition) {
 
  67             gp.setScore(Math.abs(castlePosition - trollPosition));
 
  70         public int getTrollDistance() {
 
  71             return Math.abs(castlePosition - trollPosition);
 
  75     void init(long seed) {
 
  76         random = new Random(seed);
 
  77         switch (random.nextInt(4)) {
 
  96         trollPosition = roadLength / 2;
 
  99         p0.gp = gameManager.getPlayer(0);
 
 100         p0.setCastlePosition(0);
 
 102         p0.adjustScore(trollPosition);
 
 103         p0.setStones(initialStones);
 
 106         p1.gp = gameManager.getPlayer(1);
 
 107         p1.setCastlePosition(roadLength);
 
 108         p1.setMultiplier(-1);
 
 109         p1.adjustScore(trollPosition);
 
 110         p1.setStones(initialStones);
 
 114     boolean haveWinner() {
 
 115         if (trollPosition == 0) {
 
 119         else if (trollPosition == roadLength) {
 
 127     int getWinner() { return winner; }
 
 128     int getLoser() { return 1 - winner; }
 
 130     boolean exhausted() {
 
 131         return p0.getStones() <= 0 && p1.getStones() <= 0;