summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/tankontank/game/Player.java
blob: 9bc1c2beb4e5a308ae1edca3ab878cad69220fd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package ch.asynk.tankontank.game;

import java.util.Random;
import java.util.List;
import java.util.ArrayList;

import ch.asynk.tankontank.TankOnTank;
import ch.asynk.tankontank.engine.Pawn;

public class Player
{
    private static final float MOVE_TIME = 0.4f;

    private static Random rand = new Random();

    private int turn;
    private int apSpent;
    private int actionPoints;
    private boolean deploymentDone;

    public Army army;
    public ArrayList<Unit> units;
    public ArrayList<Unit> casualties;
    public ArrayList<Unit> reinforcement;
    public ArrayList<Unit> escaped;

    public int actionCount;
    public int lostEngagementCount;
    public int wonEngagementCount;

    public Player(final TankOnTank game, Army army, int n)
    {
        this.army = army;
        this.units = new ArrayList<Unit>(n);
        this.casualties = new ArrayList<Unit>(n);
        this.reinforcement = new ArrayList<Unit>(n);
        this.escaped = new ArrayList<Unit>(n);
        this.turn = 0;
        this.apSpent = 0;
        this.actionPoints = 0;
        this.deploymentDone = false;
        this.actionCount = 0;
        this.lostEngagementCount = 0;
        this.wonEngagementCount = 0;
    }

    public String getName()
    {
        return army.toString();
    }

    public String toString()
    {
        return army + " AP: " + actionPoints +
            " units:" + units.size() + " casualties:" + casualties.size();
    }

    public String getStats()
    {
        return String.format("%s\n%4d\n%4d\n%4d\n%4d\n%4d", getName(), actionCount, unitsLeft(), casualties.size(), wonEngagementCount, lostEngagementCount);
    }

    public boolean is(Army army)
    {
        return (this.army == army);
    }

    public boolean isEnemy(Unit unit)
    {
        return unit.isEnemy(army);
    }

    public boolean isEnemy(Army other)
    {
        return army.isEnemy(other);
    }

   @SuppressWarnings("unchecked")
    public List<Pawn> unitsAsPawns()
    {
        return (List) units;
    }

    public int unitsLeft()
    {
        return (units.size() + reinforcement.size());
    }

    public int reinforcement()
    {
        return reinforcement.size();
    }

    public int casualties()
    {
        return casualties.size();
    }

    public int escaped()
    {
        return escaped.size();
    }

    public void addUnit(Unit unit)
    {
        units.add(unit);
    }

    public void addReinforcement(Unit unit)
    {
        reinforcement.add(unit);
    }

    public void unitEntry(Unit unit)
    {
        reinforcement.remove(unit);
        units.add(unit);
    }

    public void revertUnitEntry(Unit unit)
    {
        units.remove(unit);
        reinforcement.add(unit);
    }

    public void casualty(Unit unit)
    {
        units.remove(unit);
        casualties.add(unit);
    }

    public void unitEscape(Unit unit)
    {
        units.remove(unit);
        escaped.add(unit);
    }

    public int getAp()
    {
        return ((apSpent < actionPoints) ? (apSpent + 1) : apSpent);
    }

    public int getTurn()
    {
        return turn;
    }

    public boolean apExhausted()
    {
        return (apSpent == actionPoints);
    }

    public boolean isDeploymentDone()
    {
        return deploymentDone;
    }

    public void deploymentDone()
    {
        deploymentDone = true;
    }

    public void burnDownOneAp()
    {
        apSpent += 1;
        actionCount += 1;
        if (apSpent > actionPoints) TankOnTank.debug("ERROR: spent too much AP, please report");
    }

    public void turnEnd()
    {
    }

    public void turnStart()
    {
        if (!deploymentDone)
            return;
        turn += 1;
        for (Unit unit : units)
            unit.reset();
        computeActionPoints();
    }

    public int d6()
    {
        return rand.nextInt(6) + 1;
    }

    private void computeActionPoints()
    {
        this.actionPoints = 2;
        if (d6() > 2) {
            this.actionPoints += 1;
            if (d6() > 3)
                this.actionPoints += 1;
        }
        apSpent = 0;
    }

    public boolean canPromote(Unit unit)
    {
        if (unit.isHq()) return false;
        for (Unit p: casualties)
            if (p.isHqOf(unit)) return true;
        return false;
    }

    public boolean promote(Unit unit)
    {
        for (Unit p: casualties) {
            if (p.isHqOf(unit)) {
                unit.promote();
                p.degrade();
                return true;
            }
        }

        return false;
    }
}