package ir.mahshahr.ooplab;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 * 
 * @author Kevin Walker
 *
 */

public class TicTacToe extends JFrame implements ActionListener {
	public TicTacToe() {
		// Setup a JFrame window.
		super();

		// Use a grid layout, with three columns and three rows, since this is TicTacToe,
		this.setLayout(new GridLayout(3,3));

		// Fill each space on the grid with a button.
		for (int x = 0; x < 9; x++) {
			JButton temp = new JButton("-");
			squares.add(temp);
			temp.addActionListener(this);
			this.add(temp);
		}

		// Resize the window to make everything fit,
		// and make the user interface visible.
		this.pack();
		this.setLocation(500, 250);
		this.setSize(250, 250);
		this.setVisible(true);
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);

		}

	ArrayList<JButton> squares = new ArrayList<JButton>();

	String player = "X";

	public void actionPerformed(ActionEvent e) {
	// For all the buttons in the game grid.
	for (JButton square : squares) {
	// If the button clicked was the source of the event.
	if (square.equals(e.getSource())) {
	// Set the button to the player's letter, X or O.
		square.setText(player);
	
		// Change to the next player's turn.
		if (player.equals("X")) 
			player = "O";
		else 
			player = "X";
		}

	}

	// Check to see if anyone has won.
	checkWinner();

	}
	public void checkWinner() {

	// Get the contents of the board.
	String[] board = new String[9];

	// Set the winner to the blank space, "-."
	String winner = "-";

	for (int x = 0; x < 9; x++) {
		board[x] = squares.get(x).getText();
		System.out.println(x + ": " + board[x]);
	}

	// Check if the top row is full of the same letter.
	// If it is, then that player has won.
	if (board[0].equals(board[1]) && board[0].equals(board[2])) {
		winner = board[0];
	} else
	// Middle Row
	if (board[3].equals(board[4]) && board[3].equals(board[5])) {
		winner = board[3];
	} else
	// Bottom row
	if (board[6].equals(board[7]) && board[6].equals(board[8])) {
		winner = board[6];
	} else
	// Left column
	if (board[0].equals(board[3]) && board[0].equals(board[6])) {
		winner = board[0];
	} else
	// Middle column
	if (board[1].equals(board[4]) && board[1].equals(board[7])) {
		winner = board[1];
	} else
	// Right column
	if (board[2].equals(board[5]) && board[6].equals(board[8])) {
		winner = board[2];
	} else
	// Top-Left, Middle, Bottom Right Diagonal
	if (board[0].equals(board[4]) && board[0].equals(board[8])) {
		winner = board[0];
	} else
	// Top-Right, Middle, Bottom Left Diagonal
	if (board[2].equals(board[4]) && board[2].equals(board[6])) {
		winner = board[2];
	}

	// If the winner isn't the blank space "-,"
	// then someone has won. Congratulate them
	// and end the game.
	if (!winner.equals("-")) {
		JOptionPane.showMessageDialog(rootPane, "The winner is " + winner);
		System.exit(0);
	}
	}

	public static void main(String[] args) {
		new TicTacToe();
	}
}