-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGame.java
More file actions
301 lines (256 loc) · 7.58 KB
/
Copy pathGame.java
File metadata and controls
301 lines (256 loc) · 7.58 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
// Imports
import java.io.File;
import java.util.ArrayList;
import javax.sound.sampled.*;
import javax.swing.*;
/**
* Jan 22, 2022 <br>
* This is the game class. It holds the arraylist of players and tiles and variables of
* dice. This is the class where the users play monopoly inside of.
*/
public class Game
{
//Class Constants
public static final ImageIcon BOARD = new ImageIcon(Player.IMAGE_PATH+"900xBoard"+Player.IMAGE_FILE_TYPE);
public static final int X_BOARD = 10;
public static final int Y_BOARD = 10;
public static final String SONGFILE_STRING = ".//Best Song in The World.wav";
private static Clip clip;
//Instance variables
private ArrayList<Player> players;
private ArrayList<Tile> tiles;
private int currentPlayer;
private Dice diceLeft;
private Dice diceRight;
private JButton roll;
private JButton mute;
private int recursionDepth;
private JFrame frame;
/**
* The constructor creates a game object based on the numPlayers parameter.
* @param numPlayers
*/
public Game(int numPlayers)
{
//Initialize players, tiles and currentPlayer index
players = new ArrayList<Player>();
tiles = new ArrayList<Tile>();
currentPlayer = 0;
//Creates players and tiles adding them into their appropriated arraylist
for(int i=0; i < numPlayers; i++)
players.add(new Player(i));
for(int i=0; i < Tile.LIST_OF_TILES.length; i++)
tiles.add(new Tile(Tile.LIST_OF_TILES[i]));
//Creates the left and right dice and the roll button
diceLeft = new Dice(0);
diceRight = new Dice(1);
roll = new JButton("Roll");
mute = new JButton("Mute");
recursionDepth = 1;
frame = new JFrame("Monopoly");
}
//Methods
/**
* Plays the music file based on the music location parameter. This procedure is used
* by the play() method.
* @param musicLocation
*/
public static void playMusic(String musicLocation)
{
// Tries to get the music location
try
{
File musicPath = new File(musicLocation);
// If the music file exists so play the song
if(musicPath.exists())
{
AudioInputStream audioInput = AudioSystem.getAudioInputStream(musicPath);
clip = AudioSystem.getClip();
clip.open(audioInput);
clip.start();
}
}
// The music file doesn't exist
catch(Exception e)
{
System.out.println("No music found");
}
}
/**
* This method goes to the next player. If the next player is the first
* player in the list then it goes through each player and reduces their jail time.
* It also checks if there is only 1 player left using recursion to see who is the winner.
*/
public void goToNextPlayer()
{
currentPlayer++;
if(currentPlayer >= players.size())
{
currentPlayer = 0;
for(Player p: players)
p.reduceJailTime();
}
// Checks if only 1 player in list of players can play (Exit condition)
if(recursionDepth >= players.size())
{
frame.dispose();
// Finds the last player
for(Player p: players)
if(p.canPlay())
JOptionPane.showMessageDialog(null, p.getName()+" won");
}
// Gets the next player
Player player = players.get(currentPlayer);
// Using recursion run through the players until the exit condition is meet
if(!player.canPlay())
{
recursionDepth++;
player.getIndicatorLabelLost().setVisible(true);
goToNextPlayer();
}
recursionDepth = 1;
}
/**
* This method is the main one of for this class it plays the game and calls all the
* other methods.
*/
public void play()
{
//Initialize frame
frame.setLayout(null);
// frame.setSize(1900,1000);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
//Start music
playMusic(SONGFILE_STRING);
//Draws the info and adds action listeners to button for each player
for(Player p: players)
{
p.drawInfo(frame);
p.addActionListeners(this);
}
//Board image
JLabel boardLabel = new JLabel(BOARD);
boardLabel.setBounds(10, 10, 900, 900);
frame.add(boardLabel);
//Sets the bounds of the roll and mute button
roll.setBounds(960,780,60,112);
mute.setBounds(1600,860,100,50);
//Adds Action Listeners to roll
roll.addActionListener(e ->
{
// Gets the player based on the current player variable, updates its info
Player player = players.get(currentPlayer);
player.updateInfo();
player.setIndicatorLabelVisible(true);
diceLeft.getNewValue();
diceRight.getNewValue();
// If the player rolls a double
if(diceLeft.getValue() == diceRight.getValue())
{
player.increaseRollDouble();
// Player rolls 3 doubles and therefore goes to jail
if(player.getRollDouble() == 3)
{
JOptionPane.showMessageDialog(null, "You rolled 3 doubles, so you go to jail");
roll.setVisible(false);
player.setJailTime();
}
// The player rolls a double and gets to roll again
else
{
player.move(diceLeft.getValue()+diceRight.getValue());
player.showBuyingButton(this);
player.setButtonsVisible(true, Player.BUTTON_OWNED_PROPERTIES);
}
}
// If the player rolls something other than a double
else
{
// Move the player and turn of rolling
player.move(diceLeft.getValue()+diceRight.getValue());
roll.setVisible(false);
// Displays the buttons
player.setButtonsVisible(true);
player.showBuyingButton(this);
}
// Gets the current location of the player, tile, creates window and gets new type.
int currentLocation = player.getLocation();
Tile tile = tiles.get(currentLocation);
tile.createWindow(player);
// Checks if the player move and does this the tile event
if(currentLocation != player.getLocation())
{
Tile anotherTile = tiles.get(player.getLocation());
anotherTile.createWindow(player);
player.showBuyingButton(this);
}
// Updates all the players info
updateAllPlayerInfo();
});
// Adds action listener to mute button
mute.addActionListener(e -> {
// Messages asking the player if they want to mute the music
JOptionPane.showMessageDialog(null, "Are you sure you want to end this magnificent music?");
JOptionPane.showMessageDialog(null, "Really?????????");
JOptionPane.showMessageDialog(null, "OK FINE. Just click ok here");
clip.close();
});
//Adds the dice and roll to the frame
diceLeft.addToFrame(frame);
diceRight.addToFrame(frame);
frame.add(roll);
frame.add(mute);
//Sets default close and visible of frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
// Updates all the player info
public void updateAllPlayerInfo()
{
for(Player p: players)
p.updateInfo();
}
//Getters
public ArrayList<Player> getPlayers()
{
return players;
}
public ArrayList<Tile> getTiles()
{
return tiles;
}
public int getCurrentPlayer() {
return currentPlayer;
}
public JButton getRoll()
{
return roll;
}
//Setters
public int getRecursionDepth() {
return recursionDepth;
}
public void setRecursionDepth(int recursionDepth) {
this.recursionDepth = recursionDepth;
}
public void setPlayers(ArrayList<Player> players)
{
this.players = players;
}
public void setTiles(ArrayList<Tile> tiles)
{
this.tiles = tiles;
}
public void setCurrentPlayer(int currentPlayer) {
this.currentPlayer = currentPlayer;
}
public void setRoll(JButton roll)
{
this.roll = roll;
}
//toString
public String toString()
{
return "Players: "+players.toString();//+". Tiles: "+tiles.toString();
}
}