From 1a11b3d331e7e3aae835a8424ceff5fcc50a628c Mon Sep 17 00:00:00 2001 From: Macarena Rufo Date: Sun, 5 Nov 2017 22:58:13 +0100 Subject: [PATCH] =?UTF-8?q?Correcci=C3=B3n=20bug=20al=20generarTesoro?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ObjectMaze/pom.xml | 13 ++ .../java/com/mycompany/objectmaze/Celda.java | 32 +++++ .../com/mycompany/objectmaze/Laberinto.java | 116 ++++++++++++++++++ .../java/com/mycompany/objectmaze/Main.java | 17 +++ .../java/com/mycompany/objectmaze/Pared.java | 29 +++++ .../com/mycompany/objectmaze/Pintable.java | 14 +++ .../java/com/mycompany/objectmaze/Tesoro.java | 27 ++++ .../com/mycompany/objectmaze/Usuario.java | 25 ++++ 8 files changed, 273 insertions(+) create mode 100644 ObjectMaze/pom.xml create mode 100644 ObjectMaze/src/main/java/com/mycompany/objectmaze/Celda.java create mode 100644 ObjectMaze/src/main/java/com/mycompany/objectmaze/Laberinto.java create mode 100644 ObjectMaze/src/main/java/com/mycompany/objectmaze/Main.java create mode 100644 ObjectMaze/src/main/java/com/mycompany/objectmaze/Pared.java create mode 100644 ObjectMaze/src/main/java/com/mycompany/objectmaze/Pintable.java create mode 100644 ObjectMaze/src/main/java/com/mycompany/objectmaze/Tesoro.java create mode 100644 ObjectMaze/src/main/java/com/mycompany/objectmaze/Usuario.java diff --git a/ObjectMaze/pom.xml b/ObjectMaze/pom.xml new file mode 100644 index 0000000..5060507 --- /dev/null +++ b/ObjectMaze/pom.xml @@ -0,0 +1,13 @@ + + + 4.0.0 + com.mycompany + ObjectMaze + 1.0-SNAPSHOT + jar + + UTF-8 + 1.8 + 1.8 + + \ No newline at end of file diff --git a/ObjectMaze/src/main/java/com/mycompany/objectmaze/Celda.java b/ObjectMaze/src/main/java/com/mycompany/objectmaze/Celda.java new file mode 100644 index 0000000..28e4721 --- /dev/null +++ b/ObjectMaze/src/main/java/com/mycompany/objectmaze/Celda.java @@ -0,0 +1,32 @@ +/* + * 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. + */ +package com.mycompany.objectmaze; + +/** + * + * @author japar + */ +public abstract class Celda implements Pintable { + private int x,y; + + public Celda(int x, int y) { + this.x = x; + this.y = y; + } + + + + public int getX() { + return x; + } + + public int getY() { + return y; + } + + + +} diff --git a/ObjectMaze/src/main/java/com/mycompany/objectmaze/Laberinto.java b/ObjectMaze/src/main/java/com/mycompany/objectmaze/Laberinto.java new file mode 100644 index 0000000..73fd5c1 --- /dev/null +++ b/ObjectMaze/src/main/java/com/mycompany/objectmaze/Laberinto.java @@ -0,0 +1,116 @@ +/* + * 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. + */ +package com.mycompany.objectmaze; + +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * + * @author japar + */ +public class Laberinto implements Pintable { + public static final Map CELDAS_POR_DEFECTO=new HashMap<>(); + static{ + CELDAS_POR_DEFECTO.put("/",0.5); + CELDAS_POR_DEFECTO.put("\\", 0.5); + } + public static final String paredHorizonal="_"; + public static final String paredVertical="|"; + + private Celda[][] celdas; + private Usuario usuario; + private Tesoro tesoro; + + public Laberinto(int filas, int columnas){ + this(filas,columnas,CELDAS_POR_DEFECTO); + } + + public Laberinto(int filas, int columnas, Map probabilidad){ + celdas=new Celda[filas+2][columnas+2]; + generarCeldas(probabilidad); + generarUsuario(); + generarTesoro(); + } + + private void generarCeldas(Map probabilidad) { + for(int i=0;i0.5?0:(celdas.length-1); + usuario=new Usuario(x, y); + celdas[x][y]=usuario; + } + + + private void generarTesoro() { + int x = 0; + int y = 0; + + do { + x=1+(int)(Math.random()*(celdas[0].length-1)); + } + while(x==0 || x==celdas[0].length-1); + + do { + y=1+(int)(Math.random()*(celdas.length-1)); + } + while(y==0 || y==celdas.length-1); + + tesoro=new Tesoro(x, y); + celdas[x][y]=tesoro; + } + + private Celda generarInterior(int x, int y, Map probabilidad) { + String tipo=escogerPared(probabilidad); + return new Pared(x,y,tipo); + } + + private String escogerPared(Map probabilidad) { + double total=probabilidad.values().stream().collect(Collectors.summingDouble(Double::doubleValue)); + double random=Math.random()*total; + double conteo=0; + String result=null; + for(String key:probabilidad.keySet()){ + conteo+=probabilidad.get(key); + result=key; + if(conteo>=random) + break; + } + return result; + } + + public String toString(){ + return "Laberinto de "+celdas.length+" por "+celdas[0].length; + + } + + public void pintar() + { + for(int i=0;i