-
Notifications
You must be signed in to change notification settings - Fork 0
/
UltimateTCT.java
2323 lines (2114 loc) · 81.3 KB
/
UltimateTCT.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.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* Creates and implements Ultimate Tic-Tac-Toe game
*
* @author Connor 11-6-2020
*/
public class UltimateTCT {
private JFrame jfrm;
private String playerOneName = "";
private String playerTwoName = "";
private boolean smallWin = false;
private boolean bigWin = false;
private boolean playerOneGo = true;
private int[] boardWins = new int[9];
private int emptyBoards = 0;
private int playAgain = 2;
private String[] data = new String[5];
private String dataLocation = System.getProperty("user.dir") + "\\game_data\\data.txt";
private File directory;
private File dataFile;
private int totalGames = 0;
private int totalWins = 0;
private int moveCount = 0;
private double avgMoves = 0.0;
private double winPercent = 0.0;
private ArrayList<JButton> undoQueue = new ArrayList<JButton>();
private ArrayList<Color> undoColors = new ArrayList<Color>();
GridBagConstraints gbc = new GridBagConstraints();
GridBagConstraints pc = new GridBagConstraints();
GridBagConstraints sc = new GridBagConstraints();
GridBagConstraints gc = new GridBagConstraints();
GridBagConstraints sgc = new GridBagConstraints();
GridBagConstraints tbc = new GridBagConstraints();
GridBagConstraints bbc = new GridBagConstraints();
Dimension d = new Dimension(60, 40);
private JLayeredPane game1;
private JLayeredPane game2;
private JLayeredPane game3;
private JLayeredPane game4;
private JLayeredPane game5;
private JLayeredPane game6;
private JLayeredPane game7;
private JLayeredPane game8;
private JLayeredPane game9;
JLayeredPane[] gamePanels = new JLayeredPane[9];
private boolean panel1Found;
private boolean panel2Found;
private boolean panel3Found;
private boolean panel4Found;
private boolean panel5Found;
private boolean panel6Found;
private boolean panel7Found;
private boolean panel8Found;
private boolean panel9Found;
boolean[] disablePanels = new boolean[9];
private JButton button111;
private JButton button112;
private JButton button113;
private JButton button121;
private JButton button122;
private JButton button123;
private JButton button131;
private JButton button132;
private JButton button133;
private JButton button211;
private JButton button212;
private JButton button213;
private JButton button221;
private JButton button222;
private JButton button223;
private JButton button231;
private JButton button232;
private JButton button233;
private JButton button311;
private JButton button312;
private JButton button313;
private JButton button321;
private JButton button322;
private JButton button323;
private JButton button331;
private JButton button332;
private JButton button333;
private JButton button411;
private JButton button412;
private JButton button413;
private JButton button421;
private JButton button422;
private JButton button423;
private JButton button431;
private JButton button432;
private JButton button433;
private JButton button511;
private JButton button512;
private JButton button513;
private JButton button521;
private JButton button522;
private JButton button523;
private JButton button531;
private JButton button532;
private JButton button533;
private JButton button611;
private JButton button612;
private JButton button613;
private JButton button621;
private JButton button622;
private JButton button623;
private JButton button631;
private JButton button632;
private JButton button633;
private JButton button711;
private JButton button712;
private JButton button713;
private JButton button721;
private JButton button722;
private JButton button723;
private JButton button731;
private JButton button732;
private JButton button733;
private JButton button811;
private JButton button812;
private JButton button813;
private JButton button821;
private JButton button822;
private JButton button823;
private JButton button831;
private JButton button832;
private JButton button833;
private JButton button911;
private JButton button912;
private JButton button913;
private JButton button921;
private JButton button922;
private JButton button923;
private JButton button931;
private JButton button932;
private JButton button933;
private JButton playerOne;
private JButton playerTwo;
private JButton restart;
private JButton undo;
private JButton stats;
private JTextField winPercentField;
private JTextField gameCountField;
private JTextField avgMovesField;
/**
* Constructor
*/
public UltimateTCT() {
jfrm = new JFrame("Ultimate Tic-Tac-Toe Game");
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jfrm.setSize(600, 500);
jfrm.setLayout(new GridBagLayout());
jfrm.setResizable(false);
jfrm.setVisible(true);
JPanel topBar = new JPanel();
topBar.setLayout(new GridBagLayout());
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(6, 10, 0, 0);
jfrm.add(topBar, gbc);
JPanel playing = new JPanel();
playing.setLayout(new GridBagLayout());
tbc.gridx = 1;
tbc.gridy = 1;
jfrm.add(playing, tbc);
JLabel nowPlaying = new JLabel("Now Playing:");
pc.gridx = 1;
pc.insets = new Insets(0, 0, 0, 6);
playing.add(nowPlaying, pc);
playerOne = new JButton("Player O");
pc.gridx = 2;
playing.add(playerOne, pc);
playerTwo = new JButton("Player X");
pc.gridx = 3;
playing.add(playerTwo, pc);
JPanel settings = new JPanel();
settings.setLayout(new GridBagLayout());
tbc.gridx = 3;
jfrm.add(settings, tbc);
JLabel manage = new JLabel("Manage Game:");
sc.gridx = 1;
sc.insets = new Insets(0, 0, 0, 6);
settings.add(manage, sc);
restart = new JButton("Restart");
sc.gridx = 2;
settings.add(restart, sc);
undo = new JButton("Undo");
sc.gridx = 3;
settings.add(undo, sc);
JPanel games = new JPanel();
games.setLayout(new GridBagLayout());
games.setBorder(BorderFactory.createLineBorder(Color.red));
gbc.insets = new Insets(6, 0, 0, 0);
gbc.gridx = 1;
gbc.gridy = 2;
gbc.gridwidth = 3;
gbc.gridheight = 3;
gbc.weightx = 0;
gbc.weighty = 0;
jfrm.add(games, gbc);
game1 = new JLayeredPane();
game1.setLayout(new GridLayout(3, 3, 4, 4));
game1.setBorder(BorderFactory.createLineBorder(Color.red));
gc.gridx = 1;
gc.gridy = 1;
gc.gridheight = 1;
gc.gridwidth = 1;
games.add(game1, gc);
gamePanels[0] = game1;
button111 = new JButton("");
button111.setPreferredSize(d);
game1.add(button111);
button112 = new JButton("");
button112.setPreferredSize(d);
game1.add(button112);
button113 = new JButton("");
button113.setPreferredSize(d);
game1.add(button113);
button121 = new JButton("");
button121.setPreferredSize(d);
game1.add(button121);
button122 = new JButton("");
button122.setPreferredSize(d);
game1.add(button122);
button123 = new JButton("");
button123.setPreferredSize(d);
game1.add(button123);
button131 = new JButton("");
button131.setPreferredSize(d);
game1.add(button131);
button132 = new JButton("");
button132.setPreferredSize(d);
game1.add(button132);
button133 = new JButton("");
button133.setPreferredSize(d);
game1.add(button133);
game2 = new JLayeredPane();
game2.setLayout(new GridLayout(3, 3, 4, 4));
game2.setBorder(BorderFactory.createLineBorder(Color.red));
gc.gridx = 2;
games.add(game2, gc);
gamePanels[1] = game2;
button211 = new JButton("");
button211.setPreferredSize(d);
game2.add(button211);
button212 = new JButton("");
button212.setPreferredSize(d);
game2.add(button212);
button213 = new JButton("");
button213.setPreferredSize(d);
game2.add(button213);
button221 = new JButton("");
button221.setPreferredSize(d);
game2.add(button221);
button222 = new JButton("");
button222.setPreferredSize(d);
game2.add(button222);
button223 = new JButton("");
button223.setPreferredSize(d);
game2.add(button223);
button231 = new JButton("");
button231.setPreferredSize(d);
game2.add(button231);
button232 = new JButton("");
button232.setPreferredSize(d);
game2.add(button232);
button233 = new JButton("");
button233.setPreferredSize(d);
game2.add(button233);
game3 = new JLayeredPane();
game3.setLayout(new GridLayout(3, 3, 4, 4));
game3.setBorder(BorderFactory.createLineBorder(Color.red));
gc.gridx = 3;
games.add(game3, gc);
gamePanels[2] = game3;
button311 = new JButton("");
button311.setPreferredSize(d);
game3.add(button311);
button312 = new JButton("");
button312.setPreferredSize(d);
game3.add(button312);
button313 = new JButton("");
button313.setPreferredSize(d);
game3.add(button313);
button321 = new JButton("");
button321.setPreferredSize(d);
game3.add(button321);
button322 = new JButton("");
button322.setPreferredSize(d);
game3.add(button322);
button323 = new JButton("");
button323.setPreferredSize(d);
game3.add(button323);
button331 = new JButton("");
button331.setPreferredSize(d);
game3.add(button331);
button332 = new JButton("");
button332.setPreferredSize(d);
game3.add(button332);
button333 = new JButton("");
button333.setPreferredSize(d);
game3.add(button333);
game4 = new JLayeredPane();
game4.setLayout(new GridLayout(3, 3, 4, 4));
game4.setBorder(BorderFactory.createLineBorder(Color.red));
gc.gridx = 1;
gc.gridy = 2;
gc.gridheight = 1;
gc.gridwidth = 1;
games.add(game4, gc);
gamePanels[3] = game4;
button411 = new JButton("");
button411.setPreferredSize(d);
game4.add(button411);
button412 = new JButton("");
button412.setPreferredSize(d);
game4.add(button412);
button413 = new JButton("");
button413.setPreferredSize(d);
game4.add(button413);
button421 = new JButton("");
button421.setPreferredSize(d);
game4.add(button421);
button422 = new JButton("");
button422.setPreferredSize(d);
game4.add(button422);
button423 = new JButton("");
button423.setPreferredSize(d);
game4.add(button423);
button431 = new JButton("");
button431.setPreferredSize(d);
game4.add(button431);
button432 = new JButton("");
button432.setPreferredSize(d);
game4.add(button432);
button433 = new JButton("");
button433.setPreferredSize(d);
game4.add(button433);
game5 = new JLayeredPane();
game5.setLayout(new GridLayout(3, 3, 4, 4));
game5.setBorder(BorderFactory.createLineBorder(Color.red));
gc.gridx = 2;
games.add(game5, gc);
gamePanels[4] = game5;
button511 = new JButton("");
button511.setPreferredSize(d);
game5.add(button511);
button512 = new JButton("");
button512.setPreferredSize(d);
game5.add(button512);
button513 = new JButton("");
button513.setPreferredSize(d);
game5.add(button513);
button521 = new JButton("");
button521.setPreferredSize(d);
game5.add(button521);
button522 = new JButton("");
button522.setPreferredSize(d);
game5.add(button522);
button523 = new JButton("");
button523.setPreferredSize(d);
game5.add(button523);
button531 = new JButton("");
button531.setPreferredSize(d);
game5.add(button531);
button532 = new JButton("");
button532.setPreferredSize(d);
game5.add(button532);
button533 = new JButton("");
button533.setPreferredSize(d);
game5.add(button533);
game6 = new JLayeredPane();
game6.setLayout(new GridLayout(3, 3, 4, 4));
game6.setBorder(BorderFactory.createLineBorder(Color.red));
gc.gridx = 3;
games.add(game6, gc);
gamePanels[5] = game6;
button611 = new JButton("");
button611.setPreferredSize(d);
game6.add(button611);
button612 = new JButton("");
button612.setPreferredSize(d);
game6.add(button612);
button613 = new JButton("");
button613.setPreferredSize(d);
game6.add(button613);
button621 = new JButton("");
button621.setPreferredSize(d);
game6.add(button621);
button622 = new JButton("");
button622.setPreferredSize(d);
game6.add(button622);
button623 = new JButton("");
button623.setPreferredSize(d);
game6.add(button623);
button631 = new JButton("");
button631.setPreferredSize(d);
game6.add(button631);
button632 = new JButton("");
button632.setPreferredSize(d);
game6.add(button632);
button633 = new JButton("");
button633.setPreferredSize(d);
game6.add(button633);
game7 = new JLayeredPane();
game7.setLayout(new GridLayout(3, 3, 4, 4));
game7.setBorder(BorderFactory.createLineBorder(Color.red));
gc.gridx = 1;
gc.gridy = 3;
gc.gridheight = 1;
gc.gridwidth = 1;
games.add(game7, gc);
gamePanels[6] = game7;
button711 = new JButton("");
button711.setPreferredSize(d);
game7.add(button711);
button712 = new JButton("");
button712.setPreferredSize(d);
game7.add(button712);
button713 = new JButton("");
button713.setPreferredSize(d);
game7.add(button713);
button721 = new JButton("");
button721.setPreferredSize(d);
game7.add(button721);
button722 = new JButton("");
button722.setPreferredSize(d);
game7.add(button722);
button723 = new JButton("");
button723.setPreferredSize(d);
game7.add(button723);
button731 = new JButton("");
button731.setPreferredSize(d);
game7.add(button731);
button732 = new JButton("");
button732.setPreferredSize(d);
game7.add(button732);
button733 = new JButton("");
button733.setPreferredSize(d);
game7.add(button733);
game8 = new JLayeredPane();
game8.setLayout(new GridLayout(3, 3, 4, 4));
game8.setBorder(BorderFactory.createLineBorder(Color.red));
gc.gridx = 2;
games.add(game8, gc);
gamePanels[7] = game8;
button811 = new JButton("");
button811.setPreferredSize(d);
game8.add(button811);
button812 = new JButton("");
button812.setPreferredSize(d);
game8.add(button812);
button813 = new JButton("");
button813.setPreferredSize(d);
game8.add(button813);
button821 = new JButton("");
button821.setPreferredSize(d);
game8.add(button821);
button822 = new JButton("");
button822.setPreferredSize(d);
game8.add(button822);
button823 = new JButton("");
button823.setPreferredSize(d);
game8.add(button823);
button831 = new JButton("");
button831.setPreferredSize(d);
game8.add(button831);
button832 = new JButton("");
button832.setPreferredSize(d);
game8.add(button832);
button833 = new JButton("");
button833.setPreferredSize(d);
game8.add(button833);
game9 = new JLayeredPane();
game9.setLayout(new GridLayout(3, 3, 4, 4));
game9.setBorder(BorderFactory.createLineBorder(Color.red));
gc.gridx = 3;
games.add(game9, gc);
gamePanels[8] = game9;
button911 = new JButton("");
button911.setPreferredSize(d);
game9.add(button911);
button912 = new JButton("");
button912.setPreferredSize(d);
game9.add(button912);
button913 = new JButton("");
button913.setPreferredSize(d);
game9.add(button913);
button921 = new JButton("");
button921.setPreferredSize(d);
game9.add(button921);
button922 = new JButton("");
button922.setPreferredSize(d);
game9.add(button922);
button923 = new JButton("");
button923.setPreferredSize(d);
game9.add(button923);
button931 = new JButton("");
button931.setPreferredSize(d);
game9.add(button931);
button932 = new JButton("");
button932.setPreferredSize(d);
game9.add(button932);
button933 = new JButton("");
button933.setPreferredSize(d);
game9.add(button933);
JPanel bottomBar = new JPanel();
bottomBar.setLayout(new GridBagLayout());
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(0, 0, 0, 0);
gbc.gridwidth = 3;
gbc.gridheight = 1;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridx = 1;
gbc.gridy = 5;
jfrm.add(bottomBar, gbc);
stats = new JButton("Playing Stats");
bbc.insets = new Insets(0, 0, 0, 6);
bbc.gridx = 1;
bottomBar.add(stats, bbc);
JLabel winPercent = new JLabel("Win %:");
bbc.gridx = 2;
bottomBar.add(winPercent, bbc);
winPercentField = new JTextField();
winPercentField.setEditable(false);
bbc.fill = GridBagConstraints.HORIZONTAL;
bbc.gridx = 3;
bbc.weightx = 0.2;
bottomBar.add(winPercentField, bbc);
JLabel gameCount = new JLabel("Total # of Games:");
bbc.gridx = 4;
bbc.weightx = 0.0;
bottomBar.add(gameCount, bbc);
gameCountField = new JTextField();
gameCountField.setEditable(false);
bbc.gridx = 5;
bbc.weightx = 0.2;
bottomBar.add(gameCountField, bbc);
JLabel avgMoves = new JLabel("Average # of Move Per Win:");
bbc.gridx = 6;
bbc.weightx = 0;
bottomBar.add(avgMoves, bbc);
avgMovesField = new JTextField();
avgMovesField.setEditable(false);
bbc.gridx = 7;
bbc.weightx = 0.2;
bottomBar.add(avgMovesField, bbc);
addButtonListeners();
try {
directory = new File(System.getProperty("user.dir") + "\\game_data");
dataFile = new File(dataLocation);
if (!directory.exists()) {
directory.mkdir();
}
dataFile.createNewFile();
} catch (IOException e) {
System.out.println("Error creating data file.");
e.printStackTrace();
}
startGame();
}
/**
* Adds actionListeners to all buttons
*/
public void addButtonListeners() {
button111.addActionListener(new GameButton());
button112.addActionListener(new GameButton());
button113.addActionListener(new GameButton());
button121.addActionListener(new GameButton());
button122.addActionListener(new GameButton());
button123.addActionListener(new GameButton());
button131.addActionListener(new GameButton());
button132.addActionListener(new GameButton());
button133.addActionListener(new GameButton());
button211.addActionListener(new GameButton());
button212.addActionListener(new GameButton());
button213.addActionListener(new GameButton());
button221.addActionListener(new GameButton());
button222.addActionListener(new GameButton());
button223.addActionListener(new GameButton());
button231.addActionListener(new GameButton());
button232.addActionListener(new GameButton());
button233.addActionListener(new GameButton());
button311.addActionListener(new GameButton());
button312.addActionListener(new GameButton());
button313.addActionListener(new GameButton());
button321.addActionListener(new GameButton());
button322.addActionListener(new GameButton());
button323.addActionListener(new GameButton());
button331.addActionListener(new GameButton());
button332.addActionListener(new GameButton());
button333.addActionListener(new GameButton());
button411.addActionListener(new GameButton());
button412.addActionListener(new GameButton());
button413.addActionListener(new GameButton());
button421.addActionListener(new GameButton());
button422.addActionListener(new GameButton());
button423.addActionListener(new GameButton());
button431.addActionListener(new GameButton());
button432.addActionListener(new GameButton());
button433.addActionListener(new GameButton());
button511.addActionListener(new GameButton());
button512.addActionListener(new GameButton());
button513.addActionListener(new GameButton());
button521.addActionListener(new GameButton());
button522.addActionListener(new GameButton());
button523.addActionListener(new GameButton());
button531.addActionListener(new GameButton());
button532.addActionListener(new GameButton());
button533.addActionListener(new GameButton());
button611.addActionListener(new GameButton());
button612.addActionListener(new GameButton());
button613.addActionListener(new GameButton());
button621.addActionListener(new GameButton());
button622.addActionListener(new GameButton());
button623.addActionListener(new GameButton());
button631.addActionListener(new GameButton());
button632.addActionListener(new GameButton());
button633.addActionListener(new GameButton());
button711.addActionListener(new GameButton());
button712.addActionListener(new GameButton());
button713.addActionListener(new GameButton());
button721.addActionListener(new GameButton());
button722.addActionListener(new GameButton());
button723.addActionListener(new GameButton());
button731.addActionListener(new GameButton());
button732.addActionListener(new GameButton());
button733.addActionListener(new GameButton());
button811.addActionListener(new GameButton());
button812.addActionListener(new GameButton());
button813.addActionListener(new GameButton());
button821.addActionListener(new GameButton());
button822.addActionListener(new GameButton());
button823.addActionListener(new GameButton());
button831.addActionListener(new GameButton());
button832.addActionListener(new GameButton());
button833.addActionListener(new GameButton());
button911.addActionListener(new GameButton());
button912.addActionListener(new GameButton());
button913.addActionListener(new GameButton());
button921.addActionListener(new GameButton());
button922.addActionListener(new GameButton());
button923.addActionListener(new GameButton());
button931.addActionListener(new GameButton());
button932.addActionListener(new GameButton());
button933.addActionListener(new GameButton());
playerOne.addActionListener(new SettingsListener());
playerTwo.addActionListener(new SettingsListener());
restart.addActionListener(new SettingsListener());
undo.addActionListener(new SettingsListener());
stats.addActionListener(new SettingsListener());
}// End of actionListener method
/**
* Logic for tic-tac-toe game buttons. Buttons for the current game are the only
* ones enabled. Panel of buttons is turned off when a game is completed.
*
* @author Connor 11-16-2020
*/
class GameButton implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
panel1Found = game1.isAncestorOf((Component) e.getSource());
panel2Found = game2.isAncestorOf((Component) e.getSource());
panel3Found = game3.isAncestorOf((Component) e.getSource());
panel4Found = game4.isAncestorOf((Component) e.getSource());
panel5Found = game5.isAncestorOf((Component) e.getSource());
panel6Found = game6.isAncestorOf((Component) e.getSource());
panel7Found = game7.isAncestorOf((Component) e.getSource());
panel8Found = game8.isAncestorOf((Component) e.getSource());
panel9Found = game9.isAncestorOf((Component) e.getSource());
boolean[] tempList = { panel1Found, panel2Found, panel3Found, panel4Found, panel5Found, panel6Found,
panel7Found, panel8Found, panel9Found };
for (int x = 0; x < disablePanels.length; x++) {
disablePanels[x] = tempList[x];
}
// Disables all panels that aren't a part of the current small game board
for (int i = 0; i < disablePanels.length; i++) {
if (disablePanels[i] == false) {
if (gamePanels[i] != null) {
Component[] currentComponents = gamePanels[i].getComponents();
for (int j = 0; j < currentComponents.length; j++) {
currentComponents[j].setEnabled(false);
}
}
}
}
// Changes color of button and stores record of button clicked and previous
// button color
if (playerOneGo == true) {
playerOne.setEnabled(false);
playerOne.setBackground(null);
playerTwo.setEnabled(true);
playerTwo.setBackground(Color.RED);
JButton b = (JButton) e.getSource();
b.setBackground(Color.BLUE);
undoQueue.add(b);
undoColors.add(b.getBackground());
} else {
playerOne.setEnabled(true);
playerOne.setBackground(Color.BLUE);
playerTwo.setEnabled(false);
playerTwo.setBackground(null);
JButton b = (JButton) e.getSource();
b.setBackground(Color.RED);
undoQueue.add(b);
undoColors.add(b.getBackground());
}
checkSmallWin();
playerOneGo = !playerOneGo;
moveCount++;
}// End of actionPerformed method
}// End of GameButton class
/**
* Logic for menu buttons
*
* @author Connor 11-16-2020
*/
class SettingsListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JButton b = (JButton) e.getSource();
// Clears the board and restarts the game
if (b.getText().contentEquals("Restart")) {
gamePanels[0] = game1;
gamePanels[1] = game2;
gamePanels[2] = game3;
gamePanels[3] = game4;
gamePanels[4] = game5;
gamePanels[5] = game6;
gamePanels[6] = game7;
gamePanels[7] = game8;
gamePanels[8] = game9;
for (int i = 0; i < gamePanels.length; i++) {
Component[] currentComponents = gamePanels[i].getComponents();
for (int j = 0; j < currentComponents.length; j++) {
currentComponents[j].setBackground(null);
currentComponents[j].setEnabled(true);
}
}
startGame();
} // End of Restart algorithm
// Reads the data text file and displays the results in the proper text fields
if (b.getText().contentEquals("Playing Stats")) {
try {
int lineCount = 0;
Scanner scan = new Scanner(dataFile);
Scanner scanSplit = new Scanner(dataFile);
while (scan.hasNextLine()) {
lineCount++;
scan.nextLine();
}
scan.close();
if (lineCount == 0) {
JOptionPane.showMessageDialog(jfrm, "There is no data to show.");
} else {
data = scanSplit.nextLine().split(" ");
double percent = Double.valueOf(data[4]) * 100;
winPercentField.setText(String.format("%.2f", percent));
gameCountField.setText(data[0]);
avgMovesField.setText(String.format("%.2f", Double.valueOf(data[3])));
scanSplit.close();
}
} catch (FileNotFoundException e1) {
System.out.println("Error reading data file");
e1.printStackTrace();
}
} // End of Playing Statistic algorithm
// Undo logic to accommodate for a move that intersects with a completed game,
// or is before the first move
if (b.getText().contentEquals("Undo")) {
try {
JLayeredPane[] undoPanels = { game1, game2, game3, game4, game5, game6, game7, game8, game9 };
if (undoQueue.size() > 0) {
if ((undoQueue.get(undoQueue.size() - 1).getBackground() == Color.BLUE)
|| (undoQueue.get(undoQueue.size() - 1).getBackground() == Color.RED)
|| (undoQueue.get(undoQueue.size() - 1).getBackground() == Color.YELLOW)) {
Container location = undoQueue.get(undoQueue.size() - 1).getParent();
boolean valid = true;
int blueCount = 0;
int redCount = 0;
int yellowCount = 0;
for (int i = 0; i < undoPanels.length; i++) {
if (undoPanels[i] == location) {
Component[] currentComponents = undoPanels[i].getComponents();
for (int k = 0; k < currentComponents.length; k++) {
if (currentComponents[k].getBackground() == Color.BLUE) {
blueCount++;
}
if (currentComponents[k].getBackground() == Color.RED) {
redCount++;
}
if (currentComponents[k].getBackground() == Color.YELLOW) {
yellowCount++;
}
}
}
}
// If the board is completed, a move can't be undone
if (blueCount == 9 || redCount == 9 || yellowCount == 9) {
valid = false;
}
if (valid == true) {
if (undoColors.size() > 1) {
undoQueue.get(undoQueue.size() - 1)
.setBackground(undoColors.get(undoColors.size() - 2));
undoColors.remove(undoColors.get(undoColors.size() - 2));
} else {
undoQueue.get(undoQueue.size() - 1).setBackground(null);
undoColors.clear();
}
moveCount--;
if (playerOneGo == true) {
playerOne.setEnabled(false);
playerOne.setBackground(null);
playerTwo.setEnabled(true);
playerTwo.setBackground(Color.RED);
} else {
playerOne.setEnabled(true);
playerOne.setBackground(Color.BLUE);
playerTwo.setEnabled(false);
playerTwo.setBackground(null);
}
playerOneGo = !playerOneGo;
undoQueue.remove(undoQueue.get(undoQueue.size() - 1));
} else {
JOptionPane.showMessageDialog(jfrm, "You can't undo a completed game");
}