forked from fjricci/monopoly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPUPlayer.java
More file actions
261 lines (213 loc) · 5.41 KB
/
CPUPlayer.java
File metadata and controls
261 lines (213 loc) · 5.41 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package monopoly;
import java.util.LinkedList;
import java.util.Queue;
import java.util.stream.Collectors;
/**
* Created by fjricci on 6/22/2015.
* A CPU player.
*/
public class CPUPlayer implements Player {
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 CPUPlayer(int index) {
money = 1500;
properties = new LinkedList<>();
position = 0;
this.playerName = "CPU " + (index - 1);
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;
}
//TODO input stuff
public boolean inputBool(Monopoly.State state) {
switch (state.state) {
case PURCHASE:
return handlePurchase(state);
case AUCTION:
return handleAuction(state);
case BUY_JAIL:
return handleOutJail(state);
case BUY_HOUSE:
return handleBuyHouses(state);
case MORTGAGE:
return handleMortgage(state);
case UNMORTGAGE:
return handleUnmortgage(state);
case TRADE_MONEY:
return handleTradeMoney(state);
case GIVE_PROPS:
return handleGiveProps(state);
case GET_PROPS:
return handleGetProps(state);
default:
throw new IllegalArgumentException("Uh Oh. Forgot to implement a use case for boolean CPU!");
}
}
public int inputInt(Monopoly.State state) {
switch (state.state) {
default:
throw new IllegalArgumentException("Uh Oh. Forgot to implement a use case for integer CPU!");
}
}
public int inputDecision(Monopoly.State state, String[] choices) {
return 0;
}
public Player inputPlayer(Monopoly.State state, Player notAllowed) {
return notAllowed;
}
//Input handlers TODO improvements
//Boolean handlers
private boolean handlePurchase(Monopoly.State state) {
return true;
}
private boolean handleAuction(Monopoly.State state) {
int cost = state.board.square(state.current.position()).cost();
return (state.val <= 2 * cost);
}
private boolean handleOutJail(Monopoly.State state) {
//buy way out of jail if any squares are still in the bank
for (Square sq : state.board.getBoard()) {
if (sq.isOwnable() && !sq.isOwned())
return true;
}
return false;
}
private boolean handleBuyHouses(Monopoly.State state) {
Queue<Property> purchaseable = new LinkedList<>();
for (Square sq : properties) {
if (!(sq instanceof Property)) continue;
Property prop = (Property) sq;
if (prop.groupBuild())
purchaseable.add(prop);
}
for (Property p : purchaseable) {
if (p.houseCost() < money)
return true;
}
return false;
}
private boolean handleMortgage(Monopoly.State state) {
return false;
}
private boolean handleUnmortgage(Monopoly.State state) {
for (Square sq : state.current.properties()) {
if (sq.isMortgaged() && sq.mortgageCost() < money)
return true;
}
return false;
}
private boolean handleTradeMoney(Monopoly.State state) {
return false;
}
private boolean handleGiveProps(Monopoly.State state) {
return false;
}
private boolean handleGetProps(Monopoly.State state) {
return false;
}
//Int handlers
//Decision handlers
//Player handlers
}