-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtictactoe.java
More file actions
835 lines (688 loc) · 30.5 KB
/
tictactoe.java
File metadata and controls
835 lines (688 loc) · 30.5 KB
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
import java.util.Scanner;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* the game object is created and run here
*/
class TicTacToe {
public static void main(String [] args) {
Game game1 = new Game();
}
}
/**
* Game class holds the tictactoe game
*/
class Game extends JFrame {
/** game boards height */
private int height = 3;
/** game boards width */
private int width = 3;
/** length of the row reguired to win */
private int winRowLength = 3;
/** game board 2d array that holds the information of players symbols' locations */
private String [][] gameBoard;
/** keeps track of how many turns have been played */
private int turnCounter;
/** true if a player has won */
private boolean winner;
/** column coordinate of last symbol put on the game board */
private int chosenColumn;
/** line coordinate of last symbol put on the game board */
private int chosenLine;
/** symbol that the player in turn uses */
private String symbol;
/** random 0 or 1 determines who starts */
private int startNumber;
/** true if it is users turn */
private boolean playerTurn;
/** column coordinate of a placement selection made in the GUI */
private int humanChoiseLine;
/** line coordinate of a placement selection made in the GUI */
private int humanChoiseColumn;
/** true if succesful selection on the graphic gameboard is made*/
private boolean humanChoiseMade;
/** game window */
private GUI gameWindow;
/** object that forwards user selections from the GUI to the game object */
private HumanPlayer player1 = new HumanPlayer();
/** computer "AI" */
private ComPlayer player2 = new ComPlayer();
/**
* game object creates new JFrame game window
*/
public Game() {
gameWindow = new GUI();
}
/**
* disposes the old game window and creates a new one
*/
public void createNewGame(){
gameWindow.dispose();
gameWindow = new GUI();
}
/**
* method creates new empty game board array and resets the variables
*/
public void start(){
winner = false;
playerTurn = false;
humanChoiseMade = false;
gameBoard = new String [height][width];
for(int n = 0; n < height; n++){
for(int m = 0; m < width; m++){
gameBoard[n][m]= " ";
}
}
turnCounter = 0;
startNumber = (int)(Math.random()*2);
}
/**
* prints the game board array
*/
public void printBoard(){
for(int n = 0; n < height; n++){
for(int m = 0; m < width; m++){
System.out.print("["+gameBoard[n][m]+"]");
}
System.out.println();
}
}
/**
* assings the turns for players
* @param player1 HumanPlayer or ComPlayer
* @param player2 HumanPlayer or ComPlayer
*/
public void assignTurn(Player player1, Player player2){
if(!gameEnd()){
if(turnCounter%2==startNumber){
symbol = "X";
playerTurn = true;
}else{
symbol = "O";
player2.play();
}
}
}
/**
* checks if winner has been found or if the gameboard is full
* @return returns true if game is over
*/
public boolean gameEnd(){
return winner || turnCounter == height*width;
}
/**
* checks have the players won or game tied and notifies player in the game window
*/
public void winChecker(){
winner = new RowChecker().check(chosenLine, chosenColumn, winRowLength, symbol, 0, 5);
if(turnCounter%2==startNumber&&winner){
gameWindow.infoText.setText("You lose");
//System.out.println("You lose");
}else if(turnCounter%2!=startNumber&&winner){
gameWindow.infoText.setText("Victory!");
//System.out.println("Victory!");
}else if (gameEnd()){
gameWindow.infoText.setText("Tie");
//System.out.println("Tie");
}
}
/**
* checks if given location is on the game board and if it's free
* @param coordinateLine line coordinate
* @param coordinateColumn column coordinate
* @return true if location is on game board and free
*/
public boolean isValidPlacement(int coordinateLine, int coordinateColumn){
boolean isValid;
if(coordinateLine<height && coordinateLine>=0 && coordinateColumn <width && coordinateColumn>=0 && gameBoard[coordinateLine][coordinateColumn].equals(" ")){
isValid = true;
}else{
isValid = false;
}
return isValid;
}
/**
* class checks the lengths of rows formed by same symbols in given positions
* and delivers information about them
*/
class RowChecker{
/** is true if measured row has one openspace at the end */
private boolean openEndFound;
/** is true if measured row has two openspaces at the end */
private boolean twoOpenEndsFound;
/** is true if measured row has one openspace at the start */
private boolean openStartFound;
/** is true if measured row has two openspaces at the start */
private boolean twoOpenStartsFound;
/** is true if measured row fills the reguirements */
private boolean found;
/** holds the ammount of adjacent symbols that fill the conditions given around a specified location */
private int symbolsAround;
/**
* Method checks the lengths of rows formed by same symbols to all directions from a specified location
* for a row that is at least the specified length and fills the conditions of open spaces at the end
* @param coordinateLine number of the line on witch the check is made
* @param coordinateColumn number of the column on witch the check is made
* @param lengthTofind minimum length to find
* @param symbolToFind the symbol that is searched
* @param condition number given to the condition method that determines how many open places row have to have at the ends to be valid
* @param symbolsAroundCondition number given to the condition method that determines how many open places row have to have at the ends to be counted in symbolsAround variable
* @return returns boolean of did the method find row of symbols to match the length and conditions
*/
public boolean check(int coordinateLine, int coordinateColumn, int lengthTofind, String symbolToFind, int condition, int symbolsAroundCondition){
symbolsAround = 0;
found = false;
//checks vertical lines
if(rowLengthChecker(coordinateLine, coordinateColumn, 1, 0, symbolToFind, symbolsAroundCondition) >= lengthTofind && condition(condition)){
found = true;
}
//checks horisontal lines
else if(rowLengthChecker(coordinateLine, coordinateColumn, 0, 1, symbolToFind, symbolsAroundCondition) >= lengthTofind && condition(condition)){
found = true;
}
//checks rising diagonals
else if(rowLengthChecker(coordinateLine, coordinateColumn, 1, 1, symbolToFind, symbolsAroundCondition) >= lengthTofind && condition(condition)){
found = true;
}
//chacks lowering diagonals
else if(rowLengthChecker(coordinateLine, coordinateColumn, -1, 1, symbolToFind, symbolsAroundCondition) >= lengthTofind && condition(condition)){
found = true;
}
return found;
}
/**
* Method checks the length of a row formed by same symbols from specified location to specified direction
* @param coordinateLine number of the line on witch the check is made
* @param coordinateColumn number of the column on witch the check is made
* @param verticalRise adjusts the direction of the check heightwise. 1 for vertical or rising diagonal 0 for horisontal, and -1 for lowering diagonal
* @param horisontalGrowth adjusts direction of the check horizontally. 0 for vertical, 1 for rising diagonal or horisontal or lowering diagonal.
* @param symbolToFind the symbol that is searched
* @param symbolsAroundCondition number given to the condition method that determines how many open places row have to have at the ends to be counted in symbolsAround variable
* @return returns the length of the row
*/
private int rowLengthChecker(int coordinateLine, int coordinateColumn, int verticalRise, int horisontalGrowth, String symbolToFind, int symbolsAroundCondition){
int lineStartY;
int lineStartX;
int lineStartXAdjacent;
int lineStartYAdjacent;
int lineEndY;
int lineEndX;
int lineEndXAdjacent;
int lineEndYAdjacent;
openStartFound = false;
twoOpenStartsFound = false;
openEndFound = false;
twoOpenEndsFound = false;
int symbolsOnRow=1;
int y = verticalRise;
int x = horisontalGrowth;
//checks selected row "forward"
while(coordinateLine+y <height && coordinateColumn+x<width && coordinateLine+y>=0 && coordinateColumn+x>=0 && gameBoard[coordinateLine+y][coordinateColumn+x].equals(symbolToFind)){
y += verticalRise;
x += horisontalGrowth;
symbolsOnRow++;
}
lineStartX = coordinateColumn+x;
lineStartY = coordinateLine+y;
y += verticalRise;
x += horisontalGrowth;
lineStartXAdjacent = coordinateColumn+x;
lineStartYAdjacent = coordinateLine+y;
if(isValidPlacement(lineStartY,lineStartX)){
openStartFound = true;
}
if(isValidPlacement(lineStartY,lineStartX)&& isValidPlacement(lineStartYAdjacent,lineStartXAdjacent) ){
twoOpenStartsFound = true;
}
y = verticalRise;
x = horisontalGrowth;
//checks selected row "backward"
while(coordinateLine-y <height && coordinateColumn-x<width && coordinateLine-y>=0 && coordinateColumn-x>=0 &&gameBoard[coordinateLine-y][coordinateColumn-x].equals(symbolToFind)){
y += verticalRise;
x += horisontalGrowth;
symbolsOnRow++;
}
lineEndX = coordinateColumn-x;
lineEndY = coordinateLine-y;
y += verticalRise;
x += horisontalGrowth;
lineEndXAdjacent = coordinateColumn-x;
lineEndYAdjacent = coordinateLine-y;
if(isValidPlacement(lineEndY,lineEndX)){
openEndFound = true;
}
if(isValidPlacement(lineEndY,lineEndX)&& isValidPlacement(lineEndYAdjacent,lineEndXAdjacent) ){
twoOpenEndsFound = true;
}
if(condition(symbolsAroundCondition)){
symbolsAround += symbolsOnRow;
}
//System.out.println(symbolsOnRow);
return symbolsOnRow;
}
/**
* this method has six different options for different conditions regarding open spaces at the ends of the rows that are checked
* @param condition
* condition is chosen with number 0-5
* 0 no condition (always true)
* 1 one space at either end
* 2 open space at both ends
* 3 open space at one end and two open spaces at the other
* 4 two open spaces at both ends
* 5 always false (needed for check method to go through all directions)
* @return returns true if last checked row fulfills the given condition
*/
private boolean condition(int condition){
boolean checkConditions = true;
switch (condition) {
//no extra conditions chacks just length
case 0: checkConditions = true;
break;
// at least one open end
case 1: checkConditions = openEndFound || openStartFound;
break;
// both ends open
case 2: checkConditions = openEndFound && openStartFound;
break;
// at least one open on other end and two opens on other
case 3: checkConditions = (openEndFound && twoOpenStartsFound) || (openStartFound && twoOpenEndsFound);
break;
//both ends have two open
case 4: checkConditions = (twoOpenEndsFound && twoOpenStartsFound);
break;
//always false so goes through all directions
case 5: checkConditions = false;
break;
}
return checkConditions;
}
/**
* Gives ammount of adjacent symbols that have the reguired ammount of open spaces after them around given location at the gameboard
* @param line line coordinate
* @param column column coordinate
* @param symbolToCheck searched symbol
* @param condition number given to the condition method that determines how many open places row have to have at the ends to be valid
* @return returns the ammount of adjacent symbols +4 that fill the given conditions around the given location
*/
public int ammountOfSymbolsAround(int line, int column, String symbolToCheck, int condition){
check(line, column, 0, symbolToCheck, 5, condition);
return symbolsAround;
}
}
/**
* abstract class that makes sure that both computer and human players have play method
*
*/
abstract class Player{
abstract void play();
}
/**
* HumanPlayer object forwards user decisions from GUI to gameboard
*/
class HumanPlayer extends Player{
public Scanner input = new Scanner(System.in);
/**
* forwards user decisions from GUI to gameboard and ends humanplayers turn by checking for winner, moving turnCounter, and calling assignTurn method
*/
public void play(){
if(humanChoiseMade){
chosenLine = humanChoiseLine;
chosenColumn = humanChoiseColumn;
gameBoard[chosenLine][chosenColumn] = symbol;
int i = (chosenLine*width)+chosenColumn;
gameWindow.buttons[i].setText(symbol);
//printBoard();
turnCounter++;
winChecker();
humanChoiseMade = false;
playerTurn = false;
assignTurn(player1,player2);
}
}
}
/**
* ComPlayer object plays against human player and checks for different situations on the game board and reacts to them
*/
class ComPlayer extends Player{
/** object that retrieves information about symbol placements on the gameboard */
RowChecker checkSituation = new RowChecker();
/** computer players symbol */
String me = "O";
/** opponents symbol */
String opponent = "X";
/** true if "AI" has made a selectoin where to put symbol*/
boolean rightMoveMade;
/** line coordinate of "AI"'s latest choice (-1 for nothing made yet)*/
int myLastChoiseLine = -1;
/** column coordinate of "AI"'s latest choice (-1 for nothing made yet)*/
int myLastChoiseColumn = -1;
/**
* checks for different scenarions in order and reacts accordingly at the end ends turn by checking for winner, moving turnCounter, and calling assignTurn method
*/
public void play(){
rightMoveMade = false;
int randomArea = (int)((Math.random()*2)+1);
checkIfFirstMove();
//checks if win possible with next move
checkBoard(winRowLength, me, 0);
checkBoard(winRowLength, opponent, 0);
//checks if win possible with two next moves
checkBoard(winRowLength-1, me, 2);
checkBoard(winRowLength-1, opponent, 2);
//checs for placements that could create many potential rows at once
checkForDangerousCrossings(me);
checkForDangerousCrossings(opponent);
//checks for shorter rows with open spaces at the ends
checkBoard(winRowLength-2, me, 4);
//random placement near com's last selection
randomNearBy(randomArea);
//full random
random();
myLastChoiseLine = chosenLine;
myLastChoiseColumn = chosenColumn;
gameBoard[chosenLine][chosenColumn] = symbol;
int i = (chosenLine*width)+chosenColumn;
gameWindow.buttons[i].setText(symbol);
//printBoard();
turnCounter++;
winChecker();
assignTurn(player1,player2);
}
/**
* if no turns have been made com player starts from the middle
*/
public void checkIfFirstMove(){
if(turnCounter==0){
chosenLine = height/2;
chosenColumn = width/2;
rightMoveMade = true;
}
}
/**
* checks board for the possibilities to form reguired length row with reguired ammount of adjacent open spaces
* if method finds open space that matches the reguirements it chooses the location
* @param lengthToCheck reguired length of the row
* @param symbolToCheck symbol that forms the reguired row
* @param condition number given to the RowCheck objects condition method that determines how many open places row have to have at the ends to be valid
*/
public void checkBoard(int lengthToCheck, String symbolToCheck, int condition){
if(!rightMoveMade){
for(int n = 0; n < height; n++){
for(int m = 0; m < width; m++){
if(gameBoard[n][m].equals(" ") && checkSituation.check(n, m, lengthToCheck, symbolToCheck, condition, 0)){
chosenLine = n;
chosenColumn = m;
rightMoveMade = true;
}
}
if(rightMoveMade){
n=height;
}
}
}
}
/**
* checks gameboard for open spaces that have many symbols around it and
* if method finds open space that matches the reguirements it chooses the location
*/
public void checkForDangerousCrossings(String symbolToCheck){
int mostSymbols = 0;
int line = -1;
int column = -1;
if(!rightMoveMade){
for(int n = 0; n < height; n++){
for(int m = 0; m < width; m++){
if(gameBoard[n][m].equals(" ")){
if(checkSituation.ammountOfSymbolsAround(n,m, symbolToCheck, 3)>mostSymbols){
line = n;
column = m;
mostSymbols = checkSituation.ammountOfSymbolsAround(n,m, symbolToCheck, 3);
}
}
}
}
if(mostSymbols>=winRowLength+3){
chosenLine = line;
chosenColumn = column;
rightMoveMade = true;
}
}
}
/**
* randomly chooses a free space at the gameboard
*/
public void random(){
if(!rightMoveMade){
do{
chosenLine = (int)(Math.random()*height);
chosenColumn = (int)(Math.random()*width);
}while(!gameBoard[chosenLine][chosenColumn].equals(" "));
}
}
/**
* randomly chooses a spot near by the last place computer selected
*/
public void randomNearBy(int area){
if(!rightMoveMade&& myLastChoiseColumn>-1 && myLastChoiseLine>-1){
int i = 0;
int line;
int column;
do{
line = (myLastChoiseLine)+((int)(Math.random()*(area*2+1))-area);
column = (myLastChoiseColumn)+((int)(Math.random()*(area*2+1))-area);
i++;
}while(!isValidPlacement(line,column)||i>30);
if(i!=30){
chosenLine = line;
chosenColumn = column;
rightMoveMade = true;
}
}
}
}
/**
* Class for buttons in the gameboard
*/
class XOButton extends JButton implements ActionListener{
/**
* line coordinate of the location that the XOButton represents
*/
int row;
/**
* column coordinate of the location that the XOButton represents
*/
int column;
public XOButton( String text, int heightCordinate, int widthCordinate){
super(text);
row = heightCordinate;
column = widthCordinate;
this.addActionListener(this);
setFont(new Font("Arial", Font.BOLD, 50));
}
/**
* action listener for XOButton
* checks that the place player selected is free and saves the selection if it is
*/
public void actionPerformed(ActionEvent e){
if(gameBoard[row][column].equals(" ")&playerTurn){
humanChoiseColumn = column;
humanChoiseLine = row;
humanChoiseMade = true;
player1.play();
}
}
}
/**
* graphic user interface class for the game window
*/
class GUI extends JFrame{
/** graphic representation of the game board */
private JPanel graphicGameBoard;
/** container for the game info displayed */
private JPanel graphicGameInfo = new JPanel();
/** info text win lose etc..- */
private JLabel infoText;
/** ammount of buttons needed for the gameboard representation */
private int ammountOfSquares;
/** array of buttons for the game board graphical representation */
private XOButton[] buttons;
/** place for the user height input */
private UserSelection heightField;
/** place for the user width input */
private UserSelection widthField;
/** place for the user winning row length input */
private UserSelection winRowField;
/** container for all the elements */
private JPanel mainContainer;
/** true if game is on and start button turns in to reset button */
private boolean reset;
/** true if user set values for the game board are valid */
private boolean valueasAreValid;
/**
* Creates new window
*/
GUI(){
super("Tictactoe");
reset = false;
setPreferredSize(new Dimension(width*150, height*150+150));
setMinimumSize(new Dimension(800,600));
mainContainer = new JPanel();
mainContainer.setSize(width*150, height*150+150);
mainContainer.setLayout(new BoxLayout(mainContainer, BoxLayout.PAGE_AXIS));
setDefaultCloseOperation(EXIT_ON_CLOSE);
graphicGameInfo.setLayout(new BoxLayout(graphicGameInfo, BoxLayout.LINE_AXIS));
graphicGameInfo.setPreferredSize(new Dimension(800, 125));
graphicGameInfo.setMinimumSize(new Dimension(800,125));
JButton startgame = new JButton("start");
startgame.setSize(100, 75);
startgame.setFont(new Font("Arial", Font.BOLD, 24));
startgame.addActionListener( new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
valueasAreValid = true;
if(reset){
createNewGame();
}else{
checkValues();
if(valueasAreValid){
start();
startgame.setText("reset");
infoText.setText("Game on!");
createGameBoard();
assignTurn(player1,player2);
reset = true;
}
}
}
});
infoText = new JLabel("Tic Tac Toe", SwingConstants.LEFT);
infoText.setFont(new Font("Arial", Font.BOLD, 24));
infoText.setMinimumSize(new Dimension(800,100));
infoText.setAlignmentX(JLabel.LEFT_ALIGNMENT);
winRowField = new UserSelection("Win row length", ""+winRowLength);
heightField = new UserSelection("Height", ""+height);
widthField = new UserSelection("Width", ""+width);
graphicGameInfo.add(heightField);
graphicGameInfo.add(widthField);
graphicGameInfo.add(winRowField);
graphicGameInfo.add(startgame);
graphicGameBoard = new JPanel();
mainContainer.add(infoText);
mainContainer.add(graphicGameInfo);
mainContainer.add(graphicGameBoard);
add(mainContainer);
setVisible(true);
}
/**
* creates the graphic representation of the game board
*/
public void createGameBoard(){
mainContainer.remove(graphicGameBoard);
graphicGameBoard = new JPanel();
ammountOfSquares = height * width;
buttons = new XOButton[ammountOfSquares];
setMinimumSize(new Dimension(width*150, height*150+150));
setMaximumSize(new Dimension(width*150, height*150+150));
graphicGameBoard.setMaximumSize(new Dimension(width*150, height*150));
graphicGameBoard.setMinimumSize(new Dimension(width*150, height*150));
graphicGameBoard.setLayout(new GridLayout(height,width));
for(int i=0; i<ammountOfSquares;i++){
int heightCordinate = i/width;
int widthCordinate =i-(heightCordinate*width);
buttons[i] = new XOButton(gameBoard[heightCordinate][widthCordinate], heightCordinate, widthCordinate);
graphicGameBoard.add(buttons[i]);
}
mainContainer.add(graphicGameBoard);
}
/**
* checks the values entered in the height width and win row length field inputs
* and informs the player about the faulty inputs
*/
public void checkValues(){
try{
winRowLength= Integer.parseInt(winRowField.getText().trim());
width= Integer.parseInt(widthField.getText().trim());
height= Integer.parseInt(heightField.getText().trim());
}
catch (Exception e) {
infoText.setText("sizes and length must be numbers");
valueasAreValid = false;
}
if(winRowLength<3 || winRowLength>height || winRowLength>width ){
if(height<width){
infoText.setText("Win row length must be between 3-"+height);
}else{
infoText.setText("Win row length must be 3-"+width);
}
winRowLength = 3;
width = 3;
height = 3;
valueasAreValid = false;
}
if(height<3||height>30 || width<3 || width>30 ){
winRowLength = 3;
width = 3;
height = 3;
infoText.setText("height and width must be between 3-30");
valueasAreValid = false;
}
}
/**
* class used for user selection objects(height, width and win row length ) extends JPanel and contains JTextField and JLabel
*/
class UserSelection extends JPanel{
/** user enters values here */
private JTextField field;
/** tells what value user is setting */
private JLabel header;
/**
*
* @param name header for the JTextField
* @param defaultText default text
*/
UserSelection(String name, String defaultText){
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
header = new JLabel(name);
header.setPreferredSize(new Dimension(200, 50));
header.setMaximumSize(new Dimension(200,50));
header.setFont(new Font("Arial", Font.BOLD, 24));
field = new JTextField(defaultText);
field.setPreferredSize(new Dimension(100, 50));
field.setMaximumSize(new Dimension(100,50));
field.setFont(new Font("Arial", Font.BOLD, 24));
add(header);
add(field);
}
/**
*
* @return reutrns the text in the text field
*/
public String getText(){
return field.getText();
}
}
}
}