This commit is contained in:
Ninja-Jambon 2024-03-29 11:20:32 +01:00
parent ae86967b79
commit 5016c1a888
2 changed files with 43 additions and 7 deletions

View file

@ -37,8 +37,9 @@ public class Grid extends JPanel {
private int button_width, button_height; private int button_width, button_height;
private int refreshRate; private int refreshRate;
private boolean solving; private boolean solving;
private int display;
public Grid(int width, int height, Universe universe, int refreshRate) { public Grid(int width, int height, Universe universe, int refreshRate, int display) {
super(new GridLayout(height, width)); super(new GridLayout(height, width));
this.width = width; this.width = width;
this.height = height; this.height = height;
@ -48,6 +49,7 @@ public class Grid extends JPanel {
this.button_height = button_width; this.button_height = button_width;
this.refreshRate = refreshRate; this.refreshRate = refreshRate;
this.solving = false; this.solving = false;
this.display = display;
this.mat = new JButton[height][width]; this.mat = new JButton[height][width];
@ -127,6 +129,10 @@ public class Grid extends JPanel {
this.solving = solving; this.solving = solving;
} }
public void setDisplay(int display) {
this.display = display;
}
public void printUniverseGrid(int [][] universeGrid) { 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 ++) {
@ -245,10 +251,20 @@ public class Grid extends JPanel {
stack.push(currentState.copy(universe.possibleChoices(currentState))); stack.push(currentState.copy(universe.possibleChoices(currentState)));
currentState = universe.evolve(currentState); currentState = universe.evolve(currentState);
if (display == 1 && Instant.now().toEpochMilli() - lastRefresh > refreshRate) {
lastRefresh = Instant.now().toEpochMilli();
printUniverseGrid(universe.getGrid());
}
if ((universe.getFilledBoxes() > best_filled_boxes) || (universe.getFilledBoxes() == best_filled_boxes && universe.getNbMirrors() < best_nb_mirrors)) { if ((universe.getFilledBoxes() > best_filled_boxes) || (universe.getFilledBoxes() == best_filled_boxes && universe.getNbMirrors() < best_nb_mirrors)) {
this.bestGrid = universe.copyGrid(); this.bestGrid = universe.copyGrid();
best_filled_boxes = universe.getFilledBoxes(); best_filled_boxes = universe.getFilledBoxes();
best_nb_mirrors = universe.getNbMirrors(); best_nb_mirrors = universe.getNbMirrors();
if (display == 2 && Instant.now().toEpochMilli() - lastRefresh > refreshRate) {
lastRefresh = Instant.now().toEpochMilli();
printUniverseGrid(this.bestGrid);
}
} }
} }
else if (stack.size() > 0) { else if (stack.size() > 0) {
@ -257,9 +273,9 @@ public class Grid extends JPanel {
} else { } else {
break; break;
} }
if (Instant.now().toEpochMilli() - lastRefresh > refreshRate) { if (display == 0 && Instant.now().toEpochMilli() - lastRefresh > refreshRate) {
lastRefresh = Instant.now().toEpochMilli(); lastRefresh = Instant.now().toEpochMilli();
printUniverseGrid(this.bestGrid); printUniverseGrid(universe.getGrid());
} }
} while (stack.size() != 0 && solving == true); } while (stack.size() != 0 && solving == true);

View file

@ -96,6 +96,10 @@ public class Window extends JFrame {
refreshRates.add(radio200ms); refreshRates.add(radio200ms);
refreshRates.add(radio500ms); refreshRates.add(radio500ms);
refreshRates.add(radio1000ms); refreshRates.add(radio1000ms);
JMenuItem displayAll = new JMenuItem("Display progress and regress");
JMenuItem displayProgress = new JMenuItem("Display only progress");
JMenuItem displayBest = new JMenuItem("Display best grid");
fichierMenu.add(nouveauItem); fichierMenu.add(nouveauItem);
fichierMenu.add(ouvrirItem); fichierMenu.add(ouvrirItem);
@ -120,6 +124,10 @@ public class Window extends JFrame {
refreshRate.add(radio1000ms); refreshRate.add(radio1000ms);
displayMenu.add(refreshRate); displayMenu.add(refreshRate);
displayMenu.addSeparator();
displayMenu.add(displayAll);
displayMenu.add(displayProgress);
displayMenu.add(displayBest);
menuBar.add(fichierMenu); menuBar.add(fichierMenu);
menuBar.add(aideMenu); menuBar.add(aideMenu);
@ -132,7 +140,7 @@ public class Window extends JFrame {
this.universe.changeUniverseDim(5, 5); this.universe.changeUniverseDim(5, 5);
this.universe.resetUniverseObstacles(); this.universe.resetUniverseObstacles();
this.panel.remove(this.grid); this.panel.remove(this.grid);
this.grid = new Grid(3, 3, this.universe, 10); this.grid = new Grid(3, 3, this.universe, 10, 0);
this.panel.add(this.grid); this.panel.add(this.grid);
super.pack(); super.pack();
super.repaint(); super.repaint();
@ -180,7 +188,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, 10); this.grid = new Grid(universe_width, universe_height, this.universe, 10, 0);
this.panel.add(this.grid); this.panel.add(this.grid);
super.pack(); super.pack();
super.repaint(); super.repaint();
@ -198,7 +206,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, 10); this.grid = new Grid(width, height, this.universe, 10, 0);
this.panel.add(this.grid); this.panel.add(this.grid);
super.pack(); super.pack();
super.repaint(); super.repaint();
@ -235,8 +243,20 @@ public class Window extends JFrame {
radio1000ms.addActionListener(e -> { radio1000ms.addActionListener(e -> {
this.grid.setRefreshRate(1000); this.grid.setRefreshRate(1000);
}); });
displayAll.addActionListener(e -> {
this.grid.setDisplay(0);
});
displayProgress.addActionListener(e -> {
this.grid.setDisplay(1);
});
displayBest.addActionListener(e -> {
this.grid.setDisplay(2);
});
this.grid = new Grid(this.universe.getHeight() - 2, this.universe.getWidth() - 2, this.universe, 10); this.grid = new Grid(this.universe.getHeight() - 2, this.universe.getWidth() - 2, this.universe, 10, 0);
this.panel.add(grid, BorderLayout.CENTER); this.panel.add(grid, BorderLayout.CENTER);
super.setJMenuBar(menuBar); super.setJMenuBar(menuBar);