-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTicTacToeGameVSAI
More file actions
157 lines (137 loc) · 5.47 KB
/
TicTacToeGameVSAI
File metadata and controls
157 lines (137 loc) · 5.47 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class TicTacToeGame extends JFrame implements ActionListener {
private static final int SIZE = 3;
JButton[][] buttons;
private char currentPlayer;
private boolean isGameWon;
private JLabel statusLabel;
public Random random;
public TicTacToeGame() {
buttons = new JButton[SIZE][SIZE];
currentPlayer = 'X';
isGameWon = false;
random = new Random();
initializeUI();
}
private void computerMove() {
if (!isGameWon && currentPlayer == 'O') {
int row, col;
do {
row = random.nextInt(SIZE);
col = random.nextInt(SIZE);
} while (!buttons[row][col].getText().isEmpty());
buttons[row][col].setText(String.valueOf(currentPlayer));
buttons[row][col].setEnabled(false); // Disable the button
if (checkWin()) {
statusLabel.setText("Player " + currentPlayer + " wins!");
isGameWon = true;
} else if (isBoardFull()) {
statusLabel.setText("It's a draw!");
isGameWon = true;
} else {
currentPlayer = 'X';
statusLabel.setText("Player " + currentPlayer + "'s turn");
}
}
}
private void initializeUI() {
setTitle("Tic-Tac-Toe");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(350, 350);
setLayout(new BorderLayout());
JPanel boardPanel = new JPanel(new GridLayout(SIZE, SIZE));
statusLabel = new JLabel("Player " + currentPlayer + "'s turn", SwingConstants.CENTER);
for (int row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++) {
buttons[row][col] = new JButton();
buttons[row][col].setFont(new Font("Arial", Font.PLAIN, 60));
buttons[row][col].addActionListener(this);
boardPanel.add(buttons[row][col]);
}
}
JButton restartButton = new JButton("Restart");
restartButton.addActionListener(e -> restartGame());
JPanel controlPanel = new JPanel();
controlPanel.add(restartButton);
add(boardPanel, BorderLayout.CENTER);
add(statusLabel, BorderLayout.NORTH);
add(controlPanel, BorderLayout.SOUTH);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (isGameWon) {
return; // If the game is already won, ignore further moves
}
JButton clickedButton = (JButton) e.getSource();
if (!clickedButton.getText().isEmpty()) {
return; // If the button is already marked, ignore the click
}
if (currentPlayer == 'X') {
// Player's turn
clickedButton.setText("X");
clickedButton.setEnabled(false);
if (checkWin()) {
statusLabel.setText("Player " + currentPlayer + " wins!");
isGameWon = true;
} else if (isBoardFull()) {
statusLabel.setText("It's a draw!");
isGameWon = true;
} else {
currentPlayer = 'O';
statusLabel.setText("Player " + currentPlayer + "'s turn");
// Now it's the player's turn; no need to call computerMove
}
}
if (currentPlayer == 'O') {
computerMove();
}
}
boolean checkWin() {
// Check rows, columns, and diagonals for a win
for (int i = 0; i < SIZE; i++) {
if (buttons[i][0].getText().equals(String.valueOf(currentPlayer)) &&
buttons[i][1].getText().equals(String.valueOf(currentPlayer)) &&
buttons[i][2].getText().equals(String.valueOf(currentPlayer)) ||
buttons[0][i].getText().equals(String.valueOf(currentPlayer)) &&
buttons[1][i].getText().equals(String.valueOf(currentPlayer)) &&
buttons[2][i].getText().equals(String.valueOf(currentPlayer))) {
return true;
}
}
return buttons[0][0].getText().equals(String.valueOf(currentPlayer)) &&
buttons[1][1].getText().equals(String.valueOf(currentPlayer)) &&
buttons[2][2].getText().equals(String.valueOf(currentPlayer)) ||
buttons[0][2].getText().equals(String.valueOf(currentPlayer)) &&
buttons[1][1].getText().equals(String.valueOf(currentPlayer)) &&
buttons[2][0].getText().equals(String.valueOf(currentPlayer));
}
boolean isBoardFull() {
for (int row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++) {
if (buttons[row][col].getText().isEmpty()) {
return false;
}
}
}
return true;
}
void restartGame() {
for (int row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++) {
buttons[row][col].setText("");
buttons[row][col].setEnabled(true);
}
}
currentPlayer = 'X';
isGameWon = false;
statusLabel.setText("Player " + currentPlayer + "'s turn");
}
public static void main(String[] args) {
SwingUtilities.invokeLater(TicTacToeGame::new);
}
}