forked from fjricci/monopoly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProperty.java
More file actions
245 lines (207 loc) · 5.01 KB
/
Property.java
File metadata and controls
245 lines (207 loc) · 5.01 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
package monopoly;
import java.util.LinkedList;
import java.util.Queue;
public class Property implements Square {
//costs of rent for all possible property states
private final int rent;
private final int oneH;
private final int twoH;
private final int threeH;
private final int fourH;
private final int hotel;
private final int value; //cost to purchase property
private final int houses; //cost to purchase one house on property
private final int pos;
private final String name;
private int buildings; //building status
private boolean monopoly; //does one player own all properties in set?
private boolean owned; //is property owned?
private boolean mortgaged; //is property mortgaged
private Player owner;
private Property groupA;
private Property groupB;
//construct property, given its rents
public Property(String name, int pos, int rent, int oneH, int twoH, int threeH, int fourH,
int hotel, int value, int houses) {
this.rent = rent;
this.oneH = oneH;
this.twoH = twoH;
this.threeH = threeH;
this.fourH = fourH;
this.hotel = hotel;
this.value = value;
this.houses = houses;
buildings = 0;
monopoly = false;
owned = false;
this.pos = pos;
this.name = name;
}
public void setGroup(Property groupA, Property groupB) {
this.groupA = groupA;
this.groupB = groupB;
}
public int position() {
return pos;
}
public String name() {
return name;
}
public boolean isOwnable() {
return true;
}
//update status of property to owned
public void purchase(Player player) {
owned = true;
owner = player;
updateMonopoly(player);
}
private void updateMonopoly(Player player) {
boolean a = false;
boolean b = false;
if (groupB == null)
b = true;
Queue<Property> props = new LinkedList<>();
for (Square sq : player.properties())
if (sq instanceof Property)
props.add((Property) sq);
for (Property prop : props) {
if (prop.name().equals(groupA.name()))
a = true;
if (groupB != null && prop.name().equals(groupB.name()))
b = true;
}
if (a && b) {
setMonopoly();
groupA.setMonopoly();
if (groupB != null)
groupB.setMonopoly();
} else {
breakMonopoly();
groupA.breakMonopoly();
if (groupB != null)
groupB.breakMonopoly();
}
}
public boolean isOwned() {
return owned;
}
//update building status by integer input
public void build(int a) {
buildings += a;
if (buildings > 5)
throw new IllegalArgumentException("Cannot build past hotel!");
if (buildings < 0)
throw new IllegalArgumentException("Cannot build negative buildings!");
}
//switch status of monopoly
public boolean monopoly() {
return monopoly;
}
//cost to purchase property
public int cost() {
return value;
}
//return number of buildings owned
public int numHouses() {
return buildings;
}
//return cost to purchase one house
public int houseCost() {
return houses;
}
//return amount owed
public int rent(int val) {
if (!owned)
return 0;
switch (buildings) {
case 0:
if (monopoly) return 2 * rent;
return rent;
case 1:
return oneH;
case 2:
return twoH;
case 3:
return threeH;
case 4:
return fourH;
case 5:
return hotel;
default:
return 0;
}
}
public int rentDiff() {
if (!owned || !monopoly || (buildings == 5))
return 0;
switch (buildings) {
case 0:
return oneH - 2 * rent;
case 1:
return twoH - oneH;
case 2:
return threeH - twoH;
case 3:
return fourH - threeH;
case 4:
return hotel - fourH;
default:
return 0;
}
}
public Player owner() {
return owner;
}
//mortgage property
public int mortgage() {
if (mortgaged) {
mortgaged = false;
return (int) Math.round((value / 2) * 1.1);
} else {
mortgaged = true;
return value / 2;
}
}
public boolean isMortgaged() {
return mortgaged;
}
public int mortgageCost() {
return value / 2;
}
private void setMonopoly() {
monopoly = true;
}
private void breakMonopoly() {
monopoly = false;
}
public boolean groupBuild() {
if (!monopoly)
return false;
int aDiff = groupA.numHouses() - numHouses();
boolean aOkay = aDiff == 0 || aDiff == 1;
if (groupB == null)
return aOkay;
int bDiff = groupB.numHouses() - numHouses();
boolean bOkay = bDiff == 0 || bDiff == 1;
return aOkay && bOkay;
}
public boolean groupSell() {
int aDiff = groupA.numHouses() - numHouses();
boolean aOkay = aDiff == 0 || aDiff == -1;
if (groupB == null)
return aOkay;
int bDiff = groupB.numHouses() - numHouses();
boolean bOkay = bDiff == 0 || bDiff == -1;
return aOkay && bOkay;
}
public String toString() {
if (numHouses() == 5)
return name + " - Hotel";
if (numHouses() > 0)
return name + " - " + numHouses() + " Houses";
if (mortgaged)
return name + " Mortgaged";
return name;
}
}