-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToeSwing.java
More file actions
139 lines (115 loc) · 4.08 KB
/
TicTacToeSwing.java
File metadata and controls
139 lines (115 loc) · 4.08 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
//Java Swing Tic-Tac-Toe (2-player) app with win/draw detection, winning-cell highlight, and a Reset button.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TicTacToeSwing {
private JFrame frame;
private JButton[] cells = new JButton[9];
private JLabel status;
private JButton resetBtn;
private char current = 'X';
private boolean gameOver = false;
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
// Optional: native look & feel
try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception ignored) {}
new TicTacToeSwing().createAndShow();
});
}
private void createAndShow() {
frame = new JFrame("Tic Tac Toe - Swing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout(10, 10));
// Board
JPanel board = new JPanel(new GridLayout(3, 3, 6, 6));
board.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
Font cellFont = new Font("Arial", Font.BOLD, 42);
for (int i = 0; i < 9; i++) {
JButton b = new JButton("");
b.setFont(cellFont);
b.setFocusPainted(false);
b.setBackground(Color.WHITE);
final int idx = i;
b.addActionListener(e -> handleMove(idx));
cells[i] = b;
board.add(b);
}
// Status + Reset
JPanel bottom = new JPanel(new BorderLayout(8, 8));
status = new JLabel("Turn: X", SwingConstants.LEFT);
status.setBorder(BorderFactory.createEmptyBorder(0,10,10,10));
resetBtn = new JButton("Reset");
resetBtn.addActionListener(e -> resetGame());
bottom.add(status, BorderLayout.CENTER);
bottom.add(resetBtn, BorderLayout.EAST);
frame.add(board, BorderLayout.CENTER);
frame.add(bottom, BorderLayout.SOUTH);
frame.setSize(360, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private void handleMove(int idx) {
if (gameOver) return;
JButton cell = cells[idx];
if (!cell.getText().isEmpty()) return; // already taken
cell.setText(String.valueOf(current));
int[] winLine = checkWin();
if (winLine != null) {
gameOver = true;
highlightWin(winLine);
status.setText("Winner: " + current + " 🎉");
disableBoard();
return;
}
if (isBoardFull()) {
gameOver = true;
status.setText("Draw 🤝");
return;
}
// Switch player
current = (current == 'X') ? 'O' : 'X';
status.setText("Turn: " + current);
}
private void highlightWin(int[] line) {
for (int i : line) {
cells[i].setBackground(new Color(180, 255, 180));
}
}
private void disableBoard() {
for (JButton b : cells) b.setEnabled(false);
}
private void enableBoard() {
for (JButton b : cells) b.setEnabled(true);
}
private void resetGame() {
for (JButton b : cells) {
b.setText("");
b.setBackground(Color.WHITE);
b.setEnabled(true);
}
current = 'X';
gameOver = false;
status.setText("Turn: X");
}
private boolean isBoardFull() {
for (JButton b : cells) {
if (b.getText().isEmpty()) return false;
}
return true;
}
// Returns winning triple indices if someone won, else null
private int[] checkWin() {
int[][] lines = {
{0,1,2}, {3,4,5}, {6,7,8}, // rows
{0,3,6}, {1,4,7}, {2,5,8}, // cols
{0,4,8}, {2,4,6} // diagonals
};
for (int[] line : lines) {
String a = cells[line[0]].getText();
String b = cells[line[1]].getText();
String c = cells[line[2]].getText();
if (!a.isEmpty() && a.equals(b) && b.equals(c)) return line;
}
return null;
}
}