-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicTacToe.java
204 lines (176 loc) · 7.83 KB
/
TicTacToe.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
import java.util.Random;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.control.Label;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
public class TicTacToe extends Application {
final byte ROWS = 3;
final byte COLS = 3;
// declare fields which can be used by event handler
private Label resultsLabel;
private ImageView[][] imageBoard;
private Image blank = new Image("file:blank.png");
private Image x = new Image("file:x.png");
private Image o = new Image("file:o.png");
private byte[][] gameBoard;
private byte turn = 1;
// gameResult combinations
// 0 = O wins
// 1 = X wins
// 2 = Tie
// -1 = game not started or in progress
private byte gameResult = -1;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
// Initialize imageBoard with blank images
imageBoard = new ImageView[ROWS][COLS];
for (byte row = 0; row < ROWS; row++) {
for (byte col = 0; col < COLS; col++) {
imageBoard[row][col] = new ImageView(blank);
}
}
// Organize image array onto GridPane
GridPane grid = new GridPane();
grid.add(imageBoard[0][0], 0, 0);
grid.add(imageBoard[0][1], 0, 1);
grid.add(imageBoard[0][2], 0, 2);
grid.add(imageBoard[1][0], 1, 0);
grid.add(imageBoard[1][1], 1, 1);
grid.add(imageBoard[1][2], 1, 2);
grid.add(imageBoard[2][0], 2, 0);
grid.add(imageBoard[2][1], 2, 1);
grid.add(imageBoard[2][2], 2, 2);
grid.setVgap(10);
grid.setHgap(10);
grid.setAlignment(Pos.CENTER);
// Label for showing results
resultsLabel = new Label("Click New Game to play.");
// Button to run the game
Button newGameButton = new Button("New Game");
newGameButton.setOnAction(new NewGameHandler());
// Put everything in a VBox
VBox vBox = new VBox(20, grid, resultsLabel, newGameButton);
vBox.setAlignment(Pos.CENTER);
vBox.setPadding(new Insets(20));
// Add box to scene and set the stage
Scene scene = new Scene(vBox);
primaryStage.setScene(scene);
primaryStage.setTitle("Tic Tac Toe");
primaryStage.show();
}
// Event handler class for New Game button
class NewGameHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent event) {
// check if reset is needed
if (turn != 1) {
turn = 1;
gameResult = -1;
// clear the game pieces
for (byte row = 0; row < ROWS; row++) {
for (byte col = 0; col < COLS; col++) {
imageBoard[row][col].setImage(blank);
}
}
}
Random rand = new Random();
// game array initialization, set every element to three which
// will be used to indicate blank, unused element
gameBoard = new byte[ROWS][COLS];
for (byte row = 0; row < ROWS; row++) {
for (byte col = 0; col < COLS; col++) {
gameBoard[row][col] = 3;
}
}
// play the game
while (gameResult == -1) {
if (turn % 2 != 0) { // X goes first, on odd turns
byte xMoveRow, xMoveCol;
// generate random position until you find a blank element
do {
xMoveRow = (byte) rand.nextInt(3);
xMoveCol = (byte) rand.nextInt(3);
} while (gameBoard[xMoveRow][xMoveCol] != 3); // only elements equal to 3(blank) can be played on
gameBoard[xMoveRow][xMoveCol] = 1; // set element to represent X
imageBoard[xMoveRow][xMoveCol].setImage(x);
}
else { // O goes second, on even turns
byte oMoveRow, oMoveCol;
// generate random position until you find a blank element
do {
oMoveRow = (byte) rand.nextInt(3);
oMoveCol = (byte) rand.nextInt(3);
} while (gameBoard[oMoveRow][oMoveCol] != 3); // only elements equal to 3(blank) can be played on
gameBoard[oMoveRow][oMoveCol] = 0; // set element to represent O
imageBoard[oMoveRow][oMoveCol].setImage(o);
}
// check for a winner -
//
// eight ways to win
//
// does O win? - O's array elements are equal to 0
// vertical check
if (gameBoard[0][0] == 0 && gameBoard[1][0] == 0 && gameBoard[2][0] == 0)
gameResult = 0;
if (gameBoard[0][1] == 0 && gameBoard[1][1] == 0 && gameBoard[2][1] == 0)
gameResult = 0;
if (gameBoard[0][2] == 0 && gameBoard[1][2] == 0 && gameBoard[2][2] == 0)
gameResult = 0;
// horizontal check
if (gameBoard[0][0] == 0 && gameBoard[0][1] == 0 && gameBoard[0][2] == 0)
gameResult = 0;
if (gameBoard[1][0] == 0 && gameBoard[1][1] == 0 && gameBoard[1][2] == 0)
gameResult = 0;
if (gameBoard[2][0] == 0 && gameBoard[2][1] == 0 && gameBoard[2][2] == 0)
gameResult = 0;
// diagonal check
if (gameBoard[0][0] == 0 && gameBoard[1][1] == 0 && gameBoard[2][2] == 0)
gameResult = 0;
if (gameBoard[2][0] == 0 && gameBoard[1][1] == 0 && gameBoard[0][2] == 0)
gameResult = 0;
// does X win? - X's array elements are equal to 1
// vertical check
if (gameBoard[0][0] == 1 && gameBoard[1][0] == 1 && gameBoard[2][0] == 1)
gameResult = 1;
if (gameBoard[0][1] == 1 && gameBoard[1][1] == 1 && gameBoard[2][1] == 1)
gameResult = 1;
if (gameBoard[0][2] == 1 && gameBoard[1][2] == 1 && gameBoard[2][2] == 1)
gameResult = 1;
// horizontal check
if (gameBoard[0][0] == 1 && gameBoard[0][1] == 1 && gameBoard[0][2] == 1)
gameResult = 1;
if (gameBoard[1][0] == 1 && gameBoard[1][1] == 1 && gameBoard[1][2] == 1)
gameResult = 1;
if (gameBoard[2][0] == 1 && gameBoard[2][1] == 1 && gameBoard[2][2] == 1)
gameResult = 1;
// diagonal check
if (gameBoard[0][0] == 1 && gameBoard[1][1] == 1 && gameBoard[2][2] == 1)
gameResult = 1;
if (gameBoard[2][0] == 1 && gameBoard[1][1] == 1 && gameBoard[0][2] == 1)
gameResult = 1;
// is it a tie?
if (turn == 9 && gameResult == -1)
gameResult = 2;
turn++;
}
if (gameResult == 0)
resultsLabel.setText("O Wins!");
if (gameResult == 1)
resultsLabel.setText("X Wins!");
if (gameResult == 2)
resultsLabel.setText("Game is Tied!");
}
}
}