Te damos la bienvenida a la comunidad de T!Estás a un paso de acceder al mejor contenido, creado por personas como vos.

O iniciá sesión con
¿No tenés una cuenta?
hola que tal users de taringa les traigo un post sobre el juego del buscaminas hecho por mi en java :3
el poyecto contiene cuatro clases y el metodo main

A continuacion les pondre las clases :
Esta es la clase mas importante de todas pues controlara casi todo en lo que respecta al juego y se llama ControladorMatriz.java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JMenuItem;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
import java.awt.Color;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;


public class ControlarMatriz implements ActionListener{
int x;
int y;
int [][]matriz;
VentanaMina ventana;
boolean gameover = false;
int score;
int a[];
int contador;
String nombre;

//aqui va lo del hashmap
HashMap<Integer, String> scores;
//hashmap
public ControlarMatriz() {

ventana = new VentanaMina();
a=new int[ 10 ];
contador=0;
//hashmap
scores = new HashMap<Integer, String>();
//hashmap
JMenuItem nuevojuego = ventana.getNuevoJuego();
nuevojuego.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
inicializar();
}
});
inicializar();
JMenuItem Score=ventana.getScoreFrame();
Score.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
Puntuacion p=new Puntuacion();
//p.setPuntuacion(a,contador);
//hashmap
p.setPuntuacion(scores);
//hashmap
}
});
JMenuItem ayuda=ventana.getAyuda();
ayuda.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
Ayuda a=new Ayuda();

}
});


}

public void muestra(){
for(x=0;x<10;x++){
for(y=0;y<10;y++){
System.out.print(matriz[x][y]);
}
System.out.println();
}

}

public void inicializar(){
gameover = false;
matriz = new int [ 10 ][ 10 ];
score =0;


for(x=0;x<10;x++){
for(y=0;y<10;y++){
matriz[x][y]=0;
}
}
int i1, j1;
for ( int i=0;i<10;i++){
do{
i1=(int)(Math.random()*10);
j1=(int)(Math.random()*10);
}while(matriz[i1][j1]!=0);
matriz[i1][j1]=9;
PonerNumero(i1,j1);
}
ventana.reiniciar();
addListener();
System.out.println("scores"+a.toString());
}
public void PonerNumero(int x, int y){
for (int f2=Math.max(0, x-1);f2 < Math.min(10,x+2);f2++){
for (int c2=Math.max(0,y-1);c2 < Math.min(10,y+2);c2++){
if (matriz[f2][c2]!=9){
matriz[f2][c2]++;
}
}
}

}

public void click(int x, int y){
JButton jtb = ventana.getBoton(x, y);

if (!jtb.isSelected()){
jtb.setSelected(true);
score += 10;
ventana.setScore(score);
if(matriz[x][y]==9){
jtb.setIcon(ventana.iconoFeliz);
gameover = true;
MostraMina();
if(contador < 10){
a[contador] = score;
contador ++;
}
else
{
contador = 0;
a[contador] = score;
}

nombre = ventana.gameover();
//hashmap
scores.put(score,nombre);
//hashmap
}
else if (matriz[x][y]==0)


{
jtb.setBackground(java.awt.Color.DARK_GRAY);



}
else
{
jtb.setBackground(java.awt.Color.PINK);
jtb.setText("" + matriz[x][y]);
}


if (matriz[x][y]==0){
for (int f2=Math.max(0, x-1);f2<Math.min(10,x+2);f2++){
for (int c2=Math.max(0,y-1);c2<Math.min(10,y+2);c2++){
click(f2, c2);
}
}
}
}
}

public void addListener(){
for (int i = 0; i < 10; i++){
for(int j=0;j<10;j++){
ventana.getBoton(i, j).addActionListener(this);
}
}
}

