Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pearlharbor;
- import javax.swing.JOptionPane;
- public class PearlHarbor
- {
- //variable de clase
- private int f;
- private int aviones;
- private int avionesjaponeses;
- private int barcos;
- private int contauAVx1 = 0;
- private int contauAVx2 = 0;
- private int contajAVx1 = 0;
- private int contajAVx2 = 0;
- private int contaBx1 = 0;
- private int contaSx1 = 0;
- private int contaSx2 = 0;
- private int disparos;
- private int x;
- private int y;
- private int submarinos;
- private char USA[][];
- private char japan[][];
- private int a1, a2, a3, a4, b1, b2, s1, s2;
- private int avionuder, avionuaver, avionjder, avionjaver, barcohun, barcoaver, submahun, submaaver;
- 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()
- {
- JOptionPane.showMessageDialog(null,"Querido usuario, se recomienda hacer una matriz de más de 10x10 para ver más acción..!!");
- 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("\tJapó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()
- {
- do{
- aviones = Integer.parseInt(JOptionPane.showInputDialog("Cuantos aviones estadounidenses quiere..?"));
- boolean ye=true;
- if(aviones>=2 && f<6){
- JOptionPane.showMessageDialog(null, "Son muchos aviones para esta matriz");
- }
- else{
- for(int i=0; i<aviones; i++)
- { //while
- a1 = (int)(int) (Math.random() * (f - 2)) + 1;
- a2 = (int)(int) (Math.random() * (f - 2)) + 1;
- if(USA[a1][a2]=='*'&&USA[a1-1][a2]=='*'&&USA[a1+1][a2]=='*'&&USA[a1][a2-1]=='*'&&USA[a1][a2+1]=='*')
- {
- System.out.println(a1+" "+a2);
- USA[a1][a2]= 'a';
- USA[a1-1][a2]= 'A';
- USA[a1+1][a2]= 'A';
- USA[a1][a2-1]= 'A';
- USA[a1][a2+1]= 'A';
- }
- else
- ye=false;
- }
- if(ye==false)
- {
- JOptionPane.showMessageDialog(null,"Lo siento, no pude colocar todos los aviones estadounidenses\n"
- + "Le va a tocar repetir cuanto armamento quiere para satisfacerlo");
- }
- 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 (aviones>=2 && f<6);
- do{
- avionesjaponeses = Integer.parseInt(JOptionPane.showInputDialog("Cuantos aviones japoneses quiere..?"));
- boolean av=true;
- if(avionesjaponeses>=2 && f<6){
- JOptionPane.showMessageDialog(null, "Son muchos aviones para esta matriz");
- }
- else{
- for(int i=0; i<avionesjaponeses; i++)
- { //while
- a3 = (int)(Math.random()*(f-2)+1);
- a4 = (int)(Math.random()*(f-2)+1);
- if(japan[a3][a4]=='*'&&japan[a3-1][a4]=='*'&&japan[a3+1][a4]=='*'&&japan[a3][a4-1]=='*'&&japan[a3][a4+1]=='*')
- {
- System.out.println(a3+" "+a4);
- japan[a3][a4]= 'a';
- japan[a3-1][a4]= 'A';
- japan[a3+1][a4]= 'A';
- japan[a3][a4-1]= 'A';
- japan[a3][a4+1]= 'A';
- }
- else
- av=false;
- }
- if(av==false)
- JOptionPane.showMessageDialog(null,"Lo siento, no pude colocar todos los aviones japoneses\n"
- + "Le va a tocar repetir cuanto armamento quiere para satisfacerlo");
- 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 (avionesjaponeses>=2 && f<6);
- }
- private void barcos()
- {
- do{
- barcos = Integer.parseInt(JOptionPane.showInputDialog("Cuantos barcos estadounidenses quiere..?"));
- boolean bar = true;
- if(barcos>=2 && f<6){
- JOptionPane.showMessageDialog(null, "Son muchos barcos para esta matriz");
- }
- else
- {
- for(int i=0; i<barcos; i++)
- { //while
- b1 = (int)(Math.random()*((f-3)/2)+1);
- b2 = (int)(Math.random()*(f-3)+1);
- if(USA[b1][b2]=='*'&&USA[b1][b2+1]=='*'&&USA[b1][b2+2]=='*'&&USA[b1+1][b2]=='*'&&USA[b1+1][b2+1]=='*'&&USA[b1+1][b2+2]=='*')
- {
- //hay que validar para que no se repitan los aleatorios
- System.out.println(b1+" "+b2);
- USA[b1][b2]= 'B';
- USA[b1][b2+1]= 'B';
- USA[b1][b2+2]= 'B';
- USA[b1+1][b2]= 'B';
- USA[b1+1][b2+1]= 'B';
- USA[b1+1][b2+2]= 'B';
- }
- else
- bar=false;
- }
- if(bar==false)
- {
- JOptionPane.showMessageDialog(null,"Lo siento, no pude colocar todos los barcos\n"
- + "Le va a tocar repetir cuanto armamento quiere para satisfacerlo");
- }
- 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(barcos>=2 && f<6);
- }
- private void submarinos()
- {
- do{
- submarinos = Integer.parseInt(JOptionPane.showInputDialog("Cuantos submarinos estadounidenses quiere..?"));
- boolean suw=true;
- if(submarinos>=3 && f<6){
- JOptionPane.showMessageDialog(null, "Son muchos submarinos para esta matriz");
- }else{
- for(int i=0; i<submarinos; i++)
- { //while
- s1 = (int)(Math.random()*((f-2)/2)+1);
- s2 = (int)(Math.random()*(f-2));
- if(USA[s1][s2]=='*'&&USA[s1][s2+1]=='*'&&USA[s1+1][s2]=='*'&&USA[s1-1][s2]=='*')
- {
- System.out.println(s1+" "+s2); //coloco esta linea para saber donde seran las posiciones
- USA[s1][s2]= 's';
- USA[s1][s2+1]= 'S';
- USA[s1+1][s2]= 'S';
- USA[s1-1][s2]= 'S';
- }
- else
- suw=false;
- }
- if(suw==false)
- JOptionPane.showMessageDialog(null,"Lo siento, no pude colocar todos los submarinos\n"
- + "Le va a tocar repetir cuanto armamento quiere para satisfacerlo");
- 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(submarinos>=2 && f<6);
- }
- private void batalla()
- {
- JOptionPane.showMessageDialog(null,"Se han presentado los siguientes ataques, dele aceptar para verlos!!");
- System.out.println();
- System.out.println("=====BATALLA=====");
- disparos=(f*5);
- for(int i=0;i<disparos;i++)
- {
- x=(int)(Math.random()*f);
- y=(int)(Math.random()*f);
- if(USA[x][y]=='A'&&USA[x][y]!='o')
- {
- USA[x][y]= 'x';
- contauAVx1=contauAVx1+40;
- }
- else
- {
- if(USA[x][y]=='a'&& USA[x][y]!='o')
- {
- USA[x][y]= 'X';
- contauAVx2=contauAVx2+200;
- }
- else
- {
- if(USA[x][y]=='*')
- {
- USA[x][y]= 'o';
- }
- }
- }
- if(japan[x][y]=='A')
- {
- japan[x][y]='x';
- contajAVx1=contajAVx1+60;
- }
- else
- {
- if(japan[x][y]=='a')
- {
- japan[x][y]='X';
- contajAVx2=contajAVx2+300;
- }
- else
- {
- if(japan[x][y]=='*')
- {
- japan[x][y]='o';
- }
- }
- }
- //Para los barcos
- if(USA[x][y]=='B')
- {
- USA[x][y]= 'x';
- contaBx1=contaBx1+40;
- }
- else
- {
- if(USA[x][y]=='*'){
- USA[x][y]= 'o';
- }
- }
- //Para los submarinos
- if(USA[x][y]=='S')
- {
- USA[x][y]= 'x';
- contaSx1=contaSx1+50;
- }
- else
- {
- if(USA[x][y]=='s')
- {
- USA[x][y]= 'X';
- contaSx2=contaSx2+200;
- }
- else
- {
- if(USA[x][y]=='*'){
- USA[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()
- {
- System.out.println();
- System.out.println("=====INFORME=====");
- //Aviones
- System.out.println("AVIONES");
- System.out.println("\t Estados Unidos ");
- System.out.println("\t Derribados: "+avionuder);
- System.out.println("\t Averiados: "+avionuaver);
- System.out.println();
- System.out.println("\t Japón ");
- System.out.println("\t Derribados: "+avionjder);
- System.out.println("\t Averiados: "+avionjaver);
- System.out.println();
- //Barcos
- System.out.println("BARCOS");
- System.out.println("\t Hundidos: "+barcohun);
- System.out.println("\t Averiados: "+barcoaver);
- System.out.println();
- //Submarinos
- System.out.println("Submarinos");
- System.out.println("\t Hundidos: "+submahun);
- System.out.println("\t Averiados: "+submaaver);
- System.out.println();
- }
- private void resultado()
- {
- int puntosUSA,puntosJAPAN;
- puntosUSA= contauAVx1+contauAVx2+contaBx1+contaSx1+contaSx2;
- puntosJAPAN=contajAVx1+contajAVx2;
- System.out.println();
- System.out.println("=====RESULTADOS=====");
- System.out.println();
- System.out.println("Los puntos de Estados Unidos son: "+puntosJAPAN);
- System.out.println("Los puntos de Japón son: "+puntosUSA);
- System.out.println();
- if(puntosUSA<puntosJAPAN)
- System.out.println("Estados Unidos ha ganado!!!!");
- else
- {
- if(puntosUSA>puntosJAPAN)
- System.out.println("Japón ha ganado!!!!");
- else
- System.out.println("¡Tenemos un empate!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement