Skip to content

Commit 927dec8

Browse files
authored
Merge pull request #82 from SakshiDosani/tictactoe-scoreboard
Added scoreboard feature to TicTacToe game (Hacktoberfest contribution)
2 parents e1571cc + 46e69eb commit 927dec8

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

Java/TicTacToe/TicTacToeGUI.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* 🎮 TicTacToeGUI.java
33
*
44
* A simple GUI-based Tic Tac Toe game in Java using Swing.
5-
*
65
* Features:
76
* - Two-player mode (Player X and Player O)
87
* - Game board with 3x3 buttons
@@ -12,9 +11,11 @@
1211
* Complexity:
1312
* - Time: O(1) per move
1413
* - Space: O(1)
14+
* New Feature:
15+
* ✅ Added scoreboard tracking X wins, O wins, and Draws across rounds
1516
*
1617
* Author: Pradyumn Pratap Singh (Strange)
17-
* For: Hacktoberfest / Mini Games in Java
18+
* Enhanced by: Sakshi (Hacktoberfest Contribution)
1819
*/
1920

2021
import javax.swing.*;
@@ -25,23 +26,23 @@ public class TicTacToeGUI extends JFrame implements ActionListener {
2526

2627
private JButton[][] buttons = new JButton[3][3];
2728
private boolean isXTurn = true;
28-
private JLabel statusLabel;
29+
private JLabel statusLabel, scoreLabel;
2930
private JButton restartButton;
3031

32+
private int xWins = 0, oWins = 0, draws = 0;
33+
3134
public TicTacToeGUI() {
3235
setTitle("❌⭕ Tic Tac Toe Game");
33-
setSize(400, 500);
36+
setSize(400, 520);
3437
setDefaultCloseOperation(EXIT_ON_CLOSE);
3538
setLocationRelativeTo(null);
3639
setLayout(new BorderLayout(10, 10));
3740

38-
// --- Title ---
3941
JLabel title = new JLabel("Tic Tac Toe", SwingConstants.CENTER);
4042
title.setFont(new Font("Segoe UI", Font.BOLD, 22));
4143
title.setForeground(new Color(30, 144, 255));
4244
add(title, BorderLayout.NORTH);
4345

44-
// --- Game Board ---
4546
JPanel boardPanel = new JPanel(new GridLayout(3, 3, 5, 5));
4647
for (int i = 0; i < 3; i++) {
4748
for (int j = 0; j < 3; j++) {
@@ -54,13 +55,19 @@ public TicTacToeGUI() {
5455
}
5556
add(boardPanel, BorderLayout.CENTER);
5657

57-
// --- Bottom Panel (Status + Restart) ---
58-
JPanel bottomPanel = new JPanel(new GridLayout(2,1));
58+
JPanel bottomPanel = new JPanel(new GridLayout(3, 1));
5959
statusLabel = new JLabel("Player X's turn", SwingConstants.CENTER);
6060
statusLabel.setFont(new Font("Segoe UI", Font.PLAIN, 16));
61+
62+
scoreLabel = new JLabel("Scoreboard → X: 0 | O: 0 | Draws: 0", SwingConstants.CENTER);
63+
scoreLabel.setFont(new Font("Segoe UI", Font.ITALIC, 14));
64+
scoreLabel.setForeground(new Color(80, 80, 80));
65+
6166
restartButton = new JButton("Restart Game");
6267
restartButton.addActionListener(e -> resetGame());
68+
6369
bottomPanel.add(statusLabel);
70+
bottomPanel.add(scoreLabel);
6471
bottomPanel.add(restartButton);
6572

6673
add(bottomPanel, BorderLayout.SOUTH);
@@ -72,25 +79,28 @@ public TicTacToeGUI() {
7279
@Override
7380
public void actionPerformed(ActionEvent e) {
7481
JButton clicked = (JButton) e.getSource();
75-
if (!clicked.getText().equals("")) return; // Ignore if already clicked
82+
if (!clicked.getText().equals("")) return;
7683

7784
clicked.setText(isXTurn ? "X" : "O");
7885

7986
if (checkWin()) {
87+
if (isXTurn) xWins++; else oWins++;
8088
statusLabel.setText("🎉 Player " + (isXTurn ? "X" : "O") + " wins!");
8189
disableButtons();
8290
} else if (isBoardFull()) {
91+
draws++;
8392
statusLabel.setText("🤝 It's a Draw!");
8493
} else {
8594
isXTurn = !isXTurn;
8695
statusLabel.setText("Player " + (isXTurn ? "X" : "O") + "'s turn");
8796
}
97+
98+
scoreLabel.setText("Scoreboard → X: " + xWins + " | O: " + oWins + " | Draws: " + draws);
8899
}
89100

90101
private boolean checkWin() {
91102
String player = isXTurn ? "X" : "O";
92103

93-
// Check rows and columns
94104
for (int i = 0; i < 3; i++) {
95105
if (buttons[i][0].getText().equals(player) &&
96106
buttons[i][1].getText().equals(player) &&
@@ -103,7 +113,6 @@ private boolean checkWin() {
103113
return true;
104114
}
105115

106-
// Check diagonals
107116
if (buttons[0][0].getText().equals(player) &&
108117
buttons[1][1].getText().equals(player) &&
109118
buttons[2][2].getText().equals(player))
@@ -118,11 +127,9 @@ private boolean checkWin() {
118127
}
119128

120129
private boolean isBoardFull() {
121-
for (JButton[] row : buttons) {
122-
for (JButton btn : row) {
130+
for (JButton[] row : buttons)
131+
for (JButton btn : row)
123132
if (btn.getText().equals("")) return false;
124-
}
125-
}
126133
return true;
127134
}
128135

0 commit comments

Comments
 (0)