Skip to content

Commit

Permalink
cambios
Browse files Browse the repository at this point in the history
  • Loading branch information
Macro21 committed Mar 11, 2018
1 parent 3f68925 commit d2367a8
Show file tree
Hide file tree
Showing 17 changed files with 210 additions and 426 deletions.
Binary file modified Practica 1/bin/aStar/Board$1.class
Binary file not shown.
Binary file modified Practica 1/bin/aStar/Board$2.class
Binary file not shown.
Binary file modified Practica 1/bin/aStar/Board$3.class
Binary file not shown.
Binary file added Practica 1/bin/aStar/Board$4.class
Binary file not shown.
Binary file added Practica 1/bin/aStar/Board$5.class
Binary file not shown.
Binary file added Practica 1/bin/aStar/Board$6$1.class
Binary file not shown.
Binary file added Practica 1/bin/aStar/Board$6.class
Binary file not shown.
Binary file modified Practica 1/bin/aStar/Board.class
Binary file not shown.
Binary file modified Practica 1/bin/aStar/Star$1.class
Binary file not shown.
Binary file removed Practica 1/bin/aStar/Star$2.class
Binary file not shown.
Binary file modified Practica 1/bin/aStar/Star.class
Binary file not shown.
Binary file modified Practica 1/bin/dataStructures/Cell.class
Binary file not shown.
Binary file modified Practica 1/bin/main/Main.class
Binary file not shown.
508 changes: 183 additions & 325 deletions Practica 1/src/aStar/Board.java

Large diffs are not rendered by default.

113 changes: 21 additions & 92 deletions Practica 1/src/aStar/Star.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package aStar;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Comparator;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import dataStructures.Cell;
import dataStructures.Data;

