Advertisement
Alfoli

POO Jogo da memoria

Apr 2nd, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.07 KB | None | 0 0
  1. //animal
  2. public class Animal{
  3.     private String nome;
  4.     private boolean aberta;
  5.     public Animal(){
  6.         this.nome = null;
  7.         this.aberta = false;
  8.     }
  9.     public boolean getAberta(){
  10.         return this.aberta;
  11.     }
  12.     public Animal(String nome){
  13.         this.nome = nome;
  14.         this.aberta = false;
  15.     }
  16.     public void setNome (String n){
  17.         this.nome = n;
  18.     }
  19.     public String getNome(){
  20.         return this.nome;
  21.     }
  22.     public void setFechada(){
  23.         this.aberta = false;
  24.     }
  25.     public void setAberta(){
  26.         this.aberta=true;
  27.     }
  28. }
  29. //Bichos
  30. import java.util.List;
  31. import java.util.ArrayList;
  32. import java.util.Collections;
  33.  
  34. import javax.swing.ImageIcon;
  35. public class Bichos {
  36.     private List <Animal> zoologico = new ArrayList <Animal>();
  37.     public Bichos(){
  38.         Animal a;
  39.         String nome1, nome2, nome3, nome4, nome5, nome0;
  40.         nome0 = String.format ("borboleta.png");
  41.         nome1 = String.format ("cachorro.png");
  42.         nome2 = String.format ("carangueijo.png");
  43.         nome3 = String.format ("cobra.png");
  44.         nome4 = String.format ("elefante.png");
  45.         nome5 = String.format ("urso.png");
  46.         int conta, j;
  47.         for (j=0;j<2;j++){
  48.             conta = 0;
  49.             while (conta<6){
  50.                 a = new Animal();
  51.                 switch (conta){
  52.                 case 0: a.setNome (nome0); break;
  53.                 case 1: a.setNome (nome1); break;
  54.                 case 2: a.setNome (nome2); break;
  55.                 case 3: a.setNome (nome3); break;
  56.                 case 4: a.setNome (nome4); break;
  57.                 case 5: a.setNome (nome5); break;
  58.                 }
  59.                 conta++; zoologico.add(a);
  60.             }
  61.         }
  62.     }
  63.     public void zerazologico(){
  64.         zoologico.clear();
  65.     }
  66.     public int qtdade(){
  67.         return zoologico.size();
  68.     }
  69.     public void embaralha(){
  70.         Collections.shuffle(zoologico);
  71.     }
  72.     public Animal getAnimal(int posicao){
  73.         Animal c=zoologico.get(posicao);
  74.         return c;
  75.     }
  76.     public ImageIcon getIcone(int posicao){
  77.         Animal c = zoologico.get(posicao);
  78.         String nome = c.getNome();
  79.         ImageIcon ic = new ImageIcon (getClass().getResource(nome));
  80.         return ic;
  81.     }
  82.     public String getNome (int posicao){
  83.         Animal c=zoologico.get(posicao);
  84.         return c.getNome();
  85.     }
  86. }
  87. import java.util.*;
  88. import java.awt.BorderLayout;
  89. import java.awt.GridLayout;
  90. import java.awt.event.*;
  91.  
  92. import javax.swing.*;
  93. public class Jogar extends JFrame implements ActionListener{
  94.     Bichos b;
  95.     JButton btMatriz[][] = new JButton [2][6];
  96.     Icon IconeFechado;
  97.     boolean primeiro=true;
  98.     String primeiroNome=null;
  99.     int numeroPosicao;
  100.     int iant, jant;
  101.    
  102.     public Jogar(){
  103.         super ("Jogo da Memoria");
  104.         setLayout(new BorderLayout());
  105.         criacomponentes();
  106.     }
  107.    
  108.     public void criacomponentes(){
  109.         int i, j, posicao;
  110.         b=new Bichos ();
  111.         b.embaralha();
  112.         IconeFechado = new ImageIcon(getClass().getResource("fechado.png"));
  113.         JPanel painelcentro = new JPanel (new GridLayout(2,6,5,5));
  114.         for (i=0;i<2;i++){
  115.             for(j=0; j<6;j++){
  116.                 btMatriz [i][j] = new JButton();
  117.                 btMatriz [i][j].setIcon(IconeFechado);
  118.                 btMatriz [i][j].addActionListener(this);
  119.                 painelcentro.add(btMatriz[i][j]);
  120.             }
  121.         }
  122.         add(painelcentro, BorderLayout.CENTER);
  123.     }
  124.     public int retornaPosicao(int lin, int col){
  125.         int posicao=0;
  126.         if (lin== 0 && col == 0) posicao = 0;
  127.         if (lin== 0 && col == 1) posicao = 1;
  128.         if (lin== 0 && col == 2) posicao = 2;
  129.         if (lin== 0 && col == 3) posicao = 3;
  130.         if (lin== 0 && col == 4) posicao = 4;
  131.         if (lin== 0 && col == 5) posicao = 5;
  132.         if (lin== 1 && col == 0) posicao = 6;
  133.         if (lin== 1 && col == 1) posicao = 7;
  134.         if (lin== 1 && col == 2) posicao = 8;
  135.         if (lin== 1 && col == 3) posicao = 9;
  136.         if (lin== 1 && col == 4) posicao = 10;
  137.         if (lin== 1 && col == 5) posicao = 11;
  138.         return posicao;
  139.     }
  140.     public void actionPerformed (ActionEvent e){
  141.         int i, j, posicao, segundaPosicao;
  142.         for (i=0; i<2; i++){
  143.             for (j=0; j<6; j++){
  144.                    //melhorar
  145.                 if (e.getSource()== btMatriz[i][j]){
  146.                     posicao = retornaPosicao(i,j);
  147.                     Animal a = b.getAnimal(posicao);
  148.                     btMatriz[i][j].setIcon(b.getIcone(posicao));
  149.                     JOptionPane.showMessageDialog(null, b.getNome(posicao));
  150.                     //melhorar
  151.                 }
  152.             }
  153.         }
  154.     }
  155.     public static void main (String [] args){
  156.         Jogar j=new Jogar();
  157.         j.setSize(1000, 400);
  158.         j.setLocation(250, 150);
  159.         j.setVisible(true);
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement