Ir para conteúdo
Fórum CódigoFonte.net
Entre para seguir isso  
marceloP

Jogo da Velha com problemas (poo)

Recommended Posts

Ola pessoal !

sou novato em programação java, estou desenvolvendo um jogo da velha em java, orientação objeto, ja esta quase pronto ! quando o programa esta implementado tudo dentro do construtor roda na boa; mas quando vou separar em classes objeto da os seguinte erros

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at controle.ProcessJogo.ganhador(ProcessJogo.java:31)
at controle.ProcessJogo.jogador(ProcessJogo.java:23)
at controle.Interface.actionPerformed(Interface.java:58)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at controle.ProcessJogo.ganhador(ProcessJogo.java:31)
at controle.ProcessJogo.jogador(ProcessJogo.java:23)
at controle.Interface.actionPerformed(Interface.java:61)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Segue o codigo em classes; Interface:

package controle;
 
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;   
   
   
   @SuppressWarnings("serial")
   public class Interface extends JFrame implements ActionListener {
      ProcessJogo processo = new ProcessJogo();
  
  private JButton  vet[] = new JButton[9];
       private JPanel janela;
           
       
       public Interface(){     
         super("Jogo da Velha");        
           initComponents();  
           configEvents(); 
           configLayout();           
       }
    
       private  void configLayout(){    
             
        add(janela);      
           this.setSize(600,600);        
           janela.setLayout(new GridLayout(3,3));            
            
           for (JButton botao : vet) {
                  janela.add(botao);
           }           
            
           this.add(janela); 
           setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
           setVisible(true);
           }   
        
       private void initComponents(){         
           janela = new JPanel();
       }  
    
       private void configEvents(){
         for(int i=0;i<9;i++){
             vet[i] = new JButton("");
             vet[i].setFont(new Font("Calibri",Font.BOLD,70));
             vet[i].addActionListener(this); 
         }    
       }    
       public void actionPerformed(ActionEvent e) {
     
           JButton botao = (JButton) e.getSource();         
           if(botao== vet[0]){
              processo.jogador(0);              
       }
       if(botao== vet[1]){
          processo.jogador(1);         
       }
       if(botao== vet[2]){
          processo.jogador(2);        
       }
       if(botao== vet[3]){
          processo.jogador(3);          
       }     
       if(botao== vet[4]){
        processo.jogador(4);      
      
       }
       if(botao== vet[5]){
        processo.jogador(5);      
      
       }
       if(e.getSource()== vet[6]){
        processo.jogador(6);      
       }
       if(botao== vet[7]){
        processo.jogador(7);      
       }
       if(botao== vet[8]){
          processo.jogador(8);
        
       }      
     }     
   }  

Segue o codigo ProcessJogo:

 Segue o codigo ProcessJogo:
 
[code] package controle;
 
import javax.swing.JOptionPane;
 
   
   
public class ProcessJogo   {
 
private static final int MAX_RECODS = 9;
private static Interface[] grade = new Interface[MAX_RECODS];
private int vez;
private String simb=null;
 
 
 
public void jogador(int posicao){
        if(vez == 0){
           simb ="X";
         }else{
           simb = "O";
           vez =0;                 
    }
    ganhador(); 
    grade[posicao].setTitle(simb);
    grade[posicao].setEnabled(false);               
    }
 
    public void ganhador(){       
     String vet1[] = new String[8];    
     
     vet1[0] = grade[0].getTitle()+ grade[1].getTitle()+ grade[2].getTitle();
     vet1[1] = grade[3].getTitle()+ grade[4].getTitle()+ grade[5].getTitle();
     vet1[2] = grade[6].getTitle()+ grade[7].getTitle()+ grade[8].getTitle();
     vet1[3] = grade[0].getTitle()+ grade[3].getTitle()+ grade[6].getTitle();
     vet1[4] = grade[1].getTitle()+ grade[4].getTitle()+ grade[7].getTitle();
     vet1[5] = grade[2].getTitle()+ grade[5].getTitle()+ grade[8].getTitle();
     vet1[6] = grade[0].getTitle()+ grade[4].getTitle()+ grade[8].getTitle();
     vet1[7] = grade[2].getTitle()+ grade[4].getTitle()+ grade[6].getTitle();
     
     for(int i=0;i<vet1.length;i++){
      if(vet1[i].equals("XXX")){        
        JOptionPane.showMessageDialog(null,"Jogador X e Vencedor ! ");
       }else{
         if(vet1[i].equals("OOO")){
        JOptionPane.showMessageDialog(null,"Jogador O e Vencedor !" );         
      }   
         }
     }
  }
    
  public void empate(){    
 
  if(grade[0].getTitle().equals("")&&
    grade[1].getTitle().equals("")&&
    grade[2].getTitle().equals("")&&
    grade[3].getTitle().equals("")&&
    grade[4].getTitle().equals("")&&
    grade[5].getTitle().equals("")&&
    grade[6].getTitle().equals("")&&
    grade[7].getTitle().equals("")&&
    grade[8].getTitle().equals("")){
    JOptionPane.showMessageDialog(null,"Partida Empatada !" );
  }
  }
  public static void main(String[]args){
 new Interface();
 new ProcessJogo();
  }
}  

Segue o codigo Program :

 package controle;
 
public class Program {
 
public static void main(String[] args) {
new Interface();
new ProcessJogo();
}
 
}

Compartilhar este post


Link para o post
Compartilhar em outros sites

Crie uma conta ou entre para comentar

Você precisar ser um membro para fazer um comentário

Criar uma conta

Crie uma nova conta em nossa comunidade. É fácil!

Crie uma nova conta

Entrar

Já tem uma conta? Faça o login.

Entrar Agora
Entre para seguir isso  

×
×
  • Criar Novo...