0% found this document useful (0 votes)
18 views2 pages

Tic Tac Toe Code

Uploaded by

malikhibba8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views2 pages

Tic Tac Toe Code

Uploaded by

malikhibba8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import javax.swing.

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

public class TicTacToeGUI extends JFrame implements ActionListener {


private JButton[][] buttons;
private char currentPlayer;

public TicTacToeGUI() {
super("Tic Tac Toe");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buttons = new JButton[3][3];
currentPlayer = 'X';
initializeBoard();
initializeGUI();
}

private void initializeBoard() {


for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
buttons[row][col] = createButton();
add(buttons[row][col]);
}
}
}

private JButton createButton() {


JButton button = new JButton();
button.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 80));
button.addActionListener(this);
return button;
}

private void initializeGUI() {


setLayout(new GridLayout(3, 3));
setSize(300, 300);
setVisible(true);
}

private void resetBoard() {


currentPlayer = 'X';
for (JButton[] buttonRow : buttons) {
for (JButton button : buttonRow) {
button.setText("");
}
}
}

private void checkWin() {


String winner = "";

for (int i = 0; i < 3; i++) {


if (checkLine(buttons[i][0].getText(), buttons[i][1].getText(),
buttons[i][2].getText()) ||
checkLine(buttons[0][i].getText(), buttons[1][i].getText(),
buttons[2][i].getText())) {
winner = buttons[i][0].getText();
break;
}
}

if (checkLine(buttons[0][0].getText(), buttons[1][1].getText(), buttons[2]


[2].getText()) ||
checkLine(buttons[0][2].getText(), buttons[1][1].getText(),
buttons[2][0].getText())) {
winner = buttons[0][0].getText();
}

if (!winner.isEmpty()) {
JOptionPane.showMessageDialog(this, "Player " + winner + " wins!");
resetBoard();
return;
}

boolean fullBoard = true;


for (JButton[] buttonRow : buttons) {
for (JButton button : buttonRow) {
if (button.getText().isEmpty()) {
fullBoard = false;
break;
}
}
}

if (fullBoard) {
JOptionPane.showMessageDialog(this, "It's a draw!");
resetBoard();
}
}

private boolean checkLine(String a, String b, String c) {


return !a.isEmpty() && a.equals(b) && a.equals(c);
}

private void switchPlayer() {


currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';
}

public void actionPerformed(ActionEvent e) {


JButton button = (JButton) e.getSource();
if (button.getText().isEmpty()) {
button.setText(Character.toString(currentPlayer));
checkWin();
switchPlayer();
}
}

public static void main(String[] args) {


SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TicTacToeGUI();
}
});
}
}

You might also like