package logica;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author cursos
 */
public class HundirLaFlota {

    public final static int FACIL = 0;
    public final static int MEDIO = 1;
    public final static int DIFICIL = 2;

    public final static int GANADO = 0;
    public final static int PERDIDO = 1;
    public final static int AGUA = 2;
    public final static int TOCADO = 3;
    public final static int HUNDIDO = 4;

    private int barcos1;
    private int barcos2;
    private int barcos3;
    private int barcos4;
    private int barcos5;
    private String nombre;
    private int dificultad;
    private int numeroIntentos;
    private int dimTablero;
    private int[][] barcos;
    int filas;
    int columnas;
    int tipos;
    int x;
    int y;
    private int ultimoResultado;
    private String texto;
    private String celda;

    public HundirLaFlota(String nombre, int dificultad, int filas, int columnas) {
        this.celda = "";
        this.tipos = 0;
        this.filas = filas;
        this.columnas = columnas;
        this.barcos = new int[filas][columnas];
        inicializa(nombre, dificultad);
    }

    public final void inicializa(String nombre, int dificultad) {
        this.nombre = nombre;
        this.dificultad = dificultad;
        calculaIntentos();
        obtenerBarcos(this.filas, this.columnas);
        texto = "Tienes " + numeroIntentos + " intentos";
    }

    public String getBarcos() {
        for (int i=0;i<filas;i++){
             for (int j=0;j<columnas;j++){
                 celda = celda + barcos[i][j];
             }
        }
        
        return celda;
    }

    public String getMesage() {
        return texto;
    }

    private void calculaIntentos() {
        switch (dificultad) {
            case DIFICIL:
                numeroIntentos = 40;
                break;
            case MEDIO:
                numeroIntentos = 50;
                break;
            case FACIL:
                numeroIntentos = 70;
                break;
        }
    }

    public int comprobarDisparo(int pos) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public void obtenerBarcos(int filas, int columnas) {
      
        int xy = 0; // 0 de izquierda / derecha; 1 de arriba / abajo. 
        int tipoBarco;
        int sw = -1;

        switch (dificultad) {
            case FACIL:
                barcos1 = 4;
                barcos2 = 3;
                barcos3 = 2;
                barcos4 = 1;
                tipos = 4;
                for (int i = 0; i < this.filas; i++) {
                    for (int j = 0; j < this.columnas; j++) {
                        barcos[i][j] = 0;
                    }
                }

                while (sw != 0) {

                    x = (int) (Math.random() * 10);
                    y = (int) (Math.random() * 10);
                    tipoBarco = (int) (Math.random() * tipos);

                    if (tipoBarco == 0 && barcos1 > 0) {
                        while (barcos1 > 0) {
                            if(comprobarPosicion(tipoBarco, x, y)){
                                barcos[x][y] = 1;
                                x = (int) (Math.random() * 10);
                                y = (int) (Math.random() * 10);
                                barcos1--;
                            }else{
                                x = (int) (Math.random() * 10);
                                y = (int) (Math.random() * 10);
                            }
                           
                        }

                    } else if (tipoBarco == 1 && barcos2 > 0) {
                        while (barcos2 > 0) {
                            xy = (int) (Math.random()*2);
                            System.out.println(xy);
                            if(comprobarPosicion(tipoBarco, x, y)){
                                 barcos[x][y] = 2;

                                if (xy == 0) {
                                    barcos[x][y + 1] = 2;
                                } else {
                                    barcos[x + 1][y] = 2;
                                }
                            x = (int) (Math.random() * 10);
                            y = (int) (Math.random() * 10);
                                
                            barcos2--;
                            }else{
                                x = (int) (Math.random() * 10);
                                y = (int) (Math.random() * 10);
                            }
                           
                        }
                    } else if (tipoBarco == 2 && barcos3 > 0) {
                        while (barcos3 > 0) {
                            xy = (int) (Math.random()*2);
                            if (comprobarPosicion(tipoBarco, x, y)){
                                barcos[x][y] = 3;

                                if (xy == 0) {
                                    barcos[x][y + 1] = 3;
                                    barcos[x][y + 2] = 3;
                                } else {
                                    barcos[x + 1][y] = 3;
                                    barcos[x + 2][y] = 3;
                                }
                            x = (int) (Math.random() * 10);
                            y = (int) (Math.random() * 10);    
                            
                            barcos3--;
                            }else{
                                x = (int) (Math.random() * 10);
                                y = (int) (Math.random() * 10);
                            }
                        }
                    } else if (tipoBarco == 3 && barcos4 > 0) {
                        while (barcos4 > 0) {
                            xy = (int) (Math.random()*2);
                            if (comprobarPosicion(tipoBarco, x, y)){
                                barcos[x][y] = 4;

                                if (xy == 0) {
                                    barcos[x][y + 1] = 4;
                                    barcos[x][y + 2] = 4;
                                    barcos[x][y + 3] = 4;
                                } else {
                                    barcos[x + 1][y] = 4;
                                    barcos[x + 2][y] = 4;
                                    barcos[x + 3][y] = 4;
                                }
                            x = (int) (Math.random() * 10);
                            y = (int) (Math.random() * 10);
                            
                            barcos4--; 
                            }else{
                                x = (int) (Math.random() * 10);
                                y = (int) (Math.random() * 10);
                            }
                           
                        }
                    } else if (barcos1 == 0 && barcos2 == 0 && barcos3 == 0 && barcos4 == 0) {
                        sw = 0;
                    }
                }
                break;
        }
    }

    private boolean comprobarPosicion(int tipoBarco, int x, int y) {
        boolean ok = true;
        int longBarcos = 0;

        switch (tipoBarco) {
            case 0:
                longBarcos = 1;
                break;
            case 1:
                longBarcos = 2;
                break;
            case 2:
                longBarcos = 3;
                break;
            case 3:
                longBarcos = 4;
                break;
            case 4:
                longBarcos = 5;
                break;
        }
            for (int i = 0; i <= (longBarcos+1); i++){
                try {
                    if ( (x - 1) < 0 && (x + i) > 9 && (y - 1) < 0 && (y + i) > 9){
                        ok = false;
                    } else if (barcos[x][y] != 0 && barcos[x - 1][y] != 0 && barcos[x + i][y] != 0 && barcos[x][y - 1] != 0 && barcos[x][y + i] != 0 && barcos[x - 1][y - 1] != 0 && barcos[x - 1][y + i] != 0 && barcos[x + i][y - 1] != 0 && barcos[x + i][y + i] != 0) {
                        ok = false; 
                    }
                }catch(ArrayIndexOutOfBoundsException e){
                    ok = false;
                }
            }
            
        return ok;
    }
    
    public int pintarBarcos(int i, int j){
        return barcos[i][j];
    }

    @Override
    public String toString() {
        return "HundirLaFlota{" + "barcos1=" + barcos1 + ", barcos2=" + barcos2 + ", barcos3=" + barcos3 + ", barcos4=" + barcos4 + ", barcos5=" + barcos5 + ", nombre=" + nombre + ", dificultad=" + dificultad + ", numeroIntentos=" + numeroIntentos + ", dimTablero=" + dimTablero + ", barcos=" + barcos + ", filas=" + filas + ", columnas=" + columnas + ", tipos=" + tipos + ", ultimoResultado=" + ultimoResultado + ", texto=" + texto + ", celda=" + celda + '}';
    }
    
    
    
}
