Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pearl.harbor;
- import javax.swing.JOptionPane;
- public class PearlHarbor
- {
- //variable de clase
- private int f;
- private char USA[][];
- char japan[][];
- public static void main(String[]x)
- {
- PearlHarbor pearlHarbor = new PearlHarbor();
- }
- public PearlHarbor()
- {
- this.todero();
- }
- private void todero()
- {
- //Variable para la opcion del usuario
- int opcion;
- JOptionPane.showMessageDialog(null, "Bienvenido al juego: 'La batalla del Pearl Harbor'\n"+"¡Difrútalo!");
- do
- {
- //Listado y captura
- opcion=Integer.parseInt(JOptionPane.showInputDialog("====OPCIONES==== \n"+
- "1. Configuración \n"+
- "2. Armamento \n"+
- "3. Simular Batalla \n"+
- "4. Ver informe \n"+
- "5. Resultados \n"+
- "0. SALIR \n"
- ));
- switch(opcion)
- {
- case 1:
- this.configuracion();
- break;
- case 2:
- this.armamento();
- break;
- case 3:
- this.batalla();
- break;
- case 4:
- this.informe();
- break;
- case 5:
- this.resultado();
- break;
- case 0:
- JOptionPane.showMessageDialog(null,"Gracias, por participar");
- System.exit(0);
- break;
- default:
- JOptionPane.showMessageDialog(null,"ESA OPCIÓN NO EXISTE");
- break;
- }
- }
- while(opcion!=0);
- }
- private void configuracion()
- {
- do{
- f=Integer.parseInt(JOptionPane.showInputDialog("Digite el número de filas"));
- USA= new char[f][f];
- japan= new char[f][f];
- if(f<5 || f>20){
- JOptionPane.showMessageDialog(null, "No puedo hacer este juego con estas dimensiones...");
- }
- else
- {
- System.out.print("Estados Unidos");
- for (int i = 0; i < f-7; i++) {
- System.out.print(" ");
- }
- System.out.print("\t \t Japón");
- System.out.println();
- for(int fila=0;fila<f;fila++)
- {
- for(int colu=0;colu<f;colu++)
- {
- USA[fila][colu]='*';
- japan[fila][colu]='*';
- }
- }
- for(int fila=0;fila<f;fila++)
- {
- for(int colu=0;colu<f;colu++)
- {
- System.out.print(USA[fila][colu]+" ");
- }
- System.out.print("\t");
- for(int colu=0;colu<f;colu++)
- {
- System.out.print(japan[fila][colu]+" ");
- }
- System.out.println();
- }
- System.out.println();
- }
- }while (f<5 || f>20);
- }
- private void armamento()
- {
- int opcion;
- do{
- opcion=Integer.parseInt(JOptionPane.showInputDialog("Elige que armamento configurar: \n"+
- "1. Aviones \n"+
- "2. Barcos \n"+
- "3. Submarinos \n"+
- "0. REGRESAR \n"
- ));
- switch(opcion)
- {
- case 1:
- this.aviones();
- break;
- case 2:
- this.barcos();
- break;
- case 3:
- this.submarinos();
- break;
- case 0:
- break;
- default:
- JOptionPane.showMessageDialog(null,"ESA OPCIÓN NO EXISTE");
- break;
- }
- }while(opcion!=0);
- }
- private void aviones()
- {
- //AVIONES
- int avionesu,avionesj,a, b,c,d;
- avionesu = Integer.parseInt(JOptionPane.showInputDialog("Cuantos aviones estadounidenses quiere..?"));
- for(int i=0; i<avionesu; i++)
- {
- do{
- a = (int)(Math.random()*(f-1)+1);
- b = (int)(Math.random()*(f-1)+1);
- //hay que validar para que no se repitan los aleatorios (Esto aun no lo he hecho)
- //que los aleatorios ademas esten en el rango donde pueda generar aviones (ya está hecho)
- System.out.println(a+" "+b); //coloco esta linea para saber donde seran las posiciones
- USA[a][b]= '1';
- USA[a-1][b]= '2';
- USA[a+1][b]= '3';
- USA[a][b-1]= '4';
- USA[a][b+1]= '5';
- //los numeros al final los quitaré, son para guiarnos en donde aparecen.
- }while(USA[a][b]=='*'&&USA[a-1][b]=='*'&&USA[a+1][b]=='*'&&USA[a][b-1]=='*'&&USA[a][b+1]=='*');
- }
- avionesj = Integer.parseInt(JOptionPane.showInputDialog("Cuantos aviones japoneses quiere..?"));
- for(int i=0; i<avionesj; i++)
- {
- do{
- c = (int)(Math.random()*(f-1)+1);
- d = (int)(Math.random()*(f-1)+1);
- //hay que validar para que no se repitan los aleatorios
- //que los aleatorios ademas esten en el rango donde pueda generar aviones
- System.out.println(c+" "+d); //coloco esta linea para saber donde seran las posiciones
- japan[c][d]= '1';
- japan[c-1][d]= '2';
- japan[c+1][d]= '3';
- japan[c][d-1]= '4';
- japan[c][d+1]= '5';
- }while(japan[c][d]=='*'&&japan[c-1][d]=='*'&&japan[c+1][d]=='*'&&japan[c][d-1]=='*'&&japan[c][d+1]=='*');
- }
- for(int fila=0;fila<f;fila++)
- {
- for(int colu=0;colu<f;colu++)
- {
- System.out.print(USA[fila][colu]+" ");
- }
- System.out.print("\t");
- for(int colu=0;colu<f;colu++)
- {
- System.out.print(japan[fila][colu]+" ");
- }
- System.out.println();
- }
- System.out.println();
- }
- private void barcos()
- {
- //BARCOS
- int barcos;
- barcos = Integer.parseInt(JOptionPane.showInputDialog("Cuantos barcos quiere..?"));
- int ab1, ab2;
- for(int i=0; i<barcos; i++)
- {
- do{
- ab1 = (int)(Math.random()*(f/2)); //variar esto para empezar a variar desde que punto salen los objetos
- ab2 = (int)(Math.random()*f);
- //hay que validar para que no se repitan los aleatorios
- System.out.println(ab1+" "+ab2);
- USA[ab1][ab2]= '1';
- USA[ab1][ab2+1]= '2';
- USA[ab1][ab2+2]= '3';
- USA[ab1+1][ab2]= '4';
- USA[ab1+1][ab2+1]= '5';
- USA[ab1+1][ab2+2]= '6';
- }while(USA[ab1][ab2]=='*'&&USA[ab1][ab2+1]=='*'&&USA[ab1][ab2+2]=='*'&&USA[ab1-1][ab2]=='*'&&USA[ab1-1][ab2+1]=='*'&&USA[ab1-1][ab2+2]=='*');
- }
- for(int fila=0;fila<f;fila++)
- {
- for(int colu=0;colu<f;colu++)
- {
- System.out.print(USA[fila][colu]+" ");
- }
- System.out.print("\t");
- for(int colu=0;colu<f;colu++)
- {
- System.out.print(japan[fila][colu]+" ");
- }
- System.out.println();
- }
- System.out.println();
- }
- private void submarinos()
- {
- //SUBMARINOS
- int submarinos;
- submarinos = Integer.parseInt(JOptionPane.showInputDialog("Cuantos submarinos quiere..?"));
- int s1, s2;
- for(int i=0; i<submarinos; i++)
- {
- do{
- s1 = (int)(Math.random()*(f-1)+1);
- s2 = (int)(Math.random()*(f-1));
- //hay que validar para que no se repitan los aleatorios
- //que los aleatorios ademas esten en el rango donde pueda generar aviones
- System.out.println(s1+" "+s2); //coloco esta linea para saber donde seran las posiciones
- USA[s1][s2]= '1';
- USA[s1][s2+1]= '2';
- USA[s1+1][s2]= '3';
- USA[s1-1][s2]= '4';
- }while(USA[s1][s2]=='*'&&USA[s1][s2+1]=='*'&&USA[s1+1][s2]=='*'&&USA[s1-1][s2]=='*');
- }
- for(int fila=0;fila<f;fila++)
- {
- for(int colu=0;colu<f;colu++)
- {
- System.out.print(USA[fila][colu]+" ");
- }
- System.out.print("\t");
- for(int colu=0;colu<f;colu++)
- {
- System.out.print(japan[fila][colu]+" ");
- }
- System.out.println();
- }
- System.out.println();
- }
- private void batalla()
- {
- int disparos,x,y;
- /*El numero de disparos varia de acuerdo al tamaño de la matriz
- para matriz 5x5=25
- matriz 6x6=30
- 7x7=35
- va a ir aumentando de 5 en 5
- */
- disparos=f*5;
- for(int i=0;i<disparos;i++)
- {
- x=(int)(Math.random()*f);
- y=(int)(Math.random()*f);
- //falta validar para que no se repitan los numeros aleatorios.
- if(USA[x][y]=='2'||USA[x][y]=='3'||USA[x][y]=='4'||USA[x][y]=='5')
- {
- USA[x][y]= 'x';
- }
- else
- {
- if(USA[x][y]=='1')
- {
- USA[x][y]= 'X';
- }
- else
- {
- USA[x][y]= 'o';
- }
- }
- if(japan[x][y]=='2'||japan[x][y]=='3'||japan[x][y]=='4'||japan[x][y]=='5')
- {
- japan[x][y]='x';
- }
- else
- {
- if(japan[x][y]=='1')
- {
- japan[x][y]='X';
- }
- else
- japan[x][y]='o';
- }
- }
- for(int fila=0;fila<f;fila++)
- {
- for(int colu=0;colu<f;colu++)
- {
- System.out.print(USA[fila][colu]+" ");
- }
- System.out.print("\t");
- for(int colu=0;colu<f;colu++)
- {
- System.out.print(japan[fila][colu]+" ");
- }
- System.out.println();
- }
- System.out.println();
- }
- private void informe()
- {
- }
- private void resultado()
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement