diff --git a/Practica 1/bin/aStar/Board$1.class b/Practica 1/bin/aStar/Board$1.class index d253ce3..5cd02c1 100644 Binary files a/Practica 1/bin/aStar/Board$1.class and b/Practica 1/bin/aStar/Board$1.class differ diff --git a/Practica 1/bin/aStar/Board$2.class b/Practica 1/bin/aStar/Board$2.class index b1db33e..5c9d76a 100644 Binary files a/Practica 1/bin/aStar/Board$2.class and b/Practica 1/bin/aStar/Board$2.class differ diff --git a/Practica 1/bin/aStar/Board$3.class b/Practica 1/bin/aStar/Board$3.class index 0773d00..d5386ea 100644 Binary files a/Practica 1/bin/aStar/Board$3.class and b/Practica 1/bin/aStar/Board$3.class differ diff --git a/Practica 1/bin/aStar/Board.class b/Practica 1/bin/aStar/Board.class index 6dfc974..d46e308 100644 Binary files a/Practica 1/bin/aStar/Board.class and b/Practica 1/bin/aStar/Board.class differ diff --git a/Practica 1/bin/aStar/Star$2.class b/Practica 1/bin/aStar/Star$2.class index 0a04027..f86535c 100644 Binary files a/Practica 1/bin/aStar/Star$2.class and b/Practica 1/bin/aStar/Star$2.class differ diff --git a/Practica 1/bin/aStar/Star.class b/Practica 1/bin/aStar/Star.class index aa67832..d6c9ad6 100644 Binary files a/Practica 1/bin/aStar/Star.class and b/Practica 1/bin/aStar/Star.class differ diff --git a/Practica 1/bin/dataStructures/Cell.class b/Practica 1/bin/dataStructures/Cell.class index 637f0ed..5cea425 100644 Binary files a/Practica 1/bin/dataStructures/Cell.class and b/Practica 1/bin/dataStructures/Cell.class differ diff --git a/Practica 1/bin/dataStructures/Data.class b/Practica 1/bin/dataStructures/Data.class index 36b3957..b6a244d 100644 Binary files a/Practica 1/bin/dataStructures/Data.class and b/Practica 1/bin/dataStructures/Data.class differ diff --git a/Practica 1/src/aStar/Board.java b/Practica 1/src/aStar/Board.java index fcfdb00..e6ade12 100644 --- a/Practica 1/src/aStar/Board.java +++ b/Practica 1/src/aStar/Board.java @@ -1,7 +1,6 @@ package aStar; import java.awt.Color; -import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; @@ -28,31 +27,33 @@ public class Board { private JLabel rows; private JButton start; private JButton exit; - private JTextField xGoal; - private JTextField startXtxt; - private JTextField yGoal; - private JTextField startYtxt; - private JLabel lblStart; - private JLabel lblGoal; + private JTextField txtGoalX; + private JTextField txtStartX; + private JTextField txtGoalY; + private JTextField txtStartY; + private JLabel startLbl; + private JLabel goalLbl; private JButton genMatrix; - private int N_COLS = 6; - private int N_ROWS = 6; + private int N_COLS = 20; + private int N_ROWS = 20; private int startX; private int startY; private int goalX; private int goalY; - private Cell matrix[][]; + private int nClicks; + private Cell matrix[][]; public Board() { matrix = new Cell[N_ROWS+1][N_COLS+1]; + nClicks = 0; initComponents(); } - private void initComponents() { + /* private void initComponents() { pConfig = new javax.swing.JPanel(); nRows = new javax.swing.JTextField(); @@ -63,12 +64,12 @@ private void initComponents() { exit = new javax.swing.JButton(); lblConfig = new javax.swing.JLabel(); genMatrix = new javax.swing.JButton(); - startYtxt = new javax.swing.JTextField(); - startXtxt = new javax.swing.JTextField(); - yGoal = new javax.swing.JTextField(); - xGoal = new javax.swing.JTextField(); - lblStart = new javax.swing.JLabel(); - lblGoal = new javax.swing.JLabel(); + txtStartY = new javax.swing.JTextField(); + txtStartX = new javax.swing.JTextField(); + txtGoalY = new javax.swing.JTextField(); + txtGoalX = new javax.swing.JTextField(); + startLbl = new javax.swing.JLabel(); + goalLbl = new javax.swing.JLabel(); pMatrix = new javax.swing.JPanel(); this.pFrame = new JFrame(); @@ -78,8 +79,8 @@ private void initComponents() { this.pFrame.setTitle("Algorithm A* - Andrei Ionut Vaduva"); this.pFrame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); this.pFrame.setBackground(new java.awt.Color(255, 255, 204)); - pMatrix.setPreferredSize(new Dimension(700, 600)); - this.pFrame.setPreferredSize(new Dimension(983,600)); + pMatrix.setPreferredSize(new Dimension(1170, 900)); + this.pFrame.setPreferredSize(new Dimension(1450,1000)); pConfig.setBackground(new java.awt.Color(255, 255, 204)); pConfig.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, java.awt.Color.black, java.awt.Color.black, java.awt.Color.black, java.awt.Color.black)); @@ -111,15 +112,15 @@ private void initComponents() { genMatrix.setForeground(new java.awt.Color(0, 0, 204)); genMatrix.setText("New Matrix"); - startYtxt.setPreferredSize(new java.awt.Dimension(50, 50)); + txtStartY.setPreferredSize(new java.awt.Dimension(50, 50)); - lblStart.setFont(new java.awt.Font("Verdana", 0, 16)); // NOI18N - lblStart.setForeground(new java.awt.Color(0, 0, 158)); - lblStart.setText("Start"); + startLbl.setFont(new java.awt.Font("Verdana", 0, 16)); // NOI18N + startLbl.setForeground(new java.awt.Color(0, 0, 158)); + startLbl.setText("Start"); - lblGoal.setFont(new java.awt.Font("Verdana", 0, 16)); // NOI18N - lblGoal.setForeground(new Color(128,255,0)); - lblGoal.setText("Goal"); + goalLbl.setFont(new java.awt.Font("Verdana", 0, 16)); // NOI18N + goalLbl.setForeground(new Color(128,255,0)); + goalLbl.setText("Goal"); this.pMatrix.setLayout(new GridLayout(this.N_ROWS, this.N_COLS)); this.pMatrix.setVisible(true); @@ -138,18 +139,18 @@ private void initComponents() { .addGroup(pConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(pConfigLayout.createSequentialGroup() .addGroup(pConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) - .addComponent(lblGoal) - .addComponent(lblStart)) + .addComponent(goalLbl) + .addComponent(startLbl)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 43, Short.MAX_VALUE) .addGroup(pConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pConfigLayout.createSequentialGroup() - .addComponent(startXtxt, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(txtStartX, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(startYtxt, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addComponent(txtStartY, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pConfigLayout.createSequentialGroup() - .addComponent(xGoal, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(txtGoalX, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(yGoal, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))) + .addComponent(txtGoalY, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGap(30, 30, 30)) .addGroup(pConfigLayout.createSequentialGroup() .addGap(1, 1, 1) @@ -193,14 +194,14 @@ private void initComponents() { .addComponent(genMatrix) .addGap(18, 18, 18) .addGroup(pConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(startYtxt, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(startXtxt, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(lblStart)) + .addComponent(txtStartY, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(txtStartX, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(startLbl)) .addGap(18, 18, 18) .addGroup(pConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) - .addComponent(yGoal, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(xGoal, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) - .addComponent(lblGoal)) + .addComponent(txtGoalY, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(txtGoalX, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(goalLbl)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(start) .addGap(40, 40, 40) @@ -253,9 +254,223 @@ public void actionPerformed(ActionEvent e) { this.pFrame.pack(); this.pFrame.setLocationRelativeTo(null); this.pFrame.setVisible(true); - } + }*/ + + + private void initComponents() { + + pMatrix = new javax.swing.JPanel(); + pConfig = new javax.swing.JPanel(); + nRows = new javax.swing.JTextField(); + nCols = new javax.swing.JTextField(); + cols = new javax.swing.JLabel(); + rows = new javax.swing.JLabel(); + start = new javax.swing.JButton(); + exit = new javax.swing.JButton(); + lblConfig = new javax.swing.JLabel(); + genMatrix = new javax.swing.JButton(); + txtStartY = new javax.swing.JTextField(); + txtStartX = new javax.swing.JTextField(); + txtGoalY = new javax.swing.JTextField(); + txtGoalX = new javax.swing.JTextField(); + startLbl = new javax.swing.JLabel(); + goalLbl = new javax.swing.JLabel(); + + this.pFrame = new JFrame(); + + this.pFrame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + this.pFrame.setBackground(new java.awt.Color(0, 153, 153)); + this.pFrame.setMaximumSize(new java.awt.Dimension(1450, 1000)); + this.pFrame.setPreferredSize(new java.awt.Dimension(1450, 1000)); + + createMatrix(); + + this.pFrame.setTitle("Algorithm A* - Andrei Ionut Vaduva"); + this.pFrame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + this.pFrame.setBackground(new java.awt.Color(255, 255, 204)); + // pMatrix.setPreferredSize(new Dimension(1170, 900)); + // this.pFrame.setPreferredSize(new Dimension(1450,1000)); + pConfig.setBackground(new java.awt.Color(255, 255, 204)); + pConfig.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, java.awt.Color.black, java.awt.Color.black, java.awt.Color.black, java.awt.Color.black)); + + pMatrix.setBackground(new java.awt.Color(255, 255, 204)); + pMatrix.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, java.awt.Color.black, java.awt.Color.black, java.awt.Color.black, java.awt.Color.black)); + pMatrix.setMaximumSize(new java.awt.Dimension(1170, 900)); + pMatrix.setPreferredSize(new java.awt.Dimension(1170, 900)); + + javax.swing.GroupLayout pMatrixLayout = new javax.swing.GroupLayout(pMatrix); + pMatrix.setLayout(pMatrixLayout); + pMatrixLayout.setHorizontalGroup( + pMatrixLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGap(0, 1166, Short.MAX_VALUE) + ); + pMatrixLayout.setVerticalGroup( + pMatrixLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGap(0, 996, Short.MAX_VALUE) + ); + + pConfig.setBackground(new java.awt.Color(255, 255, 204)); + pConfig.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, java.awt.Color.black, java.awt.Color.black, java.awt.Color.black, java.awt.Color.black)); + pConfig.setMaximumSize(new java.awt.Dimension(260, 576)); + + cols.setFont(new java.awt.Font("Verdana", 0, 16)); // NOI18N + cols.setForeground(new java.awt.Color(255, 102, 0)); + cols.setText("Columns"); + + rows.setFont(new java.awt.Font("Verdana", 0, 16)); // NOI18N + rows.setForeground(new java.awt.Color(255, 102, 0)); + rows.setText("Rows"); + + start.setBackground(new java.awt.Color(255, 255, 255)); + start.setFont(new java.awt.Font("Verdana", 0, 16)); // NOI18N + start.setForeground(new java.awt.Color(0, 209, 0)); + start.setText("Start"); + + exit.setBackground(new java.awt.Color(255, 255, 255)); + exit.setFont(new java.awt.Font("Verdana", 0, 16)); // NOI18N + exit.setForeground(new java.awt.Color(255, 0, 0)); + exit.setText("Exit"); + + lblConfig.setFont(new java.awt.Font("Verdana", 0, 24)); // NOI18N + lblConfig.setForeground(new java.awt.Color(255, 102, 0)); + lblConfig.setText("Configuration"); + + genMatrix.setBackground(new java.awt.Color(51, 255, 204)); + genMatrix.setFont(new java.awt.Font("Verdana", 0, 16)); // NOI18N + genMatrix.setForeground(new java.awt.Color(0, 0, 204)); + genMatrix.setText("New Matrix"); + + startLbl.setFont(new java.awt.Font("Verdana", 0, 16)); // NOI18N + startLbl.setForeground(new java.awt.Color(255, 102, 0)); + startLbl.setText("Start"); + goalLbl.setFont(new java.awt.Font("Verdana", 0, 16)); // NOI18N + goalLbl.setForeground(new java.awt.Color(255, 102, 0)); + goalLbl.setText("Goal"); + + this.pMatrix.setLayout(new GridLayout(this.N_ROWS, this.N_COLS)); + this.pMatrix.setVisible(true); + + javax.swing.GroupLayout pConfigLayout = new javax.swing.GroupLayout(pConfig); + pConfig.setLayout(pConfigLayout); + pConfigLayout.setHorizontalGroup( + pConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(pConfigLayout.createSequentialGroup() + .addGap(26, 26, 26) + .addGroup(pConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(pConfigLayout.createSequentialGroup() + .addComponent(rows) + .addGap(48, 187, Short.MAX_VALUE)) + .addGroup(pConfigLayout.createSequentialGroup() + .addGroup(pConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(pConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) + .addComponent(goalLbl) + .addComponent(startLbl)) + .addComponent(cols)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addGroup(pConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) + .addComponent(nCols, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGroup(pConfigLayout.createSequentialGroup() + .addGroup(pConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) + .addComponent(txtGoalX, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(txtStartX, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addGroup(pConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(txtStartY, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(txtGoalY, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))) + .addComponent(nRows, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(exit)) + .addGap(30, 30, 30)))) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pConfigLayout.createSequentialGroup() + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addGroup(pConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pConfigLayout.createSequentialGroup() + .addComponent(genMatrix) + .addGap(67, 67, 67)) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pConfigLayout.createSequentialGroup() + .addComponent(start) + .addGap(81, 81, 81)))) + .addGroup(pConfigLayout.createSequentialGroup() + .addGap(48, 48, 48) + .addComponent(lblConfig) + .addGap(0, 0, Short.MAX_VALUE)) + ); + pConfigLayout.setVerticalGroup( + pConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(pConfigLayout.createSequentialGroup() + .addGap(42, 42, 42) + .addComponent(lblConfig) + .addGap(40, 40, 40) + .addGroup(pConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(rows) + .addComponent(nRows, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGap(18, 18, 18) + .addGroup(pConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(cols) + .addComponent(nCols, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGap(32, 32, 32) + .addComponent(genMatrix) + .addGap(33, 33, 33) + .addGroup(pConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(txtStartY, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(txtStartX, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(startLbl)) + .addGap(24, 24, 24) + .addGroup(pConfigLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(txtGoalY, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(txtGoalX, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(goalLbl)) + .addGap(35, 35, 35) + .addComponent(start) + .addGap(31, 31, 31) + .addComponent(exit) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + ); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this.pFrame.getContentPane()); + this.pFrame.getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addComponent(pMatrix, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE) + .addComponent(pConfig, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) + .addContainerGap()) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(pMatrix, javax.swing.GroupLayout.DEFAULT_SIZE, 1000, Short.MAX_VALUE) + .addComponent(pConfig, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + ); + + this.genMatrix.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + try { + N_COLS = Integer.parseInt(nCols.getText()); + N_ROWS = Integer.parseInt(nRows.getText()); + createMatrix(); + } + catch(NumberFormatException e) { + System.out.println("Error de parseo"); + } + } + }); + + + this.exit.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + pFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + System.exit(0); + } + }); + + this.pFrame.pack(); + this.pFrame.setLocationRelativeTo(null); + this.pFrame.setVisible(true); + + } /* * Borra el panel de la matriz y la pinta de nuevo @@ -268,22 +483,45 @@ private void createMatrix(){ for(int j = 1; j<=N_COLS;j++) { Cell cell = new Cell(i,j); JButton b = cell.getCell(); - if(b.getText().equals("")) { + if(!cell.isBarrier()) { cell.setDefaultColor(); } b.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { - cell.isBarrier(); + nClicks++; + if(nClicks == 1) + setStart(cell); + else if (nClicks == 2) + setGoal(cell); + else + cell.isBarrier(); + } + + private void setGoal(Cell cell) { + // TODO Auto-generated method stub + goalX = cell.getX(); + goalY = cell.getY(); + cell.setGoal(); + start.getActionListeners().notify(); + } + + private void setStart(Cell cell) { + // TODO Auto-generated method stub + startX = cell.getX(); + startY = cell.getY(); + cell.setStart(); } }); this.pMatrix.add(b); this.matrix[i][j] = cell; } } - this.pMatrix.setPreferredSize(new Dimension(700+(N_ROWS*20), 600+(N_COLS*15))); - this.pFrame.setPreferredSize(this.pMatrix.getPreferredSize()); + this.pMatrix.validate(); this.pFrame.revalidate(); + this.pFrame.repaint(); + this.pFrame.pack(); + } public void markCell(int x, int y) { @@ -304,8 +542,9 @@ public void repaintMatrix(){ this.pMatrix.add(this.matrix[i][j].getCell()); } } - this.pMatrix.repaint(); + this.pMatrix.revalidate(); this.pFrame.revalidate(); + } public void pathPaint(ArrayList path) { @@ -315,7 +554,6 @@ public void pathPaint(ArrayList path) { int y = c.getY(); if(x == startX && y == startY) { markSpecialCell(startX, startY, new Color(0,0,153)); - } else if (x == goalX && y == goalY) { markSpecialCell(goalX, goalY, new Color(128,255,0)); @@ -331,8 +569,8 @@ public void repaintDefaultMatrix(Cell matrix[][]) { for(int i = 1; i<= N_ROWS; i++) { for(int j = 1; j <= N_COLS; j++) { Cell c = matrix[i][j]; - if(!c.getCell().getText().equals("")) { - this.matrix[i][j].isBarrier(); + if(c.isBarrier()) { + this.matrix[i][j].setBarrier(); } } } @@ -355,11 +593,11 @@ public JButton getStart() { } public JTextField getStartXtxt() { - return startXtxt; + return txtStartX; } public JTextField getStartYtxt() { - return startYtxt; + return txtStartY; } public int getN_COLS() { @@ -371,11 +609,11 @@ public int getN_ROWS() { } public JTextField getxGoal() { - return xGoal; + return txtGoalX; } public JTextField getyGoal() { - return yGoal; + return txtGoalY; } public void setXs(int startX) { diff --git a/Practica 1/src/aStar/Star.java b/Practica 1/src/aStar/Star.java index 4f73182..0b3d7b4 100644 --- a/Practica 1/src/aStar/Star.java +++ b/Practica 1/src/aStar/Star.java @@ -53,10 +53,8 @@ public int compare(Cell c1, Cell c2) { }); } - - public ArrayList play() { + public void play() { boolean fail = false; - boolean hijoMejor = false; //Celda de inicio Cell c = matrix[startX][startY]; if(c.getCell().getText().equals("")) {// Si no es un obstaculo @@ -66,9 +64,9 @@ public ArrayList play() { c.setH(stim); c.setG(0); c.setF(stim); + c.setFather(null); add(c); } - //3 3 es el fin while(!fail) { //Si hay en abierta hay elems meto el primero en cerrada y lo borro if(this.open.size() > 0 ) { @@ -82,162 +80,188 @@ public ArrayList play() { //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) { fail = true; - hijoMejor = false; } //Si no es meta en cerrada se meten sus hijos else { - if(hijoMejor) { - this.close.remove(this.close.size()-1); - } Cell a = this.close.get(this.close.size()-1); - hijoMejor = getChildrens(a.getX(), a.getY()); + getChildrens(a.getX(), a.getY()); } } - return close; } //Mete en abierta todos los nodos hijos de la casilla x y - private boolean getChildrens(int x, int y) { + private void getChildrens(int x, int y) { Cell m = matrix[x][y]; double sTwo = Math.sqrt(2); double mg = m.getG(); - boolean hijoMejor = false; + //down - if(x+1 <= N) { - if(matrix[x+1][y].getCell().getText().equals("")) { - Cell c = new Cell(); - c.setX(x+1); - c.setY(y); - c.setCell(matrix[x+1][y].getCell()); - double g = matrix[x+1][y].getG(); - c.setG(mg + g + 1); - - double a = ((x+1)-goalX); - double b = (y-goalY); - double stim = Math.sqrt((Math.pow(a, 2) + Math.pow(b,2))); - c.setH(stim);//stim es la distancia estimada desde este hijo al objetivo - c.setF(c.getG() + stim); - Data d = existsCell(this.open, x+1, y); - if(d.isFound() && d.getCell().getH() < c.getH()) {//si encuentro en abierta a un hijo nuevo - hijoMejor=true;//borro al padre de cerrada y meto al hijo en cerrada y lo quito de abierta - } - else { - Data d2 = existsCell(this.close, x+1,y); - if(!d2.isFound()) { - add(c); - matrix[x+1][y] = c; - } - } + if(x+1 <= N && !matrix[x+1][y].isBarrier()) { + Cell c = new Cell(); + c.setX(x+1); + c.setY(y); + c.setG(mg + 1); + + double a = ((x+1)-goalX); + double b = (y-goalY); + double stim = Math.sqrt((Math.pow(a, 2) + Math.pow(b,2))); + + c.setH(stim);//stim es la distancia estimada desde este hijo al objetivo + c.setF(c.getG() + stim); + + //si no tenia padre, le pongo padre + if(matrix[x+1][y].getFather() == null) { + c.setFather(m); + } + Data d = existsCell(this.open, x+1, y); + Data d2 = existsCell(this.close, x+1, y); + + if(!d.isFound() && !d2.isFound()) { + 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].getCell().getText().equals("")) { + if(x+1 <= N && y+1 <= M && !matrix[x+1][y+1].isBarrier()) { + Cell c = new Cell(); c.setX(x+1); c.setY(y+1); - double g = matrix[x+1][y+1].getG(); - c.setG(mg + g + sTwo); - + c.setG(mg + sTwo); + double a = ((x+1) - goalX); double b = ((y+1) - goalY); double stim = Math.sqrt((Math.pow(a, 2) + Math.pow(b, 2))); + c.setH(stim); c.setF(c.getG() + stim); + Data d = existsCell(this.open, x+1, y+1); - if(d.isFound() && d.getCell().getH() < c.getH()) {//Si el nuevo nodo, es decir, c tiene un coste mejor que ini se actualiza, sino se descarta - hijoMejor=true; + Data d2 = existsCell(this.close, x+1, y+1); + + if(matrix[x+1][y+1].getFather() == null) { + c.setFather(m); } - else { - Data d2 = existsCell(this.close, x+1,y+1); - if(!d2.isFound()) { - add(c); - matrix[x+1][y+1] = c; - } + + if(!d.isFound() && !d2.isFound()) { + add(c); + matrix[x+1][y+1] = c; + } + + else if ( d.isFound() && d.getCell().getG() > c.getG()) { + d.getCell().setFather(m); + d.getCell().setG(m.getG() + sTwo); } } //down left - if(x+1 <= N && y-1 > 0 && matrix[x+1][y-1].getCell().getText().equals("")) { + if(x+1 <= N && y-1 > 0 && !matrix[x+1][y-1].isBarrier()) { + Cell c = new Cell(); c.setX(x+1); c.setY(y-1); - double g = matrix[x+1][y-1].getG(); - c.setG(mg + g + sTwo); + c.setG(mg + sTwo); + double a = ((x+1) - goalX); double b = ((y-1) - goalY); double stim = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)); + c.setH(stim); c.setF(c.getG() + stim); + Data d = existsCell(this.open, x+1, y-1); - if(d.isFound() && d.getCell().getH() < c.getH()) {//Si el nuevo nodo, es decir, c tiene un coste mejor que ini se actualiza, sino se descarta - hijoMejor=true; + Data d2 = existsCell(this.close, x+1, y-1); + + if(matrix[x+1][y-1].getFather() == null) { + c.setFather(m); + } - else { - Data d2 = existsCell(this.close, x+1,y-1); - if(!d2.isFound()) { - add(c); - matrix[x+1][y-1] = c; - } + + if(!d.isFound() && !d2.isFound()) { + add(c); + matrix[x+1][y-1] = c; + } + + else if ( d.isFound() && d.getCell().getG() > c.getG()) { + d.getCell().setFather(m); + d.getCell().setG(m.getG() + sTwo); } + } //right - if(y+1 <= M && matrix[x][y+1].getCell().getText().equals("")) { + if(y+1 <= M && !matrix[x][y+1].isBarrier()) { Cell c = new Cell(); c.setX(x); c.setY(y+1); - double g = matrix[x][y+1].getG(); + c.setG(mg + 1); - c.setG(mg + g + 1); double a = (x - goalX); double b = ((y+1) - goalY); double stim = Math.sqrt((Math.pow(a, 2) + Math.pow(b, 2))); + c.setH(stim); c.setF(c.getG() + stim); + Data d = existsCell(this.open, x, y+1); - if(d.isFound() && d.getCell().getH() < c.getH()) {//Si el nuevo nodo, es decir, c tiene un coste mejor que ini se actualiza, sino se descarta - hijoMejor=true; + Data d2 = existsCell(this.close, x, y+1); + + if(matrix[x][y+1].getFather() == null) { + c.setFather(m); } - else { - Data d2 = existsCell(this.close, x,y+1); - if(!d2.isFound()) { - add(c); - matrix[x][y+1] = c; - } + + if(!d.isFound() && !d2.isFound()) { + add(c); + matrix[x][y+1] = c; } + + else if ( d.isFound() && d.getCell().getG() > c.getG()) { + d.getCell().setFather(m); + d.getCell().setG(m.getG() + 1); + } + } //left - if(y-1 >0 && matrix[x][y-1].getCell().getText().equals("")) { - + if(y-1 >0 && !matrix[x][y-1].isBarrier()) { + Cell c = new Cell(); c.setX(x); c.setY(y-1); - double g = matrix[x][y-1].getG(); - c.setG(mg + g + 1); - + c.setG(mg +1); + double a = (x - goalX); double b = ((y-1) - goalY); double stim = Math.sqrt((Math.pow(a, 2) + Math.pow(b, 2))); + c.setH(stim); c.setF(c.getG() + stim); + Data d = existsCell(this.open, x, y-1); - if(d.isFound() && d.getCell().getH() < c.getH()) {//Si el nuevo nodo, es decir, c tiene un coste mejor que ini se actualiza, sino se descarta - hijoMejor=true; + Data d2 = existsCell(this.close, x, y-1); + + if(matrix[x][y-1].getFather() == null) { + c.setFather(m); } - else { - - Data d2 = existsCell(this.close, x,y-1); - if(!d2.isFound()) { - add(c); - matrix[x][y-1] = c; - } + + if(!d.isFound() && !d2.isFound()) { + add(c); + matrix[x][y-1] = c; + } + + else if ( d.isFound() && d.getCell().getG() > c.getG()) { + d.getCell().setFather(m); + d.getCell().setG(m.getG() + 1); } } //up - if(x-1 >0 && matrix[x-1][y].getCell().getText().equals("")) { + if(x-1 >0 && !matrix[x-1][y].isBarrier()) { Cell c = new Cell(); c.setX(x-1); c.setY(y); - double g = matrix[x-1][y].getG(); - c.setG(mg + g + 1); + c.setG(mg + 1); double a = ((x-1) - goalX); double b = (y - goalY); @@ -245,85 +269,97 @@ private boolean getChildrens(int x, int y) { c.setH(stim); c.setF(c.getG() + stim); Data d = existsCell(this.open, x-1, y); - if(d.isFound() && d.getCell().getH() < c.getH()) {//Si el nuevo nodo, es decir, c tiene un coste mejor que ini se actualiza, sino se descarta - hijoMejor=true; + Data d2 = existsCell(this.close, x-1, y); + + if(matrix[x-1][y].getFather() == null) { + c.setFather(m); } - else { - Data d2 = existsCell(this.close, x-1,y); - if(!d2.isFound()) { - add(c); - matrix[x-1][y] = c; - } + + if(!d.isFound() && !d2.isFound()) { + 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); } } //up right - if(x-1 >0 && y+1 <= M && matrix[x-1][y+1].getCell().getText().equals("")) { + if(x-1 >0 && y+1 <= M && !matrix[x-1][y+1].isBarrier()) { Cell c = new Cell(); c.setX(x-1); c.setY(y+1); - double g = matrix[x-1][y+1].getG(); - c.setG(mg + g + sTwo); + c.setG(mg + sTwo); double a = ((x-1) - goalX); double b = ((y+1) - goalY); double stim = Math.sqrt((Math.pow(a, 2) + Math.pow(b, 2))); c.setH(stim); c.setF(c.getG() + stim); + Data d = existsCell(this.open, x-1, y+1); - if(d.isFound() && d.getCell().getH() < c.getH()){//Si el nuevo nodo, es decir, c tiene un coste mejor que ini se actualiza, sino se descarta - hijoMejor=true; + Data d2 = existsCell(this.close, x-1, y+1); + + if(matrix[x-1][y+1].getFather() == null) { + c.setFather(m); } - else { - Data d2 = existsCell(this.close, x-1,y+1); - if(!d2.isFound()) { - add(c); - matrix[x-1][y+1] = c; - } + + if(!d.isFound() && !d2.isFound()) { + add(c); + matrix[x-1][y+1] = c; + } + + else if ( d.isFound() && d.getCell().getG() > c.getG()) { + d.getCell().setFather(m); + d.getCell().setG(m.getG() + sTwo); } } //up left - if(x-1>0 && y-1>0 && matrix[x-1][y-1].getCell().getText().equals("")) { + if(x-1>0 && y-1>0 && !matrix[x-1][y-1].isBarrier()) { Cell c = new Cell(); c.setX(x-1); c.setY(y-1); - double g = matrix[x-1][y-1].getG(); - c.setG(mg + g + sTwo); + c.setG(mg + sTwo); + double a = ((x-1) - goalX); double b = ((y-1) - goalY); double stim = Math.sqrt((Math.pow(a, 2) + Math.pow(b, 2))); c.setH(stim); c.setF(c.getG() + stim); + Data d = existsCell(this.open, x-1, y-1); - if(d.isFound() && d.getCell().getH() < c.getH()) {//Si el nuevo nodo, es decir, c tiene un coste mejor que ini se actualiza, sino se descarta - hijoMejor=true; + Data d2 = existsCell(this.close, x-1, y-1); + + if(matrix[x-1][y-1].getFather() == null) { + c.setFather(m); } - else { - Data d2 = existsCell(this.close, x-1,y-1); - if(!d2.isFound()) { - add(c); - matrix[x-1][y-1] = c; - } + + if(!d.isFound() && !d2.isFound()) { + add(c); + matrix[x-1][y-1] = c; } + else if ( d.isFound() && d.getCell().getG() > c.getG()) { + d.getCell().setFather(m); + d.getCell().setG(m.getG() + sTwo); + } } - return hijoMejor; } private Data existsCell(ArrayList list, int x, int y){ boolean ok = false; Cell found = null; - int index = -1; int i = 0; while(i path = play(); + play(); + ArrayList path = new ArrayList(); + 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); @@ -374,6 +413,32 @@ public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(new JFrame(),err,excep.getMessage(),JOptionPane.ERROR_MESSAGE); } } + + }); } + + private void getPath(Cell c, ArrayList path) { + if(c != null) { + path.add(c); + getPath(c.getFather(), path); + } + } + + public void ready2() { + String err = "Los datos introducidos son incorrectos!"; + try{ + play(); + ArrayList path = new ArrayList(); + 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); + } + } } diff --git a/Practica 1/src/dataStructures/Cell.java b/Practica 1/src/dataStructures/Cell.java index e04bc82..7ba7ecd 100644 --- a/Practica 1/src/dataStructures/Cell.java +++ b/Practica 1/src/dataStructures/Cell.java @@ -12,10 +12,13 @@ public class Cell { private double g; private double h; private double f; + private Cell father; + private boolean isBarrier; public Cell() { this.cell = new JButton(); this.cell.setBackground(new Color(255,255,204)); + isBarrier = false; } public Cell(int x, int y) { @@ -23,6 +26,7 @@ public Cell(int x, int y) { this.y = y; this.cell = new JButton(); this.cell.setBackground(new Color(255,255,204)); + isBarrier = false; } public void mark(Color c) { @@ -41,15 +45,11 @@ public JButton getCell() { return cell; } - public void isBarrier() { + public void setBarrier() { this.cell.setBackground(new java.awt.Color(0, 0, 0)); - this.cell.setFont(new java.awt.Font("Verdana", 0, 16)); - this.cell.setForeground(new java.awt.Color(255, 0, 0)); - this.cell.setText("X"); - } - - public void setCell(JButton cell) { - this.cell = cell; + 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() { @@ -99,4 +99,26 @@ else if (this.h == c2.getH()) { public void setDefaultColor() { this.cell.setBackground(new Color(255,255,204)); } + + public void setFather(Cell father) { + this.father = father; + } + + public Cell getFather() { + return this.father; + } + + public boolean isBarrier() { + return this.isBarrier; + } + + public void setStart() { + // TODO Auto-generated method stub + this.cell.setBackground(new Color(0,0,153)); + } + + public void setGoal() { + // TODO Auto-generated method stub + this.cell.setBackground(new Color(128,255,0)); + } } diff --git a/Practica 1/src/dataStructures/Data.java b/Practica 1/src/dataStructures/Data.java index 256a741..82226a2 100644 --- a/Practica 1/src/dataStructures/Data.java +++ b/Practica 1/src/dataStructures/Data.java @@ -2,14 +2,12 @@ public class Data { - private int index; private boolean found; private Cell cell; - public Data(boolean found, Cell cell, int index) { + public Data(boolean found, Cell cell) { this.found = found; this.cell = cell; - this.index = index; } public Cell getCell() { @@ -20,10 +18,6 @@ public boolean isFound() { return found; } - public int getIndex() { - return index; - } - public void setCell(Cell cell) { this.cell = cell; }