Expand All @@ -24,19 +19,18 @@ public class Star {

private ArrayList<Cell> open;
private ArrayList<Cell> close;
private Board board;
private int startX;
private int startY;
private int goalX;
private int goalY;
//private Board board;

private Cell matrix[][];
private int N;
private int M;

public Star(Board board) {
public Star(Cell[][] matrix) {
this.open = new ArrayList<Cell>();
this.close = new ArrayList<Cell>();
this.board = board;
this.matrix = matrix;
N = matrix.length;
M = matrix[0].length;
}

private void add(Cell c) {
Expand All @@ -53,11 +47,11 @@ public int compare(Cell c1, Cell c2) {
});
}

public void play() {
public Cell[][] play(int startX, int startY, int goalX, int goalY) {
boolean fail = false;
//Celda de inicio
Cell c = matrix[startX][startY];
if(c.getCell().getText().equals("")) {// Si no es un obstaculo
if(!c.isBarrier()) {// 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 @@ -84,19 +78,20 @@ public void play() {
//Si no es meta en cerrada se meten sus hijos
else {
Cell a = this.close.get(this.close.size()-1);
getChildrens(a.getX(), a.getY());
getChildrens(a.getX(), a.getY(), goalX, goalY);
}
}
return matrix;
}

//Mete en abierta todos los nodos hijos de la casilla x y
private void getChildrens(int x, int y) {
private void getChildrens(int x, int y, int goalX, int goalY) {
Cell m = matrix[x][y];
double sTwo = Math.sqrt(2);
double mg = m.getG();

//down
if(x+1 <= N && !matrix[x+1][y].isBarrier()) {
if(x+1 < N && !matrix[x+1][y].isBarrier()) {
Cell c = new Cell();
c.setX(x+1);
c.setY(y);
Expand All @@ -120,14 +115,14 @@ private void getChildrens(int x, int y) {
add(c);
matrix[x+1][y] = c;
}

else if (d.isFound() && d.getCell().getG() >= c.getG()) {
d.getCell().setFather(m);
d.getCell().setG(m.getG() + 1);
}
}
//down right
if(x+1 <= N && y+1 <= M && !matrix[x+1][y+1].isBarrier()) {
if(x+1 < N && y+1 < M && !matrix[x+1][y+1].isBarrier()) {

Cell c = new Cell();
c.setX(x+1);
Expand Down Expand Up @@ -159,7 +154,7 @@ else if ( d.isFound() && d.getCell().getG() > c.getG()) {
}
}
//down left
if(x+1 <= N && y-1 > 0 && !matrix[x+1][y-1].isBarrier()) {
if(x+1 < N && y-1 > 0 && !matrix[x+1][y-1].isBarrier()) {

Cell c = new Cell();
c.setX(x+1);
Expand Down Expand Up @@ -193,7 +188,7 @@ else if ( d.isFound() && d.getCell().getG() > c.getG()) {

}
//right
if(y+1 <= M && !matrix[x][y+1].isBarrier()) {
if(y+1 < M && !matrix[x][y+1].isBarrier()) {
Cell c = new Cell();
c.setX(x);
c.setY(y+1);
Expand Down Expand Up @@ -286,7 +281,7 @@ else if ( d.isFound() && d.getCell().getG() > c.getG()) {
}
}
//up right
if(x-1 >0 && y+1 <= M && !matrix[x-1][y+1].isBarrier()) {
if(x-1 >0 && y+1 < M && !matrix[x-1][y+1].isBarrier()) {

Cell c = new Cell();
c.setX(x-1);
Expand Down Expand Up @@ -362,60 +357,10 @@ private Data existsCell(ArrayList<Cell> list, int x, int y){
return new Data(ok,found);
}

private void iniStar() throws Exception {
try {
startX = Integer.parseInt(board.getStartXtxt().getText());
startY = Integer.parseInt(board.getStartYtxt().getText());
goalX = Integer.parseInt(board.getxGoal().getText());
goalY = Integer.parseInt(board.getyGoal().getText());
board.setXs(startX);
board.setYs(startY);
board.setXg(goalX);
board.setYg(goalY);
N = board.getN_ROWS();
M = board.getN_COLS();

if(startX > N || startY > M || goalX > N || goalY > M)
throw new Exception("Error, fuera de rango!");

matrix = board.getMatrix();
if(!matrix[startX][startY].getCell().getText().equals(""))
throw new Exception("Error, inicio en obstaculo!");
if(!matrix[goalX][goalY].getCell().getText().equals(""))
throw new Exception("Error, fin en obstaculo!");
board.repaintDefaultMatrix(matrix);
this.close.clear();
this.open.clear();

}
catch(NumberFormatException e) {
throw new NumberFormatException();
}
}

public void ready() {
this.board.getStart().addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String err = "Los datos introducidos son incorrectos!";
try{
iniStar();
play();
ArrayList<Cell> path = new ArrayList<Cell>();
getPath(close.get(close.size()-1), path);
board.pathPaint(path);
//board.pathPaint(posibleCamino);
}
catch(NumberFormatException ex) {
JOptionPane.showMessageDialog(new JFrame(),err,"Error, formato incorrecto",JOptionPane.ERROR_MESSAGE);
}
catch(Exception excep) {
JOptionPane.showMessageDialog(new JFrame(),err,excep.getMessage(),JOptionPane.ERROR_MESSAGE);
}
}


});
public ArrayList<Cell> getPath(){
ArrayList<Cell> path = new ArrayList<Cell>();
getPath(this.close.get(this.close.size()-1), path);
return path;
}

private void getPath(Cell c, ArrayList<Cell> path) {
Expand All @@ -425,20 +370,4 @@ private void getPath(Cell c, ArrayList<Cell> path) {
}
}

public void ready2() {
String err = "Los datos introducidos son incorrectos!";
try{
play();
ArrayList<Cell> path = new ArrayList<Cell>();
getPath(close.get(close.size()-1), path);
board.pathPaint(path);
//board.pathPaint(posibleCamino);
}
catch(NumberFormatException ex) {
JOptionPane.showMessageDialog(new JFrame(),err,"Error, formato incorrecto",JOptionPane.ERROR_MESSAGE);
}
catch(Exception excep) {
JOptionPane.showMessageDialog(new JFrame(),err,excep.getMessage(),JOptionPane.ERROR_MESSAGE);
}
}
}
10 changes: 4 additions & 6 deletions Practica 1/src/dataStructures/Cell.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,9 @@ public JButton getCell() {
return cell;
}

public void setBarrier() {
this.cell.setBackground(new java.awt.Color(0, 0, 0));
public void setBarrier(Color c) {
this.cell.setBackground(c);
isBarrier = true;
//this.cell.setFont(new java.awt.Font("Verdana", 0, 16));
//this.cell.setForeground(new java.awt.Color(255, 0, 0));
}

public double getG() {
Expand Down Expand Up @@ -96,8 +94,8 @@ else if (this.h == c2.getH()) {
return 1;
}

public void setDefaultColor() {
this.cell.setBackground(new Color(255,255,204));
public void setDefaultColor(Color c) {
this.cell.setBackground(c);
}

public void setFather(Cell father) {
Expand Down
5 changes: 2 additions & 3 deletions Practica 1/src/main/Main.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package main;

import aStar.Board;
import aStar.Star;

public class Main {

public static void main(String[] args) {
// TODO Apéndice de método generado automáticamente
Board b = new Board();
Star s = new Star(b);
s.ready();
b.ready();

}
}

0 comments on commit d2367a8

Please sign in to comment.