Tic-Tac-Toe

 

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main extends JFrame implements ActionListener {
private static final int SIZE = 3;
private JButton[][] buttons = new JButton[SIZE][SIZE];
private boolean playerX = true;
private JLabel statusLabel;

public Main() {
setTitle("Tic Tac Toe Game");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());

JPanel titlePanel = new JPanel();
JLabel titleLabel = new JLabel("Tic Tac Toe Game");
titleLabel.setFont(new Font("Arial", Font.BOLD, 30));
titlePanel.add(titleLabel);
add(titlePanel, BorderLayout.NORTH);

JPanel panel = new JPanel();
panel.setLayout(new GridLayout(SIZE, SIZE));
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
buttons[i][j] = new JButton("");
buttons[i][j].setFont(new Font("Arial", Font.PLAIN, 60));
buttons[i][j].setFocusPainted(false);
buttons[i][j].addActionListener(this);
buttons[i][j].setBackground(Color.pink);
panel.add(buttons[i][j]);
}
}
add(panel, BorderLayout.CENTER);

statusLabel = new JLabel("Player X's turn");
statusLabel.setHorizontalAlignment(SwingConstants.CENTER);
statusLabel.setFont(new Font("Arial", Font.PLAIN, 20));
add(statusLabel, BorderLayout.SOUTH);
}


@Override
public void actionPerformed(ActionEvent e) {
JButton buttonClicked = (JButton) e.getSource();
if (buttonClicked.getText().equals("")) {
buttonClicked.setText(playerX ? "X" : "O");
buttonClicked.setEnabled(false);
playerX = !playerX;
statusLabel.setText("Player " + (playerX ? "X" : "O") + "'s turn");
if (checkForWin()) {
JOptionPane.showMessageDialog(this, "Player " + (playerX ? "O" : "X") + " wins!");
resetBoard();
} else if (isBoardFull()) {
JOptionPane.showMessageDialog(this, "The game is a tie!");
resetBoard();
}
}
}


private boolean checkForWin() {
for (int i = 0; i < SIZE; i++) {
if (buttons[i][0].getText().equals(buttons[i][1].getText()) &&
buttons[i][0].getText().equals(buttons[i][2].getText()) &&
!buttons[i][0].getText().equals("")) {
return true;
}
}


for (int j = 0; j < SIZE; j++) {
if (buttons[0][j].getText().equals(buttons[1][j].getText()) &&
buttons[0][j].getText().equals(buttons[2][j].getText()) &&
!buttons[0][j].getText().equals("")) {
return true;
}
}


if (buttons[0][0].getText().equals(buttons[1][1].getText()) &&
buttons[0][0].getText().equals(buttons[2][2].getText()) &&
!buttons[0][0].getText().equals("")) {
return true;
}


if (buttons[0][2].getText().equals(buttons[1][1].getText()) &&
buttons[0][2].getText().equals(buttons[2][0].getText()) &&
!buttons[0][2].getText().equals("")) {
return true;
}

return false;
}


private boolean isBoardFull() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (buttons[i][j].getText().equals("")) {
return false;
}
}
}

return true;
}


private void resetBoard() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
buttons[i][j].setText("");
buttons[i][j].setEnabled(true);
}
}
playerX = true;
statusLabel.setText("Player X's turn");
}


public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
Main frame = new Main();
frame.setVisible(true);
});

}
}

Comments

Popular posts from this blog

JAVA Lab Programs

HANGMAN GAME

WD 22. StylingLinks&Buttons