@Override
public void actionPerformed(ActionEvent e) {
if (!gameover){
System.out.println(e.getActionCommand());
int boton=Integer.parseInt(e.getActionCommand());
int i=boton/10;
int j=boton%10;
click(i,j);
}
}
public void MostraMina(){
for(int x=0;x<10;x++){
for(int y=0;y<10;y++){
JButton jtb = ventana.getBoton(x, y);
if(matriz[x][y]==9){
jtb.setIcon(ventana.iconoFeliz);

}
}
}

}
}

La segunda clase seria VentanaMina que se encargara de la interfaz como los botones , el menu etc
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import java.awt.GridLayout;
import java.awt.BorderLayout;
public class VentanaMina extends JFrame{
private JPanel panel3;

private JPanel panel;
private JPanel panel2;
private JLabel buscaminas;
private JButton [][] boton;
ImageIcon iconoFeliz=new ImageIcon(getClass().getResource("soul.png" ) );
ImageIcon iconoSalir=new ImageIcon(getClass().getResource("exit.png" ) );
ImageIcon iconoAyuda= new ImageIcon(getClass().getResource("ayuda.png" ) );
ImageIcon iconoMina=new ImageIcon(getClass().getResource("Mina.png" ) );
JMenuItem NuevoJuego= new JMenuItem("Nuevo Juego",iconoFeliz);
JMenuItem ayuda;
JMenuItem Score;
private JTextField nombre;
public VentanaMina(){


JMenuBar menubar=new JMenuBar();

JMenu Archivo=new JMenu( " Archivo " );
Archivo.setMnemonic(KeyEvent.VK_A );
JMenu Ayuda=new JMenu( " Ayuda " );
Ayuda.setMnemonic(KeyEvent.VK_S);
JMenu Scor=new JMenu( " Score " );
ayuda=new JMenuItem("Reglas Del Buscaminas",iconoAyuda);
buscaminas = new JLabel( " puntos: 0 " );
Score=new JMenuItem( " Score " ,iconoMina );
Score.setMnemonic(KeyEvent.VK_G);
NuevoJuego.setMnemonic(KeyEvent.VK_N);
JMenuItem Salir = new JMenuItem( "Salir Del Juego " ,iconoSalir ) ;
Salir.setMnemonic(KeyEvent.VK_O);
Salir.setToolTipText( " Salir Del Juego ._.? Seguro " );

ayuda.setMnemonic(KeyEvent.VK_P);
Salir.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Y, ActionEvent.CTRL_MASK));
Salir.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){


System.exit(0);
}
});

Archivo.add(NuevoJuego);
Archivo.add(Salir);
menubar.add(Archivo);
menubar.add(Scor);
Scor.add(Score);
Ayuda.add(ayuda);
setJMenuBar(menubar);
menubar.add(Ayuda);
boton=new JButton[ 10 ][ 10 ];
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++){
boton[j]=new JButton( "" );
boton[j].setActionCommand(""+(i* 10 +j));
}
}
setLayout(new BorderLayout());
panel=new JPanel();
panel2=new JPanel();
panel2.setLayout(new BorderLayout());
panel.setLayout(new GridLayout(10,10));
for (int i = 0; i < 10; i++){
for(int j=0;j<10;j++){

panel.add(boton[j]);
}

}
panel2.add(buscaminas);
add(panel2, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
setSize(600, 600);
setVisible(true);
setTitle( " Buscaminas " );
}

public JButton getBoton(int x, int y){
return boton[x][y];
}
public void icono(JButton boton){
boton.setIcon(iconoFeliz);
}

public String gameover(){
JOptionPane.showMessageDialog(null, "GAME OVER " );
String nombre =JOptionPane.showInputDialog("ingrese su nombre " );
System.out.println(nombre);
return nombre;

}

public JMenuItem getNuevoJuego(){
return NuevoJuego;
}

