This repository has been archived by the owner on Jun 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.java
73 lines (62 loc) · 1.76 KB
/
Player.java
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
import java.util.ArrayList;
/**
* This class represents either a human or computer player
*/
public class Player {
//public static enum Side {LEFT, RIGHT, TOP, BOTTOM};
public static final int PRICE_SOLDIER = 10;
public static final int PRICE_TANK = 15;
public static final int PRICE_PLANE = 20;
private final int num;
private int money = 100;
private ArrayList<Unit> units = new ArrayList<Unit>(10);
private int soldierCount = 0;
private int tankCount = 0;
private int planeCount = 0;
private int currentIndex = 0;
/**
* Represents a human (or possibly computer) player
* @param number The player's unique number (1, 2, etc)
*/
public Player(int number) {
this.num = number;
}
public int getNumber() { return num; }
public int moneyRemaining() { return money; }
public int getUnitCount() { return units.size(); }
public boolean buySoldier() {
if (PRICE_SOLDIER > money)
return false;
money = money - PRICE_SOLDIER;
soldierCount++;
return units.add(new Unit(Unit.Type.SOLDIER, this));
}
public boolean buyTank() {
if (PRICE_TANK > money)
return false;
money = money - PRICE_TANK;
tankCount++;
return units.add(new Unit(Unit.Type.TANK, this));
}
public boolean buyPlane() {
if (PRICE_PLANE > money)
return false;
money = money - PRICE_PLANE;
planeCount++;
return units.add(new Unit(Unit.Type.PLANE, this));
}
public int sizeOfSoldiers() { return soldierCount; }
public int sizeOfTanks() { return tankCount; }
public int sizeOfPlanes() { return planeCount; }
public int sizeOfUnits() { return units.size(); }
public Unit getNextUnit() {
Unit u = null;
if (currentIndex < units.size())
u = units.get(currentIndex);
currentIndex++;
return u;
}
public void removeUnit(Unit u) {
units.remove(u);
}
}