major improvements
This commit is contained in:
parent
fc18dea7f4
commit
3068c5ba94
11 changed files with 190 additions and 30 deletions
|
@ -23,6 +23,8 @@ import javax.imageio.ImageIO;
|
|||
import java.net.URL;
|
||||
|
||||
import universe.Universe;
|
||||
import universe.Stack;
|
||||
import universe.Situation;
|
||||
|
||||
public class Grid extends JPanel {
|
||||
private int width;
|
||||
|
@ -43,43 +45,183 @@ public class Grid extends JPanel {
|
|||
for (int i = 0; i < this.height; i++) {
|
||||
for (int j = 0; j < this.width; j++) {
|
||||
this.mat[i][j] = new JButton();
|
||||
switch (this.universe.getGrid()[i + 1][j + 1]) {
|
||||
case -1:
|
||||
this.mat[i][j].setBackground(Color.RED);
|
||||
break;
|
||||
case 0:
|
||||
this.mat[i][j].setBackground(Color.WHITE);
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
this.mat[i][j].setPreferredSize(new Dimension(Math.max(50, 600 / this.height), Math.max(50, (this.width * 600 / this.height) / this.width)));
|
||||
|
||||
final int coord_i = i;
|
||||
final int coord_j = j;
|
||||
final int final_selected = this.selected;
|
||||
final Universe tempUniverse = this.universe;
|
||||
|
||||
this.mat[i][j].addActionListener(e -> {
|
||||
switch (this.universe.getGrid()[coord_i + 1][coord_j + 1]) {
|
||||
case 0:
|
||||
this.mat[coord_i][coord_j].setBackground(Color.RED);
|
||||
if (this.selected == 1) {
|
||||
this.universe.changeUniverseStart(coord_i + 1, coord_j + 1, 10);
|
||||
break;
|
||||
}
|
||||
|
||||
this.universe.addObstacle(coord_i + 1, coord_j + 1);
|
||||
break;
|
||||
case -1:
|
||||
this.mat[coord_i][coord_j].setBackground(Color.WHITE);
|
||||
this.universe.removeObstacle(coord_i + 1, coord_j + 1);
|
||||
break;
|
||||
case 10:
|
||||
if (this.selected == 1) {
|
||||
this.universe.changeUniverseStart(coord_i + 1, coord_j + 1, 11);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 11:
|
||||
if (this.selected == 1) {
|
||||
this.universe.changeUniverseStart(coord_i + 1, coord_j + 1, 12);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 12:
|
||||
if (this.selected == 1) {
|
||||
this.universe.changeUniverseStart(coord_i + 1, coord_j + 1, 13);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 13:
|
||||
if (this.selected == 1) {
|
||||
this.universe.changeUniverseStart(coord_i + 1, coord_j + 1, 10);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
//Universe.print(this.universe.getGrid(), this.width + 2, this.height + 2, 4, 4);
|
||||
this.printUniverse();
|
||||
});
|
||||
|
||||
super.add(this.mat[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
this.printUniverse();
|
||||
}
|
||||
|
||||
public void setSelected(int selected) {
|
||||
this.selected = selected;
|
||||
}
|
||||
|
||||
public void setButtonsEnabled(boolean enabled) {
|
||||
for (int i = 0; i < this.height; i++) {
|
||||
for (int j = 0; j < this.width; j ++) {
|
||||
this.mat[i][j].setEnabled(enabled);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void printUniverse() {
|
||||
for (int i = 0; i < this.height; i++) {
|
||||
for (int j = 0; j < this.width; j ++) {
|
||||
Image photo;
|
||||
switch (this.universe.getGrid()[i + 1][j + 1]) {
|
||||
case -1:
|
||||
photo = new ImageIcon(this.getClass().getResource("../images/wall.png")).getImage();
|
||||
this.mat[i][j].setIcon(new ImageIcon(photo));
|
||||
break;
|
||||
case 0:
|
||||
this.mat[i][j].setBackground(Color.WHITE);
|
||||
this.mat[i][j].setIcon(null);
|
||||
break;
|
||||
case 1:
|
||||
photo = new ImageIcon(this.getClass().getResource("../images/verticalLaser.png")).getImage();
|
||||
this.mat[i][j].setIcon(new ImageIcon(photo));
|
||||
break;
|
||||
case 2:
|
||||
photo = new ImageIcon(this.getClass().getResource("../images/horizontalLaser.png")).getImage();
|
||||
this.mat[i][j].setIcon(new ImageIcon(photo));
|
||||
break;
|
||||
case 3:
|
||||
photo = new ImageIcon(this.getClass().getResource("../images/wall.png")).getImage();
|
||||
this.mat[i][j].setIcon(new ImageIcon(photo));
|
||||
break;
|
||||
case 4:
|
||||
photo = new ImageIcon(this.getClass().getResource("../images/wall.png")).getImage();
|
||||
this.mat[i][j].setIcon(new ImageIcon(photo));
|
||||
break;
|
||||
case 5:
|
||||
photo = new ImageIcon(this.getClass().getResource("../images/wall.png")).getImage();
|
||||
this.mat[i][j].setIcon(new ImageIcon(photo));
|
||||
break;
|
||||
case 10:
|
||||
photo = new ImageIcon(this.getClass().getResource("../images/startUp.png")).getImage();
|
||||
this.mat[i][j].setIcon(new ImageIcon(photo));
|
||||
break;
|
||||
case 11:
|
||||
photo = new ImageIcon(this.getClass().getResource("../images/startBot.png")).getImage();
|
||||
this.mat[i][j].setIcon(new ImageIcon(photo));
|
||||
break;
|
||||
case 12:
|
||||
photo = new ImageIcon(this.getClass().getResource("../images/startRight.png")).getImage();
|
||||
this.mat[i][j].setIcon(new ImageIcon(photo));
|
||||
break;
|
||||
case 13:
|
||||
photo = new ImageIcon(this.getClass().getResource("../images/startLeft.png")).getImage();
|
||||
this.mat[i][j].setIcon(new ImageIcon(photo));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
this.universe.resetUniverse();
|
||||
this.printUniverse();
|
||||
}
|
||||
|
||||
public void solve() {
|
||||
this.setButtonsEnabled(false);
|
||||
this.universe.resetUniverse();
|
||||
final Universe universe = this.universe;
|
||||
|
||||
Thread t1 = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
int [] startCoords = universe.getStartCoords();
|
||||
|
||||
int firstState_i = startCoords[0];
|
||||
int firstState_j = startCoords[1];
|
||||
int start_dir = universe.getGrid()[startCoords[0]][startCoords[1]];
|
||||
|
||||
if (start_dir == 10) firstState_i = firstState_i - 1;
|
||||
else if (start_dir == 11) firstState_i = firstState_i + 1;
|
||||
else if (start_dir == 12) firstState_j = firstState_j + 1;
|
||||
else if (start_dir == 13) firstState_j = firstState_j - 1;
|
||||
|
||||
Stack <Situation> stack = new Stack <Situation>();
|
||||
Situation currentState = new Situation(firstState_i, firstState_j, start_dir, 0);
|
||||
|
||||
int [][] bestGrid = universe.copyGrid();
|
||||
int best_filled_boxes = 0;
|
||||
int best_nb_mirrors = 0;
|
||||
|
||||
do {
|
||||
if (universe.canEvolve(currentState)) {
|
||||
stack.push(currentState.copy(universe.possibleChoices(currentState)));
|
||||
currentState = universe.evolve(currentState);
|
||||
|
||||
if ((universe.getFilledBoxes() > best_filled_boxes) || (universe.getFilledBoxes() == best_filled_boxes && universe.getNbMirrors() < best_nb_mirrors)) {
|
||||
bestGrid = universe.copyGrid();
|
||||
best_filled_boxes = universe.getFilledBoxes();
|
||||
best_nb_mirrors = universe.getNbMirrors();
|
||||
|
||||
printUniverse();
|
||||
}
|
||||
}
|
||||
else if (stack.size() > 0) {
|
||||
currentState = stack.pop();
|
||||
universe.reset(currentState);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} while (stack.size() != 0);
|
||||
|
||||
}
|
||||
});
|
||||
t1.start();
|
||||
|
||||
this.setButtonsEnabled(true);
|
||||
}
|
||||
}
|
|
@ -67,8 +67,9 @@ public class Window extends JFrame {
|
|||
buttonGroup.add(radioWall);
|
||||
buttonGroup.add(radioStart);
|
||||
|
||||
JMenuItem changeSize = new JMenuItem("Change Size");
|
||||
JMenuItem solve = new JMenuItem("Solve");
|
||||
JMenuItem changeSize = new JMenuItem("Change Size");
|
||||
JMenuItem solve = new JMenuItem("Solve");
|
||||
JMenuItem reset = new JMenuItem("Reset");
|
||||
|
||||
fichierMenu.add(nouveauItem);
|
||||
fichierMenu.add(ouvrirItem);
|
||||
|
@ -83,6 +84,8 @@ public class Window extends JFrame {
|
|||
toolsMenu.add(changeSize);
|
||||
toolsMenu.addSeparator();
|
||||
toolsMenu.add(solve);
|
||||
toolsMenu.addSeparator();
|
||||
toolsMenu.add(reset);
|
||||
|
||||
menuBar.add(fichierMenu);
|
||||
menuBar.add(aideMenu);
|
||||
|
@ -105,11 +108,20 @@ public class Window extends JFrame {
|
|||
});
|
||||
|
||||
radioWall.addActionListener(e -> {
|
||||
this.grid.setSelected(1);
|
||||
this.grid.setSelected(0);
|
||||
});
|
||||
|
||||
radioStart.addActionListener(e -> {
|
||||
this.grid.setSelected(0);
|
||||
this.grid.setSelected(1);
|
||||
});
|
||||
|
||||
solve.addActionListener(e -> {
|
||||
this.grid.solve();
|
||||
});
|
||||
|
||||
|
||||
reset.addActionListener(e -> {
|
||||
this.grid.reset();
|
||||
});
|
||||
|
||||
this.grid = new Grid(this.universe.getHeight() - 2, this.universe.getWidth() - 2, this.universe);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue