-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe.java
More file actions
271 lines (232 loc) · 8.41 KB
/
TicTacToe.java
File metadata and controls
271 lines (232 loc) · 8.41 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class TicTacToe extends JFrame {
private JButton[][] buttons = new JButton[3][3];
private char[][] board = new char[3][3];
private char currentPlayer = 'X';
private String playerName = "Player";
private int playerWins = 0;
private int computerWins = 0;
private int draws = 0;
private JLabel statsLabel;
private JButton resetButton;
public TicTacToe() {
setTitle("Tic Tac Toe - Player Profile");
setExtendedState(JFrame.MAXIMIZED_BOTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// Get player name
playerName = JOptionPane.showInputDialog(this, "Enter your name:");
if (playerName == null || playerName.trim().isEmpty()) {
playerName = "Player";
}
setTitle("Tic Tac Toe - " + playerName);
initializeBoard();
JPanel boardPanel = new JPanel(new GridLayout(3, 3));
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.BOLD, 60));
buttons[i][j].setFocusPainted(false);
final int row = i;
final int col = j;
buttons[i][j].addActionListener(e -> buttonClicked(row, col));
boardPanel.add(buttons[i][j]);
}
}
JPanel controlPanel = new JPanel(new FlowLayout());
resetButton = new JButton("Reset Game");
resetButton.addActionListener(e -> resetGame());
controlPanel.add(resetButton);
statsLabel = new JLabel(playerName + ": 0 | Computer: 0 | Draws: 0");
controlPanel.add(statsLabel);
add(boardPanel, BorderLayout.CENTER);
add(controlPanel, BorderLayout.SOUTH);
setVisible(true);
}
private void initializeBoard() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = ' ';
}
}
}
private void buttonClicked(int row, int col) {
if (board[row][col] == ' ' && currentPlayer == 'X') {
board[row][col] = 'X';
buttons[row][col].setText("X");
buttons[row][col].setEnabled(false);
if (checkWin('X')) {
playerWins++;
updateStats();
JOptionPane.showMessageDialog(this, "Player wins!");
disableAllButtons();
return;
} else if (isBoardFull()) {
draws++;
updateStats();
JOptionPane.showMessageDialog(this, "It's a draw!");
return;
}
currentPlayer = 'O';
computerMove();
}
}
private void computerMove() {
// Simple AI: first try to win, then block, then random move
int[] move = findWinningMove('O'); // Try to win
if (move == null) {
move = findWinningMove('X'); // Block player
}
if (move == null) {
move = getRandomMove(); // Random move
}
if (move != null) {
int row = move[0];
int col = move[1];
board[row][col] = 'O';
buttons[row][col].setText("O");
buttons[row][col].setEnabled(false);
if (checkWin('O')) {
computerWins++;
updateStats();
JOptionPane.showMessageDialog(this, "Computer wins!");
disableAllButtons();
} else if (isBoardFull()) {
draws++;
updateStats();
JOptionPane.showMessageDialog(this, "It's a draw!");
}
currentPlayer = 'X';
}
}
private int[] findWinningMove(char player) {
// Check rows
for (int i = 0; i < 3; i++) {
if (board[i][0] == player && board[i][1] == player && board[i][2] == ' ') {
return new int[]{i, 2};
}
if (board[i][0] == player && board[i][2] == player && board[i][1] == ' ') {
return new int[]{i, 1};
}
if (board[i][1] == player && board[i][2] == player && board[i][0] == ' ') {
return new int[]{i, 0};
}
}
// Check columns
for (int j = 0; j < 3; j++) {
if (board[0][j] == player && board[1][j] == player && board[2][j] == ' ') {
return new int[]{2, j};
}
if (board[0][j] == player && board[2][j] == player && board[1][j] == ' ') {
return new int[]{1, j};
}
if (board[1][j] == player && board[2][j] == player && board[0][j] == ' ') {
return new int[]{0, j};
}
}
// Check diagonals
if (board[0][0] == player && board[1][1] == player && board[2][2] == ' ') {
return new int[]{2, 2};
}
if (board[0][0] == player && board[2][2] == player && board[1][1] == ' ') {
return new int[]{1, 1};
}
if (board[1][1] == player && board[2][2] == player && board[0][0] == ' ') {
return new int[]{0, 0};
}
if (board[0][2] == player && board[1][1] == player && board[2][0] == ' ') {
return new int[]{2, 0};
}
if (board[0][2] == player && board[2][0] == player && board[1][1] == ' ') {
return new int[]{1, 1};
}
if (board[1][1] == player && board[2][0] == player && board[0][2] == ' ') {
return new int[]{0, 2};
}
return null;
}
private int[] getRandomMove() {
Random random = new Random();
int emptyCells = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == ' ') {
emptyCells++;
}
}
}
if (emptyCells == 0) return null;
int randomIndex = random.nextInt(emptyCells);
int count = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[i][j] == ' ') {
if (count == randomIndex) {
return new int[]{i, j};
}
count++;
}
}
}
return null;
}
private boolean checkWin(char player) {
// Check rows
for (int i = 0; i < 3; i++) {
if (board[i][0] == player && board[i][1] == player && board[i][2] == player) {
return true;
}
}
// Check columns
for (int j = 0; j < 3; j++) {
if (board[0][j] == player && board[1][j] == player && board[2][j] == player) {
return true;
}
}
// Check diagonals
if (board[0][0] == player && board[1][1] == player && board[2][2] == player) {
return true;
}
if (board[0][2] == player && board[1][1] == player && board[2][0] == player) {
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] == ' ') {
return false;
}
}
}
return true;
}
private void disableAllButtons() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
buttons[i][j].setEnabled(false);
}
}
}
private void resetGame() {
initializeBoard();
currentPlayer = 'X';
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
buttons[i][j].setText("");
buttons[i][j].setEnabled(true);
}
}
}
private void updateStats() {
statsLabel.setText(String.format("%s: %d | Computer: %d | Draws: %d",
playerName, playerWins, computerWins, draws));
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new TicTacToe());
}
}