-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicTacToeGame.java
52 lines (45 loc) · 1.33 KB
/
TicTacToeGame.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TicTacToeGame extends JFrame {
private JButton[][] buttons = new JButton[3][3];
private boolean isXTurn = true;
public TicTacToeGame() {
super("Tic-Tac-Toe Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(3, 3));
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
JButton button = new JButton();
button.addActionListener(new ButtonClickListener());
add(button);
buttons[i][j] = button;
}
}
pack();
setVisible(true);
}
private class ButtonClickListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
if (isXTurn) {
button.setText("X");
} else {
button.setText("O");
}
isXTurn = !isXTurn;
checkForWin();
}
}
private void checkForWin() {
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TicTacToeGame();
}
});
}
}