-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameBoard.java
1310 lines (1092 loc) · 52.6 KB
/
GameBoard.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
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Font;
import java.awt.Color;
import java.io.*;
import java.util.*;
import javax.sound.sampled.*;
import java.net.Socket.*;
import java.net.*;
public class GameBoard extends JFrame implements ActionListener{
String chatIp = "localhost";
int chatPort = 15000;
String IPADDRESS1 = "10.190.100.5";
String IPADDRESS2 = "10.190.100.6";
String IPADDRESS3 = "10.190.100.7";
String IPADDRESS4 = "10.190.100.8";
String IPADDRESS5 = "10.190.100.5";
String IPADDRESS6 = "10.190.100.6";
String IPADDRESS7 = "10.190.100.7";
String IPADDRESS8 = "localhost";
int PORT1 = 15001;
int PORT2 = 15002;
int PORT3 = 15003;
int PORT4 = 15004;
int PORT5 = 15005;
int PORT6 = 15006;
int PORT7 = 15007;
int PORT8 = 15008;
int port;
String message;
int turnNumber;
String userName;
String ipAddress;
String player1Name;
String player2Name;
String player3Name;
String player4Name;
boolean wait = false;
boolean go = true;
int clientNumber = 0;
int lettersNeeded = 0;
boolean usernameSet = false;
int boardPoints = 0;
//Socket and in/out streams for the CHAT
Socket chatSocket = null;InputStream chatIn = null;OutputStream chatOut = null;
BufferedReader chatBin = null;PrintStream chatPout = null;
//Socket and in/out streams for the GAME
Socket gameSocket = null;InputStream gameIn = null;OutputStream gameOut = null;
BufferedReader gameBin;PrintStream gamePout;
//Icon object for the game board.
ImageIcon undoImage = new ImageIcon("../Assets/Image/undoButton.png");
ImageIcon shuffleImage = new ImageIcon("../Assets/Image/shuffleButton.png");
ImageIcon endTurnImage = new ImageIcon("../Assets/Image/sendMove.png");
ImageIcon tileBack = new ImageIcon("tile.jpg");
ImageIcon rackTile = new ImageIcon("blankTileResized.jpg");
ImageIcon icon = new ImageIcon("../Assets/Image/Letters/rack/W.jpg");
//Array to hold all the background tiles
ArrayList<BoardTile> backgroundTileArray = new ArrayList<BoardTile>();
ArrayList<JButton> rackTileArray = new ArrayList<JButton>();
ArrayList<String> lettersFromServer = new ArrayList<String>();
//ArrayList<String> playerMoves = new ArrayList<String>();
//method that creates actual gameboard buttons. Stored in backgroundTileArray
public void createBoard(){
for(int i = 0; i<225;i++){
if( i != 112){
BoardTile bt = new BoardTile("board");
backgroundTileArray.add(bt);
}
else{
BoardTile bt1 = new BoardTile("center");
backgroundTileArray.add(bt1);
}
BoardTile.tileNumber++;
}
for(int j = 0; j < 225; j++){
//System.out.println("Size of Array: " + backgroundTileArray.size() );
gameBoardPanel.add( (backgroundTileArray.get(j)).fetchButton() );
}
}
//JButton jb = new JButton("test");
JButton shuffleButton = new JButton(shuffleImage);
JButton endTurnButton = new JButton(endTurnImage);
JButton test = new JButton("bloop");
//all the panels for the UI. Will need to do some work here
JPanel gameBoardPanel = new JPanel(new GridLayout(15,15));JPanel playerPanel = new JPanel(new FlowLayout());
JPanel chatPanel = new JPanel(new BorderLayout());JPanel chatWindowPanel = new JPanel(new GridLayout(1,1));
JPanel chatPane = new JPanel(new GridBagLayout());GridBagConstraints gbc = new GridBagConstraints();
JPanel bottomPanel = new JPanel(new BorderLayout());JPanel rackPanel = new JPanel(new GridLayout(1,7));
JPanel separatorPanel = new JPanel(new GridLayout(1,1));JPanel playButtonPanel = new JPanel(new GridLayout(1,3));
JPanel serverListPanel = new JPanel();
//Top Row Player names panel
JLabel player1Label = new JLabel("Player 1");JTextField player1ScoreField = new JTextField(2);
JLabel player2Label = new JLabel("Player 2");JTextField player2ScoreField = new JTextField(2);
JLabel player3Label = new JLabel("Player 3");JTextField player3ScoreField = new JTextField(2);
JLabel player4Label = new JLabel("Player 4");JTextField player4ScoreField = new JTextField(2);
//individual parts for the chat, add the textArea for the list of names in the chat
JTextArea chatArea = new JTextArea(10, 10);JScrollPane chatScroll = new JScrollPane(chatArea);
JTextField chatField = new JTextField(10);JButton enterButton = new JButton("Enter");
//JMenuBar
JMenuBar jmb = new JMenuBar();JMenu menu = new JMenu("Menu");
JMenuItem newGameItem = new JMenuItem("New Game");JMenuItem quitItem = new JMenuItem("Quit");
JButton undoButton = new JButton(undoImage);
public GameBoard(){
//size is set, can be changed, "relativeToNull" puts the window in the middle of screen
setSize(830, 680);
setResizable(false);
setLocationRelativeTo(null);
setTitle("Werds with Nerds");
//serverlist pop up, disable serverSelect() to disable popup
serverListPanel.setLayout(new GridLayout(8,1));
ButtonGroup bg = new ButtonGroup();
JRadioButton server1 = new JRadioButton("Winter is Coming");
JRadioButton server2 = new JRadioButton("Billy's Server");
JRadioButton server3 = new JRadioButton("Kennedy's Backet of Deplorables");
JRadioButton server4 = new JRadioButton("La casa de tu madre");
JRadioButton server5 = new JRadioButton("Bush did Harambe");
JRadioButton server6 = new JRadioButton("Trump's America");
JRadioButton server7 = new JRadioButton("More Cowbell");
JRadioButton server8 = new JRadioButton("localhost(launch server on port 15008)");
bg.add(server1);bg.add(server2);bg.add(server3);bg.add(server4);bg.add(server5);
bg.add(server6);bg.add(server7);bg.add(server8);
serverListPanel.add(server1);serverListPanel.add(server2);serverListPanel.add(server3);
serverListPanel.add(server4);serverListPanel.add(server5);serverListPanel.add(server6);
serverListPanel.add(server7);serverListPanel.add(server8);
serverSelect();//disable this to disable pop up
if(server1.isSelected()){ipAddress = IPADDRESS1;port = PORT1;}
else if(server2.isSelected()){ipAddress = IPADDRESS2;port = PORT2;}
else if(server3.isSelected()){ipAddress = IPADDRESS3;port = PORT3;}
else if(server4.isSelected()){ipAddress = IPADDRESS4;port = PORT4;}
else if(server5.isSelected()){ipAddress = IPADDRESS5;port = PORT5;}
else if(server6.isSelected()){ipAddress = IPADDRESS6;port = PORT6;}
else if(server7.isSelected()){ipAddress = IPADDRESS7;port = PORT7;}
else if(server8.isSelected()){ipAddress = IPADDRESS8;port = PORT8;}
player1Label.setBackground(Color.YELLOW);player2Label.setBackground(Color.YELLOW);
player3Label.setBackground(Color.YELLOW);player4Label.setBackground(Color.YELLOW);
//adding gameBoardPanel to background panel
add(gameBoardPanel, BorderLayout.CENTER);
//add(leftSidePanel, BorderLayout.WEST);
add(bottomPanel, BorderLayout.SOUTH);
add(playerPanel, BorderLayout.NORTH);
add(chatPanel, BorderLayout.EAST);
setJMenuBar(jmb);
gameBoardPanel.setBackground(Color.BLACK);
gameBoardPanel.setOpaque(true);
//left side panel stuff
bottomPanel.add(rackPanel, BorderLayout.CENTER);
bottomPanel.add(separatorPanel, BorderLayout.WEST);
bottomPanel.add(playButtonPanel, BorderLayout.EAST);
createRack();
undoButton.setOpaque(true);
//undoButton.setContentAreaFilled(false);
//undoButton.setBorderPainted(false);
shuffleButton.setOpaque(true);
//shuffleButton.setContentAreaFilled(false);
//shuffleButton.setBorderPainted(false);
endTurnButton.setOpaque(true);
//endTurnButton.setContentAreaFilled(false);
//endTurnButton.setBorderPainted(false);
JButton blankButton = new JButton("");
blankButton.setOpaque(false);
blankButton.setBorderPainted(false);
separatorPanel.add(undoButton);
playButtonPanel.add(blankButton);
playButtonPanel.add(shuffleButton);
playButtonPanel.add(endTurnButton);
createBoard(); //This method creates and add tiles to panel
//rewriteBoard();
//populating top row player panel
playerPanel.add(player1Label);playerPanel.add(player1ScoreField);playerPanel.add(player2Label);
playerPanel.add(player2ScoreField);playerPanel.add(player3Label);playerPanel.add(player3ScoreField);
playerPanel.add(player4Label);playerPanel.add(player4ScoreField);
player1ScoreField.setHorizontalAlignment(JTextField.CENTER);player2ScoreField.setHorizontalAlignment(JTextField.CENTER);
player3ScoreField.setHorizontalAlignment(JTextField.CENTER);player4ScoreField.setHorizontalAlignment(JTextField.CENTER);
player1ScoreField.setText("0");player2ScoreField.setText("0");
player3ScoreField.setText("0");player4ScoreField.setText("0");
//putting the chat panel stuff together
chatPanel.add(chatWindowPanel, BorderLayout.CENTER);
chatPanel.add(chatPane, BorderLayout.SOUTH);
chatField.setText("Enter Username!");
//code for the textField and enter button
gbc.gridx = 0;gbc.gridy = 0;gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.WEST;chatPane.add(chatField, gbc);
gbc.gridx++;gbc.insets = new Insets(0, 0, 0, 0);chatPane.add(enterButton, gbc);
chatWindowPanel.add(chatScroll);
chatArea.setEditable(false);
chatArea.setLineWrap(true);
chatArea.setWrapStyleWord(true);
//**menu items
jmb.add(menu);
menu.add(newGameItem);
menu.add(quitItem);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
chatField.requestFocusInWindow(); //this has to stay AFTER the setVisible command
//ActionListeners *********
undoButton.addActionListener(this);
enterButton.addActionListener(this);
shuffleButton.addActionListener(this);
endTurnButton.addActionListener(this);
chatField.addActionListener(this);
quitItem.addActionListener(this);
newGameItem.addActionListener(this);
enterButton.setActionCommand("keyboarded");chatField.setActionCommand("clicked");
//***********************Socket Stuff**********
try{
chatSocket = new Socket(chatIp, chatPort);
gameSocket = new Socket(ipAddress, port);
chatIn = chatSocket.getInputStream();
chatOut = chatSocket.getOutputStream();
chatPout = new PrintStream(chatOut);
chatBin = new BufferedReader(new InputStreamReader(chatIn));
gameIn = gameSocket.getInputStream();
gameOut = gameSocket.getOutputStream();
gamePout = new PrintStream(gameOut);
gameBin = new BufferedReader(new InputStreamReader(gameIn));
ChatInnerClass chatThread = new ChatInnerClass(chatArea,chatBin);
GameInnerClass gameThread = new GameInnerClass(gameBin, gamePout);
//ListenInnerClass listenThread = new ListenInnerClass();
Thread game = new Thread(gameThread);
Thread chat = new Thread(chatThread);
//Thread listen = new Thread(listenThread);
game.start();
chat.start();
}
catch(UnknownHostException uhe) {
System.out.println("no host");
uhe.printStackTrace();
}
catch(IOException ioe)
{
System.out.println("IO error");
//ioe.printStackTrace();
JOptionPane.showMessageDialog(null, "¡Server Not Found!", "ERROR", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
//*********************************End of chat socket stuff********************************************
//createRack();//maybe change
}
public void createRack(){
for(int j = 0; j <= 6; j++){
//RackTile rt = new RackTile(lettersFromServer.get(j) );// lettersFromServer.get(j) ); //lettersFromServers is the ArrayList
RackTile rt = new RackTile("_");
rackTileArray.add(rt.fetchButton() );
rt.setEnabled(true);
rackPanel.add(rackTileArray.get(j));
}
}
public void populateRack(){
rackPanel.removeAll();
//getContentPane().removeAll();
rackPanel.revalidate();
rackPanel.repaint();
for(int y = 0; y <= 6; y++){
RackTile rt = new RackTile(lettersFromServer.get(y) );// lettersFromServer.get(j) ); //lettersFromServers is the ArrayList
rackTileArray.add(rt.fetchButton());
rackPanel.add(rackTileArray.get(y));
rackPanel.revalidate();
rackPanel.repaint();
}
}
public void actionPerformed(ActionEvent ae){
String actionCommand = ae.getActionCommand();
if(actionCommand.equals("keyboarded") || actionCommand.equals("clicked"))
{
try
{
if(usernameSet == false){
userName = chatField.getText();
chatPout.println(userName);
chatField.setText("");
//setPlayerNumber();
chatPout.flush();
usernameSet = true;
}
else{
message = chatField.getText();
chatPout.println(message);
chatField.setText("");
chatPout.flush();
}
}
catch(Exception ioe)
{}
}
else if(actionCommand.equals("Quit") ){
try{
chatSocket.close();
System.exit(0);
}
catch(Exception e){}
}
else if(/*actionCommand.equals("End Turn") */endTurnButton.equals(ae.getSource())){
doMove();
}
else if(/*actionCommand.equals("Shuffle")*/shuffleButton.equals(ae.getSource())){
shuffleRackTiles();
}
else if(/*actionCommand.equals("Undo")*/undoButton.equals(ae.getSource())){
undo();
}
}
public boolean waitForMoves(){
// String skipGoWin;
//
// System.out.println("waitForMoves() started");
// try{
//
// System.out.println("Reading in GO, WIN, or SKIP");
// int numLettersFromServer = 0;
// skipGoWin = gameBin.readLine();
// System.out.println("From server - SkipGoWin = " + skipGoWin);
// gamePout.println("ACKskipGoWin");
// System.out.println("ACKskipGoWin sent to server");
//
// if(skipGoWin.equals("WIN")){
// int player1Score = Integer.parseInt(player1ScoreField.getText());
// int player2Score = Integer.parseInt(player2ScoreField.getText());
// int player3Score = Integer.parseInt(player3ScoreField.getText());
// int player4Score = Integer.parseInt(player1ScoreField.getText());
// System.out.println("Player 1 score = " + player1Score);
// System.out.println("Player 2 score = " + player2Score);
// System.out.println("Player 3 score = " + player3Score);
// System.out.println("Player 4 score = " + player4Score);
//
// if(player1Score > player2Score && player1Score > player3Score
// && player1Score > player4Score){
//
// JOptionPane.showMessageDialog(null,"Player 1 WINS!!");
// disableButtons();
// }
// else if(player2Score > player1Score && player2Score > player3Score
// && player2Score > player4Score){
//
// JOptionPane.showMessageDialog(null,"Player 2 WINS!!");
// disableButtons();
// }
// else if(player3Score > player1Score && player3Score > player2Score
// && player3Score > player4Score){
//
// JOptionPane.showMessageDialog(null,"Player 3 WINS!!");
// disableButtons();
// }
// else if(player4Score > player1Score && player4Score > player2Score
// && player4Score > player3Score){
//
// JOptionPane.showMessageDialog(null,"Player 4 WINS!!");
// disableButtons();
// }
// else if(player4Score == player1Score && player4Score == player2Score
// && player4Score == player3Score){
//
// JOptionPane.showMessageDialog(null,"TIE GAME, YOU'RE ALL WINNERS!!");
// disableButtons();
// }
// }
// else if(skipGoWin.equals("SKIP")){
// System.out.println("SKIP triggered, reading turnNumber");
// this.turnNumber = Integer.parseInt(gameBin.readLine());
// System.out.println("Turn Number = " + turnNumber);
// gamePout.println("ACK After turnNumber received");
// System.out.println("ACK After turnNumber, sent to server");
// //JOptionPane.showMessageDialog(null,"Move Skipped!");
// whosTurn();
// disableButtons();
// System.out.println("whosTurn() triggered");
//
// if(turnNumber != clientNumber){
//
// System.out.println("clientNumber != turnNumber, waitForMoves() triggered again");
// waitForMoves();
// }
// else if(this.turnNumber == this.clientNumber){
// //JOptionPane.showMessageDialog(null, "It's your turn again!");
// enablePlayTiles();
// endTurnButton.setEnabled(true);
// return true;
// //break;
// }
// }
// else if(skipGoWin.equals("GO")){
// numLettersFromServer = Integer.parseInt(gameBin.readLine());
// System.out.println("Length recieved from SERVER =" + numLettersFromServer);
// gamePout.println("ACK(received number of letters that will be sent from server)");
// System.out.println("Sent ACK for numLettersFromServer");
// //System.out.println("number of letters to be sent from server: " + numLettersFromServer + "Client:" + clientNumber);
// String reep;
// for(int h = 0; h < numLettersFromServer; h++){
//
// playerMoves.add(gameBin.readLine());
//
// gamePout.println("ACK(Received player moves)Client:"+clientNumber);
// System.out.println("sending ack after receiving a player's moves");
// //System.out.println(playerMoves.get(h));
// }
// rewriteBoard();
// revalidate();
// repaint();
// for(int h = 0; h < 3; h++){
//
//
// String pl = gameBin.readLine();
// System.out.println("Player Number points recieved:::::::::::::::::::::::::: "+pl);
// gamePout.println("ACK");
//
// String pt = gameBin.readLine();
// System.out.println("Player points recieved:::::::::::::::::::::::::: "+pt);
// gamePout.println("ACK");
//
// setPoints(pl,pt);
// }
//
//
// //System.out.println("\nListening for turn number Client:"+ clientNumber);
// this.turnNumber = Integer.parseInt(gameBin.readLine());
// whosTurn();
// System.out.println(this.turnNumber+" Turn recieved");
// //System.out.println("Client Number: " + clientNumber);
// //System.out.println("Turn Number: " + turnNumber);
// String ackTurnNumRec = "ACKturnNumberClient" + clientNumber;
//
// //if(go = true){
//
// gamePout.println(ackTurnNumRec);
// //go = false;
// //System.out.println("Go set to false");
// //}
// System.out.println("sending ack after receiving turn number in waitForMoves()");
// whosTurn();//Line 454ish
// System.out.println("Turn number: " + turnNumber);
// // if(this.turnNumber == this.clientNumber){
// // JOptionPane.showMessageDialog(null, "It's your turn again!");
// // enablePlayTiles();
// // endTurnButton.setEnabled(true);
// // return true;
// // //break;
// // }
//
// if(this.turnNumber == this.clientNumber){
// //JOptionPane.showMessageDialog(null, "It's your turn again!");
// enablePlayTiles();
// endTurnButton.setEnabled(true);
// return true;
// //break;
// }
// }
//
// // if(this.turnNumber == this.clientNumber){
// // JOptionPane.showMessageDialog(null, "It's your turn again!");
// // enablePlayTiles();
// // endTurnButton.setEnabled(true);
// // return true;
// // //break;
// // }
//
// }
// catch(Exception e){
// //System.out.println("Catch triggered while listening for a 1");
// }
return false;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
public synchronized void setPoints(String _player, String value)
{
int player = Integer.parseInt(_player);
if(player == 1)
{
this.player1ScoreField.setText(value);
}
else if(player == 2)
{
this.player2ScoreField.setText(value);
}
else if(player == 3)
{
this.player3ScoreField.setText(value);
}
else if(player == 4)
{
this.player4ScoreField.setText(value);
}
}
public void repaintLabels(){
if(turnNumber == 1){
turnNumber = 2;
whosTurn();
}
else if(turnNumber == 2){
turnNumber = 3;
whosTurn();
}
else if(turnNumber == 3){
turnNumber = 4;
whosTurn();
}
else if(turnNumber == 4){
turnNumber = 1;
whosTurn();
}
}
public void doMove(){
repaintLabels();
BoardTile retriever = new BoardTile("");
lettersNeeded = retriever.getLengthMoves2Server();
//lettersNeeded = 0;
try{
if(lettersNeeded == 0){
gamePout.println("SKIP");
System.out.println("Ack for lettersNeeded size = 0 - " + gameBin.readLine());
disableButtons();
endTurnButton.setEnabled(false);
//waitForMoves();
System.out.println("skip triggered, listen thread started");
ListenInnerClass listenThread = new ListenInnerClass();
Thread listen = new Thread(listenThread);
listen.start();
}
else{
enablePlayTiles();
endTurnButton.setEnabled(true);
gamePout.println("END");
System.out.println("Sending END to SERVER");
System.out.println(gameBin.readLine());
gamePout.println(lettersNeeded);
System.out.println(gameBin.readLine());
//*********************************************************************
for(int i = 0; i < lettersNeeded; i++){
System.out.println("lettersNeeded = " + lettersNeeded);
//System.out.println(retriever.popMoves2Server() + " sent to server");
String sent = retriever.popMoves2Server();
gamePout.println(sent);
System.out.println("sent = " + sent);
System.out.println(gameBin.readLine());
}
//**********************************************************************
// gamePout.println("0,0,a");
// System.out.println("sending 00a");
// System.out.println(gameBin.readLine());
// gamePout.println("0,1,s");
// System.out.println("sending 01s");
// System.out.println(gameBin.readLine());
// gamePout.println("0,2,s");
// System.out.println("sending 02s");
// System.out.println(gameBin.readLine());
//**********************************************************************
//System.out.println(gameBin.readLine() + "getting ack from server for letters sentClient:"+clientNumber);
gamePout.println("SYNCafterSendingLettersClient:"+clientNumber);
System.out.println("SYNC after sending letters(move) to server");
//**************************************
//System.out.println("reading whether move is accepted or notClient:"+clientNumber);
String moveAccepted = gameBin.readLine();
String ackMoveAccepted = "ackMovAccptClient" + clientNumber;
gamePout.println(ackMoveAccepted);
System.out.println("sending ack for move accepted");
System.out.println("Move accepted = " + moveAccepted);
////////////////////////////////////////Recieve POINTS TO CLIENT/////////////////////////////////////////////////
// String points = "";
// points = gameBin.readLine();
// System.out.println(points+" GOT ME SOME POINTS");
// gamePout.println("ACK BABY");
// setPoints(points);
////////////////////////////////////////Recieve POINTS TO CLIENT/////////////////////////////////////////////////
if(moveAccepted.equals("YES")){
disableButtons();
endTurnButton.setEnabled(false);
//JOptionPane.showMessageDialog(null,"Move Accepted!");
for(int t = 0; t < lettersNeeded; t++){
lettersFromServer.add(gameBin.readLine());
//System.out.println(gameBin.readLine());
String ackRecLetters = "ackRecLettersClient:" + clientNumber;
gamePout.println(ackRecLetters);
System.out.println("sending ack that letters were reveived");
//System.out.println("ack(received replacement lettersClient:"+clientNumber);
}
String point = gameBin.readLine();
gamePout.println("Recieved points");
setPoints(Integer.toString(this.clientNumber),point);
turnNumber = Integer.parseInt(gameBin.readLine());
whosTurn();
System.out.println("Turn number: " + turnNumber);
gamePout.println("ACK(turnNumber after replacement lettersClient:" + clientNumber);
System.out.println("sending ack for turn number after getting replacement letter");
//System.out.println("transitioning from receiving letters to waiting for movesClient:"+clientNumber);
ListenInnerClass listenThread = new ListenInnerClass();
Thread listen = new Thread(listenThread);
listen.start();
//******************************************************
// while(true){
// //System.out.println("in doMove() waiting for other players");
//
// //if( wait != true){
// if(clientNumber != turnNumber){
// System.out.println("wait = false, keep waiting");
// //wait = waitForMoves();
// //break;
// //waitForMoves();
// System.out.println
// Thread listen = new Thread();
// listen.start();
// }
// //else if(wait == true){
// else if(clientNumber == turnNumber){
// System.out.println("wait = true, stop waiting");
// enablePlayTiles();
// endTurnButton.setEnabled(true);
// break;
// }
// }
}
else if(moveAccepted.equals("NO")){
JOptionPane.showMessageDialog(null,"Move declined, try again!");
}
else if(moveAccepted.equals("WIN")){
System.out.println("Received a WIN");
int player1Score = Integer.parseInt(player1ScoreField.getText());
int player2Score = Integer.parseInt(player2ScoreField.getText());
int player3Score = Integer.parseInt(player3ScoreField.getText());
int player4Score = Integer.parseInt(player1ScoreField.getText());
System.out.println("Player 1 score = " + player1Score);
System.out.println("Player 2 score = " + player2Score);
System.out.println("Player 3 score = " + player3Score);
System.out.println("Player 4 score = " + player4Score);
if(player1Score > player2Score && player1Score > player3Score
&& player1Score > player4Score){
JOptionPane.showMessageDialog(null,"Player 1 WINS!!");
disableButtons();
}
else if(player2Score > player1Score && player2Score > player3Score
&& player2Score > player4Score){
JOptionPane.showMessageDialog(null,"Player 2 WINS!!");
disableButtons();
}
else if(player3Score > player1Score && player3Score > player2Score
&& player3Score > player4Score){
JOptionPane.showMessageDialog(null,"Player 3 WINS!!");
disableButtons();
}
else if(player4Score > player1Score && player4Score > player2Score
&& player4Score > player3Score){
JOptionPane.showMessageDialog(null,"Player 4 WINS!!");
disableButtons();
}
else if(player4Score == player1Score && player4Score == player2Score
&& player4Score == player3Score){
JOptionPane.showMessageDialog(null,"TIE GAME, YOU'RE ALL WINNERS!!");
disableButtons();
}
}
}
}
catch(NullPointerException npe){
npe.printStackTrace();
//System.out.println("NPE Error in doMove()");
}
catch(IOException ioe){
System.out.println("IOException in doMove()");
}
}
public void serverSelect(){
if(clientNumber < 4){
JOptionPane.showMessageDialog(null, serverListPanel, "Werds with Nerds! \nSelect A Server", JOptionPane.INFORMATION_MESSAGE, icon);
}
else{
JOptionPane.showMessageDialog(null, serverListPanel, "Server FULL! Select A Different Server", JOptionPane.WARNING_MESSAGE/*, icon*/);
}
}
public void disableButtons(){
for(int x = 0; x < backgroundTileArray.size(); x++){
((backgroundTileArray.get(x))).disableButton();
}
for(int y = 0; y < rackTileArray.size(); y++){
rackTileArray.get(y).setEnabled(false);
}
endTurnButton.setEnabled(false);
}
// public void setPlayerNumber(){
//
// if(clientNumber == 1){
// player1Label.setText(userName);
// }
// else if(clientNumber == 2){player2Label.setText(userName);}
// else if(clientNumber == 3){player3Label.setText(userName);}
// else if(clientNumber == 4){player4Label.setText(userName);}
// }
public void enablePlayTiles(){
for(int k = 0; k < backgroundTileArray.size(); k++){
backgroundTileArray.get(k).fetchButton().setEnabled(true);
}
for(int x = 0; x <rackTileArray.size(); x++){
rackTileArray.get(x).setEnabled(true);
}
}
public void whosTurn(){
if(turnNumber == 1){
player1Label.setOpaque(true);
player2Label.setOpaque(false);
player3Label.setOpaque(false);
player4Label.setOpaque(false);
revalidate();
repaint();
}
else if(turnNumber == 2){
player1Label.setOpaque(false);
player2Label.setOpaque(true);
player3Label.setOpaque(false);
player4Label.setOpaque(false);
revalidate();
repaint();
}
else if(turnNumber == 3){
player1Label.setOpaque(false);
player2Label.setOpaque(false);
player3Label.setOpaque(true);
player4Label.setOpaque(false);
revalidate();
repaint();
}
else if(turnNumber == 4){
player1Label.setOpaque(false);
player2Label.setOpaque(false);
player3Label.setOpaque(false);
player4Label.setOpaque(true);
revalidate();
repaint();
}
}
public static void main(String [] args){
GameBoard gb = new GameBoard();
}
class GameInnerClass implements Runnable{
BufferedReader br;
PrintStream pw;
public GameInnerClass(BufferedReader _br, PrintStream _pw ){
this.br = _br;
this.pw = _pw;
}
public void run(){
boolean keepGoing = true;
disableButtons();//disable buttons by default
try{
pw.println("Initial Send");
System.out.println("Initial Send");
GameBoard.this.clientNumber = Integer.parseInt(br.readLine());System.out.println("I am client #: " + clientNumber);
String initSendAck = "ACKInitialSendClient" + clientNumber;
gamePout.println(initSendAck);
System.out.println(initSendAck);
rackTileArray.clear();
//System.out.println("did it clear?: " + rackTileArray.size() );
for(int i = 0; i < 7; i++){//gets 7 letters back from server
lettersFromServer.add(br.readLine());
//System.out.println(lettersFromServer.get(i));
pw.println("receiving letters..." + clientNumber);
System.out.println("ACK after receiving letters");
}
populateRack();
//rackPanel.remove(rt);
JOptionPane.showMessageDialog(null, "You are player number " + clientNumber + "!");
if(clientNumber <= 3){
JOptionPane.showMessageDialog(null, "Waiting for others to join");
}
GameBoard.this.turnNumber = Integer.parseInt(br.readLine());
System.out.println("Turn Number: " + turnNumber);
whosTurn();
pw.println("ACKClient" + clientNumber);
System.out.println("sending ack that turn number is received on initial run");
//System.out.println(br.readLine());
System.out.println(br.readLine());
if(GameBoard.this.clientNumber == GameBoard.this.turnNumber){
System.out.println("It's your turn");
//JOptionPane.showMessageDialog(null, "It's your turn!");
enablePlayTiles();
endTurnButton.setEnabled(true);
//break;
}
else if(GameBoard.this.clientNumber != GameBoard.this.turnNumber){
System.out.println("not your turn");
//System.out.println("Waiting for moves Client" + clientNumber);
//waitForMoves();//351
ListenInnerClass listenThread = new ListenInnerClass();
Thread listen = new Thread(listenThread);
listen.start();
//break;
}
//createRack();
}
catch(UnknownHostException uhe) {
System.out.println("no host");
uhe.printStackTrace();
}
catch(IOException ioe)
{
System.out.println("IO error");
//ioe.printStackTrace();
JOptionPane.showMessageDialog(null, "Game server Not Found!", "ERROR", JOptionPane.ERROR_MESSAGE);
System.exit(0);
}
}
}
class ListenInnerClass implements Runnable{
public ListenInnerClass(){
}
public void run(){
String skipGoWin;
System.out.println("Listen thread started");
//System.out.println("waitForMoves() started");
try{
System.out.println("Reading in GO, WIN, or SKIP");
int numLettersFromServer = 0;
skipGoWin = gameBin.readLine();
System.out.println("From server - SkipGoWin = " + skipGoWin);
gamePout.println("ACKskipGoWin");
System.out.println("ACKskipGoWin sent to server");
if(skipGoWin.equals("WIN")){
System.out.println("Received a WIN");
int player1Score = Integer.parseInt(player1ScoreField.getText());
int player2Score = Integer.parseInt(player2ScoreField.getText());
int player3Score = Integer.parseInt(player3ScoreField.getText());
int player4Score = Integer.parseInt(player4ScoreField.getText());
System.out.println("Player 1 score = " + player1Score);
System.out.println("Player 2 score = " + player2Score);
System.out.println("Player 3 score = " + player3Score);
System.out.println("Player 4 score = " + player4Score);
if(player1Score > player2Score && player1Score > player3Score
&& player1Score > player4Score){
JOptionPane.showMessageDialog(null,"Player 1 WINS!!");
disableButtons();
}
else if(player2Score > player1Score && player2Score > player3Score
&& player2Score > player4Score){
JOptionPane.showMessageDialog(null,"Player 2 WINS!!");
disableButtons();
}
else if(player3Score > player1Score && player3Score > player2Score
&& player3Score > player4Score){
JOptionPane.showMessageDialog(null,"Player 3 WINS!!");
disableButtons();
}
else if(player4Score > player1Score && player4Score > player2Score
&& player4Score > player3Score){
JOptionPane.showMessageDialog(null,"Player 4 WINS!!");
disableButtons();
}
else if(player4Score == player1Score && player4Score == player2Score
&& player4Score == player3Score){
JOptionPane.showMessageDialog(null,"TIE GAME, YOU'RE ALL WINNERS!!");
disableButtons();
}
}
else if(skipGoWin.equals("SKIP")){