-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTile.java
More file actions
285 lines (246 loc) · 7.53 KB
/
Copy pathTile.java
File metadata and controls
285 lines (246 loc) · 7.53 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
import javax.swing.*;
/**
* Jan 22, 2022 <br>
* This class holds the info related to each tile in the game.
*/
public class Tile
{
//Class constants related to special tiles
public static final int EVENT_STARTING_POSITION = -1; //No Event
public static final int EVENT_COMMUNITY_CHEST = -2;
public static final int EVENT_INCOME_TAX = -3; //Finished
public static final int EVENT_CHANCE = -4;
public static final int EVENT_JAIL = -5; //No Event
public static final int EVENT_FREE_PARKING = -6; //No Event
public static final int EVENT_GO_TO_JAIL = -7; //Finished
public static final int EVENT_LUXURY_TAX = -8; //Finished
//Constants related to tax
public static final int INCOME_TAX_AMOUNT = 200;
public static final int LUXURY_TAX_AMOUNT = 100;
//Class constants
public static final int MIN_PROPERTY_VALUE = 0;
public static final int[] LIST_OF_TILES = {EVENT_STARTING_POSITION, 0, EVENT_COMMUNITY_CHEST, 1, EVENT_INCOME_TAX, 2, 3, EVENT_CHANCE, 4, 5, EVENT_JAIL, 6, 7, 8, 9, 10, 11, EVENT_COMMUNITY_CHEST, 12, 13, EVENT_FREE_PARKING, 14, EVENT_CHANCE, 15, 16, 17, 18, 19, 20, 21, EVENT_GO_TO_JAIL, 22, 23, EVENT_COMMUNITY_CHEST, 24, 25, EVENT_CHANCE, 26, EVENT_LUXURY_TAX, 27};
public static final String[] NAMES = {"Property", "Starting Position", "Community Chest", "Income Tax", "Chance", "Jail", "Free Parking", "Go to jail", "Luxury Tax"};
//Class constants related to specific locations
public static final int[] LOCATION_RAILROADS = {5, 15, 25, 35};
public static final int[] LOCATION_UTILITIES = {12, 28};
public static final int LOCATION_JAIL = 10;
public static final int LOCATION_ST_CHARLES = 11;
public static final int LOCATION_ILLINOIS = 24;
public static final int LOCATION_BROADWALK = 39;
// Window constants
public static final int JLABEL_TEXT_LENGTH = 400;
//Instance variables
private int type;
private Property property;
private Card card;
//Constructor
/**
* The constructor takes in an integer for the type and creates a tile off of it. <br>
* If the type is greater than 0 and less than Property.NAMES_LIST.length, it creates
* a tile with a property. <br>
* If the type is inputed is equal to the constants EVENT_CHANCE or EVENT_COMMUNITY_CHEST
* than a chance card or a community card is created respectively. <br>
* Else the type inputed becomes a type matching the event constants.
* @param type
*/
public Tile(int type)
{
this.type = type;
// Creates the property if the type is a property
if(type >= MIN_PROPERTY_VALUE && type < Property.NAMES_LIST.length)
property = new Property(type);
// Creates the chance card if correct type
if(type == EVENT_CHANCE)
card = new Card(true);
// Creates community card if correct type
else if(type == EVENT_COMMUNITY_CHEST)
card = new Card(false);
}
//Methods
/**
* This method gets the location of a tile as a string from the index inputed based on the
* LIST_OF_TILES string array constant. If the tile at that index is a property it would
* get the name of that property instead of simply type property.
* @param index
* @return location or ""
*/
public static String locationToString(int index)
{
if(index >=0 && index < LIST_OF_TILES.length)
{
int type = LIST_OF_TILES[index];
if(type >= MIN_PROPERTY_VALUE && type < Property.NAMES_LIST.length)
return Property.NAMES_LIST[type];
else
return NAMES[-type];
}
return "";
}
/**
* This method creates the window where the info of the tile and whether the
* event was successfully completed is displayed. It also does the event.
* @param player
*/
public void createWindow(Player player)
{
// Gets new type of card
getNewTypeCard();
// Pays the rent to the owner
if(hasProperty())
property.payRent(player);
// Does the event
if(hasEvent())
{
// Creates the frame, set size, layout and location
JFrame frame = new JFrame("Tile Event");
frame.setLayout(null);
frame.setLocationRelativeTo(null);
// Create labels and add them to frame
// Text label
JLabel textLabel = new JLabel("You landed on "+typeToString());
frame.add(textLabel);
// Sets up exit button
JButton exitButton = new JButton("Ok");
exitButton.addActionListener(e -> {
frame.dispose();
});
frame.add(exitButton);
exitButton.setVisible(true);
// Sets the size based on the image
addCardToFrame(frame);
if(hasCard())
{
// Sets the bounds of the labels and frame size
frame.setSize(600, 600);
textLabel.setBounds(150, 50, JLABEL_TEXT_LENGTH, 30);
exitButton.setBounds(200, 400, 200, 50);
}
else
{
// Sets the bounds of the labels and frame size
frame.setSize(500, 500);
textLabel.setBounds(100, 50, JLABEL_TEXT_LENGTH, 30);
exitButton.setBounds(100, 300, 200, 50);
}
doEvent(player);
// Sets frame to visible and dispose on close
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
}
/**
* This method does the event on the player based on the type and whether it can
* do the event.
* @param player
*/
public void doEvent(Player player)
{
if(canDoEvent(player))
{
//Performs the event
if(type == EVENT_INCOME_TAX)
player.takeMoney(INCOME_TAX_AMOUNT);
else if(type == EVENT_LUXURY_TAX)
player.takeMoney(LUXURY_TAX_AMOUNT);
else if(type == EVENT_GO_TO_JAIL)
player.setJailTime();
else if(hasCard())
card.doEvent(player);
player.placeImage();
}
// Mortgages properties of players to pay for event
else if(hasCard())
player.mortgageProperties(card);
else if(type == EVENT_INCOME_TAX)
player.mortgageProperties(INCOME_TAX_AMOUNT);
else if(type == EVENT_LUXURY_TAX)
player.mortgageProperties(LUXURY_TAX_AMOUNT);
else if(hasProperty())
property.payRent(player);
}
/**
* This method makes sure that the event can be done on the player based on the fee
* associated with the type.
* @param player
* @return boolean
*/
public boolean canDoEvent(Player player)
{
if(type == EVENT_INCOME_TAX)
return player.canPay(INCOME_TAX_AMOUNT);
else if(type == EVENT_LUXURY_TAX)
return player.canPay(LUXURY_TAX_AMOUNT);
else if(hasCard())
return card.canDoEvent(player);
return true;
}
//This tile has a card
public boolean hasCard()
{
return card != null;
}
//This tile has a property
public boolean hasProperty()
{
return property != null;
}
// This tile has an event
public boolean hasEvent()
{
return type < MIN_PROPERTY_VALUE && type != EVENT_STARTING_POSITION && type != EVENT_FREE_PARKING && type != EVENT_JAIL;
}
//Prints out the type as a string
public String typeToString()
{
if(hasCard())
return card.toString();
else if(hasProperty())
return property.getName();
else if(type < MIN_PROPERTY_VALUE && -type < NAMES.length)
return NAMES[-type];
return "";
}
//Adds the card to the frame if this tile has a card
public void addCardToFrame(JFrame frame)
{
if(hasCard())
card.addToFrame(frame);
}
// Gets a new type of card if the tile has one
public void getNewTypeCard()
{
if(hasCard())
card.getNewType();
}
//Getters
public int getType()
{
return type;
}
public Property getProperty()
{
return property;
}
public Card getCard() {
return card;
}
//Setters
public void setCard(Card card) {
this.card = card;
}
//Needs to create property if correct type?
public void setType(int type)
{
this.type = type;
}
public void setProperty(Property property)
{
this.property = property;
}
//ToString
public String toString()
{
return "Tile of type: "+typeToString(); //+propertyToString();
}
}