-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathTicTacToeGUI.java
More file actions
155 lines (128 loc) · 4.94 KB
/
TicTacToeGUI.java
File metadata and controls
155 lines (128 loc) · 4.94 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
/**
* 🎮 TicTacToeGUI.java
*
* A simple GUI-based Tic Tac Toe game in Java using Swing.
* Features:
* - Two-player mode (Player X and Player O)
* - Game board with 3x3 buttons
* - Detects Win, Draw, or ongoing game
* - Restart button to reset the game
*
* Complexity:
* - Time: O(1) per move
* - Space: O(1)
* New Feature:
* ✅ Added scoreboard tracking X wins, O wins, and Draws across rounds
*
* Author: Pradyumn Pratap Singh (Strange)
* Enhanced by: Sakshi (Hacktoberfest Contribution)
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TicTacToeGUI extends JFrame implements ActionListener {
private JButton[][] buttons = new JButton[3][3];
private boolean isXTurn = true;
private JLabel statusLabel, scoreLabel;
private JButton restartButton;
private int xWins = 0, oWins = 0, draws = 0;
public TicTacToeGUI() {
setTitle("❌⭕ Tic Tac Toe Game");
setSize(400, 520);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout(10, 10));
JLabel title = new JLabel("Tic Tac Toe", SwingConstants.CENTER);
title.setFont(new Font("Segoe UI", Font.BOLD, 22));
title.setForeground(new Color(30, 144, 255));
add(title, BorderLayout.NORTH);
JPanel boardPanel = new JPanel(new GridLayout(3, 3, 5, 5));
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("Segoe UI", Font.BOLD, 40));
buttons[i][j].setFocusPainted(false);
buttons[i][j].addActionListener(this);
boardPanel.add(buttons[i][j]);
}
}
add(boardPanel, BorderLayout.CENTER);
JPanel bottomPanel = new JPanel(new GridLayout(3, 1));
statusLabel = new JLabel("Player X's turn", SwingConstants.CENTER);
statusLabel.setFont(new Font("Segoe UI", Font.PLAIN, 16));
scoreLabel = new JLabel("Scoreboard → X: 0 | O: 0 | Draws: 0", SwingConstants.CENTER);
scoreLabel.setFont(new Font("Segoe UI", Font.ITALIC, 14));
scoreLabel.setForeground(new Color(80, 80, 80));
restartButton = new JButton("Restart Game");
restartButton.addActionListener(e -> resetGame());
bottomPanel.add(statusLabel);
bottomPanel.add(scoreLabel);
bottomPanel.add(restartButton);
add(bottomPanel, BorderLayout.SOUTH);
getContentPane().setBackground(new Color(245, 245, 245));
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
JButton clicked = (JButton) e.getSource();
if (!clicked.getText().equals("")) return;
clicked.setText(isXTurn ? "X" : "O");
if (checkWin()) {
if (isXTurn) xWins++; else oWins++;
statusLabel.setText("🎉 Player " + (isXTurn ? "X" : "O") + " wins!");
disableButtons();
} else if (isBoardFull()) {
draws++;
statusLabel.setText("🤝 It's a Draw!");
} else {
isXTurn = !isXTurn;
statusLabel.setText("Player " + (isXTurn ? "X" : "O") + "'s turn");
}
scoreLabel.setText("Scoreboard → X: " + xWins + " | O: " + oWins + " | Draws: " + draws);
}
private boolean checkWin() {
String player = isXTurn ? "X" : "O";
for (int i = 0; i < 3; i++) {
if (buttons[i][0].getText().equals(player) &&
buttons[i][1].getText().equals(player) &&
buttons[i][2].getText().equals(player))
return true;
if (buttons[0][i].getText().equals(player) &&
buttons[1][i].getText().equals(player) &&
buttons[2][i].getText().equals(player))
return true;
}
if (buttons[0][0].getText().equals(player) &&
buttons[1][1].getText().equals(player) &&
buttons[2][2].getText().equals(player))
return true;
if (buttons[0][2].getText().equals(player) &&
buttons[1][1].getText().equals(player) &&
buttons[2][0].getText().equals(player))
return true;
return false;
}
private boolean isBoardFull() {
for (JButton[] row : buttons)
for (JButton btn : row)
if (btn.getText().equals("")) return false;
return true;
}
private void disableButtons() {
for (JButton[] row : buttons)
for (JButton btn : row)
btn.setEnabled(false);
}
private void resetGame() {
for (JButton[] row : buttons)
for (JButton btn : row) {
btn.setText("");
btn.setEnabled(true);
}
isXTurn = true;
statusLabel.setText("Player X's turn");
}
public static void main(String[] args) {
SwingUtilities.invokeLater(TicTacToeGUI::new);
}
}