public void reiniciar(){
for (int i = 0; i < 10; i++){
for(int j=0;j<10;j++){
boton[j].setText( " );
boton[j].setBackground(null);
boton[j].setSelected(false);
boton[j].setIcon(null);
}
}
}
public void setScore(int score){
buscaminas.setText("puntos:" + score);
}
public JMenuItem getScoreFrame(){
return Score;
}



public JMenuItem getAyuda(){
return ayuda;
}
}

La siguiente clase seria Ayuda que no es muy complicada
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;

public class Ayuda extends JFrame {

private JPanel panel;
private JLabel etiqueta1;
private JLabel etiqueta2;
private JLabel etiqueta3;
private JLabel etiqueta4;
private JLabel etiqueta;
private JLabel etiqueta5;
private JLabel etiqueta6;
public Ayuda(){
super("Reglas Del Buscaminas" );

etiqueta=new JLabel("1._ El jugador tendrá que desbloquear las casillas haciendo click con el mouse." );
etiqueta.setBounds(10,10,700,20);
this.add(etiqueta);
etiqueta1=new JLabel("2._ Si el jugador se encuentra con una mina y hace clic en ella,pierde automaticamente" );
etiqueta1.setBounds(10,70,700,20);
this.add(etiqueta1);
etiqueta2=new JLabel("3._Por cada casilla que desbloque el jugador le marcara el puntuaje acumulado." );
etiqueta2.setBounds(10,130,700,20);
this.add(etiqueta2);
etiqueta3=new JLabel("4._Al desbloquear las casillas algunas estaran vacias pero otras tendran un numero ese numero significa el numeero de minas que hay alrededor de esa casilla." );
etiqueta3.setBounds(10,190,1000,20);
this.add(etiqueta3);
etiqueta=new JLabel("5._En el caso del que el jugador llege a desbloquear todas las casillas evitando las minas, gana el juego." );
etiqueta.setBounds(10,250,700,20);
this.add(etiqueta);
etiqueta5=new JLabel("6._El juego solo tiene un nivel, y tiene la matriz de 10x10, son 100 casillas en total a desbloquear" );
etiqueta5.setBounds(10,300,800,20);
this.add(etiqueta5);
etiqueta6=new JLabel("El juego solo tiene un nivel, y tiene la matriz de 10x10, son 100 casillas en total a desbloquear. " );
etiqueta6.setBounds(10,250,800,20);
this.add(etiqueta6);


panel = new JPanel();
setVisible(true);
setSize(1000,400);
add(panel);
}
}

y la ultima clase seria Puntuacion que se encarga de llevar el puntaje acumulado en el juego
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class Puntuacion extends JFrame{
private JPanel panel;

public Puntuacion(){


panel = new JPanel();
panel.setLayout(new GridLayout(10,1));
setVisible(true);
setSize(300,300);



}
public void setPuntuacion(int[] punto,int contador){



int[] array = new int[contador];
for(int x=0;x<contador;x++)
{
array[x] = punto[x];
}
Arrays.sort(array);
for(int x=0;x<contador;x++)
{
panel.add(new JLabel((x+1)+"._ "+array[x]));


add(panel);
}
}
//hashmap
public void setPuntuacion(HashMap map){
Set<Integer> set = map.keySet();
List<Integer> list = new ArrayList<Integer>();
Iterator i = set.iterator();
while(i.hasNext()){
list.add((Integer)i.next());
}
Collections.sort(list);
i = list.iterator();
while(i.hasNext()){
Integer numero = (Integer)i.next();
String nombre = (String)map.get(numero);
panel.add(new JLabel(nombre+":"+numero));
add(panel);
}
}
//hashmap
}



por ultimo viene el metodo main que lo llame BuscaminasApp
import javax.swing.SwingUtilities;


public class BuscaMinasApp {

public static void main(String[]args){


ControlarMatriz control=new ControlarMatriz();


}


}



las minas son de soul eater jajajaaja e aqui unas capturas de pantallas




espero que les halla gustado si tienen alguna duda no tengo problemas en responder cualquier duda sera bienvenida espero que les halla gustado el post :3