-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCard.java
More file actions
314 lines (280 loc) · 7.89 KB
/
Copy pathCard.java
File metadata and controls
314 lines (280 loc) · 7.89 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import java.util.Random;
import javax.swing.*;
/**
* Jan 22, 2022 <br>
* This is the Card class which handles the chance and community
* cards in the game on monopoly.
*/
public class Card
{
//Class constants related to getting of image of file
public static final String CARD_FILE_PATH = ".//Images//";
public static final String CHANCE_EXTENSION = "chance//";
public static final String COMMUNITY_EXTENSION = "community//";
public static final String CARD_FILE_TYPE = ".png";
//Chance
public static final String[] CHANCE_NAMES = {"Advance To Boardwalk", "Go Back 3 Spaces", "Advance To Illinois", "Advance To Nearest Railroad", "Advance To Nearest Utility", "Advance To St. Charles", "Bank Pays You 50", "Building and Loan Matures", "Get Out of Jail Free", "Advance To Go", "Go To Jail", "Make General Repairs", "Poor Tax"};
public static final int[] CHANCE_MONEY = {0, 0, 0, 0, 0, 0, 50, 150, 0, 200, 0, -25, -15};
//Edge cases
public static final int CHANCE_BOARDWALK = 0;
public static final int CHANCE_GO_BACK3 = 1;
public static final int CHANCE_ILLINOIS = 2;
public static final int CHANCE_RAILROAD = 3;
public static final int CHANCE_UTILITY = 4;
public static final int CHANCE_ST_CHARLES = 5;
public static final int CHANCE_ESCAPE_JAIL = 8;
public static final int CHANCE_GO_TO_START = 9;
public static final int CHANCE_GO_TO_JAIL = 10;
public static final int CHANCE_MAKE_GENERAL_REPAIRS = 11;
//Community
public static final String[] COMMUNITY_NAMES = {"Advance To Go", "Bank Error", "Beauty Contest", "Doctor's Fee", "From Sale of Stock", "Get Out of Jail Free", "Go To Jail", "Income Tax Refund", "Inheritance", "Life Insurance Matures", "Pay Hospital", "Pay School Tax", "Receive for Services", "XMas Fund Matures"};
public static final int[] COMMUNITY_MONEY = {200, 200, 10, -50, 45, 0, 0, 20, 100, 100, -100, -150, 25, 100};
//Edge cases
public static final int COMMUNITY_GO = 0;
public static final int COMMUNITY_ESCAPE_JAIL = 5;
public static final int COMMUNITY_GO_TO_JAIL = 6;
//Constants related to setting x and y positions
public static final int DEFAULT_POSITION = 200;
//Instance variables
private boolean isChance;
private int type;
private int xPosition;
private int yPosition;
private JLabel label;
private Random random;
public Card(boolean isChance)
{
this.isChance = isChance;
random = new Random();
label = new JLabel();
xPosition = DEFAULT_POSITION;
yPosition = DEFAULT_POSITION;
getNewType();
}
// Methods
public int getNewType()
{
//Creates the type and image for chance card
if(isChance)
{
type = random.nextInt(CHANCE_NAMES.length);
}
//Creates the type and image for community card
else
{
type = random.nextInt(COMMUNITY_NAMES.length);
}
drawImage();
return type;
}
// This method draws the image based on the type and chance or community.
public void drawImage()
{
ImageIcon image = null;
if(isChance)
image = new ImageIcon(CARD_FILE_PATH+CHANCE_EXTENSION+CHANCE_NAMES[type]+CARD_FILE_TYPE);
else
image = new ImageIcon(CARD_FILE_PATH+COMMUNITY_EXTENSION+COMMUNITY_NAMES[type]+CARD_FILE_TYPE);
label.setIcon(image);
label.setBounds(xPosition, yPosition, image.getIconWidth(), image.getIconHeight());
}
/**
* This method checks to make sure the card can perform the event on the
* player. As in the player has enough money to pay for the fee associated with
* type if there is one.
* @param player
* @return
*/
public boolean canDoEvent(Player player)
{
// The card is a chance
if(isChance)
{
int amount = CHANCE_MONEY[type];
// Money being added
if(amount >=0)
return true;
// Special case where the fee depends on the players number of properties
if(type == CHANCE_MAKE_GENERAL_REPAIRS)
{
return player.canPay(amount*player.getProperties().size());
}
// Money being taken away
return player.canPay(-amount);
}
// The card is a community
else
{
int amount = COMMUNITY_MONEY[type];
// Money being added
if(amount >=0)
return true;
// Money being taken away
return player.canPay(-amount);
}
}
/**
* This method does the event based on the type and whether the card is a chance
* or community card on the player. It also makes sure the event can be done on the
* player by calling canDoEvent(Player) see this method for more info.
* @param player
*/
public void doEvent(Player player)
{
// Makes sure the card can do the event on the player
if(canDoEvent(player))
{
// Does the event if the card is a chance card
if(isChance)
{
if(type == CHANCE_BOARDWALK)
player.setLocation(Tile.LOCATION_BROADWALK);
else if(type == CHANCE_GO_BACK3)
player.move(-3);
else if(type == CHANCE_ILLINOIS)
player.setLocation(Tile.LOCATION_ILLINOIS);
else if(type == CHANCE_RAILROAD)
setLocationRailRoadUtility(player, Tile.LOCATION_RAILROADS);
else if(type == CHANCE_UTILITY)
setLocationRailRoadUtility(player, Tile.LOCATION_UTILITIES);
else if(type == CHANCE_ST_CHARLES)
player.setLocation(Tile.LOCATION_ST_CHARLES);
else if(type == CHANCE_ESCAPE_JAIL)
player.addNumJailFrees();
else if(type == CHANCE_GO_TO_START)
player.setLocation(Player.STARTING_LOCATION);
else if(type == CHANCE_GO_TO_JAIL)
player.setJailTime();
else if(type == CHANCE_MAKE_GENERAL_REPAIRS)
player.takeMoney(CHANCE_MONEY[type]*player.getProperties().size());
else
{
int amount = CHANCE_MONEY[type];
if(amount > 0)
player.addMoney(amount);
else
player.takeMoney(-amount);
}
}
// Does the event if the card is a community card
else
{
if(type == COMMUNITY_GO)
player.setLocation(Player.STARTING_LOCATION);
else if(type == COMMUNITY_GO_TO_JAIL)
player.setJailTime();
else if(type == COMMUNITY_ESCAPE_JAIL)
player.addNumJailFrees();
else
{
int amount = COMMUNITY_MONEY[type];
if(amount >= 0)
player.addMoney(amount);
else
player.takeMoney(-amount);
}
}
}
else
player.mortgageProperties(this);
}
/**
* Returns if the card is a chance card of type make general repairs
* @return
*/
public boolean isMakeGeneralRepair()
{
return isChance && type == CHANCE_MAKE_GENERAL_REPAIRS;
}
/**
* Gets the cost of the card on the player
* @return
*/
public int getCost()
{
if(isChance)
return CHANCE_MONEY[type];
else
return COMMUNITY_MONEY[type];
}
/**
* This helper method sets the location of the player to the nearest railroad or utility
* before the current position.
* @param player
* @param list
*/
private void setLocationRailRoadUtility(Player player, int[] list)
{
int location = -1;
for(int position: list)
if(position >= player.getLocation())
{
location = position;
player.setLocation(location);
break;
}
if(location == -1)
player.setLocation(Tile.LOCATION_RAILROADS[0]);
}
//Adds the card to the frame
public void addToFrame(JFrame frame)
{
frame.add(label);
}
public String typeToString()
{
if(isChance)
return CHANCE_NAMES[type];
else
return COMMUNITY_NAMES[type];
}
public String isChanceToString()
{
if(isChance)
return "Chance";
else
return "Community";
}
// Getters
public boolean isChance() {
return isChance;
}
public int getType() {
return type;
}
public JLabel getLabel() {
return label;
}
public int getXPosition()
{
return xPosition;
}
public int getYPosition()
{
return yPosition;
}
// Setters
public void setChance(boolean isChance) {
this.isChance = isChance;
}
public void setType(int type) {
this.type = type;
drawImage();
}
public void setLabel(JLabel label) {
this.label = label;
}
public void setXPosition(int xPosition)
{
this.xPosition = xPosition;
}
public void setYPosition(int yPosition)
{
this.yPosition = yPosition;
}
// ToString
public String toString()
{
return isChanceToString()+" Card of "+typeToString();
}
}