forked from fjricci/monopoly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRailroad.java
More file actions
110 lines (91 loc) · 1.86 KB
/
Railroad.java
File metadata and controls
110 lines (91 loc) · 1.86 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
package monopoly;
public class Railroad implements Square {
private final int COST = 200;
private final String name;
private final int pos;
private final Railroad[] others = new Railroad[3];
private int numOwned; //number of railroads owned by a player
private Player owner;
private boolean owned; //is property owned?
private boolean mortgaged;
//constructor
public Railroad(String name, int pos) {
numOwned = 1;
this.name = name;
this.pos = pos;
}
public void createGroup(Railroad a, Railroad b, Railroad c){
others[0] = a;
others[1] = b;
others[2] = c;
}
private void updateOwners() {
numOwned = 1;
for (Railroad r : others){
if (r.isOwned() && r.owner().equals(owner))
numOwned++;
}
}
public int position() {
return pos;
}
public String name() {
return name;
}
//update status of property to owned
public void purchase(Player player) {
owned = true;
owner = player;
updateOwners();
}
public boolean isOwnable() {
return true;
}
//return rent owed
public int rent(int val) {
updateOwners();
switch (numOwned) {
case 1:
return 25;
case 2:
return 50;
case 3:
return 100;
case 4:
return 200;
default:
return 0;
}
}
public boolean isOwned() {
return owned;
}
public Player owner() {
return owner;
}
public int cost() {
return COST;
}
//mortgage or unmortgage property
public int mortgage() {
updateOwners();
if (mortgaged) {
mortgaged = false;
return (int) Math.round((COST / 2) * 1.1);
} else {
mortgaged = true;
return COST / 2;
}
}
public boolean isMortgaged() {
return mortgaged;
}
public int mortgageCost() {
return COST / 2;
}
public String toString() {
if (mortgaged)
return name + " Mortgaged";
return name;
}
}