Advertisement
luistavares

Jogo Da Velha em Java

Apr 11th, 2013
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.09 KB | None | 0 0
  1. import java.awt.GridLayout;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4.  
  5. import javax.swing.JButton;
  6. import javax.swing.JFrame;
  7. import javax.swing.JLabel;
  8. import javax.swing.JPanel;
  9.  
  10. public class JogoDaVelha {
  11.     private JButton botao1;
  12.     private JButton botao2;
  13.     private JButton botao3;
  14.     private JButton botao4;
  15.     private JButton botao5;
  16.     private JButton botao6;
  17.     private JButton botao7;
  18.     private JButton botao8;
  19.     private JButton botao9;
  20.     private JPanel painel;
  21.     private JFrame frame;
  22.    
  23.     private JLabel labelResultado;
  24.     private JPanel painelResultado;
  25.  
  26.     private String marcador = "X";
  27.     private int numJogadas = 0;
  28.  
  29.     public JogoDaVelha() {
  30.         botao1 = new JButton();
  31.         botao2 = new JButton();
  32.         botao3 = new JButton();
  33.         botao4 = new JButton();
  34.         botao5 = new JButton();
  35.         botao6 = new JButton();
  36.         botao7 = new JButton();
  37.         botao8 = new JButton();
  38.         botao9 = new JButton();
  39.  
  40.         painel = new JPanel(new GridLayout(3, 3));
  41.         painel.add(botao1);
  42.         painel.add(botao2);
  43.         painel.add(botao3);
  44.         painel.add(botao4);
  45.         painel.add(botao5);
  46.         painel.add(botao6);
  47.         painel.add(botao7);
  48.         painel.add(botao8);
  49.         painel.add(botao9);
  50.  
  51.         frame = new JFrame("Jogo da Velha");
  52.         frame.getContentPane().add(painel);
  53.         frame.setSize(300, 300);
  54.         frame.setVisible(true);
  55.  
  56.         botao1.addActionListener(new ActionListener() {
  57.  
  58.             @Override
  59.             public void actionPerformed(ActionEvent arg0) {
  60.                 botao1.setText(marcador);
  61.                 verificaFimPartida();
  62.                 mudaMarcador();
  63.             }
  64.         });
  65.  
  66.         botao2.addActionListener(new ActionListener() {
  67.  
  68.             @Override
  69.             public void actionPerformed(ActionEvent arg0) {
  70.                 botao2.setText(marcador);
  71.                 verificaFimPartida();
  72.                 mudaMarcador();
  73.             }
  74.         });
  75.  
  76.         botao3.addActionListener(new ActionListener() {
  77.  
  78.             @Override
  79.             public void actionPerformed(ActionEvent arg0) {
  80.                 botao3.setText(marcador);
  81.                 verificaFimPartida();
  82.                 mudaMarcador();
  83.             }
  84.         });
  85.        
  86.         botao4.addActionListener(new ActionListener() {
  87.  
  88.             @Override
  89.             public void actionPerformed(ActionEvent arg0) {
  90.                 botao4.setText(marcador);
  91.                 verificaFimPartida();
  92.                 mudaMarcador();
  93.             }
  94.         });
  95.        
  96.         botao5.addActionListener(new ActionListener() {
  97.  
  98.             @Override
  99.             public void actionPerformed(ActionEvent arg0) {
  100.                 botao5.setText(marcador);
  101.                 verificaFimPartida();
  102.                 mudaMarcador();
  103.             }
  104.         });
  105.        
  106.         botao6.addActionListener(new ActionListener() {
  107.  
  108.             @Override
  109.             public void actionPerformed(ActionEvent arg0) {
  110.                 botao6.setText(marcador);
  111.                 verificaFimPartida();
  112.                 mudaMarcador();
  113.             }
  114.         });
  115.        
  116.         botao7.addActionListener(new ActionListener() {
  117.  
  118.             @Override
  119.             public void actionPerformed(ActionEvent arg0) {
  120.                 botao7.setText(marcador);
  121.                 verificaFimPartida();
  122.                 mudaMarcador();
  123.             }
  124.         });
  125.        
  126.         botao8.addActionListener(new ActionListener() {
  127.  
  128.             @Override
  129.             public void actionPerformed(ActionEvent arg0) {
  130.                 botao8.setText(marcador);
  131.                 verificaFimPartida();
  132.                 mudaMarcador();
  133.             }
  134.         });
  135.        
  136.         botao9.addActionListener(new ActionListener() {
  137.  
  138.             @Override
  139.             public void actionPerformed(ActionEvent arg0) {
  140.                 botao9.setText(marcador);
  141.                 verificaFimPartida();
  142.                 mudaMarcador();
  143.             }
  144.         });
  145.     }
  146.  
  147.     private void mudaMarcador() {
  148.         if (marcador.equals("X")) {
  149.             marcador = "O";
  150.         } else {
  151.             marcador = "X";
  152.         }      
  153.     }
  154.    
  155.     private void verificaFimPartida(){ 
  156.         String b1 = botao1.getText();
  157.         String b2 = botao2.getText();
  158.         String b3 = botao3.getText();
  159.         String b4 = botao4.getText();
  160.         String b5 = botao5.getText();
  161.         String b6 = botao6.getText();
  162.         String b7 = botao7.getText();
  163.         String b8 = botao8.getText();
  164.         String b9 = botao9.getText();      
  165.        
  166.         numJogadas++;
  167.         if (b1.equals(b2) && b1.equals(b3) && b1.equals(marcador)){
  168.             apresentaVitoria();
  169.         }
  170.         else if (b4.equals(b5) && b4.equals(b6) && b4.equals(marcador)){
  171.             apresentaVitoria();
  172.         }
  173.         else if (b7.equals(b8) && b7.equals(b9) && b7.equals(marcador)){
  174.             apresentaVitoria();
  175.         }
  176.         else if (b1.equals(b4) && b1.equals(b7) && b1.equals(marcador)){
  177.             apresentaVitoria();
  178.         }
  179.         else if (b2.equals(b5) && b2.equals(b8) && b2.equals(marcador)){
  180.             apresentaVitoria();
  181.         }
  182.         else if (b3.equals(b6) && b3.equals(b9) && b3.equals(marcador)){
  183.             apresentaVitoria();
  184.         }
  185.         else if (b1.equals(b5) && b1.equals(b9) && b1.equals(marcador)){
  186.             apresentaVitoria();
  187.         }
  188.         else if (b7.equals(b5) && b7.equals(b3) && b7.equals(marcador)){
  189.             apresentaVitoria();
  190.         }
  191.         else if (numJogadas == 9){
  192.             apresentaEmpate();
  193.         }
  194.     }
  195.    
  196.     private void apresentaVitoria(){
  197.         labelResultado = new JLabel("Fim de Jogo. Vitória do Jogador [ " + marcador + " ]!");
  198.         painelResultado = new JPanel();
  199.         painelResultado.add(labelResultado);
  200.         frame.getContentPane().removeAll();
  201.         frame.getContentPane().add(painelResultado);
  202.     }
  203.    
  204.     private void apresentaEmpate(){
  205.         labelResultado = new JLabel("Fim de Jogo. Empate!");
  206.         painelResultado = new JPanel();
  207.         painelResultado.add(labelResultado);
  208.         frame.getContentPane().removeAll();
  209.         frame.getContentPane().add(painelResultado);
  210.     }
  211.    
  212.     public static void main(String[] args) {
  213.         new JogoDaVelha();
  214.     }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement