Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions ObjectMaze/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>ObjectMaze</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
32 changes: 32 additions & 0 deletions ObjectMaze/src/main/java/com/mycompany/objectmaze/Celda.java
Original file line number Diff line number Diff line change
@@ -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;
}



}
116 changes: 116 additions & 0 deletions ObjectMaze/src/main/java/com/mycompany/objectmaze/Laberinto.java
Original file line number Diff line number Diff line change
@@ -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<String, Double> 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<String,Double> probabilidad){
celdas=new Celda[filas+2][columnas+2];
generarCeldas(probabilidad);
generarUsuario();
generarTesoro();
}

private void generarCeldas(Map<String,Double> probabilidad) {
for(int i=0;i<celdas.length;i++){
celdas[0][i]=new Pared(0, i, paredHorizonal);
celdas[celdas[i].length-1][i]=new Pared(0, i, paredHorizonal);
}
for(int i=0;i<celdas[0].length;i++){
celdas[i][0]=new Pared(0, i, paredVertical);
celdas[i][celdas[i].length-1]=new Pared(0, i, paredVertical);
}
for(int i=1;i<celdas.length-1;i++){
for(int j=1;j<celdas[i].length-1;j++){
celdas[i][j]=generarInterior(i,j,probabilidad);
}
}
}

private void generarUsuario() {
int y=1+(int)(Math.random()*(celdas[0].length-1));
int x=Math.random()>0.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<String, Double> probabilidad) {
String tipo=escogerPared(probabilidad);
return new Pared(x,y,tipo);
}

private String escogerPared(Map<String, Double> 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<celdas.length;i++){
for(int j=0;j<celdas[i].length;j++)
{
celdas[i][j].pintar();
}
System.out.println();
}
}
}
17 changes: 17 additions & 0 deletions ObjectMaze/src/main/java/com/mycompany/objectmaze/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* 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 class Main {
public static void main(String[] args) {
Laberinto laberinto=new Laberinto(20,20);
laberinto.pintar();
}
}
29 changes: 29 additions & 0 deletions ObjectMaze/src/main/java/com/mycompany/objectmaze/Pared.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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 class Pared extends Celda{

private String muro;
public Pared(int x, int y,String muro) {
super(x, y);
this.muro=muro;
}

@Override
public void pintar() {
System.out.print(muro);
}

public String toString(){
return "Pared("+muro+")";
}

}
14 changes: 14 additions & 0 deletions ObjectMaze/src/main/java/com/mycompany/objectmaze/Pintable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* 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 interface Pintable {
public void pintar();
}
27 changes: 27 additions & 0 deletions ObjectMaze/src/main/java/com/mycompany/objectmaze/Tesoro.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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 class Tesoro extends Celda {

public Tesoro(int x, int y) {
super(x, y);
}

@Override
public void pintar() {
System.out.print("*");
}

public String toString(){
return "*";
}

}
25 changes: 25 additions & 0 deletions ObjectMaze/src/main/java/com/mycompany/objectmaze/Usuario.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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 class Usuario extends Celda{

String simboloUsuario="o";

public Usuario(int x, int y) {
super(x, y);
}

@Override
public void pintar() {
System.out.print(simboloUsuario);
}

}