You are on page 1of 4

import java.awt.

*;
import java.awt.event.*;

import java.io.IOException;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

public class batallaNaval{


public static void main(String[] args) throws IOException {

int puntero = 12;


JFrame f = new JFrame();

PanelCombate panel = new PanelCombate();


f.add(panel);

f.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent we) {


System.exit(0);
}
});

f.setTitle("Combate Naval ");


f.setCursor(puntero);

f.setSize(450, 450);
f.show();

}
}
class PanelCombate extends Panel {

Casilla tablero[][];
boolean naves [][];
boolean maquina[][];
int pos, disparos, fallos,aciertos = 0;

PanelCombate() {
int f, c;
setLayout(new GridLayout(11, 11));
tablero = new Casilla[11][11];
naves = new boolean[11][11];
pos = seleccion();

for (f = 0; f < 11; f++) {


for (c = 0; c < 11; c++) {
naves[f][c] = false;
tablero[f][c] = new Casilla("");
add(tablero[f][c]);
tablero[f][c].addActionListener(new AccionBoton(this, c, f));
}
}
for (int i = 1; i <= 3; i++) {
int tipo = (int) (Math.random() * 2);
f = (int) (Math.random() * 11);
c = (int) (Math.random() * 11);
switch (i) {
case 1:
try{
if (tipo > 2) {
if (f == 11) {
naves[f - 1][c] = true;
} else {
naves[f + 1][c] = true;
}
} else {
if (c == 11) {
naves[f][c - 1] = true;
} else {
naves[f][c + 1] = true;
}
}}
catch(ArrayIndexOutOfBoundsException q){

}
break;
case 2:
try{
if (tipo > 2) {
if (f == 9) {
naves[f - 1][c] = true;
naves[f - 2][c] = true;
naves[f - 3][c] = true;
} else {
naves[f + 1][c] = true;
naves[f + 2][c] = true;
naves[f + 3][c] = true;
}
} else {
if (c == 9) {
naves[f][c - 1] = true;
naves[f][c - 2] = true;
naves[f][c - 3] = true;
} else {
naves[f][c + 1] = true;
naves[f][c + 2] = true;
naves[f][c + 3] = true;
}
}
break;
}
catch(ArrayIndexOutOfBoundsException w){

}
case 3:
try{
if (f == 9) {
naves[f - 1][c] = true;
naves[f - 1][c + 1] = true;
} else {
naves[f + 1][c] = true;
naves[f + 1][c - 1] = true;
}
break;
}
catch(ArrayIndexOutOfBoundsException q){

}
}

naves[f][c] = true;
}
}
private int seleccion() {

int a = 9;
return a;
}

class Casilla extends Button {

boolean aciertos;

Casilla(String t) {
super.setLabel("");
setBackground(new Color(250, 210, 250));
aciertos = false;
}
}

class AccionBoton implements ActionListener {

PanelCombate pc;
int vert, hori, cant = 0;

AccionBoton(PanelCombate p, int v, int h) {


pc = p;
vert = v;
hori = h;
}

public void actionPerformed(ActionEvent ae) {


boolean salida = false;

if (!pc.tablero[hori][vert].aciertos) {
pc.tablero[hori][vert].aciertos = true;
pc.tablero[hori][vert].setBackground(new Color(0));
pc.disparos++;
if (pc.naves[hori][vert]) {
pc.tablero[vert][hori].setLabel("");
for (int x = 0; x < 11; x++) {
for (int y = 0; y < 11; y++) {
if (pc.naves[x][y]) {
if (x == hori && y == vert) {
pc.tablero[x][y].setBackground(new
Color(0,0,210));
pc.tablero[x][y].setLabel("");
pc.aciertos++;
}
}
}
}

}
pc.fallos = disparos - aciertos;
if (pc.aciertos >=9){
JOptionPane.showMessageDialog(pc.getParent(), "Ganaste");
System.exit(0);
}
if ((pc.disparos >= 6 && pc.aciertos == 0) || (pc.disparos >= 24))
{

JOptionPane.showMessageDialog(pc.getParent(), "Se te han


acabado los disparos");
JOptionPane.showMessageDialog(pc.getParent(), "Obtuviste
"+aciertos+" aciertos y " +fallos+ " fallas " + " de un total de " +disparos+ "
disparos ");
if(JOptionPane.showConfirmDialog(pc.getParent(), "Desea
Reiniciar el juego","Aviso", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION){
if(salida != true){
System.exit(0);
}else{

}
}
}

}
}
}
}

You might also like