-
Notifications
You must be signed in to change notification settings - Fork 0
/
Yahtzee.java
1487 lines (1367 loc) · 48.1 KB
/
Yahtzee.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Course: CSC421
* Assignment: #3
* Author: Andrew Seligman
* Date: May 7, 2014
* File: Yahtzee.java
* Note: The networking code in this file has been adapted from
* SocketClient.java, a file provided by Dr. Spiegel
*******************************************************************************/
import java.io.*;
import java.util.*;
import java.applet.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
import java.io.IOException;
import java.net.*;
import java.util.concurrent.*;
/**
* Yahtzee.java is the main class for the game. It contains the set of dice and
* the players, and drives the Turn class as the game is played.
**/
public class Yahtzee extends JApplet implements ActionListener
{
int numPlayers = 1;
Dice PlayerDice;
Player[] ThePlayers;
Player CurrentPlayer, myPlayer;
int[] Cheats;
LinkedList<Player> PlayerQ;
LinkedList<Player> FinishedQ;
Turn PlayerTurn;
JLabel LblName;
JLabel LblTurns;
JLabel LblPrompt;
JButton BtnRoll;
//main container for applet
JLayeredPane Window;
//start menu components
JPanel StartMenu, StartMenuSub1, StartMenuSub2, StartMenuSub3;
ButtonGroup NumPlayers;
JPanel LblSingle;
JPanel LblComputer;
int length;
JLabel[] tempLabelArray;
JRadioButton OnePlayer;
JRadioButton TwoPlayers;
JButton Play;
//cheat panels
JPanel CheatPanel;
JPanel CheatPanelSub1;
JPanel CheatPanelSub2;
JPanel CheatPanelSub3;
JPanel CheatPanelSub4;
JPanel[] CheatCategory;
JTextField[] CheatScores;
//panels for main game
public PlayerJP PlayerPanel;
public JPanel ScorePanel;
public JPanel DicePanel;
public PreviousJP PreviousPanel;
public DialogJP DialogPanel;
private Image background;
private ImageIcon bg;
private JLabel BG;
private CharacterImage ImageGet;
//network components
public static final int PORT = 0;
final String SERVERNAME = "redacted";
boolean useServer = false;
Socket socket = null;
PrintWriter out = null;
BufferedReader in = null;
String playerName;
String isInternet;
String[] playerNames;
int orderIdx;
volatile boolean stillWaitingForServer;
ImageIcon[] icons;
private JLabel Dots;
boolean isGameOver = false;
//sounds
SoundLib Sounds;
SoundLib PlayBtnSounds;
SoundTask TurnAlert;
SoundTask LastTurn;
JButton muteButton;
/**
* Init gets parameters from the HTML launch page and calls
* setUpStartMenu() to prepare the game setup screen.
**/
public void init()
{try{
//applet parameters
playerName = getParameter("Name");
isInternet = getParameter("IsNet");
if(isInternet.equals("true"))
{
useServer = true;
icons = new ImageIcon[4];
icons[0] = new ImageIcon(getClass().getResource("waiting0.png"));
icons[1] = new ImageIcon(getClass().getResource("waiting1.png"));
icons[2] = new ImageIcon(getClass().getResource("waiting2.png"));
icons[3] = new ImageIcon(getClass().getResource("waiting3.png"));
Dots = new JLabel();
Dots.setIcon(icons[0]);
}
else
{
useServer = false;
}
//transform text into images
ImageGet = new CharacterImage();
loadSounds();
setUpStartMenu();
}catch (Exception e) { e.printStackTrace();}
}
/**
* Terminate timers and threads to allow the applet to close properly
**/
public void destroy()
{
try
{
PlayerPanel.killThreads();
TurnAlert.cancelSound();
}
catch(Exception ex){}
}
/**
* Load sounds that will be played during the course of the game.
**/
public void loadSounds()
{
String[] soundFiles = new String[13];
soundFiles[0] = new String("buttonClicked.wav");
soundFiles[1] = new String("mouseOver.wav");
soundFiles[2] = new String("yourTurn.wav");
soundFiles[3] = new String("gameStarting.wav");
soundFiles[4] = new String("ready1.wav");
soundFiles[5] = new String("ready2.wav");
soundFiles[6] = new String("ready3.wav");
soundFiles[7] = new String("metalHit.wav");
soundFiles[8] = new String("anotherRound.wav");
soundFiles[9] = new String("putOnTab.wav");
soundFiles[10] = new String("smallClick.wav");
soundFiles[11] = new String("won.wav");
soundFiles[12] = new String("lost.wav");
Sounds = new SoundLib(soundFiles);
if(useServer)
{
soundFiles = new String[3];
soundFiles[0] = new String("ready1.wav");
soundFiles[1] = new String("ready2.wav");
soundFiles[2] = new String("ready3.wav");
PlayBtnSounds = new SoundLib(soundFiles);
}
}
/**
* Initialize game components and display the background image
**/
public void setUpStartMenu()
{
boolean errorOccurred = false;
PlayerQ = new LinkedList<Player>();
FinishedQ = new LinkedList<Player>();
LblName = new JLabel();
LblTurns = new JLabel();
LblPrompt = new JLabel("Roll the dice");
//panels for main game
PlayerPanel = new PlayerJP();
ScorePanel = new JPanel();
DicePanel = new JPanel();
PlayerDice = new Dice(-1, !useServer);
muteButton = PlayerDice.getMute();
PreviousPanel = new PreviousJP();
DicePanel.setOpaque(false);
ScorePanel.setOpaque(false);
this.setSize(800,600);
//Window is the main container for all game content
Window = new JLayeredPane();
Window.setLayout(new BorderLayout());
if(useServer)
{
errorOccurred = startConnection();
if(!errorOccurred)
{
setupInternet();
bg = new ImageIcon(getClass().getResource("waiting.png"));
}
}
else
{
setupLocal();
orderIdx = 0;
playerNames = new String[2];
playerNames[0] = playerName;
playerNames[1] = "Robot";
bg = new ImageIcon(getClass().getResource("paperfullbg.png"));
}
if(!errorOccurred)
{
//background image
BG = new JLabel();
BG.setIcon(bg);
Window.setLayer(BG, 9);
Window.add(BG);
add(Window);
Window.validate();
Window.repaint();
revalidate();
repaint();
if(useServer)
{
//run waitForServer() in a separate thread so EDT can draw GUI
SwingWorker worker = new SwingWorker(){
protected String doInBackground()
{
waitForServer();
return null;
}
};
worker.execute();
}
}
}
/**
* Connect to the server and send this player's name
*
* @return whether an exception was thrown while attempting to connect
**/
public boolean startConnection()
{
boolean errorOccurred = false;
try
{
socket = new Socket(SERVERNAME, PORT);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
catch(UnknownHostException e)
{
System.out.println("Unknown host: " + SERVERNAME);
errorOccurred = true;
}
catch(IOException e)
{
System.out.println("No I/O");
errorOccurred = true;
}
catch(Exception e)
{
System.out.println("General Exception: " + e);
errorOccurred = true;
}
if(errorOccurred)
{
bg = new ImageIcon(getClass().getResource("serverUnavailable.png"));
BG = new JLabel();
BG.setIcon(bg);
add(BG);
revalidate();
repaint();
}
else
{
//Send player's name to server
out.println(playerName);
}
return errorOccurred;
}
/**
* Cycle images on the waiting screen
**/
public void showDots(int idx)
{
Window.remove(Dots);
Dots.setIcon(icons[idx]);
Window.add(Dots);
Window.repaint();
}
/**
* Animate the waiting screen, receive order index (this applet's turn
* order), number of players, and their names from server, then
* start the game
**/
public void waitForServer()
{
stillWaitingForServer = true;
Window.setLayer(Dots, 11);
//animate waiting message using new thread
SwingWorker BGworker = new SwingWorker(){
protected String doInBackground()
{
while(true)
{
for(int idx = 0; idx < 4; idx++)
{
showDots(idx);
try
{
if(stillWaitingForServer)
Thread.sleep(250);
else
return null;
}
catch(Exception ex)
{
System.out.println(
"waitForServer Exception: sleep failed");
}
}
}
}
};
BGworker.execute();
try
{
orderIdx = Integer.parseInt(in.readLine());
numPlayers = Integer.parseInt(in.readLine());
playerNames = new String[numPlayers];
for(int idx = 0; idx < numPlayers; idx++)
{
playerNames[idx] = in.readLine();
}
}
catch(IOException e){
System.out.println("************* Name read failed *************");
System.exit(1);
}
stillWaitingForServer = false;
try
{//wait for worker thread to finish
Thread.sleep(2000);
}
catch(Exception ex)
{
System.out.println(
"waitForServer Exception2: sleep failed");
System.exit(-1);
}
Window.remove(Dots);
startGame(numPlayers, -1);
getAnotherMessage();
}
/**
* Initialize the dice and players then call playGame() to set up
* the game board.
*
* @param numPlayers is the number of players in the game
*
* @param seed determines the outcome of the PRNG used for the dice
**/
public void startGame(int numPlayers, int seed)
{
boolean drawABox;
ThePlayers = new Player[numPlayers];
//change background for network game
if(useServer)
{
bg = new ImageIcon(getClass().getResource("paperfullbg.png"));
BG.setIcon(bg);
Window.repaint();
}
for(int playerNum = 1; playerNum <= numPlayers; playerNum++)
{
if(orderIdx == playerNum-1)
drawABox = true;
else
drawABox = false;
switch(playerNum)
{
case 1:
{
ThePlayers[0] = new Human(playerNames[0], this, drawABox);
//only add action listeners to a scoresheet if this player
//is playing with this applet
if(orderIdx == 0)
{
createListeners(ThePlayers[0].getScoresheet());
myPlayer = ThePlayers[0];
}
if(!useServer)
addCheatsToScoresheet(ThePlayers[0], CheatPanel);
PlayerQ.add(ThePlayers[0]);
break;
}
case 2:
{
if(useServer)
{
ThePlayers[1] = new Human(playerNames[1], this,
drawABox);
if(orderIdx == 1)
{
createListeners(ThePlayers[1].getScoresheet());
myPlayer = ThePlayers[1];
}
}
else
{
ThePlayers[1] = new Robot(playerNames[1], this, false);
}
PlayerQ.add(ThePlayers[1]);
break;
}
case 3:
{
ThePlayers[2] = new Human(playerNames[2], this, drawABox);
if(orderIdx == 2)
{
createListeners(ThePlayers[2].getScoresheet());
myPlayer = ThePlayers[2];
}
PlayerQ.add(ThePlayers[2]);
break;
}
case 4:
{
ThePlayers[3] = new Human(playerNames[3], this, drawABox);
if(orderIdx == 3)
{
createListeners(ThePlayers[3].getScoresheet());
myPlayer = ThePlayers[3];
}
PlayerQ.add(ThePlayers[3]);
break;
}
case 5:
{
ThePlayers[4] = new Human(playerNames[4], this, drawABox);
if(orderIdx == 4)
{
createListeners(ThePlayers[4].getScoresheet());
myPlayer = ThePlayers[4];
}
PlayerQ.add(ThePlayers[4]);
break;
}
}
PlayerPanel.addPlayer(ThePlayers[playerNum-1]);
}
PlayerPanel.doneAddingPlayers();
playGame();
}
/**
* Add action listeners to buttons on a Scoresheet JPanel.
**/
public void createListeners(JPanel PPanel)
{
Component[] Fields = PPanel.getComponents();
for(int idx = 3; idx < 43; idx+=3)
{
((JButton)Fields[idx]).addActionListener(this);
}
}
/**
* Prepare the game board and initiate the first player's turn.
*
* Precondition: startGame has already been called
**/
public void playGame()
{
Window.remove(StartMenu);
JLayeredPane GameInfo = new JLayeredPane();
GameInfo.setLayout(new BoxLayout(GameInfo, BoxLayout.PAGE_AXIS));
GameInfo.setOpaque(false);
Window.setLayer(GameInfo, 10);
Window.setLayer(ScorePanel, 10);
Window.setLayer(PlayerPanel, 10);
Window.setLayer(DicePanel, 10);
//score panel
ScorePanel.setLayout(new GridLayout(1,1));
Window.add(ScorePanel, BorderLayout.EAST);
//player panel
Window.add(PlayerPanel, BorderLayout.NORTH);
//dice panel
DicePanel.setLayout(new BoxLayout(DicePanel, BoxLayout.LINE_AXIS));
DicePanel.add(Box.createRigidArea(new Dimension(0,115)));
DicePanel.add(PlayerDice);
Window.add(DicePanel, BorderLayout.SOUTH);
Window.add(GameInfo, BorderLayout.CENTER);
CurrentPlayer = PlayerQ.pop();
ScorePanel.add(CurrentPlayer.getScoresheet());
DialogPanel = new DialogJP(CurrentPlayer.getName(),
CurrentPlayer.getNumTurns(), muteButton);
GameInfo.add(PreviousPanel);
GameInfo.add(DialogPanel);
Window.revalidate();
Window.repaint();
BtnRoll = DialogPanel.getButton();
BtnRoll.addActionListener(this);
if(CurrentPlayer == myPlayer)
{
BtnRoll.setEnabled(true);
if(muteButton.isEnabled())
Sounds.playSound("yourTurn.wav");
if(CurrentPlayer.getNumTurns() == 1 && muteButton.isEnabled())
{//possible if cheat is used
LastTurn = new SoundTask(1000 * 1, "lastTurn.wav");
}
if(useServer && muteButton.isEnabled())
TurnAlert = new SoundTask(1000 * 60, "hurryUp.wav");
}
else
BtnRoll.setEnabled(false);
PlayerTurn = new Turn(PlayerDice, CurrentPlayer, DialogPanel);
}
/**
* Call methods for the end of a player's turn, enable or disable certain
* features depending on the player to go next, and start the next
* player's turn
**/
public void nextPlayer()
{try{
CurrentPlayer.updateScore();
//remove the previously scored category's potential score
CurrentPlayer.showPotential(PlayerDice.getDieValues());
CurrentPlayer.hidePotential();
CurrentPlayer.disableCategories();
PlayerDice.animateDice(true);
revalidate();
repaint();
if(CurrentPlayer.getNumTurns() != 0)
PlayerQ.add(CurrentPlayer);
else
FinishedQ.add(CurrentPlayer);
PlayerDice.resetHolds();
PreviousPanel.setName(CurrentPlayer.getName());
PreviousPanel.setCategory(CurrentPlayer.getLastCategory());
PreviousPanel.setScore("" + CurrentPlayer.getLastScore());
if(PlayerQ.peek() != null)
{
ScorePanel.remove(CurrentPlayer.getScoresheet());
CurrentPlayer = PlayerQ.pop();
ScorePanel.add(CurrentPlayer.getScoresheet());
PlayerPanel.shiftPlayers(CurrentPlayer);
if(CurrentPlayer == myPlayer)
BtnRoll.setEnabled(true);
else
BtnRoll.setEnabled(false);
if(CurrentPlayer instanceof Human)
{
if(CurrentPlayer == myPlayer)
{
if(muteButton.isEnabled())
Sounds.playSound("yourTurn.wav");
if(CurrentPlayer.getNumTurns() == 1 &&
muteButton.isEnabled())
LastTurn = new SoundTask(1700, "lastTurn.wav");
if(useServer && muteButton.isEnabled())
TurnAlert = new SoundTask(1000 * 60, "hurryUp.wav");
}
DialogPanel.setAction("Roll the dice");
}
else
{
DialogPanel.setAction("Robot is rolling the dice");
}
DialogPanel.setPlayer(CurrentPlayer.getName());
DialogPanel.setTurns(CurrentPlayer.getNumTurns());
DialogPanel.setButton(1);
PlayerTurn = new Turn(PlayerDice, CurrentPlayer, DialogPanel);
//automatically take the turn for a computer player
if(CurrentPlayer instanceof Robot)
{
SwingWorker waitRobot = new SwingWorker(){
protected String doInBackground()
{
for(int roll = 1; roll < 4; roll++)
{
try
{//allow human players to observe robot's play
Thread.sleep(2000);
}
catch(Exception ex)
{
System.out.println(
"nextPlayer() Exception: sleep failed");
}
process(new String[1]);
}
return null;
}
protected void process(String[] stuff)
{
doRollBtn();
repaint();
}
};
waitRobot.execute();
}
}
else
{
//display scoresheet for the player in control of the applet
ScorePanel.remove(CurrentPlayer.getScoresheet());
ScorePanel.add(myPlayer.getScoresheet());
myPlayer.hidePotential();
if(useServer)
{
isGameOver = true;
//tell server the game has finished
out.println("GAMEOVER");
}
gameOver();
}}catch (Exception e) { e.printStackTrace();}
}
/**
* Display the final score for player and announce the winner
**/
public void gameOver()
{
int waitTime;
if(numPlayers == 1)
{
waitTime = 1700;
DialogPanel.gameOver(true, myPlayer.getFinalScore(), true);
}
else
{
waitTime = 4000;
int score;
int highest = 0;
for(int idx = 0; idx < numPlayers; idx++)
{
CurrentPlayer = FinishedQ.pop();
score = CurrentPlayer.getFinalScore();
if(highest < score)
{
highest = score;
}
}
if(myPlayer.getFinalScore() == highest)
{
if(muteButton.isEnabled())
Sounds.playSound("won.wav");
DialogPanel.gameOver(false, myPlayer.getFinalScore(), true);
}
else
{
if(muteButton.isEnabled())
Sounds.playSound("lost.wav");
DialogPanel.gameOver(false, myPlayer.getFinalScore(), false);
}
}
final JButton playAgain = DialogPanel.getButton();
playAgain.setEnabled(false);
final int timer = waitTime;
SwingWorker waitTimer = new SwingWorker(){
protected String doInBackground()
{
try
{
Thread.sleep(timer);
}
catch(Exception ex)
{
System.out.println(
"Yahtzee.java gameOver() Exception: sleep failed");
}
return null;
}
protected void done()
{
playAgain.setEnabled(true);
if(muteButton.isEnabled())
Sounds.playSound("anotherRound.wav");
}
};
waitTimer.execute();
playAgain.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
if(SwingUtilities.isLeftMouseButton(e))
{
if(muteButton.isEnabled())
Sounds.playSound("putOnTab.wav");
resetGame();
}
}
});
}
/**
* Reset the applet for a new game
**/
public void resetGame()
{
if(useServer)
{
try
{
socket.close();
isGameOver = false;
}
catch(IOException e)
{
System.out.println("Could not close socket");
System.exit(-1);
}
}
remove(Window);
revalidate();
repaint();
numPlayers = 1;
setUpStartMenu();
}
/**
* Place labels and text fields on the given JPanel
**/
public void addCheats()
{
//instantiate a player to get enumerated score categories
Player Dummy = new Robot("Dummy", this, false);
CheatCategory = new JPanel[13];
CheatScores = new JTextField[13];
//create category names
for(int idx = 0; idx < 13; idx++)
{
CheatCategory[idx] = new JPanel();
CheatCategory[idx].setOpaque(false);
CheatCategory[idx].setLayout(new BoxLayout(
CheatCategory[idx], BoxLayout.LINE_AXIS));
tempLabelArray = ImageGet.getLetters(Dummy.getCategoryName(idx));
length = tempLabelArray.length;
CheatCategory[idx].setPreferredSize(new Dimension((15*length),23));
for(int index = 0; index < length; index++)
CheatCategory[idx].add(tempLabelArray[index]);
}
//create textfields
for(int idx = 0; idx < 13; idx++)
{
CheatScores[idx] = new JTextField("0");
CheatScores[idx].setOpaque(false);
CheatScores[idx].setPreferredSize(new Dimension(30,23));
}
//add category names and textfields to cheat panels
for(int idx = 0; idx < 4; idx++)
{
CheatPanelSub1.add(CheatCategory[idx]);
CheatPanelSub1.add(CheatScores[idx]);
CheatPanelSub1.add(Box.createRigidArea(new Dimension(30,0)));//spacer
}
for(int idx = 4; idx < 7; idx++)
{
CheatPanelSub2.add(CheatCategory[idx]);
CheatPanelSub2.add(CheatScores[idx]);
CheatPanelSub2.add(Box.createRigidArea(new Dimension(30,0)));//spacer
}
for(int idx = 7; idx < 10; idx++)
{
CheatPanelSub3.add(CheatCategory[idx]);
CheatPanelSub3.add(CheatScores[idx]);
CheatPanelSub3.add(Box.createRigidArea(new Dimension(30,0)));//spacer
}
for(int idx = 10; idx < 13; idx++)
{
CheatPanelSub4.add(CheatCategory[idx]);
CheatPanelSub4.add(CheatScores[idx]);
CheatPanelSub4.add(Box.createRigidArea(new Dimension(30,0)));//spacer
}
}
/**
* Add values given at the setup menu to a player's scoresheet
**/
public void addCheatsToScoresheet(Player P1, JPanel CheatPanel)
{
int score = 0;
for(int idx = 0; idx < 13; idx++)
{
try
{
score = Integer.parseInt(CheatScores[idx].getText());
}
catch(Exception ex)
{
score = 0;
}
if(score != 0)
P1.cheatCategory(idx, score);
}
//need to enable categories to set disabled properties
P1.enableCategories();
P1.disableCategories();
}
/**
* Set up the applet for a network game
**/
public void setupInternet()
{
//start menu components
StartMenu = new JPanel();
StartMenuSub1 = new JPanel();
StartMenuSub2 = new JPanel();
StartMenuSub3 = new JPanel();
Play = new JButton();
StartMenu.setLayout(new GridLayout(3,1));
StartMenu.setSize(800,600);
StartMenu.setOpaque(false);
StartMenuSub1.setLayout(new BoxLayout(
StartMenuSub1, BoxLayout.LINE_AXIS));
StartMenuSub1.add(Box.createRigidArea(new Dimension(200,115)));
StartMenuSub1.setOpaque(false);
StartMenuSub2.setLayout(new BoxLayout(
StartMenuSub2, BoxLayout.LINE_AXIS));
StartMenuSub2.add(Box.createRigidArea(new Dimension(0,115)));
StartMenuSub2.setOpaque(false);
StartMenuSub3.setLayout(new BoxLayout(
StartMenuSub3, BoxLayout.LINE_AXIS));
StartMenuSub3.setOpaque(false);
//add content to start menu
//add JPanels with no layout set
JPanel CheatPanel1 = new JPanel();
CheatPanel1.setOpaque(false);
JPanel CheatPanel2 = new JPanel();
CheatPanel2.setOpaque(false);
JPanel CheatPanel3 = new JPanel();
CheatPanel3.setOpaque(false);
JPanel CheatPanel4 = new JPanel();
CheatPanel4.setOpaque(false);
//play button
final JButton currentButton = new JButton();
currentButton.addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent e)
{
if(currentButton.isEnabled())
{
currentButton.setBorder(
BorderFactory.createLoweredBevelBorder());
if(muteButton.isEnabled())
Sounds.playSound("mouseOver.wav");
currentButton.setIcon(new ImageIcon(
getClass().getResource("PlayNowHighlight.png")));
}
}
public void mouseExited(MouseEvent e)
{
currentButton.setBorder(BorderFactory.createEmptyBorder());
if(currentButton.isEnabled())
{
currentButton.setIcon(new ImageIcon(
getClass().getResource("PlayNow.png")));
}
}
});
currentButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(muteButton.isEnabled())
Sounds.playSound("buttonClicked.wav");
//send ready signal to server
out.println("READY");
currentButton.setEnabled(false);
try
{//wait for first sound to play
Thread.sleep(200);
}
catch(Exception ex)
{
System.out.println(
"setUpInternet() Exception: sleep failed");
}
if(muteButton.isEnabled())
PlayBtnSounds.playRandom();
}
}
);
Play = currentButton;
Play.setPreferredSize(new Dimension(250,75));
Play.setBorder(BorderFactory.createEmptyBorder());
Play.setOpaque(false);
Play.setBackground(new Color(0,0,0,0));
Play.setFocusPainted(false);
Play.setContentAreaFilled(false);
Play.setIcon(new ImageIcon(getClass().getResource("PlayNow.png")));
//add JPanel with no layout set
JPanel BtnPanel = new JPanel();
BtnPanel.setOpaque(false);
BtnPanel.add(Play);
StartMenuSub3.add(BtnPanel);
StartMenu.add(StartMenuSub1);
StartMenu.add(StartMenuSub2);
StartMenu.add(StartMenuSub3);
Window.setLayer(StartMenu, 10);
Window.add(StartMenu, BorderLayout.CENTER);
//add listeners to dice to transmit holds across the network
for(int idx = 0; idx < 5; idx++)
{
final int index = idx;
PlayerDice.getDie(idx).addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
if(SwingUtilities.isLeftMouseButton(e) &&
CurrentPlayer == myPlayer)
{
out.println("16");
out.println(""+index);
}
}
}
);
}
}