-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphicalTicTacToe.java
126 lines (115 loc) · 4.17 KB
/
GraphicalTicTacToe.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GraphicalTicTacToe extends JFrame {
private static final char EMPTY = ' ';
private static final char PLAYER_X = 'X';
private static final char PLAYER_O = 'O';
private char[][] board;
private char currentPlayer;
private JButton[][] buttons;
public GraphicalTicTacToe() {
board = new char[3][3];
buttons = new JButton[3][3];
currentPlayer = PLAYER_X;
initializeBoard();
setTitle("Tic Tac Toe");
setSize(300, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new GridLayout(3, 3));
initializeButtons();
}
private void initializeBoard() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = EMPTY;
}
}
}
private void initializeButtons() {
ActionListener buttonListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
int row = (int) button.getClientProperty("row");
int col = (int) button.getClientProperty("col");
if (makeMove(row, col)) {
button.setText(String.valueOf(currentPlayer));
if (checkWin()) {
JOptionPane.showMessageDialog(null, "Player " + currentPlayer + " wins!");
resetBoard();
} else if (isBoardFull()) {
JOptionPane.showMessageDialog(null, "The game is a draw!");
resetBoard();
} else {
switchPlayer();
}
} else {
JOptionPane.showMessageDialog(null, "This move is not valid");
}
}
};
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
buttons[i][j] = new JButton();
buttons[i][j].setFont(new Font("Arial", Font.PLAIN, 60));
buttons[i][j].setFocusPainted(false);
buttons[i][j].putClientProperty("row", i);
buttons[i][j].putClientProperty("col", j);
buttons[i][j].addActionListener(buttonListener);
add(buttons[i][j]);
}
}
}
private boolean makeMove(int row, int col) {
if (row >= 0 && row < 3 && col >= 0 && col < 3 && board[row][col] == EMPTY) {
board[row][col] = currentPlayer;
return true;
}
return false;
}
private boolean checkWin() {
// Check rows and columns
for (int i = 0; i < 3; i++) {
if ((board[i][0] == currentPlayer && board[i][1] == currentPlayer && board[i][2] == currentPlayer) ||
(board[0][i] == currentPlayer && board[1][i] == currentPlayer && board[2][i] == currentPlayer)) {
return true;
}
}
// Check diagonals
if ((board[0][0] == currentPlayer && board[1][1] == currentPlayer && board[2][2] == currentPlayer) ||
(board[0][2] == currentPlayer && board[1][1] == currentPlayer && board[2][0] == currentPlayer)) {
return true;
}
return false;
}
private boolean isBoardFull() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == EMPTY) {
return false;
}
}
}
return true;
}
private void switchPlayer() {
currentPlayer = (currentPlayer == PLAYER_X) ? PLAYER_O : PLAYER_X;
}
private void resetBoard() {
initializeBoard();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
buttons[i][j].setText("");
}
}
currentPlayer = PLAYER_X;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
GraphicalTicTacToe game = new GraphicalTicTacToe();
game.setVisible(true);
});
}
}