Asesoramiento con un método.
Hola, qué tal. Me han dejado de tarea hacer un Maze como programa, pero donde indique aparte de la ruta que debe tomar ("el gusano") el costo que realiza si toma una u otra ruta para llegar al objetivo, partiendo éste de un lugar de inicio. Mi problema es que no me imprime el costo de cada ruta que toma, por lo demás y por lo visto ya está bien. Sólo necesito que me imprima eso y ya. Intenté jalarlo desde la clase principal pero me sigue marcando error, ¿hay algo que deba hacer? Saludos y gracias por responder.
Lo que hice para solucionarlo fue lo siguiente:
Según yo, la clase RunMaze la hace falta jalar es ParentCost y LocalCost. Lo que hice fue lo siguiente:
Square costo = new Square(Square goal);
double cost = costo.getLocalCost(costo);
System.out.println(cost);
Y esto lo meti en la funcion findBestPath
while (opened.size() > 0) {
Square best = findBestPassThrough();
opened.remove(best);
closed.add(best);
if (best.isEnd()) {
System.out.println("Se llego a la meta");
populateBestList(goal);
draw();
double cost = goal.getLocalCost(goal);
System.out.println(cost);
Pero me sigue marcando error. No sé qué está mal. Espero me puedan responder y darle una revisada al código, muchas gracias.
import java.io.IOException
public class RunMaze {
/**
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int renglones,columnas;
System.out.println("Ingrese no. de renglones:");
renglones=sc.nextInt();
System.out.println("Ingrese no. de columnas:");
columnas=sc.nextInt();
Maze maze = new Maze(renglones,columnas);
maze.draw();
maze.findBestPath();
}
}
import java.util.HashSet;
import java.util.Set;
public class Square {
private int x;
private int y;
private boolean start;
private boolean end;
private double localCost; // cost of getting from this square to goal
private double parentCost; // cost of getting from parent square to this node
private double passThroughCost;// cost of getting from the start to the goal
// through this square
private Maze maze;
private Set<Square> adjacencies = new HashSet<Square>();
private Square parent;
public Square(int x, int y, Maze maze) {
this.x = x;
this.y = y;
this.maze = maze;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public boolean isStart() {
return start;
}
public void setStart(boolean start) {
this.start = start;
}
public boolean isEnd() {
return end;
}
public void setEnd(boolean end) {
this.end = end;
}
public Set<Square> getAdjacencies() {
return adjacencies;
}
public void setAdjacencies(Set<Square> adjacencies) {
this.adjacencies = adjacencies;
}
public Square getParent() {
return parent;
}
public void setParent(Square parent) {
this.parent = parent;
}
public void calculateAdjacencies() {
int top = x - 1;
int bottom = x + 1;
int left = y - 1;
int right = y + 1;
if (bottom < maze.getRows()) {
if (isAdjacent()) {
maze.getSquare(bottom, y).addAdjacency(this);
this.addAdjacency(maze.getSquare(bottom, y));
}
}
if (right < maze.getColumns()) {
if (isAdjacent()) {
maze.getSquare(x, right).addAdjacency(this);
this.addAdjacency(maze.getSquare(x, right));
}
}
}
public void addAdjacency(Square square) {
adjacencies.add(square);
}
public void removeAdjacency(Square square) {
adjacencies.remove(square);
}
public double getPassThrough(Square goal) {
if (isStart()) {
return 0.0;
}
return getLocalCost(goal) + getParentCost();
}
public double getLocalCost(Square goal) {
if (isStart()) {
return 0.0;
}
localCost = 1.0 * (Math.abs(x - goal.getX()) + Math.abs(y - goal.getY()));
return localCost;
}
public double getParentCost() {
if (isStart()) {
return 0.0;
}
if (parentCost == 0.0) {
parentCost = 1.0 + .5 * (parent.getParentCost() - 1.0);
}
return parentCost;
}
public boolean isAdjacent() {
if (Math.random() > .5) {
return true;
}
return false;
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
public class Maze {
private int rows;
private int columns;
private Square[][] elements;
private Square goal;
private static final String CLOSED_TOP = "+ - ";
private static final String OPEN_TOP = "+ ";
private static final String CLOSED_LEFT = "| ";
private static final String CLOSED_LEFT_PATH = "| . ";
private static final String CLOSED_LEFT_START = "| S ";
private static final String CLOSED_LEFT_GOAL = "| G ";
private static final String OPEN_LEFT = " ";
private static final String OPEN_LEFT_PATH = " . ";
private static final String OPEN_LEFT_START = " S ";
private static final String OPEN_LEFT_GOAL = " G ";
private List<Square> opened = new ArrayList<Square>();
private List<Square> closed = new ArrayList<Square>();
private List<Square> bestList = new ArrayList<Square>();
int goalX,goalY;
int startX,startY;
public Maze(int rows, int columns) {
this.rows = rows;
this.columns = columns;
elements = new Square[rows][columns];
init();
}
private void init() {
createSquares();
setStartAndGoal();
generateAdjacenies();
}
public int getRows() {
return rows;
}
public int getColumns() {
return columns;
}
private void setStartAndGoal() {
Scanner sc = new Scanner (System.in);
System.out.println("Ingrese coordenadas para el Inicio:");
startX=sc.nextInt();
startY=sc.nextInt();
elements[startX][startY].setStart(true);
System.out.println("Ingrese coordenadas para la Meta:");
goalX = sc.nextInt();
goalY = sc.nextInt();
goal = elements[goalX][goalY];
goal.setEnd(true);
}
private void generateAdjacenies() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
elements[i][j].calculateAdjacencies();
}
}
}
private void createSquares() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
elements[i][j] = new Square(i, j, this);
}
}
}
public Square getSquare(int x, int y) {
return elements[x][y];
}
public void setSquare(Square square) {
elements[square.getX()][square.getY()] = square;
}
public void draw() {
System.out.println("Dibujando el Laberinto");
drawContents();
drawBorder();
}
private void drawContents() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
Square square = elements[i][j];
drawTop(square);
}
System.out.println("+");
for (int j = 0; j < columns; j++) {
Square square = elements[i][j];
drawLeft(square);
}
System.out.println("|");
}
}
private void drawLeft(Square square) {
int x = square.getX();
int y = square.getY();
if (y - 1 < 0) {
if (square.isStart()) {
System.out.print(CLOSED_LEFT_START);
return;
}
if (square.isEnd()) {
System.out.print(CLOSED_LEFT_GOAL);
return;
}
if (bestList.contains(square)) {
System.out.print(CLOSED_LEFT_PATH);
return;
}
System.out.print(CLOSED_LEFT);
return;
}
for (Square neighbor : square.getAdjacencies()) {
if (neighbor.getX() == x && neighbor.getY() == y - 1) {
if (square.isStart()) {
System.out.print(OPEN_LEFT_START);
return;
}
if (square.isEnd()) {
System.out.print(OPEN_LEFT_GOAL);
return;
}
if (bestList.contains(square)) {
System.out.print(OPEN_LEFT_PATH);
return;
}
System.out.print(OPEN_LEFT);
return;
}
}
if (square.isEnd()) {
System.out.print(CLOSED_LEFT_GOAL);
return;
}
if (bestList.contains(square)) {
System.out.print(CLOSED_LEFT_PATH);
return;
}
System.out.print(CLOSED_LEFT);
}
private void drawTop(Square square) {
int x = square.getX();
int y = square.getY();
if (x == 0) {
System.out.print(CLOSED_TOP);
return;
}
for (Square neighbor : square.getAdjacencies()) {
if (neighbor.getX() == x - 1 && neighbor.getY() == y) {
System.out.print(OPEN_TOP);
return;
}
}
System.out.print(CLOSED_TOP);
}
private void drawBorder() {
for (int i = 0; i < columns; i++) {
System.out.print(CLOSED_TOP);
}
System.out.println("+");
}
public void findBestPath() {
System.out.println("Calculando el mejor camino...");
Set<Square> adjacencies = elements[startX][startY].getAdjacencies();
for (Square adjacency : adjacencies) {
adjacency.setParent(elements[startX][startY]);
if (adjacency.isStart() == false) {
opened.add(adjacency);
}
}
while (opened.size() > 0) {
Square best = findBestPassThrough();
opened.remove(best);
closed.add(best);
if (best.isEnd()) {
System.out.println("Se llego a la meta");
populateBestList(goal);
draw();
return;
} else {
Set<Square> neighbors = best.getAdjacencies();
for (Square neighbor : neighbors) {
if (opened.contains(neighbor)) {
Square tmpSquare = new Square(neighbor.getX(),
neighbor.getY(), this);
tmpSquare.setParent(best);
if (tmpSquare.getPassThrough(goal) >= neighbor
.getPassThrough(goal)) {
continue;
}
}
if (closed.contains(neighbor)) {
Square tmpSquare = new Square(neighbor.getX(),
neighbor.getY(), this);
tmpSquare.setParent(best);
if (tmpSquare.getPassThrough(goal) >= neighbor
.getPassThrough(goal)) {
continue;
}
}
neighbor.setParent(best);
opened.remove(neighbor);
closed.remove(neighbor);
opened.add(0, neighbor);
}
}
}
System.out.println("No hay camino viable a la meta");
}
private void populateBestList(Square square) {
bestList.add(square);
if (square.getParent().isStart() == false) {
populateBestList(square.getParent());
}
return;
}
private Square findBestPassThrough() {
Square best = null;
for (Square square : opened) {
if (best == null
|| square.getPassThrough(goal) < best.getPassThrough(goal)) {
best = square;
}
}
return best;
}
}
<blockcode>
- Inicie sesión o regístrese para enviar comentarios
¿Que error marca?
¿Que error marca?