import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class TicTacToe implements ActionListener{
	
	Random random = new Random();
	JFrame frame = new JFrame();
	JPanel title_panel = new JPanel();
	JPanel button_panel = new JPanel();
	JLabel textfield = new JLabel();
	JButton[] buttons = new JButton[9];
	JButton resetbutton = new JButton();
	Color color = new Color(0,0,0);
	boolean player1_turn;
	boolean win = false;
	
	TicTacToe() {
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(500,500);
		frame.getContentPane().setBackground(new Color(50,50,50));
		frame.setLayout(new BorderLayout());
		frame.setVisible(true);
		
		textfield.setBackground(new Color(0,0,0));
		textfield.setForeground(new Color(25,255,0));
		textfield.setFont(new Font("Ahorni",Font.BOLD,65));
		textfield.setHorizontalAlignment(JLabel.CENTER);
		textfield.setText("Tic-Tac-Toe");
		textfield.setOpaque(true);
		
		title_panel.setLayout(new GridLayout(2,1));
		title_panel.setBounds(0,0,800,100);
		
		resetbutton.setText("Restart Game");
		resetbutton.setFont(new Font("Ahorni", Font.BOLD,35));
		resetbutton.setSize(100,50);
		resetbutton.addActionListener(this);
		resetbutton.setForeground(new Color(0,0,0));
		color = resetbutton.getBackground();
		
		button_panel.setLayout(new GridLayout(3,3));
		button_panel.setBackground(new Color(150,150,150));
		
		for(int i=0;i<9;i++) {
			buttons[i] = new JButton();
			button_panel.add(buttons[i]);
			buttons[i].setFont(new Font("Ahorni",Font.BOLD,100));
			buttons[i].setFocusable(false);
			buttons[i].addActionListener(this);
			
		}
		
		title_panel.add(textfield);
		title_panel.add(resetbutton);
		frame.add(title_panel,BorderLayout.NORTH);
		frame.add(button_panel);
		
		
		firstTurn();
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		//TODO Auto-generated method stub
		
		if(e.getSource()==resetbutton) {
			for(int i=0;i<9;i++) {
				buttons[i].setText("");
				buttons[i].setEnabled(true);
				buttons[i].setBackground(color);
			}
			firstTurn();	
		}
		
		for(int i=0;i<9;i++) {
			if(e.getSource()==buttons[i]) {
				if(player1_turn) {
					if(buttons[i].getText()=="") {
						buttons[i].setForeground(new Color(255,0,0));
						textfield.setText("X Turn");
						buttons[i].setText("O");
						player1_turn=false;
						check();
					}
				}
				else {
					if(buttons[i].getText()=="") {
						buttons[i].setForeground(new Color(0,0,255));
						textfield.setText("O turn");
						buttons[i].setText("X");
						player1_turn=true;
						check();
						
					}
				}
			
			
			}
		}
		
	}
	
	public void firstTurn() {
		
		try {
			Thread.sleep(900);	
		}	
		catch (InterruptedException e) {
				//TODO Auto-generated catch block
			e.printStackTrace();
		}
			
		if(random.nextInt(2)==0) {
			player1_turn=true;
			textfield.setText("O Turn");
			
			}
		else {
			player1_turn=false;
			textfield.setText("X Turn");
		
		}
	}
	
	public void check() { 
		//check X win conditions
		if(
				(buttons[0].getText()=="X") &&
				(buttons[1].getText()=="X") &&
				(buttons[2].getText()=="X") 
				) {
			xWins(0,1,2);		
		}
		if(
				(buttons[3].getText()=="X") &&
				(buttons[4].getText()=="X") &&
				(buttons[5].getText()=="X") 
				) {
			xWins(3,4,5);
		}
		if(
				(buttons[6].getText()=="X") &&
				(buttons[7].getText()=="X") &&
				(buttons[8].getText()=="X") 
				) {
			xWins(6,7,8);
		}
		if(
				(buttons[0].getText()=="X") &&
				(buttons[3].getText()=="X") &&
				(buttons[6].getText()=="X") 
				) {
			xWins(0,3,6);
		}
		if(
				(buttons[1].getText()=="X") &&
				(buttons[4].getText()=="X") &&
				(buttons[7].getText()=="X") 
				) {
			xWins(1,4,7);
		}		if(
				(buttons[2].getText()=="X") &&
				(buttons[5].getText()=="X") &&
				(buttons[8].getText()=="X") 
				) {
			xWins(2,5,8);
		}
		if(
				(buttons[0].getText()=="X") &&
				(buttons[4].getText()=="X") &&
				(buttons[8].getText()=="X") 
				) {
			xWins(0,4,8);
		}
		if(
				(buttons[2].getText()=="X") &&
				(buttons[4].getText()=="X") &&
				(buttons[6].getText()=="X") 
				) {
			xWins(2,4,6);
		}
		//check O win conditions
		if(
				(buttons[0].getText()=="O") &&
				(buttons[1].getText()=="O") &&
				(buttons[2].getText()=="O") 
				) {
			oWins(0,1,2);
		}
		if(
				(buttons[3].getText()=="O") &&
				(buttons[4].getText()=="O") &&
				(buttons[5].getText()=="O") 
				) {
			oWins(3,4,5);
		}
		if(
				(buttons[6].getText()=="O") &&
				(buttons[7].getText()=="O") &&
				(buttons[8].getText()=="O") 
				) {
			oWins(6,7,8);
		}
		if(
				(buttons[0].getText()=="O") &&
				(buttons[3].getText()=="O") &&
				(buttons[6].getText()=="O") 
				) {
			oWins(0,3,6);
		}
		if(
				(buttons[1].getText()=="O") &&
				(buttons[4].getText()=="O") &&
				(buttons[7].getText()=="O") 
				) {
			oWins(1,4,7);
		}		if(
				(buttons[2].getText()=="O") &&
				(buttons[5].getText()=="O") &&
				(buttons[8].getText()=="O") 
				) {
			oWins(2,5,8);
		}
		if(
				(buttons[0].getText()=="O") &&
				(buttons[4].getText()=="O") &&
				(buttons[8].getText()=="O") 
				) {
			oWins(0,4,8);
		}
		if(
				(buttons[2].getText()=="O") &&
				(buttons[4].getText()=="O") &&
				(buttons[6].getText()=="O") 
				) {
			oWins(2,4,6);
		}
		
		boolean gameover = true;
		
		for(int i=0;i<9;i++) {
			if(buttons[i].getText()=="") { 
			gameover = false;
			}
		}
			
		if(
				win == false &&
				gameover == true
				) {
			textfield.setText("Draw");
			buttons[0].setEnabled(false);
			buttons[1].setEnabled(false);
			buttons[2].setEnabled(false);
			buttons[3].setEnabled(false);
			buttons[4].setEnabled(false);
			buttons[5].setEnabled(false);
			buttons[6].setEnabled(false);
			buttons[7].setEnabled(false);
			buttons[8].setEnabled(false);
		}
	}
	
	public void xWins(int a,int b ,int c) {
		buttons[a].setBackground(Color.GREEN);
		buttons[b].setBackground(Color.GREEN);
		buttons[c].setBackground(Color.GREEN);
		
		for(int i=0;i<9;i++) {
			buttons[i].setEnabled(false);
		}
		textfield.setText("X Wins");
		
		win = true;
	}
	
	public void oWins(int a,int b,int c) {
		buttons[a].setBackground(Color.GREEN);
		buttons[b].setBackground(Color.GREEN);
		buttons[c].setBackground(Color.GREEN);
		
		for(int i=0;i<9;i++) {
			buttons[i].setEnabled(false);
		}
		textfield.setText("O Wins");
		
		win = true;
	}
}
