-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe.java
More file actions
133 lines (123 loc) · 5.26 KB
/
Copy pathTicTacToe.java
File metadata and controls
133 lines (123 loc) · 5.26 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
package src;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TicTacToe extends JFrame implements ActionListener {
private final JButton[][] buttons = new JButton[3][3];
private final JLabel statusLabel = new JLabel();
private char currentPlayer = 'X';
private boolean gameEnded = false;
public TicTacToe() {
setTitle("Tic-Tac-Toe");
setSize(400, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
JPanel statusPanel = new JPanel();
statusLabel.setFont(new Font("Arial", Font.BOLD, 20));
statusPanel.add(statusLabel);
add(statusPanel, BorderLayout.NORTH);
JPanel gridPanel = new JPanel();
gridPanel.setLayout(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, 100));
buttons[i][j].setFocusable(false);
buttons[i][j].addActionListener(this);
gridPanel.add(buttons[i][j]);
}
}
add(gridPanel, BorderLayout.CENTER);
JPanel controlPanel = new JPanel();
JButton resetButton = new JButton("New Game");
resetButton.addActionListener(e -> resetGame());
controlPanel.add(resetButton);
add(controlPanel, BorderLayout.SOUTH);
resetGame();
}
private void resetGame() {
currentPlayer = 'X';
gameEnded = false;
statusLabel.setText("Player " + currentPlayer + "'s turn");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
buttons[i][j].setText("");
buttons[i][j].setEnabled(true);
buttons[i][j].setBackground(null);
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (gameEnded) {
return;
}
JButton clickedButton = (JButton) e.getSource();
if (!clickedButton.getText().equals("")) {
return;
}
clickedButton.setText(String.valueOf(currentPlayer));
clickedButton.setForeground(currentPlayer == 'X' ? Color.BLUE : Color.RED);
if (checkForWinner()) {
statusLabel.setText("Player " + currentPlayer + " wins! 🎉");
gameEnded = true;
for (int i = 0; i < 3; i++) {
for(int j=0; j<3; j++) {
buttons[i][j].setEnabled(false);
}
}
} else if (isBoardFull()) {
statusLabel.setText("It's a draw! 🤝");
gameEnded = true;
} else {
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
statusLabel.setText("Player " + currentPlayer + "'s turn");
}
}
private boolean isBoardFull() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (buttons[i][j].getText().equals("")) {
return false;
}
}
}
return true;
}
private boolean checkForWinner() {
String p = String.valueOf(currentPlayer);
for (int i = 0; i < 3; i++) {
if (buttons[i][0].getText().equals(p) && buttons[i][1].getText().equals(p) && buttons[i][2].getText().equals(p)) {
highlightWinner(buttons[i][0], buttons[i][1], buttons[i][2]);
return true;
}
}
for (int j = 0; j < 3; j++) {
if (buttons[0][j].getText().equals(p) && buttons[1][j].getText().equals(p) && buttons[2][j].getText().equals(p)) {
highlightWinner(buttons[0][j], buttons[1][j], buttons[2][j]);
return true;
}
}
if (buttons[0][0].getText().equals(p) && buttons[1][1].getText().equals(p) && buttons[2][2].getText().equals(p)) {
highlightWinner(buttons[0][0], buttons[1][1], buttons[2][2]);
return true;
}
if (buttons[0][2].getText().equals(p) && buttons[1][1].getText().equals(p) && buttons[2][0].getText().equals(p)) {
highlightWinner(buttons[0][2], buttons[1][1], buttons[2][0]);
return true;
}
return false;
}
private void highlightWinner(JButton b1, JButton b2, JButton b3) {
b1.setBackground(Color.GREEN);
b2.setBackground(Color.GREEN);
b3.setBackground(Color.GREEN);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
TicTacToe game = new TicTacToe();
game.setVisible(true);
});
}
}