-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToe.java
277 lines (243 loc) · 8.71 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
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
272
273
274
275
276
277
/*
* Author - Mohammed Shahid
* Project - TIc-Tac-Toe
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
public class TicTacToe extends JFrame implements ActionListener {
private JButton[] buttons = new JButton[9];
private int[] board = new int[9];
private boolean xTurn = true;
private int gameMode; // 1 for Single-Player, 2 for Multi-Player
private boolean gameWon = false; // Flag to track if the game has been won
public TicTacToe() {
setTitle("Tic Tac Toe");
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(3, 3));
getContentPane().setBackground(new Color(0, 255, 0, 100));
playIntroSound();
// Ask for game mode choice
String[] options = {"Single-Player", "Multi-Player"};
int choice = JOptionPane.showOptionDialog(this, "Choose game mode", "Game Mode",
JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
if (choice == 0) {
gameMode = 1; // Single-Player
} else {
gameMode = 2; // Multi-Player
}
// Initialize buttons
for (int i = 0; i < 9; i++) {
buttons[i] = new JButton();
buttons[i].setFont(new Font("Arial", Font.PLAIN, 40));
buttons[i].setBackground(Color.CYAN);
buttons[i].setOpaque(true);
buttons[i].addActionListener(this);
buttons[i].setCursor(new Cursor(Cursor.HAND_CURSOR)); // Change cursor to hand on hover
add(buttons[i]);
}
setVisible(true);
}
private void playIntroSound() {
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("intro.wav").getAbsoluteFile());
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
} catch (Exception ex) {
System.out.println("Error playing intro sound: " + ex.getMessage());
}
}
public void actionPerformed(ActionEvent e) {
JButton clickedButton = (JButton) e.getSource();
int pos = -1;
for (int i = 0; i < 9; i++) {
if (buttons[i] == clickedButton) {
pos = i;
break;
}
}
if (gameWon || board[pos] != 0) { // Check if the game is won or the position is already occupied
JOptionPane.showMessageDialog(this, "Wrong move!!");
return;
}
if (xTurn) {
clickedButton.setText("X");
board[pos] = -1;
} else {
clickedButton.setText("O");
board[pos] = 1;
}
xTurn = !xTurn;
playClickSound(); // Play sound after each click
checkForWin();
if (gameMode == 1 && !xTurn && !gameWon) {
// If single-player mode and it's computer's turn and game is not won
computerTurn();
}
// Check for a tie after X's move
if (isBoardFull(board)) {
JOptionPane.showMessageDialog(this, "It's a tie!");
resetGame();
}
}
private void computerTurn() {
int[] move = minimax(board, 1, Integer.MIN_VALUE, Integer.MAX_VALUE, true);
buttons[move[1]].setText("O");
board[move[1]] = 1;
xTurn = true;
checkForWin(); // Check for win after computer's move
}
private int[] minimax(int[] board, int depth, int alpha, int beta, boolean maximizingPlayer) {
int result = checkForWinner(board);
if (result != 0) {
return new int[]{result * depth, -1};
}
if (isBoardFull(board)) {
return new int[]{0, -1};
}
if (maximizingPlayer) {
int[] bestMove = {-1, -1};
int maxEval = Integer.MIN_VALUE;
for (int i = 0; i < 9; i++) {
if (board[i] == 0) {
board[i] = 1;
int[] eval = minimax(board, depth + 1, alpha, beta, false);
board[i] = 0;
if (eval[0] > maxEval) {
maxEval = eval[0];
bestMove[0] = maxEval;
bestMove[1] = i;
}
alpha = Math.max(alpha, maxEval);
if (beta <= alpha) {
break;
}
}
}
return bestMove;
} else {
int[] bestMove = {-1, -1};
int minEval = Integer.MAX_VALUE;
for (int i = 0; i < 9; i++) {
if (board[i] == 0) {
board[i] = -1;
int[] eval = minimax(board, depth + 1, alpha, beta, true);
board[i] = 0;
if (eval[0] < minEval) {
minEval = eval[0];
bestMove[0] = minEval;
bestMove[1] = i;
}
beta = Math.min(beta, minEval);
if (beta <= alpha) {
break;
}
}
}
return bestMove;
}
}
private int checkForWinner(int[] board) {
int[][] winConditions = {
{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, // Rows
{0, 3, 6}, {1, 4, 7}, {2, 5, 8}, // Columns
{0, 4, 8}, {2, 4, 6} // Diagonals
};
for (int[] condition : winConditions) {
int a = condition[0];
int b = condition[1];
int c = condition[2];
if (board[a] != 0 && board[a] == board[b] && board[a] == board[c]) {
return board[a];
}
}
return 0;
}
private boolean isBoardFull(int[] board) {
for (int i = 0; i < 9; i++) {
if (board[i] == 0) {
return false;
}
}
return true;
}
private void checkForWin() {
int winner = checkForWinner(board);
if (winner != 0) {
if (winner == -1)
{
playerXsound();
JOptionPane.showMessageDialog(this, "Player X wins");
}
else {
playerOsound();
JOptionPane.showMessageDialog(this, "Player O wins");
}
resetGame(); // Reset the game after either player wins
return;
}
if (isBoardFull(board)) {
playTieSound();
JOptionPane.showMessageDialog(this, "It's a tie!");
resetGame();
return;
}
}
private void playerXsound() {
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("PlayerX.wav").getAbsoluteFile());
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
} catch (Exception ex) {
System.out.println("Error playing sound: " + ex.getMessage());
}
}
private void playerOsound() {
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("PlayerO.wav").getAbsoluteFile());
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
} catch (Exception ex) {
System.out.println("Error playing sound: " + ex.getMessage());
}
}
private void playTieSound() {
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("Tie.wav").getAbsoluteFile());
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
} catch (Exception ex) {
System.out.println("Error playing sound: " + ex.getMessage());
}
}
private void resetGame() {
for (int i = 0; i < 9; i++) {
buttons[i].setText("");
board[i] = 0;
}
xTurn = true;
gameWon = false; // Reset the gameWon flag
}
private void playClickSound() {
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("mixkit-arcade-game-jump-coin-216.wav").getAbsoluteFile());
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
} catch (Exception ex) {
System.out.println("Error playing sound: " + ex.getMessage());
}
}
public static void main(String[] args) {
new TicTacToe();
}
}