-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathHumanPlayer.java
More file actions
162 lines (133 loc) · 3.2 KB
/
HumanPlayer.java
File metadata and controls
162 lines (133 loc) · 3.2 KB
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
package monopoly;
import java.util.LinkedList;
import java.util.Queue;
import java.util.stream.Collectors;
public class HumanPlayer implements Player {
private final Input input;
private final int TO_JAIL = 30;
private final int IN_JAIL = 10;
private final Queue<Square> properties;
private final String playerName;
private int money;
private int position;
private boolean inJail;
private int jailTurn;
private int numJailFree;
private boolean chanceFree;
public HumanPlayer(String playerName) {
input = new Input();
money = 1500;
properties = new LinkedList<>();
position = 0;
this.playerName = playerName;
inJail = false;
numJailFree = 0;
chanceFree = false;
}
public void addProperty(Square square) {
if (!square.isOwnable())
throw new IllegalArgumentException("This property cannot be purchased!");
properties.add(square);
square.purchase(this);
}
public void move(int numSpaces) {
position += numSpaces;
int BOARD_SIZE = 40;
if (position >= BOARD_SIZE) {
position -= BOARD_SIZE;
excMoney(200);
}
if (position == TO_JAIL) {
position = IN_JAIL;
toJail();
}
}
public void moveTo(int pos) {
if (pos < position && !inJail)
excMoney(200);
position = pos;
if (position == TO_JAIL) {
position = IN_JAIL;
toJail();
}
}
public int position() {
return position;
}
public Queue<Square> properties() {
return properties.stream().collect(Collectors.toCollection(LinkedList::new));
}
public String name() {
return playerName;
}
public int getMoney() {
return money;
}
public void excMoney(int money) {
this.money += money;
}
public void toJail() {
inJail = true;
move(40);
jailTurn = 0;
}
public boolean stayJail() {
jailTurn++;
if (jailTurn == 3) {
inJail = false;
return false;
}
return true;
}
public void sellProp(Square sq) {
properties.remove(sq);
}
public void leaveJail() {
inJail = false;
moveTo(10);
}
public boolean inJail() {
return inJail;
}
public void addJailFree(boolean chance) {
numJailFree++;
chanceFree = chance;
}
public boolean useJailFree() {
if (numJailFree < 1)
throw new RuntimeException("You do not have any cards!");
numJailFree--;
boolean deck = chanceFree;
chanceFree = !chanceFree;
return deck;
}
public int numJailFree() {
return numJailFree;
}
public int getAssets() {
int assets = this.money;
for (Square s : properties) {
assets += s.cost();
if (s instanceof Property)
assets += getHouseVal((Property) s);
}
return assets;
}
private int getHouseVal(Property prop) {
int numHouses = prop.numHouses();
int houseCost = prop.houseCost();
return numHouses * houseCost;
}
public boolean inputBool(Monopoly.State state) {
return input.inputBool();
}
public int inputInt(Monopoly.State state) {
return input.inputInt();
}
public int inputDecision(Monopoly.State state, String[] choices) {
return input.inputDecision(choices);
}
public Player inputPlayer(Monopoly.State state, Player notAllowed) {
return input.inputPlayer(state.players, notAllowed);
}
}