Skip to content

Commit

Permalink
finish
Browse files Browse the repository at this point in the history
  • Loading branch information
Macro21 committed Mar 24, 2018
1 parent 7dfabe1 commit 8207adb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
35 changes: 31 additions & 4 deletions Practica 1/src/aStar/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class Board {
private final Color startCellColor = new Color(0, 0, 153);
private final Color goalCellColor = new Color(128, 255, 0);
private final Color barrierColor = new Color(0, 0, 0);
//private final Color defaultCellColor = new Color(255,255,204);
private final Color defaultCellColor = new Color(036, 231, 017);
private final Color pathCellColor = new Color(102, 0, 0);
private final Color cost1Color = new Color(255, 102, 102);
private final Color cost2Color = new Color(255, 204, 51);
Expand Down Expand Up @@ -498,8 +498,9 @@ public void actionPerformed(ActionEvent e) {
this.pFrame.setVisible(true);
}

private void wPointsMode(Star s) {
private void wPointsMode(Star s) throws Exception {
ArrayList<WayPointStructure> allPath = new ArrayList<WayPointStructure>();
Cell[][] auxMa = this.matrix;
wPointsList.remove(0);
while (!wPointsList.isEmpty()) {
Cell goal = wPointsList.get(0);
Expand All @@ -512,10 +513,12 @@ private void wPointsMode(Star s) {
startY = goalY;
orderWayPoints();
wPointsList.remove(goal);
repaintMatrix();
createMatrix();
s = new Star(matrix);
}
repaintMatrix(auxMa);
wayPointsPathPaint(allPath);

}
//Tengo la lista ordenada respecto al origen

Expand Down Expand Up @@ -625,7 +628,7 @@ private void setStart(Cell cell) {
}

public void markCell(int x, int y) {
this.pMatrix.getComponent(((x - 1) * N_COLS) + (y - 1)).setBackground(new Color(036, 231, 017));
this.pMatrix.getComponent(((x - 1) * N_COLS) + (y - 1)).setBackground(defaultCellColor);
}

public void markSpecialCell(int x, int y, Color c) {
Expand Down Expand Up @@ -733,6 +736,7 @@ public void mouseClicked(MouseEvent e) {
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(new JFrame(), err, "Error, formato incorrecto", JOptionPane.ERROR_MESSAGE);
} catch (Exception excep) {
System.out.print(excep);
JOptionPane.showMessageDialog(new JFrame(), err, excep.getMessage(), JOptionPane.ERROR_MESSAGE);
}
}
Expand Down Expand Up @@ -772,4 +776,27 @@ public Cell[][] getMatrix() {
return matrix;
}

private void repaintMatrix(Cell[][] auxMa) {
for (int i = 1; i <= N_ROWS; i++) {
for (int j = 1; j <= N_COLS; j++) {
switch (auxMa[i][j].getCost()) {
case "cost1":
this.matrix[i][j].setCost1();
markSpecialCell(i, j, cost1Color);
break;
case "cost2":
this.matrix[i][j].setCost2();
markSpecialCell(i, j, cost2Color);
break;
case "cost3":
this.matrix[i][j].setCost3();
markSpecialCell(i, j, cost3Color);
break;
default:
break;
}
}
}
}

}
13 changes: 10 additions & 3 deletions Practica 1/src/aStar/Star.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,18 @@ public int compare(Cell c1, Cell c2) {
});
}

public Cell[][] play(int startX, int startY, int goalX, int goalY) {
public Cell[][] play(int startX, int startY, int goalX, int goalY) throws Exception {
boolean fail = false;
//Celda de inicio
Cell c = matrix[startX][startY];
if (!c.isBarrier()) {// Si no es un obstaculo
Cell c2 = matrix[goalX][goalY];
if(c.isBarrier()){
throw new Exception("Inicio en obstáculo!");
}
else if (c2.isBarrier()){
throw new Exception("Fin en obstáculo!");
}
else{// Si no es un obstaculo
double a = (startX - goalX);
double b = (startY - goalY);
double stim = Math.sqrt((Math.pow(a, 2) + Math.pow(b, 2)));
Expand All @@ -65,7 +72,7 @@ public Cell[][] play(int startX, int startY, int goalX, int goalY) {
this.close.add(a);
this.open.remove(0);
} else {
fail = true;
throw new Exception("Error, destino inalcanzable!");
}
//Si en cerrada es meta se acaba el proceso
if (this.close.get(this.close.size() - 1).getX() == goalX && this.close.get(this.close.size() - 1).getY() == goalY) {
Expand Down

0 comments on commit 8207adb

Please sign in to comment.