
import java.awt.*;
import javax.swing.*; 
import java.awt.event.*;
import javax.swing.event.*; 
import java.util.Random;


public class JogoDaVelha extends JFrame implements ActionListener
{ 
    	int [] estados = new int [] {1, 1, 1, 1, 1, 1, 1, 1, 1};
    	int [] numeros = new int [] {3, 3, 3, 3, 3, 3, 3, 3, 3};
    	JButton [] bt_Botoes = new JButton [9];
    	JButton bt_Reiniciar, bt_Sair, bt_Sobre; 
    	JPanel Painel_Central, Painel_Sul, Painel_Oeste; 
    	ImageIcon icone_o 	 = new ImageIcon("botao_o.png");	
    	ImageIcon icone_x 	 = new ImageIcon("botao_x.png");	
    	ImageIcon iconejogo  = new ImageIcon("trevo.png");	
    	ImageIcon iconeandre = new ImageIcon("profandre.png");	
    	int Operador_O_X = 0; 
    		
    	public JogoDaVelha() 
    	{
    			getContentPane().setLayout(new BorderLayout());
    			getContentPane().add(Painel_Central = new JPanel(new GridLayout(3, 3))); 
     			Metodo_Monta_Tela(Painel_Central, bt_Botoes);
    			Painel_Central.setBorder(javax.swing.BorderFactory.createEtchedBorder());
				getContentPane().add("West", Painel_Oeste = new JPanel(new FlowLayout(FlowLayout.CENTER))); 
				JLabel lbl_Lateral = new JLabel(""); 
				Painel_Oeste.add(lbl_Lateral);
				//lbl_Lateral.setIcon(iconeandre); 
 				getContentPane().add("South", Painel_Sul = new JPanel(new FlowLayout(FlowLayout.CENTER)));   			
    			Painel_Sul.add(bt_Sobre 	= new JButton("SOBRE O JOGO"));  
    			Painel_Sul.add(bt_Reiniciar = new JButton("REINICIAR"));  
    			Painel_Sul.add(bt_Sair 		= new JButton("SAIR DO JOGO"));    			
    			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			setTitle(":::: OXNAR - Jogo da Velha");
    			this.setLocationRelativeTo(null);

    			pack(); 
    			////Abrir a tela centralizada
    			Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
      			int w = this.getSize().width;
    			int h = this.getSize().height;
    			int x = (dim.width-w)/2;
    			int y = (dim.height-h)/2;
    			// Move the window
    			setLocation(x, y);
    			////////
    			setVisible(true);
    			setResizable(false);
    			bt_Reiniciar.addActionListener(this); 
    			bt_Sair.addActionListener(this); 
    			bt_Sobre.addActionListener(this);
    			 
    	}
    	public void Metodo_Verifica_Vencedor (int [] num, int x)
    	{
    			String mensagem = ("UNINTER - TECNOLOGIA EM DESENVOLVIMENTO DE APPS \n PARABÉNS!!! \n VOCÊ GANHOU O JOGO"); 
    			if(((num[0])==x)&&(num[1]==x)&&(num[2]==x))
    			{
    					JOptionPane.showMessageDialog (null, mensagem);
    					Metodo_Inativa_Botoes(bt_Botoes);
    			}	
  				else if(((num[3])==x)&&(num[4]==x)&&(num[5]==x))
    			{
    					JOptionPane.showMessageDialog (null, mensagem);
    					Metodo_Inativa_Botoes(bt_Botoes);
    			}
  				else if(((num[6])==x)&&(num[7]==x)&&(num[8]==x))
    			{
    					JOptionPane.showMessageDialog (null, mensagem);
    					Metodo_Inativa_Botoes(bt_Botoes);
    			}
				else if(((num[0])==x)&&(num[4]==x)&&(num[8]==x))
    			{
    					JOptionPane.showMessageDialog (null, mensagem);
    					Metodo_Inativa_Botoes(bt_Botoes);
    			}    			
    			else if(((num[6])==x)&&(num[4]==x)&&(num[8]==x))
    			{
    					JOptionPane.showMessageDialog (null, mensagem);
    					Metodo_Inativa_Botoes(bt_Botoes);
    			}    			
    			else if(((num[0])==x)&&(num[3]==x)&&(num[6]==x))
    			{
    					JOptionPane.showMessageDialog (null, mensagem);
    					Metodo_Inativa_Botoes(bt_Botoes);
    			}    			
	   			else if(((num[1])==x)&&(num[4]==x)&&(num[7]==x))
    			{
    					JOptionPane.showMessageDialog (null, mensagem);
    					Metodo_Inativa_Botoes(bt_Botoes);
    			}    			
	   			else if(((num[2])==x)&&(num[4]==x)&&(num[6]==x))
    			{
    					JOptionPane.showMessageDialog (null, mensagem);
    					Metodo_Inativa_Botoes(bt_Botoes);
    			}    			
	   			else if(((num[0])==x)&&(num[4]==x)&&(num[8]==x))
    			{
    					JOptionPane.showMessageDialog (null, mensagem);
    					Metodo_Inativa_Botoes(bt_Botoes);
    			}
	   			else if(((num[2])==x)&&(num[5]==x)&&(num[8]==x))
    			{
    					JOptionPane.showMessageDialog (null, mensagem);
    					Metodo_Inativa_Botoes(bt_Botoes);
    			}			
    	}	
    		
