|
| 1 | +import javax.swing.*; |
| 2 | +import java.awt.*; |
| 3 | +import java.awt.event.ActionEvent; |
| 4 | +import java.awt.event.ActionListener; |
| 5 | + |
| 6 | +public class TicTacToeGUI { |
| 7 | + private JFrame frame; |
| 8 | + private JButton[] buttons = new JButton[9]; |
| 9 | + private char currentPlayer = 'X'; |
| 10 | + |
| 11 | + public static void main(String[] args) { |
| 12 | + SwingUtilities.invokeLater(TicTacToeGUI::new); |
| 13 | + } |
| 14 | + |
| 15 | + public TicTacToeGUI() { |
| 16 | + frame = new JFrame("Tic-Tac-Toe"); |
| 17 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 18 | + frame.setSize(400, 400); |
| 19 | + frame.setLayout(new GridLayout(3, 3)); |
| 20 | + |
| 21 | + initializeButtons(); |
| 22 | + |
| 23 | + frame.setVisible(true); |
| 24 | + } |
| 25 | + |
| 26 | + private void initializeButtons() { |
| 27 | + for (int i = 0; i < 9; i++) { |
| 28 | + buttons[i] = new JButton(""); |
| 29 | + buttons[i].setFont(new Font("Arial", Font.BOLD, 60)); |
| 30 | + buttons[i].setFocusPainted(false); |
| 31 | + buttons[i].addActionListener(new ButtonClickListener(i)); |
| 32 | + frame.add(buttons[i]); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + private void resetGame() { |
| 37 | + for (JButton button : buttons) { |
| 38 | + button.setText(""); |
| 39 | + button.setEnabled(true); |
| 40 | + } |
| 41 | + currentPlayer = 'X'; |
| 42 | + } |
| 43 | + |
| 44 | + private boolean checkWinner() { |
| 45 | + int[][] winPatterns = { |
| 46 | + {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, // Rows |
| 47 | + {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, // Columns |
| 48 | + {0, 4, 8}, {2, 4, 6} // Diagonals |
| 49 | + }; |
| 50 | + |
| 51 | + for (int[] pattern : winPatterns) { |
| 52 | + if (!buttons[pattern[0]].getText().isEmpty() && |
| 53 | + buttons[pattern[0]].getText().equals(buttons[pattern[1]].getText()) && |
| 54 | + buttons[pattern[1]].getText().equals(buttons[pattern[2]].getText())) { |
| 55 | + return true; |
| 56 | + } |
| 57 | + } |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + private boolean isBoardFull() { |
| 62 | + for (JButton button : buttons) { |
| 63 | + if (button.getText().isEmpty()) return false; |
| 64 | + } |
| 65 | + return true; |
| 66 | + } |
| 67 | + |
| 68 | + private class ButtonClickListener implements ActionListener { |
| 69 | + private final int index; |
| 70 | + |
| 71 | + public ButtonClickListener(int index) { |
| 72 | + this.index = index; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void actionPerformed(ActionEvent e) { |
| 77 | + buttons[index].setText(String.valueOf(currentPlayer)); |
| 78 | + buttons[index].setEnabled(false); |
| 79 | + |
| 80 | + if (checkWinner()) { |
| 81 | + JOptionPane.showMessageDialog(frame, "Player " + currentPlayer + " wins!"); |
| 82 | + resetGame(); |
| 83 | + } else if (isBoardFull()) { |
| 84 | + JOptionPane.showMessageDialog(frame, "It's a draw!"); |
| 85 | + resetGame(); |
| 86 | + } else { |
| 87 | + currentPlayer = (currentPlayer == 'X') ? 'O' : 'X'; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments