lunes, 12 de marzo de 2012

Juego Memorama


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;


public class memo extends JFrame{

JButton inijuego=new JButton("INICIAR JUEGO");
JButton salir=new JButton("SALIR DEL JUEGO");

JButton b1;
JButton b2;

static JButton ima1=new JButton();
static JButton ima2=new JButton();
static JButton ima3=new JButton();
static JButton ima4=new JButton();
static JButton ima5=new JButton();
static JButton ima6=new JButton();
JPanel p=new JPanel();

Activa act=new Activa();
int y[], contador, contador2;
static ImageIcon x[]=new ImageIcon[4];

memo(){
setTitle("Mi Memorama");
setSize(1000,400);
Container contenedor=getContentPane();
contenedor.add(p);
p.setLayout(null);
p.add(inijuego);inijuego.setBounds(800,50,170,30);
p.add(salir);salir.setBounds(800,100,170,30);
   p.add(ima1);ima1.setBounds(0,10,170,150);
   p.add(ima2);ima2.setBounds(200,10,170,150);
   p.add(ima3);ima3.setBounds(400,10,170,150);
   p.add(ima4);ima4.setBounds(0,200,170,150);
   p.add(ima5);ima5.setBounds(200,200,170,150);
   p.add(ima6);ima6.setBounds(400,200,170,150);
 
   x[0]=new ImageIcon("f.jpg");
   x[1]=new ImageIcon("f1.jpg");
   x[2]=new ImageIcon("f2.jpg");
   x[3]=new ImageIcon("f3.jpg");
     
   ima1.setIcon(x[3]);
   ima2.setIcon(x[3]);
   ima3.setIcon(x[3]);
   ima4.setIcon(x[3]);
   ima5.setIcon(x[3]);
   ima6.setIcon(x[3]);
   y=Aleatorios.calcula();

ima1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent w){

ima1.setIcon(x[y[0]]);
if(contador==0){
b1=ima1;
contador++;}
else{
b2=ima1;
act=new Activa();
act.start();
contador=0;}
}});

ima2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent w){
ima2.setIcon(x[y[1]]);
if(contador==0){
b1=ima2;
contador++;}
else{
b2=ima2;
act=new Activa();
act.start();
contador=0;}
}});

ima3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent w){
ima3.setIcon(x[y[2]]);
if(contador==0){
b1=ima3;
contador++;}
else{
b2=ima3;
act=new Activa();
act.start();
contador=0;}
}});

ima4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent w){
ima4.setIcon(x[y[3]]);
if(contador==0){
b1=ima4;
contador++;}
else{
b2=ima4;
act=new Activa();
act.start();
contador=0;}
}});

ima5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent w){
ima5.setIcon(x[y[4]]);
if(contador==0){
b1=ima5;
contador++;}
else{
b2=ima5;
act=new Activa();
act.start();
contador=0;}
}});

ima6.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent w){
ima6.setIcon(x[y[5]]);
if(contador==0){
b1=ima6;
contador++;}
else{
b2=ima6;
act=new Activa();
act.start();
contador=0;}
}});

inijuego.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent w){
inicializa.inicializa();
y=Aleatorios.calcula();
}});
}



public static class inicializa{
public static void inicializa(){
ima1.setIcon(x[3]);
ima2.setIcon(x[3]);
ima3.setIcon(x[3]);
ima4.setIcon(x[3]);
ima5.setIcon(x[3]);
ima6.setIcon(x[3]);

ima1.setEnabled(true);
ima2.setEnabled(true);
ima3.setEnabled(true);
ima4.setEnabled(true);
ima5.setEnabled(true);
ima6.setEnabled(true);
}
}

class Activa extends Thread{
public void run(){
try{
Thread.sleep(800);
if(b1.getIcon().equals(b2.getIcon())==true){
b2.setEnabled(false);
b1.setEnabled(false);
}
else{
b1.setIcon(x[3]);
b2.setIcon(x[3]);
}
contador2++;
}catch (Exception er){
}
}
}
public static class Aleatorios{
public static  int ar[][]=new int[6][3];
public static  void aleatorios(){

int p=3, a=0;

for(int i=0; i<p; i++){

for(int j=0; j<p; j++){

for(int k=0; k<p; k++){

if(i!=j && j!=k && i!=k){


ar[a][0]=i;
ar[a][1]=j;
ar[a++][2]=k;


}
}

}


}

}

public static int[] calcula(){
aleatorios();

int ar2[]=new int[6];
int x=(int)(Math.random()*6);
int y=(int)(Math.random()*6);

for(int m=0; m<ar2.length; m++){

if(m<3){

ar2[m]=ar[x][m];
}
else{
ar2[m]=ar[y][m-3];

}


}

return ar2;
}}

public static void main(String[] arg){
new memo().setVisible(true);
}}

4 comentarios:

  1. me lo podrías facilitar??

    ResponderEliminar
  2. hola amigo, discula, como puedo agregarle imagenes al memorama....ojala puedas ayudarme...gracias

    suastegui_dolls@hotmail.com

    ResponderEliminar
  3. Me lo puedes enviar??? porfa XD

    manu.hamster@gmail.com

    ResponderEliminar