Esta clase es de un juego de Batalla Naval que escribí. Espero te de una idea:
import java.awt.Component;
import java.awt.event.*;
import java.util.*;
class Tablero extends Component {
static final int HORIZONTAL = 0;
static final int VERTICAL = 1;
static final int DRAGAMINAS = 2;
static final int DESTRUCTOR = 3;
static final int ACORAZADO = 4;
static final int PORTAAVIONES = 5;
static final int TIRO_ERROR = 0;
static final int TIRO_ACIERTO = 1;
static final int TIRO_NULO = 2;
static final char VACIO = 'V';
static final char NAVE = 'N';
static final char ACIERTO = 'A';
static final char ERROR = 'E';
private static String[] nombre_nave = {"Dragaminas",
"Destructor",
"Acorazado",
"Portaaviones"};
private char[][] tablero;
private int[][] posicion_flota;
private ArrayList flota;
private int fils,cols;
private int ind_nave;
private int restan_naves;
class Nave {
String id;
int longitud;
Nave() {
id = "";
longitud = 0;
}
Nave(int i,int l) {
id = new String(nombre_nave[i-2]);
longitud = l;
}
}
Tablero(int m,int n) {
fils = m;
cols = n;
ind_nave = 0;
tablero = new char[m][n];
posicion_flota = new int[m][n];
flota = new ArrayList();
inicia_tablero();
}
public void inicia_tablero() {
for(int i=0; i<fils; i++) {
for(int j=0; j<cols; j++)
tablero[i][j] = 'V';
for(int k=0; k<cols; k++)
posicion_flota[i][k] = -1;
}
}
public boolean despliega_nave(int tipo,int xp,int yp,int h) {
if(xp < 1 || yp < 1) return false;
if(h == HORIZONTAL) {
if(yp<=fils && xp+tipo-1 <= cols) {
for(int i=0; i<tipo; i++)
if(tablero[yp-1][xp+i-1] != 'V')
return false;
for(int j=0; j<tipo; j++) {
tablero[yp-1][xp+j-1] = 'N';
posicion_flota[yp-1][xp+j-1] = ind_nave;
}
flota.add(new Nave(tipo,tipo));
ind_nave++;
restan_naves++;
}
else
return false;
}
else {
if(xp <= cols && yp+tipo-1 <= fils) {
for(int i=0; i<tipo; i++)
if(tablero[yp+i-1][xp-1] != 'V')
return false;
for(int j=0; j<tipo; j++) {
tablero[yp+j-1][xp-1] = 'N';
posicion_flota[yp+j-1][xp-1] = ind_nave;
}
flota.add(new Nave(tipo,tipo));
ind_nave++;
restan_naves++;
}
else
return false;
}
return true;
}
private boolean disparo_acertado(int x,int y) {
if(tablero[y-1][x-1] == 'N')
return true;
else
return false;
}
private boolean disparo_al_agua(int x,int y) {
if(tablero[y-1][x-1] == 'V')
return true;
else
return false;
}
public int disparo(int a,int b) {
if((a>=1 && a<=cols) && (b>=1 && b<=fils)) {
if(disparo_acertado(a,b)) {
tablero[b-1][a-1] = 'A';
((Nave)flota.get(posicion_flota[b-1][a-1])).longitud--;
if(((Nave)flota.get(posicion_flota[b-1][a-1])).longitud == 0)
restan_naves--;
return TIRO_ACIERTO;
}
else if(disparo_al_agua(a,b)) {
tablero[b-1][a-1] = 'E';
return TIRO_ERROR;
}
else
return TIRO_NULO;
}
return TIRO_NULO;
}
public static String tipoNave(int n) {
if(n > 1 && n < nombre_nave.length+2)
return new String(nombre_nave[n-2]);
else
return new String("");
}
public String naveAtacada(int a,int b) {
if(tablero[b-1][a-1] == 'A' || tablero[b-1][a-1] == 'N')
return ((Nave)flota.get(posicion_flota[b-1][a-1])).id;
else
return new String("");
}
public boolean naveHundida(int a,int b) {
if(tablero[b-1][a-1] == 'A' || tablero[b-1][a-1] == 'N')
if(((Nave)flota.get(posicion_flota[b-1][a-1])).longitud == 0)
return true;
return false;
}
public char status_casilla(int a,int b) {
return tablero[b-1][a-1];
}
public int num_filas() {
return fils;
}
public int num_columnas() {
return cols;
}
public boolean flota_hundida() {
return(restan_naves <= 0);
}
public int obtener_resto() {
return restan_naves;
}
public void display() {
for(int i=0; i<fils; i++) {
for(int j=0; j<cols; j++)
System.out.print(tablero[i][j]+" ");
System.out.println();
}
System.out.println();
}
}