Skip to content

TicTacToe GUI Project issue #11 #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 144 additions & 0 deletions src/TicTacToeGUI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TicTacToeGUI extends JFrame {
private JButton[][] buttons;
private char currentPlayer = 'X';
private JLabel statusLabel;

public TicTacToeGUI() {
setTitle("Tic-Tac-Toe");
setSize(300, 350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);

JPanel panel = new JPanel(new GridLayout(3, 3));
buttons = new JButton[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.PLAIN, 48));
buttons[i][j].setFocusPainted(false);
buttons[i][j].addActionListener(new ButtonClickListener(i, j));
panel.add(buttons[i][j]);
}
}

statusLabel = new JLabel("Player " + currentPlayer + "'s turn", JLabel.CENTER);
statusLabel.setFont(new Font("Arial", Font.PLAIN, 16));

JPanel statusPanel = new JPanel(new BorderLayout());
statusPanel.add(statusLabel, BorderLayout.CENTER);

add(panel, BorderLayout.CENTER);
add(statusPanel, BorderLayout.SOUTH);

setVisible(true);
}

private class ButtonClickListener implements ActionListener {
private int row, col;

ButtonClickListener(int row, int col) {
this.row = row;
this.col = col;
}

@Override
public void actionPerformed(ActionEvent e) {
if (buttons[row][col].getText().equals("")) {
buttons[row][col].setText(String.valueOf(currentPlayer));
buttons[row][col].setEnabled(false);

if (checkWin(row, col)) {
JOptionPane.showMessageDialog(null, "Player " + currentPlayer + " wins!");
resetBoard();
} else if (isBoardFull()) {
JOptionPane.showMessageDialog(null, "It's a draw!");
resetBoard();
} else {
currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
statusLabel.setText("Player " + currentPlayer + "'s turn");
}
}
}

private boolean checkWin(int r, int c) {
String symbol = String.valueOf(currentPlayer);

// Check row
for (int i = 0; i < 3; i++) {
if (!buttons[r][i].getText().equals(symbol)) {
break;
}
if (i == 2) {
return true;
}
}

// Check column
for (int i = 0; i < 3; i++) {
if (!buttons[i][c].getText().equals(symbol)) {
break;
}
if (i == 2) {
return true;
}
}

// Check diagonals
if (r == c) {
for (int i = 0; i < 3; i++) {
if (!buttons[i][i].getText().equals(symbol)) {
break;
}
if (i == 2) {
return true;
}
}
}

if (r + c == 2) {
for (int i = 0; i < 3; i++) {
if (!buttons[i][2 - i].getText().equals(symbol)) {
break;
}
if (i == 2) {
return true;
}
}
}

return false;
}

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 void resetBoard() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
buttons[i][j].setText("");
buttons[i][j].setEnabled(true);
}
}
currentPlayer = 'X';
statusLabel.setText("Player " + currentPlayer + "'s turn");
}
}
//Main method to run the file
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new TicTacToeGUI());
}
}