forked from fjricci/monopoly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtility.java
More file actions
108 lines (88 loc) · 2.08 KB
/
Utility.java
File metadata and controls
108 lines (88 loc) · 2.08 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
package monopoly;
public class Utility implements Square {
private final int COST = 150; //cost to purchase utility
private final Dice dice;
private final String name;
private final int pos;
private Player owner; //stores utility owner
private boolean owned; //is utility owned?
private int numOwned; //number of utilities owned by a player
private boolean mortgaged; //is property mortgaged?
private Utility other;
//utility constructor
public Utility(String name, int pos, boolean deterministic) {
numOwned = 0;
mortgaged = false;
this.name = name;
this.pos = pos;
if (deterministic)
this.dice = new InputDice(new Input());
else
this.dice = new ProbDice();
}
public void setOther(Utility other) {
this.other = other;
}
public int increasedRent() {
return 10 * dice.roll().val;
}
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;
numOwned = 1;
for (Square sq : player.properties())
if (sq instanceof Utility)
numOwned++;
}
//return rent on utility, given a roll
public int rent(int roll) {
if (roll == 0)
roll = dice.roll().val;
int TWO = 10;
if (owner.equals(other.owner()))
return TWO * roll;
int ONE = 4;
return ONE * roll;
}
//return total utilities owned by player owning this utility
public boolean isOwned() {
return owned;
}
//return player object of owner
public Player owner() {
return owner;
}
//return cost to purchase utility
public int cost() {
return COST;
}
//mortgage property
public int mortgage() {
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() {
return name;
}
}