-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dice.java
411 lines (376 loc) · 11.6 KB
/
Dice.java
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
* Course: CSC421
* Assignment: #3
* Author: Andrew Seligman
* Date: May 7, 2014
* File: Dice.java
*******************************************************************************/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* Dice.java holds an array of five Die objects. It contains methods to get and
* set values held by the Die objects.
**/
public class Dice extends JPanel
{
private RandomNum RandomDieValue;
private Die[] TheDice;
private SoundLib Sound, RemoveSound;
private String[] soundFiles1, soundFiles2;
private boolean showingDice;
private JButton muteButton;
/**
* Constructor initializes the pseudorandom number generator and the array
* of Die objects.
*
* @param seed is a seed for a pseudorandom number generator that
* represents a value on a die. Using a value < 0 results in a different
* number each time; using a value >= 0 allows a game to be repeated
* for testing purposes.
**/
public Dice(int seed, boolean canCheat)
{
super();
RandomDieValue = new RandomNum(seed);
showingDice = false;
//mute button
final JButton currentButton = new JButton();
currentButton.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
if(currentButton.isEnabled())
{
currentButton.setEnabled(false);
}
else
{
currentButton.setEnabled(true);
}
}
});
muteButton = currentButton;
muteButton.setBorder(BorderFactory.createEmptyBorder());
muteButton.setOpaque(false);
muteButton.setBackground(new Color(0,0,0,0));
muteButton.setFocusPainted(false);
muteButton.setBorderPainted(false);
muteButton.setContentAreaFilled(false);
muteButton.setIcon(new ImageIcon(
getClass().getResource("MuteUnchecked.png")));
muteButton.setDisabledIcon(new ImageIcon(
getClass().getResource("MuteChecked.png")));
muteButton.setBounds(0,70,115,27);
add(muteButton);
TheDice = new Die[5];
for(int idx = 0; idx < 5; idx++)
{
TheDice[idx] = new Die(RandomDieValue.getNum(), idx, canCheat,
muteButton);
final Die theDie = TheDice[idx];
theDie.addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent e)
{
theDie.setBounds(theDie.getLocation().x,
theDie.getLocation().y - 3, 100, 100);
repaint();
}
public void mouseExited(MouseEvent e)
{
theDie.setBounds(theDie.getLocation().x,
theDie.getLocation().y + 3, 100, 100);
repaint();
}
});
}
soundFiles1 = new String[1];
soundFiles1[0] = new String("putDie.wav");
soundFiles2 = new String[2];
soundFiles2[0] = new String("removeDie1.wav");
soundFiles2[1] = new String("removeDie2.wav");
RemoveSound = new SoundLib(soundFiles2);
setOpaque(false);
setLayout(null);
setBounds(0,0,800,115);
revalidate();
}
/**
* @return a reference to the mute JButton
**/
public JButton getMute()
{
return muteButton;
}
/**
* @return a reference to a Die object contained in the array TheDice
*
* @param index is the index of the Die to be returned
**/
public Die getDie(int index)
{
return TheDice[index];
}
/**
* add the dice to this panel using animation
**/
private void addDice()
{
removeAll();
add(muteButton);
for(int idx = 0; idx < 5; idx++)
{
add(TheDice[idx]);
TheDice[idx].putImage();
slideDieIn(idx);
}
revalidate();
showingDice = true;
}
/**
* Move a die upwards into position. Timed using a worker thread.
* The start of the animation is delayed more for each successive
* die to be animated, starting at no delay for the first die.
*
* @param index is the index of the Die in TheDice[] to animate
**/
private void slideDieIn(final int index)
{
if(!isDieHeld(index))
{
int count = 0;
//gather a count of the held dice for proper delay timing
for(int idx = 0; idx < index; idx++)
{
if(isDieHeld(idx))
count++;
}
final int numHeld = count;
SwingWorker animateDie = new SwingWorker(){
protected String doInBackground()
{
try
{ //delay the start of the animation based on the index
//and number of held dice
Thread.sleep(100*(index-numHeld));
}
catch(Exception ex)
{
System.out.println(
"Dice.java slideDieIn() Exception1: sleep failed");
}
if(muteButton.isEnabled())
{
Sound = new SoundLib(soundFiles1);
Sound.playSound("putDie.wav");
}
for(int idx = 0; idx < 20; idx++)
{
try
{
Thread.sleep(12);
}
catch(Exception ex)
{
System.out.println(
"Dice.java slideDieIn() Exception2: sleep failed");
}
process(new String[1], idx);
}
return null;
}
protected void process(String[] stuff, int idx)
{
TheDice[index].setBounds(150+100*index,105-5*idx,100,100);
repaint();
}
};
animateDie.execute();
}
}
/**
* Move a die downward off the board using a worker thread for animation
* timing.
*
* @param index is the index of the Die in TheDice[] to animate
*
* @param allDice determines whether to ignore the held flag in order
* to remove all of the dice
**/
private void slideDieOut(final int index, boolean allDice)
{
if(allDice || !isDieHeld(index))
{
if(muteButton.isEnabled())
RemoveSound.playSound("removeDie1.wav");
SwingWorker animateDie = new SwingWorker(){
protected String doInBackground()
{
for(int idx = 0; idx < 20; idx++)
{
try
{
Thread.sleep(12);
}
catch(Exception ex)
{
System.out.println(
"Dice.java slideDieOut() Exception2: sleep failed");
}
process(new String[1], idx);
}
return null;
}
protected void process(String[] stuff, int idx)
{
TheDice[index].setBounds(150+100*index,6*idx+10,100,100);
repaint();
}
};
animateDie.execute();
}
}
/**
* Remove the dice from this panel using animation
*
* @param allDice determines whether to ignore the held flag in order
* to remove all of the dice
**/
private void removeDice(boolean allDice)
{
for(int idx = 0; idx < 5; idx++)
slideDieOut(idx, allDice);
showingDice = false;
}
/**
* Main method to control dice animation
*
* @param endTurn determines whether the dice reappear if they are taken
* off of the game board
**/
public void animateDice(boolean endTurn)
{
if(endTurn)
removeDice(true);
else if(showingDice)
{
removeDice(false);
//wait for dice removal animation to complete before adding
//them again
SwingWorker timer = new SwingWorker()
{
protected String doInBackground()
{
try
{
Thread.sleep(500);
}
catch(Exception ex)
{
System.out.println(
"Dice.java animateDice() Exception1: sleep failed");
}
return null;
}
protected void done()
{
addDice();
}
};
timer.execute();
}
else
addDice();
}
/**
* Set the number held by a Die at a specific location in the array
*
* @param location is the index of the array holding the Die to be modified
*
* @param newValue is the new number held by the Die
**/
public void setNumber(int location, int newValue)
{
TheDice[location].setNumber(newValue, false);
}
/**
* Get the number held by a Die at a specific location in the array
*
* @param index of the array holding the Die to be examined
*
* @return the number held by a Die
**/
public int getNum(int index)
{
return TheDice[index].getNumber();
}
/**
* Set or remove flags for dice that the player would like to hold
*
* @param index of the array corresponding to the die to be held
**/
public void holdDie(int index)
{
TheDice[index].hold();
}
/**
* Return the status of a Die being held
*
* @param index of the Die to examine
*
* @return whether the Die is currently held
**/
public boolean isDieHeld(int index)
{
return TheDice[index].isHeld();
}
/**
* Reset flags for dice to be held
**/
public void resetHolds()
{
for(int idx = 0; idx < 5; idx++)
{
TheDice[idx].resetHold();
}
}
/**
* Generate new values for dice that are not held, but do not update the
* images on the dice
**/
public void rollDice()
{
for(int idx = 0; idx < 5; idx++)
{
if(!TheDice[idx].isHeld())
{
TheDice[idx].setNumber(RandomDieValue.getNum(), false);
}
}
}
/**
* Return the value of each Die
*
* @return a string containing the five Die values
**/
public String getDieValues()
{
String DieValues = new String();
for(int idx = 0; idx < 5; idx++)
{
DieValues = DieValues +
((Integer)(TheDice[idx].getNumber())).toString() + " ";
}
return DieValues;
}
/**
* Set the flag for each die to "held"
**/
public void holdAll()
{
resetHolds();
for(int idx = 0; idx < 5; idx++)
holdDie(idx);
}
}