Announce it's the loser who gets destroyed instead of the winner. (spotted by @Astrob...
[troll.git] / src / main / java / com / codingame / game / Model.java
1 package com.codingame.game;
2
3 import java.util.Random;
4
5 import com.codingame.gameengine.core.MultiplayerGameManager;
6 import com.google.inject.Inject;
7
8 class Model {
9     @Inject private MultiplayerGameManager<com.codingame.game.Player> gameManager;
10     Random random;
11     int roadLength;
12     int initialStones;
13     int trollPosition;
14     Player p0, p1;
15
16     class Player {
17         com.codingame.game.Player gp;
18         int index;
19
20         Player(int i) { index = i; }
21
22         private int castlePosition;
23         public int getCastlePosition() { return castlePosition; }
24         public void setCastlePosition(int pos) { castlePosition = pos; }
25
26         private int multiplier;
27         public int getMultiplier() { return multiplier; }
28         public void setMultiplier(int m){ multiplier = m; }
29
30         private int stones;
31         public int getStones() { return stones; }
32         public void consumeStones(int n) throws InvalidAction {
33             if (n > stones) {
34                 throw new InvalidAction("attempted to throw more stones than they had.");
35             }
36             setStones(stones - n);
37         }
38         public void setStones(int n) {
39             stones = n;
40         }
41         public int getOppStones() {
42             return p0.stones + p1.stones - stones;
43         }
44
45         public void adjustScore(int trollPosition) {
46             gp.setScore(Math.abs(castlePosition - trollPosition));
47         }
48
49         public int getTrollDistance() {
50             return Math.abs(castlePosition - trollPosition);
51         }
52     }
53
54     void init(long seed) {
55         random = new Random(seed);
56         switch (random.nextInt(4)) {
57         case 0:
58             roadLength = 6;
59             initialStones = 15;
60             break;
61         case 1:
62             roadLength = 6;
63             initialStones = 30;
64             break;
65         case 2:
66             roadLength = 14;
67             initialStones = 30;
68             break;
69         case 3:
70             roadLength = 14;
71             initialStones = 50;
72             break;
73         }
74
75         trollPosition = roadLength / 2;
76
77         p0 = new Player(0);
78         p0.gp = gameManager.getPlayer(0);
79         p0.setCastlePosition(0);
80         p0.setMultiplier(1);
81         p0.adjustScore(trollPosition);
82         p0.setStones(initialStones);
83
84         p1 = new Player(1);
85         p1.gp = gameManager.getPlayer(1);
86         p1.setCastlePosition(roadLength);
87         p1.setMultiplier(-1);
88         p1.adjustScore(trollPosition);
89         p1.setStones(initialStones);
90     }
91
92     private int winner;
93     boolean haveWinner() {
94         if (trollPosition == 0) {
95             winner = 1;
96             return true;
97         }
98         else if (trollPosition == roadLength) {
99             winner = 0;
100             return true;
101         }
102         else {
103             return false;
104         }
105     }
106     int getWinner() { return winner; }
107     int getLoser() { return 1 - winner; }
108
109     boolean exhausted() {
110         return p0.getStones() <= 0 && p1.getStones() <= 0;
111     }
112 }