-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScrabbleServer.java
1613 lines (1220 loc) · 71.5 KB
/
ScrabbleServer.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.net.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Font;
import java.awt.Color;
public class ScrabbleServer extends JFrame{
//*********************************************************
public JPanel portNumPanel = new JPanel(new FlowLayout());
public JPanel numClientsConnPanel = new JPanel(new GridLayout(20,1));
public JPanel serverMessagesPanel = new JPanel(new GridLayout(1,1));
public JTextArea serverMessagesArea = new JTextArea();
public JScrollPane serverMessagesScroll = new JScrollPane(serverMessagesArea);
public JLabel playerConn1 = new JLabel("");
public JLabel playerConn2 = new JLabel("");
public JLabel playerConn3 = new JLabel("");
public JLabel playerConn4 = new JLabel("");
public JLabel connectedLabel = new JLabel("Clients Connected:");
public JLabel portLabel = new JLabel("Port Number: ");
public JLabel portNumLabel = new JLabel("");
//**********GUI*************************************************
public ArrayList<String> preStackArray = new ArrayList<String>(); //Alphabelt scrabble letters before shuffle
public Stack<String> scrabbleLetterStack = new Stack<String>(); //Alphabelt scrabble letters after shuffle
public Vector<String> sendBackClient = new Vector<String>(); //ArrayList that sends letters and coordinates back to other clients after move is validated
public int[] gamePoints = new int[4];
public ArrayList<String> dictionaryWords = new ArrayList<String>(); //ArrayList of all the words in the dictionary
public Queue<Integer> rows = new LinkedList<Integer>(); //list of x coordinates of letters sent from client to server
public Queue<Integer> columns = new LinkedList<Integer>(); //list of y coordinates of letters sent from client to server
public Queue<String> wordQGame = new LinkedList<String>(); //list of letters sent from client to server that is used to place on the board if wordQ array validates
public ArrayList<String> wordQ = new ArrayList<String>(); //list of letters sent from client to server that is used to check the dictionary
int outerLengthOfword = 0; //Length of thedictionaryWordComparepassed from thread to the outer class for dictionary validation
public String dictionaryWordCompare = ""; //Word concat thatis used to compare against the dictionary
public Stack<PrintWriter> globalWriters = new Stack<PrintWriter>(); //Vector of PrintWriter that will print to all clients
public Stack<BufferedReader> globalReaders = new Stack<BufferedReader>(); //Vector of BufferedReaders that will print to all clients
public Stack<String> readerWriterNumber = new Stack<String>();
public String [][] logicBoard = new String [15][15]; //Logical scrabble board on the server
Socket controlSocket; //Server socket
public int playerTurnNumber = 1; //Client player who's turn it is
public int playerConnectionNumber = 0; //Number of users connected count
Object globalONE = new Object();
Object addGlobalWR = new Object();
Object startObject = new Object();
Object addWriterSync = new Object();
Object winSync = new Object();
Object skipSync = new Object();
Object sendBackSync = new Object();
public int socketWaitCount = 0; //The amount of clients at the wait
public int sentWordValue = 0; //The value of the incoming word
public int skipTurnWinCount = 0; //The amount of skips for a win condition
public int winSkipCounter = 0; //The skip turn counter that increments for a win condition
public boolean wordCheck; //boolean value to see if word validated
public boolean winFirstSkipCounter = false; //
public boolean winFlag = false; //boolean value to see if word validated
//Parameterized constructor
public ScrabbleServer(int portPass){
//************************GUI**********************
setSize(830, 680);
setResizable(true);
setLocationRelativeTo(null);
setTitle("Werds with Nerds GUI");
add(portNumPanel, BorderLayout.NORTH);
add(serverMessagesPanel, BorderLayout.CENTER);
add(numClientsConnPanel, BorderLayout.EAST);
serverMessagesPanel.add(serverMessagesScroll);
numClientsConnPanel.add(connectedLabel);
numClientsConnPanel.add(playerConn1);
numClientsConnPanel.add(playerConn2);
numClientsConnPanel.add(playerConn3);
numClientsConnPanel.add(playerConn4);
portNumPanel.add(portLabel);
portNumPanel.add(portNumLabel);
portNumLabel.setText(Integer.toString(portPass));
//portField.setEditable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
//************************GUI**********************
ServerSocket socket = null;
try {
//Output Server data to screen
System.out.println("getLocalHost: "+InetAddress.getLocalHost() );
serverMessagesArea.append("getLocalHost: "+InetAddress.getLocalHost()+"\n");
serverMessagesArea.setCaretPosition(serverMessagesArea.getText().length());
System.out.println("getByName: "+InetAddress.getByName("localhost") );
serverMessagesArea.append("getByName: "+InetAddress.getByName("localhost") +"\n");
serverMessagesArea.setCaretPosition(serverMessagesArea.getText().length());
//Pass the port into the socket and instantiate controlSocket
socket = new ServerSocket(portPass);
controlSocket = null;
//Call newgame method which populates the board and dictionary
newGame();
//Keep the program running and accept connections
while(true){
//Allow only four connections in each game
if(playerConnectionNumber<=4){
controlSocket = socket.accept(); // wait for connection
ScrabbleServer.this.playerConnectionNumber++;
System.out.println("Player connection number " + playerConnectionNumber);
serverMessagesArea.append("Player connection number " + playerConnectionNumber +"\n");
serverMessagesArea.setCaretPosition(serverMessagesArea.getText().length());
if(playerConnectionNumber == 1)
{
createLetters();
randomizeLetters();
System.out.println("Created letters for two players");
serverMessagesArea.append("Created letters for two players\n");
serverMessagesArea.setCaretPosition(serverMessagesArea.getText().length());
}
else if(playerConnectionNumber == 4)
{
createLetters();
randomizeLetters();
System.out.println("Created letters for four players");
serverMessagesArea.append("Created letters for four players\n");
serverMessagesArea.setCaretPosition(serverMessagesArea.getText().length());
}
System.out.println("STACKS STACKS STACKS SIZEEEEE " + ScrabbleServer.this.scrabbleLetterStack.size());
serverMessagesArea.append("STACKS STACKS STACKS SIZEEEEE " + ScrabbleServer.this.scrabbleLetterStack.size() + "\n");
serverMessagesArea.setCaretPosition(serverMessagesArea.getText().length());
//Create object of thread class and start the thread
clientThread connectThread = new clientThread(controlSocket, playerConnectionNumber);
connectThread.start();
}
//Else if there are already four clients in the game
else{
controlSocket = socket.accept();
//Reader and writer for denied connections
BufferedReader deniedRead = new BufferedReader(
new InputStreamReader(
controlSocket.getInputStream()));
PrintWriter deniedWrite = new PrintWriter(
new OutputStreamWriter(
controlSocket.getOutputStream()));
//Increment the number of clients trying to connect
playerConnectionNumber++;
deniedWrite.println(playerConnectionNumber); //Write out client playerConnectionNumber
deniedWrite.flush();
//Close socket and reader/writer
deniedWrite.close();
deniedRead.close();
controlSocket.close();
System.out.println("Client connection denied");
serverMessagesArea.append("Client connection denied\n");
serverMessagesArea.setCaretPosition(serverMessagesArea.getText().length());
}
}
}
catch( BindException be ){
System.out.println("Server already running on this computer.");
}
catch( IOException ioe ){
System.out.println("IO Error");
ioe.printStackTrace();
}
}
//This method reads in dictionary.txt file and stores all the words into an arrayList
public void buildDictionary()
{
try{
File file = new File("dictionary.txt");
BufferedReader read = new BufferedReader(new FileReader(file));
ArrayList<String> untrimmed = new ArrayList<String>();
//178691
//Grab all the words from the txt file and store them into an arraylist
for(int i = 0;i<178691;i++)
{
String stn = read.readLine();
untrimmed.add(stn);
dictionaryWords.add(untrimmed.get(i).trim());
}
}
catch(Exception e)
{
System.out.println("Error reading dictionary");
}
}
//This method builds the word from the letters the client sent and searches the dictionary arraylist for the word
public boolean checkWord(ArrayList<String> stringer, int length)
{
ArrayList<String> wordBreak = new ArrayList<String>(); //ArrayList for letters sent from the client
wordBreak = stringer; //ArrayList passed in from inside the client thread Turn method
outerLengthOfword = length; //The length of the word that the client sent to the server, this is passed from the Turn method inside the client thread
dictionaryWordCompare =""; //This is the word builder that concatinates each letter from the array
//Grabs the letters from the array and builds the string
for(int i = 0;i<outerLengthOfword;i++)
{
dictionaryWordCompare+= wordBreak.get(i);
}
System.out.println(dictionaryWordCompare);
serverMessagesArea.append(dictionaryWordCompare + "\n");
serverMessagesArea.setCaretPosition(serverMessagesArea.getText().length());
//Checks to see if word is in array
boolean checky = false;
checky = this.dictionaryWords.contains(dictionaryWordCompare);
//Returns true or false
if(checky==true)
{
return true;
}
else
{
return false;
}
}
//This method creates the board and initializes it with all zeros
public void newGame()
{
//Set default values of board to 0
for(int i = 0;i<15;i++)
{
for(int j = 0;j<15;j++)
{
logicBoard[i][j] = "0";
//System.out.print(logicBoard[i][j]+" ");
}
//System.out.print("row "+i);
//System.out.println();
}
System.out.println("Array Loaded");
serverMessagesArea.append("Array Loaded\n");
serverMessagesArea.setCaretPosition(serverMessagesArea.getText().length());
//createLetters(); //Method to create the letter tiles in the game
//randomizeLetters(); //Method to randomize the letter tiles and puts them into a stack
buildDictionary(); //Method to load the dictionary into arraylist
}
//Method to randomize the letter tiles and puts them into a stack
public void randomizeLetters()
{
Collections.shuffle(preStackArray); //Shuffle pre-stack array
//Populate stack from array
for(String let : preStackArray){
scrabbleLetterStack.push(let);
}
preStackArray.clear();
}
//Method to create the letter tiles in the game
public void createLetters()
{
for(int a = 0;a<9;a++)
{
preStackArray.add("a");
}
for(int b = 0;b<2;b++)
{
preStackArray.add("b");
}
for(int c = 0;c<2;c++)
{
preStackArray.add("c");
}
for(int d = 0;d<4;d++)
{
preStackArray.add("d");
}
for(int e = 0;e<12;e++)
{
preStackArray.add("e");
}
for(int f = 0;f<2;f++)
{
preStackArray.add("f");
}
for(int g = 0;g<3;g++)
{
preStackArray.add("g");
}
for(int h = 0;h<2;h++)
{
preStackArray.add("h");
}
for(int i = 0;i<9;i++)
{
preStackArray.add("i");
}
for(int j = 0;j<1;j++)
{
preStackArray.add("j");
}
for(int k = 0;k<1;k++)
{
preStackArray.add("k");
}
for(int l = 0;l<4;l++)
{
preStackArray.add("l");
}
for(int m = 0;m<2;m++)
{
preStackArray.add("m");
}
for(int n = 0;n<6;n++)
{
preStackArray.add("n");
}
for(int o = 0;o<8;o++)
{
preStackArray.add("o");
}
for(int p = 0;p<2;p++)
{
preStackArray.add("p");
}
for(int q = 0;q<1;q++)
{
preStackArray.add("q");
}
for(int r = 0;r<6;r++)
{
preStackArray.add("r");
}
for(int s = 0;s<4;s++)
{
preStackArray.add("s");
}
for(int t = 0;t<6;t++)
{
preStackArray.add("t");
}
for(int u = 0;u<2;u++)
{
preStackArray.add("u");
}
for(int v = 0;v<2;v++)
{
preStackArray.add("v");
}
for(int w = 0;w<2;w++)
{
preStackArray.add("w");
}
for(int x = 0;x<1;x++)
{
preStackArray.add("x");
}
for(int y = 0;y<2;y++)
{
preStackArray.add("y");
}
for(int z = 0;z<1;z++)
{
preStackArray.add("z");
}
}
public void checkTheBoardForPoints(int xaxis,int yaxis,String letter)
{
for(int row = yaxis;row<=yaxis;row++)
{
for(int col = 0;col<14;col++)
{
}
}
}
//Method to get the player turn number for write
public synchronized String getTurn()
{
String num = Integer.toString(playerTurnNumber);
return num;
}
//Method to get the player turn number in int form
public synchronized int getTurnInt()
{
return this.playerTurnNumber;
}
//Method to add printWriter to vector in order to global write
public synchronized void setGlobalWriters(PrintWriter value)
{
globalWriters.add(value);
}
//Method to add BufferedReader to vector in order to global write
public synchronized void setGlobalReaders(BufferedReader value)
{
globalReaders.add(value);
}
//Method to add BufferedReader to vector in order to global write
public synchronized void setRWNumber(String value)
{
readerWriterNumber.add(value);
}
//Set the wait count for the clients waiting
public synchronized void setWaitCount()
{
this.socketWaitCount++;
}
//Get the count of clients waiting
public int getWaitCount()
{
return socketWaitCount;
}
//Method to change the turn after each valid move
public synchronized void changeTurn(String client)
{
int clientNum = 0;
clientNum = Integer.parseInt(client);
if(this.playerConnectionNumber == 4)
{
if(clientNum == 1)
{
this.playerTurnNumber = 2;
}
else if(clientNum == 2)
{
this.playerTurnNumber = 3;
}
else if(clientNum == 3)
{
this.playerTurnNumber = 4;
}
else if(clientNum == 4)
{
this.playerTurnNumber = 1;
}
}
else if(this.playerConnectionNumber == 3)
{
if(clientNum == 1)
{
this.playerTurnNumber = 2;
}
else if(clientNum == 2)
{
this.playerTurnNumber = 3;
}
else if(clientNum == 3)
{
this.playerTurnNumber = 1;
}
}
else if(this.playerConnectionNumber == 2)
{
if(clientNum == 1)
{
this.playerTurnNumber = 2;
}
else if(clientNum == 2)
{
this.playerTurnNumber = 1;
}
}
}
//Get the value of letters
public int getLetterValue(String in)
{
int value = 0;
switch(in){
case "a": value = 1;
break;
case "b": value = 3;
break;
case "c": value = 3;
break;
case "d": value = 2;
break;
case "e": value = 1;
break;
case "f": value = 4;
break;
case "g": value = 2;
break;
case "h": value = 4;
break;
case "i": value = 1;
break;
case "j": value = 8;
break;
case "k": value = 5;
break;
case "l": value = 1;
break;
case "m": value = 3;
break;
case "n": value = 1;
break;
case "o": value = 1;
break;
case "p": value = 3;
break;
case "q": value = 10;
break;
case "r": value = 1;
break;
case "s": value = 1;
break;
case "t": value = 1;
break;
case "u": value = 1;
break;
case "v": value = 4;
break;
case "w": value = 4;
break;
case "x": value = 8;
break;
case "y": value = 4;
break;
case "z": value = 10;
break;
}
return value;
}
//Set points for each player
public void setPoints(int number)
{
int cNumber = number;
if(cNumber == 1)
{
gamePoints[0] += this.sentWordValue;
}
else if(cNumber == 2)
{
gamePoints[1] += this.sentWordValue;
}
else if(cNumber == 3)
{
gamePoints[2] += this.sentWordValue;
}
else if(cNumber == 4)
{
gamePoints[3] += this.sentWordValue;
}
}
//Set wincondition based on the number of players
public void winConditionSet()
{
if(this.playerConnectionNumber == 2)
{
this.skipTurnWinCount = 4;
}
else if(this.playerConnectionNumber == 3)
{
this.skipTurnWinCount = 6;
}
else if(this.playerConnectionNumber == 4)
{
this.skipTurnWinCount = 8;
}
}
//Check for the number of skips to declare winner
public boolean winCheck()
{
if(this.skipTurnWinCount == this.winSkipCounter)
{
return true;
}
else
{
return false;
}
}
//Inner class that is used to run the threads
class clientThread extends Thread{
Socket cs; //Accepted socket passed in from the outer class
BufferedReader reader; //Thread reader
PrintWriter writer; //Thread writer
PrintWriter gWriter; //Global writer
BufferedReader gReader; //Global Reader
int clientNumber; //client number passed in from the outer class
int intwordLength = 0; //Length of the word recieved from the client's move
int wordValue = 0;
String stringWordLength = ""; //Length of the word recieved from the client's move in string
String listenForEnd = ""; //String for used when endturn is pressed on the client
int turn;
//Parameterized constructor, takes in socket and client number. Creates readers and writers
public clientThread( Socket _cs, int num){
this.cs = _cs;
clientNumber = num;
try{
reader = new BufferedReader(
new InputStreamReader(
cs.getInputStream()));
writer = new PrintWriter(
new OutputStreamWriter(
cs.getOutputStream()));
}
catch(Exception ert)
{
}
}
//Thread run method
public void run(){
try{
setName(Integer.toString(clientNumber));
if(clientNumber == 1){
playerConn1.setText("Player 1 connected");
repaint();
}
else if(clientNumber == 2){
playerConn2.setText("Player 2 connected");
repaint();
}
else if(clientNumber == 3){
playerConn3.setText("Player 3 connected");
repaint();
}
else if(clientNumber == 4){
playerConn4.setText("Player 4 connected");
repaint();
}
reader.readLine(); //initial communication from client
// String cNum = "";
// cNum = Integer.toString(clientNumber); //Parse client number to string
writer.println(getName()); //Send the client number to client
writer.flush();
System.out.println("Player "+getName()+" connected!");
serverMessagesArea.append("Player "+getName()+" connected!\n");
serverMessagesArea.setCaretPosition(serverMessagesArea.getText().length());
reader.readLine(); //client number recieved ack
//Pass starting letters to the clients
for(int let = 0;let<7;let++){
String sendLetter = "";
sendLetter = (String) ScrabbleServer.this.scrabbleLetterStack.pop(); //pull random letter from stack
writer.println(sendLetter); //Send letter to client
writer.flush();
reader.readLine(); //Read letter ack from client
}
if(Integer.parseInt(getName()) < 4)
{
synchronized(ScrabbleServer.this.startObject)
{
try{
ScrabbleServer.this.startObject.wait();
}
catch(Exception erry)
{}
}
}
else if(Integer.parseInt(getName()) == 4)
{
synchronized(ScrabbleServer.this.startObject)
{
ScrabbleServer.this.startObject.notifyAll();
}
}
writer.println(getTurn()); //Send the turn number to the clients
writer.flush();
reader.readLine(); //Read turn ack from client
writer.println("sync"); //Send a Sync to the client
writer.flush();
//Loop to continue game
while(true){
//If the client number equals the turn number then that particular thread fires
turn = getTurnInt();
System.out.println(getName() +" Is about to read the turn " +turn);
serverMessagesArea.append(getName() +" Is about to read the turn " +turn + "\n");
serverMessagesArea.setCaretPosition(serverMessagesArea.getText().length());
System.out.println();
serverMessagesArea.append("\n");
serverMessagesArea.setCaretPosition(serverMessagesArea.getText().length());
if(turn == Integer.parseInt(getName())){
boolean stoppper = Turn(); //Call the turn method to proceed with move
//If returns false client exited game
if(stoppper == false)
{
break;
}
}//Else other clients wait to hear from the first thread if the move is valid and then prints it out
else if(turn != Integer.parseInt(getName())){
System.out.println();
serverMessagesArea.append("\n");
serverMessagesArea.setCaretPosition(serverMessagesArea.getText().length());
synchronized(ScrabbleServer.this.addWriterSync)
{
setGlobalWriters(writer);
setGlobalReaders(reader);
System.out.println("Server thread "+getName()+" added global READER and WRITER");
serverMessagesArea.append("Server thread "+getName()+" added global READER and WRITER\n");
serverMessagesArea.setCaretPosition(serverMessagesArea.getText().length());
setRWNumber(getName());
setWaitCount();
System.out.println("Server thread "+getName()+" incremented Socket COUNT");
serverMessagesArea.append("Server thread "+getName()+" incremented Socket COUNT\n");
serverMessagesArea.setCaretPosition(serverMessagesArea.getText().length());
}
synchronized(ScrabbleServer.this.globalONE)
{
try{
ScrabbleServer.this.globalONE.wait();
}
catch(Exception erry)
{}
}
System.out.println("Client "+getName()+" DONE WITH LISTEN LOOP");
serverMessagesArea.append("Client "+getName()+" DONE WITH LISTEN LOOP\n");
serverMessagesArea.setCaretPosition(serverMessagesArea.getText().length());
System.out.println();
serverMessagesArea.append("\n");
serverMessagesArea.setCaretPosition(serverMessagesArea.getText().length());
}
}
}
catch(Exception e ){
System.out.println("Error");
e.printStackTrace();
}
}
//Method used to listen for the endturn after move is placed
public boolean Turn()
{
try{
////////////////////////////////////////LISTEN FOR END TURN/////////////////////////////////////////////////
listenForEnd = "";
this.listenForEnd = reader.readLine(); //Read end turn from the client
System.out.println("Server thread "+getName()+" should recieve end: "+this.listenForEnd);
serverMessagesArea.append("Server thread "+getName()+" should recieve end: "+this.listenForEnd + "\n");
serverMessagesArea.setCaretPosition(serverMessagesArea.getText().length());
////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(ScrabbleServer.this.scrabbleLetterStack.size()< 7)
{
System.out.println("Server thread "+getName()+" sent OUT OF WORDS ACK");
serverMessagesArea.append("Server thread "+getName()+" sent OUT OF WORDS ACK\n");
serverMessagesArea.setCaretPosition(serverMessagesArea.getText().length());
Winner();
}
else if(this.listenForEnd.equals("SKIP")){
////////////////////////////////////////END TURN ACK///////////////////////////////////////////////////////
System.out.println("Server thread "+getName()+" sent SKIP ACK");
serverMessagesArea.append("Server thread "+getName()+" sent SKIP ACK\n");
serverMessagesArea.setCaretPosition(serverMessagesArea.getText().length());
writer.println("Server recieved SKIP before check"); //Send ack if server got the END
writer.flush();
////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////SET THE AMOUNT OF SKIPS FOR WIN CONDITION THE FRIST TIME GOING THROUGH//////////////////
if(ScrabbleServer.this.winFirstSkipCounter == false)
{
winConditionSet();
ScrabbleServer.this.winFirstSkipCounter = true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////INCRIMENT THE SKIP COUNTER//////////////////////////////////////////
ScrabbleServer.this.winSkipCounter += 1;
////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////CHECK FOR WIN////////////////////////////////////////////////////
ScrabbleServer.this.winFlag = winCheck();
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////IF WIN FLAG IS TRIPPED////////////////////////////////////////////
if(ScrabbleServer.this.winFlag == true)
{
Winner();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////ELSE IF THE WIN FLAG ISNT TRIPPED/////////////////////////////////////////
else{
/////////////////////////////////////////////////SEND SKIP TO LOCAL CLIENT//////////////////////////////////////////
writer.println("SKIP");
System.out.println("intWordLength == 0");
serverMessagesArea.append("intWordLength == 0\n");
serverMessagesArea.setCaretPosition(serverMessagesArea.getText().length());
writer.flush();
System.out.println(reader.readLine());
/////////////////////////////////////////////////SEND SKIP TO LOCAL CLIENT//////////////////////////////////////////
/////////////////////////////////////////////////CHANGE TURN////////////////////////////////////////////////////////
changeTurn(getName());
/////////////////////////////////////////////////CHANGE TURN////////////////////////////////////////////////////////
while(true){
////////////////////////////////////////PCHECK TO SEE THAT ALL CLIENTS ARE WAITING/////////////////////////////////////////////////
if(getWaitCount() == ScrabbleServer.this.playerConnectionNumber-1){
System.out.println("//////////////////////////////////////////////////////SOCKET COUNT: "+getWaitCount());
System.out.println("//////////////////////////////////////////////////////READER COUNT: "+ScrabbleServer.this.globalReaders.size());
System.out.println("//////////////////////////////////////////////////////WRITER COUNT: "+ScrabbleServer.this.globalWriters.size());
serverMessagesArea.append("//////////////////////////////////////////////////////SOCKET COUNT: "+getWaitCount() + "\n");
serverMessagesArea.setCaretPosition(serverMessagesArea.getText().length());
serverMessagesArea.append("//////////////////////////////////////////////////////READER COUNT: "+ScrabbleServer.this.globalReaders.size() + "\n");
serverMessagesArea.setCaretPosition(serverMessagesArea.getText().length());
serverMessagesArea.append("//////////////////////////////////////////////////////WRITER COUNT: "+ScrabbleServer.this.globalWriters.size() + "\n");
serverMessagesArea.setCaretPosition(serverMessagesArea.getText().length());
synchronized(ScrabbleServer.this.skipSync)
{
////////////////////////////////////////SEND BACK FOR THE NUMBER OF CLIENTS CONNECTED/////////////////////////////////////////////////
for(int go = 0;go<getWaitCount();go++){
gReader = ScrabbleServer.this.globalReaders.pop();
gWriter = ScrabbleServer.this.globalWriters.pop();
System.out.println("CREATED READER AND WRITER");