Laberinto en Java Swing

DESCARGAR


public class generarTablero {

public static void main(String[] args) {
// TODO Auto-generated method stub
int n = (int)(Math.random()*8);
n=15
;
System.out.println(n);
String[][] tablero = new String[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int a=(int)(Math.random()*2);
if (a==0||a==5||a==8)
                    tablero[i][j]="0";
                else
                    tablero[i][j]="1";
}
}
tablero[0][0]="0";
tablero[n-1][n-1]="0";
tablero[(int)(Math.random()*(n-1))][(int)(Math.random()*(n-1))]="*";
for (int i = 0; i < tablero.length; i++) {
for (int j = 0; j < tablero.length; j++) {
System.out.print(tablero[i][j]);
}
System.out.println();
}
}

}






import java.util.Scanner;

public class Laberinto {
private static boolean solucion = false;

private static int contadorSolucion = 0;
private static int contadorPremio = 0;

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner lee = new Scanner(System.in);
int n = lee.nextInt();
String[][] tablero = new String[n][n];
for (int i = 0; i < n; i++) {
String lineas = lee.next();
for (int j = 0; j < n; j++) {
tablero[i][j]=lineas.substring(j, j+1);
}
}
lee.close();

recorrer(tablero, 0 , 0, solucion);
if(triviales(tablero)){
System.out.println("NO");
}else{
if(contadorPremio>0){
System.out.println("SI, CON PREMIO");
}else{
if(contadorSolucion>0){
System.out.println("SI, SIN PREMIO");
}else{
System.out.println("NO");
}
}
}

}

//relcursion, todos los caminos posibles

private static void recorrer(String[][] laberinto, int fil, int col, boolean haySolucion) {
// TODO Auto-generated method stub
int n = laberinto.length-1;
if(fil==n && col==n && (laberinto[fil][col].equals("0")||laberinto[fil][col].equals("*"))){
if(haySolucion){
// System.out.println("SI, CON PREMIO");
contadorPremio++;
}else{
// System.out.println("SI, SIN PREMIO");
contadorSolucion++;
}

}
else{
if(laberinto[fil][col].equals("*")){
haySolucion=true;
laberinto[fil][col]="B";
}else{
laberinto[fil][col]="x";
}




boolean arriba = (fil-1)>=0 && (fil-1)<=n;
boolean abajo = (fil+1)<=n && (fil+1)>=0;
boolean derecha = (col+1)<=n && (col+1)>=0;
boolean izquierda = (col-1)>=0 && (col-1)<=n;
boolean arribaizq = arriba && izquierda;
boolean arribader = arriba && derecha;
boolean abajoizq = abajo && izquierda;
boolean abajoder = abajo && derecha;

boolean hayArriba = arriba && (laberinto[fil-1][col].equals("0")||laberinto[fil-1][col].equals("*"));
boolean hayAbajo = abajo && (laberinto[fil+1][col].equals("0")||laberinto[fil+1][col].equals("*"));
boolean hayDerecha = derecha && (laberinto[fil][col+1].equals("0")||laberinto[fil][col+1].equals("*"));
boolean hayIzquierda = izquierda && (laberinto[fil][col-1].equals("0")||laberinto[fil][col-1].equals("*"));
boolean hayArribaizq = arribaizq && (laberinto[fil-1][col-1].equals("0")||laberinto[fil-1][col-1].equals("*"));
boolean hayArribader = arribader && (laberinto[fil-1][col+1].equals("0")||laberinto[fil-1][col+1].equals("*"));
boolean hayAbajoizq = abajoizq && (laberinto[fil+1][col-1].equals("0")||laberinto[fil+1][col-1].equals("*"));
boolean hayAbajoder = abajoder && (laberinto[fil+1][col+1].equals("0")||laberinto[fil+1][col+1].equals("*"));

//va buscando en orden, y si hay la posibilidad de desplazarse en esa direccion, se mueve hacia ella
if(hayArriba){
recorrer( laberinto, fil-1, col, haySolucion);
}
if(hayAbajo){
recorrer( laberinto, fil+1, col, haySolucion);
}
if(hayDerecha){
recorrer( laberinto, fil, col+1, haySolucion);
}
if(hayIzquierda){
recorrer( laberinto, fil, col-1, haySolucion);
}
if(hayArribaizq){
recorrer( laberinto, fil-1, col-1, haySolucion);
}
if(hayArribader){
recorrer( laberinto, fil-1, col+1, haySolucion);
}
if(hayAbajoizq){
recorrer( laberinto, fil+1, col-1, haySolucion);
}
if(hayAbajoder){
recorrer( laberinto, fil+1, col+1, haySolucion);
}
if(laberinto[fil][col].equals("B")){
laberinto[fil][col]="*";
}else{
laberinto[fil][col]="0";
}

haySolucion=false;
// System.out.println("no");
}
}
//casos en los que siempre va a ser no
private static boolean triviales(String[][] tablero) {
// TODO Auto-generated method stub
if(tablero[0][0].equals("1")){
return true;
}else if(tablero[tablero.length-1][tablero.length-1].equals("1")){
return true;
}else{
for (int i = 0; i < tablero.length; i++) {
int contador = 0;
for (int j = 0; j < tablero.length; j++) {
if(tablero[i][j].equals("1")){
contador++;
}
}
if(contador==tablero.length){
return true;
}
}
for (int j = 0; j < tablero.length; j++) {
int contador = 0;
for (int i = 0; i < tablero.length; i++) {
if(tablero[i][j].equals("1")){
contador++;
}
}
if(contador==tablero.length){
return true;
}
}
}
return false;
}

}