	    public void actionPerformed (ActionEvent e)
	    {
    			int i = 0; 
    			while(i < 9)
    			{
    					if (e.getSource()==bt_Botoes[i])
    					{
    							if(Operador_O_X==0)
    							{
    									if(estados[i]==1)
    									{
    										    bt_Botoes[i].setIcon(icone_o);
    											estados[i] = 0;  
    											numeros[i]=0; 
    											Operador_O_X = 1; 
    											Metodo_Verifica_Vencedor (numeros, 0);
    									}
    										
    							}
    							else if(Operador_O_X==1)
    							{
    									if(estados[0]==1)
    									{
    											bt_Botoes[i].setIcon(icone_x);
    											estados[i] = 0;  
    											numeros[i]=1; 
    											Operador_O_X = 0;
    											Metodo_Verifica_Vencedor (numeros, 1);
    									}
    									 	
    							}
    							
    					}
    					i++;
    			}
    			if(e.getSource()==bt_Reiniciar)
    			{
    					int z = 0; 
    					while(z < 9)
    					{
    					  		numeros[z] = -1; 
    					  		bt_Botoes[z].setEnabled(true);
    					  		bt_Botoes[z].setIcon(null);
    					  		bt_Botoes[z].setText("JOGO DA VELHA");
    					  		z++;
    					}
    			
    					 	
    			}
    			if(e.getSource()==bt_Sobre)
    			{
    					String mensagem = ("UNINTER - TECNOLOGIA EM DESENVOLVIMENTO DE APPS \n Atividade Extensionista\n APLICATIVO DESENVOLVIDO PELO PROF. ANDRÉ APARECIDO SILVA \n PARA AS AULAS DE PROGRAMAÇÃO JAVA \n Código fonte em http:\\www.oxnar.com.br ");
    					JOptionPane.showMessageDialog (null, mensagem);  			
    			}
    			if(e.getSource()==bt_Sair)
    			{System.exit(0);}
				
	    		
	    }
	    public JPanel Metodo_Monta_Tela(JPanel p1, JButton [] Botoes)
	    {
 				int i = 0; 
 				while(i < 9)
 				{
 						p1.add(Botoes[i] = new JButton(""));
 						Color Cor1 = new Color(255, 255, 255);
               			Botoes[i].setBackground(Cor1);
 						Botoes[i].addActionListener(this);
 						Botoes[i].setIcon(iconejogo);
 						i++;	
 				}	
 				return p1; 		
	    }
	    public void Metodo_Inativa_Botoes( JButton [] bt)
	    {
 				int i = 0; 
 				while(i < 9)
 				{ 
 						bt[i].setEnabled(false);
 						i++;	
 				}	
 				return; 		
	    }	    
    	public static void main (String args [])
    	{
    			new JogoDaVelha();
    	}
   
    
}