/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tictac;


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;

public class TicTacToeEn extends JFrame implements ActionListener {
   

    private JFrame window = new JFrame("Tic-Tac-Toe");
    //panels and labels and Text fields
    JPanel infoPanel = new JPanel(new GridLayout(2, 0));
    JPanel playersPanel = new JPanel(new GridLayout(3, 2, 0, 8));
    JLabel player1lbl = new JLabel("Player 1(X)");
    JTextField txPlayer1 = new JTextField(6);
    JLabel player2lbl = new JLabel("Player 2(O)");
    JTextField txPlayer2 = new JTextField(6);
    JButton ok = new JButton("Start");
    JPanel buttPanel = new JPanel(new GridLayout(3, 3));
    JPanel ResultPanel = new JPanel(new GridLayout(2, 2));
    JLabel resPlay1 = new JLabel("player 1");
    JLabel resPlay2 = new JLabel("player 2");
    JTextField resTxpl1 = new JTextField();
    JTextField resTxpl2 = new JTextField();
    //end
    
    //menu
    private JButton buttons[] = new JButton[9];
    JMenuBar menu = new JMenuBar();
    JMenuItem newGame = new JMenuItem("New Game"),
            instr = new JMenuItem("Instructions"),
            exit = new JMenuItem("Exit"),
            about = new JMenuItem("about");
    //end
    private int[][] winCombinations = new int[][]{
        {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, //horizontal wins
        {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, //virticle wins
        {0, 4, 8}, {2, 4, 6} //diagonal wins
    };
    private int count = 0;
    private String letter = "";
    private boolean win = false;
    static String x = "X";
    static String y = "Y";

    public TicTacToeEn() {
        //add panels,labels,text fields to frame
        playersPanel.setBorder(new TitledBorder("Players' names :"));
        playersPanel.add(player1lbl);
        playersPanel.add(txPlayer1);
        playersPanel.add(player2lbl);
        playersPanel.add(txPlayer2);
        playersPanel.add(ok);
        infoPanel.add(playersPanel);
        //---Result Panel-----
        ResultPanel.setBorder(new TitledBorder("Result :"));
        ResultPanel.add(resPlay1);
        ResultPanel.add(resPlay2);
        ResultPanel.add(resTxpl1);
        ResultPanel.add(resTxpl2);
        infoPanel.add(ResultPanel);
        //----------
        window.add(infoPanel, BorderLayout.WEST);
        //----XO------------

        window.add(buttPanel, BorderLayout.CENTER);
        buttPanel.setVisible(false);
        ok.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent a) {

                buttPanel.setVisible(true);
                resPlay1.setText(txPlayer1.getText());
                resPlay2.setText(txPlayer2.getText());

            }
        });




        //Create Window
        window.setSize(450, 400);
        window.setLocationRelativeTo(null);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        //Add Buttons To The Window
        for (int i = 0; i <= 8; i++) {
            buttons[i] = new JButton();
            buttPanel.add(buttons[i]);
            buttons[i].addActionListener(this);
        }

        menu.add(newGame);
        menu.add(instr);
        menu.add(about);
        menu.add(exit);

        //Add Score count


       about.addActionListener(this);
        newGame.addActionListener(this);
        exit.addActionListener(this);
        instr.addActionListener(this);
        window.setJMenuBar(menu);

        //Make The Window Visible
        window.setVisible(true);

        
    }

    public void actionPerformed(ActionEvent a) {
        Object source = a.getSource();
        

        if (source == newGame) {
            int answer = JOptionPane.showConfirmDialog(null, "Your current game will not be saved...\nContinue Anyways??", "Do you want to start a new game?", JOptionPane.YES_NO_OPTION);

            if (answer == JOptionPane.YES_OPTION) {
                clearIt();
            }

        } else if (source == instr) {
            JOptionPane.showMessageDialog(null, "Your goal is to be the first player to get 3 X's or O's in a row. (horizontally, diagonally, or vertically)", "Instructions", JOptionPane.INFORMATION_MESSAGE);
        } else if (source == about) {
            JOptionPane.showMessageDialog(null, "AbdulRahman Mealy Al-Harbi\nAli Muharib Al-Harbi\nKhalid Hamzah Al-Grafi", "Who are we ??", JOptionPane.INFORMATION_MESSAGE);
        } else if (source == exit) {
            int answer = JOptionPane.showConfirmDialog(null, "EXIT", "Are You sure you want to exit??", JOptionPane.YES_NO_OPTION);

            if (answer == JOptionPane.YES_OPTION) {
                JOptionPane.showMessageDialog(null, "Thank you " + txPlayer1.getText() + " and " + txPlayer2.getText() + " for playing");
                System.exit(0);
            }
        } else {
            count++;

            /*Calculate whose turn it is*/
            if (count % 2 == 0) {
                letter = "O";
            } else {
                letter = "X";
            }

            /*Write the letter to the button and deactivate it*/
            JButton pressedButton = (JButton) source;
            pressedButton.setText(letter);
            pressedButton.setEnabled(false);

            /*Determine who won*/
            for (int i = 0; i <= 7; i++) {
                if (buttons[winCombinations[i][0]].getText().equals(buttons[winCombinations[i][1]].getText())
                        && buttons[winCombinations[i][1]].getText().equals(buttons[winCombinations[i][2]].getText())
                        && buttons[winCombinations[i][0]].getText() != "") {
                    win = true;
                }
            }

            /*Show a dialog when game is over*/
            if (win == true) {
                if (letter.equals("X")) {
                    letter = txPlayer1.getText();
                } else {
                    letter = txPlayer2.getText();
                }

                JOptionPane.showMessageDialog(null, letter + " wins the game!");
                int answer = JOptionPane.showConfirmDialog(null, "Do you want to start a new game??", "Start", JOptionPane.YES_NO_OPTION);

                if (answer == JOptionPane.YES_OPTION) {
                    clearIt();
                } else {
                    JOptionPane.showMessageDialog(null, "Thank you " + txPlayer1.getText() + " and " + txPlayer2.getText() + " for playing");
                    System.exit(0);
                }

            } else if (count == 9 && win == false) {
                JOptionPane.showMessageDialog(null, "The game was tie!");
                int answer = JOptionPane.showConfirmDialog(null, "Do you want to start a new game??", "Start", JOptionPane.YES_NO_OPTION);
                if (answer == JOptionPane.YES_OPTION) {
                    clearIt();
                } else {
                    JOptionPane.showMessageDialog(null, "Thank you " + txPlayer1.getText() + " and " + txPlayer2.getText() + " for playing");
                    System.exit(0);
                }
            }
        }
    }

    public void clearIt() {
        window.setVisible(false);
        this.startIt();
    }

    public void startIt() {
        new TicTacToeEn();
    }
}