commit
This commit is contained in:
parent
9afffd4297
commit
2f51df0d82
2 changed files with 129 additions and 56 deletions
|
@ -26,6 +26,8 @@ import universe.Universe;
|
||||||
import universe.Stack;
|
import universe.Stack;
|
||||||
import universe.Situation;
|
import universe.Situation;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
|
||||||
public class Grid extends JPanel {
|
public class Grid extends JPanel {
|
||||||
private int width;
|
private int width;
|
||||||
private int height;
|
private int height;
|
||||||
|
@ -33,8 +35,10 @@ public class Grid extends JPanel {
|
||||||
private int selected;
|
private int selected;
|
||||||
private Universe universe;
|
private Universe universe;
|
||||||
private int button_width, button_height;
|
private int button_width, button_height;
|
||||||
|
private int refreshRate;
|
||||||
|
private boolean solving;
|
||||||
|
|
||||||
public Grid(int width, int height, Universe universe) {
|
public Grid(int width, int height, Universe universe, int refreshRate) {
|
||||||
super(new GridLayout(height, width));
|
super(new GridLayout(height, width));
|
||||||
this.width = width;
|
this.width = width;
|
||||||
this.height = height;
|
this.height = height;
|
||||||
|
@ -42,6 +46,8 @@ public class Grid extends JPanel {
|
||||||
this.universe = universe;
|
this.universe = universe;
|
||||||
this.button_width = 600 / this.height;
|
this.button_width = 600 / this.height;
|
||||||
this.button_height = button_width;
|
this.button_height = button_width;
|
||||||
|
this.refreshRate = refreshRate;
|
||||||
|
this.solving = false;
|
||||||
|
|
||||||
this.mat = new JButton[height][width];
|
this.mat = new JButton[height][width];
|
||||||
|
|
||||||
|
@ -55,6 +61,10 @@ public class Grid extends JPanel {
|
||||||
final int coord_j = j;
|
final int coord_j = j;
|
||||||
|
|
||||||
this.mat[i][j].addActionListener(e -> {
|
this.mat[i][j].addActionListener(e -> {
|
||||||
|
if (solving) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
switch (this.universe.getGrid()[coord_i + 1][coord_j + 1]) {
|
switch (this.universe.getGrid()[coord_i + 1][coord_j + 1]) {
|
||||||
case 0:
|
case 0:
|
||||||
if (this.selected == 1) {
|
if (this.selected == 1) {
|
||||||
|
@ -93,101 +103,113 @@ public class Grid extends JPanel {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.printUniverse();
|
this.printUniverseGrid(this.universe.getGrid());
|
||||||
});
|
});
|
||||||
|
|
||||||
super.add(this.mat[i][j]);
|
super.add(this.mat[i][j]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.printUniverse();
|
this.printUniverseGrid(this.universe.getGrid());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSelected(int selected) {
|
public void setSelected(int selected) {
|
||||||
this.selected = selected;
|
this.selected = selected;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setButtonsEnabled(boolean enabled) {
|
public void setRefreshRate(int refreshRate) {
|
||||||
for (int i = 0; i < this.height; i++) {
|
this.refreshRate = refreshRate;
|
||||||
for (int j = 0; j < this.width; j ++) {
|
|
||||||
this.mat[i][j].setEnabled(enabled);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void printUniverse() {
|
public void setSolving(boolean solving) {
|
||||||
|
this.solving = solving;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void printUniverseGrid(int [][] universeGrid) {
|
||||||
for (int i = 0; i < this.height; i++) {
|
for (int i = 0; i < this.height; i++) {
|
||||||
for (int j = 0; j < this.width; j ++) {
|
for (int j = 0; j < this.width; j ++) {
|
||||||
Image photo;
|
Image photo;
|
||||||
switch (this.universe.getGrid()[i + 1][j + 1]) {
|
switch (universeGrid[i + 1][j + 1]) {
|
||||||
case -1:
|
case -1:
|
||||||
photo = new ImageIcon(this.getClass().getResource("../images/wall.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH);
|
this.changeButtonState(i, j, "../images/wall.png");
|
||||||
this.mat[i][j].setIcon(new ImageIcon(photo));
|
|
||||||
break;
|
break;
|
||||||
case 0:
|
case 0:
|
||||||
this.mat[i][j].setBackground(Color.WHITE);
|
this.changeButtonState(i, j, null);
|
||||||
this.mat[i][j].setIcon(null);
|
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
photo = new ImageIcon(this.getClass().getResource("../images/verticalLaser.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH);
|
this.changeButtonState(i, j, "../images/verticalLaser.png");
|
||||||
this.mat[i][j].setIcon(new ImageIcon(photo));
|
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
photo = new ImageIcon(this.getClass().getResource("../images/horizontalLaser.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH);
|
this.changeButtonState(i, j, "../images/horizontalLaser.png");
|
||||||
this.mat[i][j].setIcon(new ImageIcon(photo));
|
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
photo = new ImageIcon(this.getClass().getResource("../images/crossLaser.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH);
|
this.changeButtonState(i, j, "../images/crossLaser.png");
|
||||||
this.mat[i][j].setIcon(new ImageIcon(photo));
|
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
photo = new ImageIcon(this.getClass().getResource("../images/miror1.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH);
|
this.changeButtonState(i, j, "../images/miror1.png");
|
||||||
this.mat[i][j].setIcon(new ImageIcon(photo));
|
|
||||||
break;
|
break;
|
||||||
case 5:
|
case 5:
|
||||||
photo = new ImageIcon(this.getClass().getResource("../images/miror2.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH);
|
this.changeButtonState(i, j, "../images/miror2.png");
|
||||||
this.mat[i][j].setIcon(new ImageIcon(photo));
|
|
||||||
break;
|
break;
|
||||||
case 6:
|
case 6:
|
||||||
photo = new ImageIcon(this.getClass().getResource("../images/miror3.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH);
|
this.changeButtonState(i, j, "../images/miror3.png");
|
||||||
this.mat[i][j].setIcon(new ImageIcon(photo));
|
|
||||||
break;
|
break;
|
||||||
case 7:
|
case 7:
|
||||||
photo = new ImageIcon(this.getClass().getResource("../images/miror4.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH);
|
this.changeButtonState(i, j, "../images/miror4.png");
|
||||||
this.mat[i][j].setIcon(new ImageIcon(photo));
|
|
||||||
break;
|
break;
|
||||||
case 10:
|
case 10:
|
||||||
photo = new ImageIcon(this.getClass().getResource("../images/startUp.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH);
|
this.changeButtonState(i, j, "../images/startUp.png");
|
||||||
this.mat[i][j].setIcon(new ImageIcon(photo));
|
|
||||||
break;
|
break;
|
||||||
case 11:
|
case 11:
|
||||||
photo = new ImageIcon(this.getClass().getResource("../images/startBot.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH);
|
this.changeButtonState(i, j, "../images/startBot.png");
|
||||||
this.mat[i][j].setIcon(new ImageIcon(photo));
|
|
||||||
break;
|
break;
|
||||||
case 12:
|
case 12:
|
||||||
photo = new ImageIcon(this.getClass().getResource("../images/startRight.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH);
|
this.changeButtonState(i, j, "../images/startRight.png");
|
||||||
this.mat[i][j].setIcon(new ImageIcon(photo));
|
|
||||||
break;
|
break;
|
||||||
case 13:
|
case 13:
|
||||||
photo = new ImageIcon(this.getClass().getResource("../images/startLeft.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH);
|
this.changeButtonState(i, j, "../images/startLeft.png");
|
||||||
this.mat[i][j].setIcon(new ImageIcon(photo));
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void changeButtonState(int coord_i, int coord_j, String url) {
|
||||||
|
if (url == null) {
|
||||||
|
Thread t = new Thread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
mat[coord_i][coord_j].setBackground(Color.WHITE);
|
||||||
|
mat[coord_i][coord_j].setIcon(null);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
t.start();
|
||||||
|
} else {
|
||||||
|
Thread t = new Thread(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
Image image = new ImageIcon(getClass().getResource(url)).getImage().getScaledInstance(button_width, button_height, Image.SCALE_SMOOTH);
|
||||||
|
mat[coord_i][coord_j].setIcon(new ImageIcon(image));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
t.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void reset() {
|
public void reset() {
|
||||||
this.universe.resetUniverse();
|
this.universe.resetUniverse();
|
||||||
this.printUniverse();
|
this.printUniverseGrid(this.universe.getGrid());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void solve() {
|
public void solve() {
|
||||||
this.setButtonsEnabled(false);
|
|
||||||
this.universe.resetUniverse();
|
this.universe.resetUniverse();
|
||||||
final Universe universe = this.universe;
|
final Universe universe = this.universe;
|
||||||
|
|
||||||
Thread t1 = new Thread(new Runnable() {
|
this.solving = true;
|
||||||
|
|
||||||
|
Thread computeThread = new Thread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
int [] startCoords = universe.getStartCoords();
|
int [] startCoords = universe.getStartCoords();
|
||||||
|
@ -208,6 +230,8 @@ public class Grid extends JPanel {
|
||||||
int best_filled_boxes = 0;
|
int best_filled_boxes = 0;
|
||||||
int best_nb_mirrors = 0;
|
int best_nb_mirrors = 0;
|
||||||
|
|
||||||
|
long lastPrinted = Instant.now().toEpochMilli();
|
||||||
|
|
||||||
do {
|
do {
|
||||||
if (universe.canEvolve(currentState)) {
|
if (universe.canEvolve(currentState)) {
|
||||||
stack.push(currentState.copy(universe.possibleChoices(currentState)));
|
stack.push(currentState.copy(universe.possibleChoices(currentState)));
|
||||||
|
@ -218,9 +242,9 @@ public class Grid extends JPanel {
|
||||||
best_filled_boxes = universe.getFilledBoxes();
|
best_filled_boxes = universe.getFilledBoxes();
|
||||||
best_nb_mirrors = universe.getNbMirrors();
|
best_nb_mirrors = universe.getNbMirrors();
|
||||||
|
|
||||||
System.out.println(best_filled_boxes + " " + best_nb_mirrors);
|
if (Instant.now().toEpochMilli() - lastPrinted > refreshRate) {
|
||||||
|
printUniverseGrid(bestGrid);
|
||||||
printUniverse();
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (stack.size() > 0) {
|
else if (stack.size() > 0) {
|
||||||
|
@ -229,12 +253,13 @@ public class Grid extends JPanel {
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} while (stack.size() != 0);
|
} while (stack.size() != 0 && solving == true);
|
||||||
|
|
||||||
|
solving = false;
|
||||||
|
Universe.print(bestGrid, width + 2, height + 2, 4, 4);
|
||||||
|
printUniverseGrid(bestGrid);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
t1.start();
|
computeThread.start();
|
||||||
|
|
||||||
this.setButtonsEnabled(true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -48,6 +48,9 @@ public class Window extends JFrame {
|
||||||
JMenu fichierMenu = new JMenu("File");
|
JMenu fichierMenu = new JMenu("File");
|
||||||
JMenu aideMenu = new JMenu("Help");
|
JMenu aideMenu = new JMenu("Help");
|
||||||
JMenu toolsMenu = new JMenu("Tools");
|
JMenu toolsMenu = new JMenu("Tools");
|
||||||
|
JMenu solveMenu = new JMenu("Solve");
|
||||||
|
JMenu displayMenu = new JMenu("Display");
|
||||||
|
JMenu refreshRate = new JMenu("Refresh Rate");
|
||||||
|
|
||||||
URL newUrl = getClass().getResource("../images/new.png");
|
URL newUrl = getClass().getResource("../images/new.png");
|
||||||
URL openUrl = getClass().getResource("../images/open.png");
|
URL openUrl = getClass().getResource("../images/open.png");
|
||||||
|
@ -57,9 +60,9 @@ public class Window extends JFrame {
|
||||||
ImageIcon openIcon = new ImageIcon(openUrl.getPath());
|
ImageIcon openIcon = new ImageIcon(openUrl.getPath());
|
||||||
ImageIcon saveIcon = new ImageIcon(saveUrl.getPath());
|
ImageIcon saveIcon = new ImageIcon(saveUrl.getPath());
|
||||||
|
|
||||||
JMenuItem nouveauItem = new JMenuItem("Nouveau", nouveauIcon);
|
JMenuItem nouveauItem = new JMenuItem("New", nouveauIcon);
|
||||||
JMenuItem ouvrirItem = new JMenuItem("Ouvrir", openIcon);
|
JMenuItem ouvrirItem = new JMenuItem("Open", openIcon);
|
||||||
JMenuItem enregistrerItem = new JMenuItem("Enregistrer", saveIcon);
|
JMenuItem enregistrerItem = new JMenuItem("Save", saveIcon);
|
||||||
|
|
||||||
JMenuItem apropos = new JMenuItem("About");
|
JMenuItem apropos = new JMenuItem("About");
|
||||||
JMenuItem regles = new JMenuItem("Rules");
|
JMenuItem regles = new JMenuItem("Rules");
|
||||||
|
@ -75,8 +78,24 @@ public class Window extends JFrame {
|
||||||
buttonGroup.add(radioStart);
|
buttonGroup.add(radioStart);
|
||||||
|
|
||||||
JMenuItem changeSize = new JMenuItem("Change Size");
|
JMenuItem changeSize = new JMenuItem("Change Size");
|
||||||
JMenuItem solve = new JMenuItem("Solve");
|
|
||||||
JMenuItem reset = new JMenuItem("Reset");
|
JMenuItem reset = new JMenuItem("Reset");
|
||||||
|
|
||||||
|
JMenuItem solve = new JMenuItem("Start");
|
||||||
|
JMenuItem stop = new JMenuItem("Stop");
|
||||||
|
|
||||||
|
JRadioButtonMenuItem radio10ms = new JRadioButtonMenuItem("10ms");
|
||||||
|
JRadioButtonMenuItem radio200ms = new JRadioButtonMenuItem("200ms");
|
||||||
|
JRadioButtonMenuItem radio500ms = new JRadioButtonMenuItem("500ms");
|
||||||
|
JRadioButtonMenuItem radio1000ms = new JRadioButtonMenuItem("1000ms");
|
||||||
|
|
||||||
|
radio10ms.setSelected(true);
|
||||||
|
|
||||||
|
ButtonGroup refreshRates = new ButtonGroup();
|
||||||
|
|
||||||
|
refreshRates.add(radio10ms);
|
||||||
|
refreshRates.add(radio200ms);
|
||||||
|
refreshRates.add(radio500ms);
|
||||||
|
refreshRates.add(radio1000ms);
|
||||||
|
|
||||||
fichierMenu.add(nouveauItem);
|
fichierMenu.add(nouveauItem);
|
||||||
fichierMenu.add(ouvrirItem);
|
fichierMenu.add(ouvrirItem);
|
||||||
|
@ -94,15 +113,28 @@ public class Window extends JFrame {
|
||||||
toolsMenu.addSeparator();
|
toolsMenu.addSeparator();
|
||||||
toolsMenu.add(reset);
|
toolsMenu.add(reset);
|
||||||
|
|
||||||
|
solveMenu.add(solve);
|
||||||
|
solveMenu.add(stop);
|
||||||
|
|
||||||
|
refreshRate.add(radio10ms);
|
||||||
|
refreshRate.add(radio200ms);
|
||||||
|
refreshRate.add(radio500ms);
|
||||||
|
refreshRate.add(radio1000ms);
|
||||||
|
|
||||||
|
displayMenu.add(refreshRate);
|
||||||
|
|
||||||
menuBar.add(fichierMenu);
|
menuBar.add(fichierMenu);
|
||||||
menuBar.add(aideMenu);
|
menuBar.add(aideMenu);
|
||||||
menuBar.add(toolsMenu);
|
menuBar.add(toolsMenu);
|
||||||
|
menuBar.add(solveMenu);
|
||||||
|
menuBar.add(displayMenu);
|
||||||
|
|
||||||
nouveauItem.addActionListener(e -> {
|
nouveauItem.addActionListener(e -> {
|
||||||
this.universe.changeUniverseDim(5, 5);
|
|
||||||
this.universe.changeUniverseStart(1, 1, 11);
|
this.universe.changeUniverseStart(1, 1, 11);
|
||||||
|
this.universe.changeUniverseDim(5, 5);
|
||||||
|
this.universe.resetUniverseObstacles();
|
||||||
this.panel.remove(this.grid);
|
this.panel.remove(this.grid);
|
||||||
this.grid = new Grid(3, 3, this.universe);
|
this.grid = new Grid(3, 3, this.universe, 10);
|
||||||
this.panel.add(this.grid);
|
this.panel.add(this.grid);
|
||||||
super.pack();
|
super.pack();
|
||||||
super.repaint();
|
super.repaint();
|
||||||
|
@ -149,7 +181,7 @@ public class Window extends JFrame {
|
||||||
}
|
}
|
||||||
|
|
||||||
this.panel.remove(this.grid);
|
this.panel.remove(this.grid);
|
||||||
this.grid = new Grid(universe_width, universe_height, this.universe);
|
this.grid = new Grid(universe_width, universe_height, this.universe, 10);
|
||||||
this.panel.add(this.grid);
|
this.panel.add(this.grid);
|
||||||
super.pack();
|
super.pack();
|
||||||
super.repaint();
|
super.repaint();
|
||||||
|
@ -167,7 +199,7 @@ public class Window extends JFrame {
|
||||||
|
|
||||||
this.universe.changeUniverseDim(width + 2, height + 2);
|
this.universe.changeUniverseDim(width + 2, height + 2);
|
||||||
this.panel.remove(this.grid);
|
this.panel.remove(this.grid);
|
||||||
this.grid = new Grid(width, height, this.universe);
|
this.grid = new Grid(width, height, this.universe, 10);
|
||||||
this.panel.add(this.grid);
|
this.panel.add(this.grid);
|
||||||
super.pack();
|
super.pack();
|
||||||
super.repaint();
|
super.repaint();
|
||||||
|
@ -185,11 +217,27 @@ public class Window extends JFrame {
|
||||||
this.grid.solve();
|
this.grid.solve();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
stop.addActionListener(e -> {
|
||||||
|
this.grid.setSolving(false);
|
||||||
|
});
|
||||||
|
|
||||||
reset.addActionListener(e -> {
|
reset.addActionListener(e -> {
|
||||||
this.grid.reset();
|
this.grid.reset();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
radio200ms.addActionListener(e -> {
|
||||||
|
this.grid.setRefreshRate(200);
|
||||||
|
});
|
||||||
|
|
||||||
|
radio500ms.addActionListener(e -> {
|
||||||
|
this.grid.setRefreshRate(500);
|
||||||
|
});
|
||||||
|
|
||||||
|
radio1000ms.addActionListener(e -> {
|
||||||
|
this.grid.setRefreshRate(1000);
|
||||||
|
});
|
||||||
|
|
||||||
this.grid = new Grid(this.universe.getHeight() - 2, this.universe.getWidth() - 2, this.universe);
|
this.grid = new Grid(this.universe.getHeight() - 2, this.universe.getWidth() - 2, this.universe, 10);
|
||||||
this.panel.add(grid, BorderLayout.CENTER);
|
this.panel.add(grid, BorderLayout.CENTER);
|
||||||
|
|
||||||
super.setJMenuBar(menuBar);
|
super.setJMenuBar(menuBar);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue