Korte inleiding: Vanaf 1 februari doe ik een carriere-switch naar de IT. Mij is gevraagd om voor die tijd mijn OCA te halen. Hierbij kreeg ik wat tips, zoals eerst een boek lezen en wanneer je Tic Tac Toe kan maken, kun je aan het OCA boek beginnen. Daarbij was het verstandig om niet met een IDE te werken om daadwerkelijk alles zelf te moeten doen en leren. Nou goed, het boek is uit en het was tijd voor Tic Tac Toe. Voor het geheel gebruik ik Context (http://www.contexteditor.org/index.php).
Tic Tac Toe is zichtbaar en er verschijnt een X op de button zodra ik een button aanklik. Daar houdt het echter op. Dit zijn mijn classen:
cell
board
player
game
Om het geheel te testen schreef begon ik met een test class voor de cell class... maar daar ging het ook al in de problemen en nu weet ik het even niet meer. Al mijn zoektochten op Google (javafx extend button class e.d.) leiden niet tot een oplossing.
Test
Dit levert namelijk bij het runnen de volgende melding op:
Exception in thread "main" java.lang.ExceptionInInitializerError at Test.main(Test.java:16)
Caused by: java.lang.IllegalStateException: Toolkit not initialized
at javafx.graphics/com.sun.javafx.application.PlatformImpl.runLater(Unknown Source)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.runLater(Unknown Source)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.setPlatformUserAgentStylesheet(Unknown Source)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.setDefaultPlatformUserAgentStylesheet
at javafx.controls/javafx.scene.control.Control.<clinit>(Unknown Source)
Het geheel gaat mijn pet nog even te boven
Tic Tac Toe is zichtbaar en er verschijnt een X op de button zodra ik een button aanklik. Daar houdt het echter op. Dit zijn mijn classen:
cell
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
| import javafx.scene.control.Button; public class cell extends Button{ //int length; //length of all sides private String state; // X, O, or unset "" //constructor public cell(){ super(); state = ""; //standard length //length = 100; } public boolean isSet(){ if(!state.equals("")){ return true; } return false; } public void setState(String x){ state = x; } public String getState(){ return state; } } |
board
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
| public class board{ public cell[][] board; //constructor public board(){ board = new cell[3][3]; for(int x = 0; x<3; x++){ for(int y = 0; y<3; y++){ //creates 9 cells with empty states board[x][y]= new cell(""); } } } // checks if cells at row Y have the same state boolean checkRow(int y){ //isSet check for all cells for(int x = 0; x<3; x++){ if(!board[x][y].isSet()){ return false; } } //if all cells are set, check for win-condition if( board[0][y].getState().equals( board[1][y].getState() ) && board[1][y].getState().equals(board[2][y].getState()) ){ return true; } return false; } // checks if cells at column X have the same state boolean checkColumn(int x){ //isSet check for all cells for(int y = 0; y<3; y++){ if(!board[x][y].isSet()){ return false; } } //if all cells are set, check for win-condition if( board[x][0].getState().equals( board[x][1].getState() ) && board[x][1].getState().equals(board[x][2].getState()) ){ return true; } return false; } boolean checkDiagonals(){ boolean check1 = true; boolean check2 = true; // diagonal 1 (top left, bottom right) isSet check for(int i = 0; i<3; i++){ if(!board[i][i].isSet()){ check1 = false; } } //if all cells are set, check for win-condition if(check1){ if( board[0][0].getState().equals( board[1][1].getState() ) && board[1][1].getState().equals(board[2][2].getState()) ){ return check1; } } // diagonal 2 (bottom left, top right) isSet check (only if diagonal 1 didn't have the win-condition for(int i = 0; i<3; i++){ for(int j = 2; j>=0; j--){ if(!board[i][j].isSet()){ check2 = false; } } } //if all cells are set, check for win-condition if(check2){ if( board[0][2].getState().equals( board[1][1].getState() ) && board[1][1].getState().equals(board[2][0].getState()) ){ return check2; } } // no diagonal had a win-condition return false; } public boolean checkBoard(){ if(checkRow(0) || checkRow(1) || checkRow(2) || checkColumn(0) || checkColumn(1) || checkColumn(2) || checkDiagonals()){ return true; } return false; } } |
player
Java:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| class player{ private int number; private String userSymbol; public player(String u){ userSymbol = u; } public void setNumber(int n){ number = n; } public int getNumber(){ return number; } public void setUserSymbol(String s){ userSymbol = s; } public String getUserSymbol(){ return userSymbol; } } |
game
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
| /* My own Tic-Tac-Toe game. */ import javafx.application.*; import javafx.scene.*; import javafx.stage.*; import javafx.scene.layout.*; import javafx.scene.control.*; import javafx.event.*; import javafx.geometry.*; public class game extends Application{ boolean isPlayer1 = true; boolean finished = false; public static void main(String[] args){ //start the JavaFX application by calling launch() launch(args); } // override the start() method public void start(Stage myStage){ board gameBoard = new board(); player player1 = new player("X"); player player2 = new player("O"); player currentPlayer; if(isPlayer1){ currentPlayer = player1; } else{ currentPlayer = player2; } // give the stage a title myStage.setTitle("Tic-Tac-Toe"); // use a flowpane for the root node. In this case, // vertical and horizontal gaps of 10 GridPane rootNode = new GridPane(); // center the controls in the scene rootNode.setAlignment(Pos.CENTER); // create a scene Scene myScene = new Scene(rootNode, 340, 340); // set the scene on the stage myStage.setScene(myScene); // handle the action events for the cell-buttons gameBoard.board[0][0].setOnAction(new EventHandler<ActionEvent>(){ public void handle(ActionEvent ae){ if(!finished){ gameBoard.board[0][0].setText(currentPlayer.getUserSymbol()); gameBoard.board[0][0].setText(currentPlayer.getUserSymbol()); if(gameBoard.checkBoard()){ finished = true; System.out.print(currentPlayer.getUserSymbol() + " has won!"); } else{ isPlayer1 = !isPlayer1; } } } }); gameBoard.board[0][1].setOnAction(new EventHandler<ActionEvent>(){ public void handle(ActionEvent ae){ if(!finished){ gameBoard.board[0][1].setText(currentPlayer.getUserSymbol()); gameBoard.board[0][1].setText(currentPlayer.getUserSymbol()); if(gameBoard.checkBoard()){ finished = true; System.out.print(currentPlayer.getUserSymbol() + " has won!"); } else{ isPlayer1 = !isPlayer1; } } } }); gameBoard.board[0][2].setOnAction(new EventHandler<ActionEvent>(){ public void handle(ActionEvent ae){ if(!finished){ gameBoard.board[0][2].setText(currentPlayer.getUserSymbol()); gameBoard.board[0][2].setText(currentPlayer.getUserSymbol()); if(gameBoard.checkBoard()){ finished = true; System.out.print(currentPlayer.getUserSymbol() + " has won!"); } else{ isPlayer1 = !isPlayer1; } } } }); gameBoard.board[1][0].setOnAction(new EventHandler<ActionEvent>(){ public void handle(ActionEvent ae){ if(!finished){ gameBoard.board[1][0].setText(currentPlayer.getUserSymbol()); gameBoard.board[1][0].setText(currentPlayer.getUserSymbol()); if(gameBoard.checkBoard()){ finished = true; System.out.print(currentPlayer.getUserSymbol() + " has won!"); } else{ isPlayer1 = !isPlayer1; } } } }); gameBoard.board[1][1].setOnAction(new EventHandler<ActionEvent>(){ public void handle(ActionEvent ae){ if(!finished){ gameBoard.board[1][1].setText(currentPlayer.getUserSymbol()); gameBoard.board[1][1].setText(currentPlayer.getUserSymbol()); if(gameBoard.checkBoard()){ finished = true; System.out.print(currentPlayer.getUserSymbol() + " has won!"); } else{ isPlayer1 = !isPlayer1; } } } }); gameBoard.board[1][2].setOnAction(new EventHandler<ActionEvent>(){ public void handle(ActionEvent ae){ if(!finished){ gameBoard.board[1][2].setText(currentPlayer.getUserSymbol()); gameBoard.board[1][2].setText(currentPlayer.getUserSymbol()); if(gameBoard.checkBoard()){ finished = true; System.out.print(currentPlayer.getUserSymbol() + " has won!"); } else{ isPlayer1 = !isPlayer1; } } } }); gameBoard.board[2][0].setOnAction(new EventHandler<ActionEvent>(){ public void handle(ActionEvent ae){ if(!finished){ gameBoard.board[2][0].setText(currentPlayer.getUserSymbol()); gameBoard.board[2][0].setText(currentPlayer.getUserSymbol()); if(gameBoard.checkBoard()){ finished = true; System.out.print(currentPlayer.getUserSymbol() + " has won!"); } else{ isPlayer1 = !isPlayer1; } } } }); gameBoard.board[2][1].setOnAction(new EventHandler<ActionEvent>(){ public void handle(ActionEvent ae){ if(!finished){ gameBoard.board[2][1].setText(currentPlayer.getUserSymbol()); gameBoard.board[2][1].setState(currentPlayer.getUserSymbol()); if(gameBoard.checkBoard()){ finished = true; System.out.print(currentPlayer.getUserSymbol() + " has won!"); } else{ isPlayer1 = !isPlayer1; } } } }); gameBoard.board[2][2].setOnAction(new EventHandler<ActionEvent>(){ public void handle(ActionEvent ae){ if(!finished){ gameBoard.board[2][2].setText(currentPlayer.getUserSymbol()); gameBoard.board[2][2].setText(currentPlayer.getUserSymbol()); if(gameBoard.checkBoard()){ finished = true; System.out.print(currentPlayer.getUserSymbol() + " has won!"); } else{ isPlayer1 = !isPlayer1; } } } }); // add the label and buttons to the scene graph rootNode.add(gameBoard.board[0][0],0, 0, 1, 1); rootNode.add(gameBoard.board[0][1],0, 1, 1, 1); rootNode.add(gameBoard.board[0][2],0, 2, 1, 1); rootNode.add(gameBoard.board[1][0],1, 0, 1, 1); rootNode.add(gameBoard.board[1][1],1, 1, 1, 1); rootNode.add(gameBoard.board[1][2],1, 2, 1, 1); rootNode.add(gameBoard.board[2][0],2, 0, 1, 1); rootNode.add(gameBoard.board[2][1],2, 1, 1, 1); rootNode.add(gameBoard.board[2][2],2, 2, 1, 1); //show the stage and its scene myStage.show(); } } |
Om het geheel te testen schreef begon ik met een test class voor de cell class... maar daar ging het ook al in de problemen en nu weet ik het even niet meer. Al mijn zoektochten op Google (javafx extend button class e.d.) leiden niet tot een oplossing.
Test
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
| // test class voor alle andere classen import javafx.application.*; import javafx.scene.*; import javafx.stage.*; import javafx.scene.layout.*; import javafx.scene.control.*; import javafx.event.*; import javafx.geometry.*; class Test{ public static void main(String[] args){ //maak een cell cell testCell = new cell(); System.out.println("test cell aangemaakt"); boolean bool1 = testCell.isSet(); System.out.println("Is test cell geSet?: "+ bool1); testCell.setState("X"); System.out.println("test cell is op waarde X gezet"); bool1 = testCell.isSet(); System.out.println("Is test cell nu geSet?: "+ bool1); System.out.println("Test cell heeft de waarde: "+ testCell.getState()); } } |
Dit levert namelijk bij het runnen de volgende melding op:
Exception in thread "main" java.lang.ExceptionInInitializerError at Test.main(Test.java:16)
Caused by: java.lang.IllegalStateException: Toolkit not initialized
at javafx.graphics/com.sun.javafx.application.PlatformImpl.runLater(Unknown Source)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.runLater(Unknown Source)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.setPlatformUserAgentStylesheet(Unknown Source)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.setDefaultPlatformUserAgentStylesheet
at javafx.controls/javafx.scene.control.Control.<clinit>(Unknown Source)
Het geheel gaat mijn pet nog even te boven

Rebuilding knowledge database