major improvements

This commit is contained in:
Lukian LEIZOUR 2024-03-28 12:00:57 +01:00
parent fc18dea7f4
commit 3068c5ba94
11 changed files with 190 additions and 30 deletions

View file

@ -48,11 +48,10 @@ public class AppLaser {
else if (start_dir == 12) firstState_j = start_j + 1;
else if (start_dir == 13) firstState_j = start_j - 1;
Stack <Situation> stack = new Stack <Situation>();
Universe universe = new Universe(universe_width + 2, universe_height + 2, start_i + 1, start_j + 1, start_dir);
Situation currentState = new Situation(firstState_i + 1, firstState_j + 1, start_dir, 0);
if (cli == true) {
Stack <Situation> stack = new Stack <Situation>();
Universe universe = new Universe(universe_width + 2, universe_height + 2, start_i + 1, start_j + 1, start_dir);
Situation currentState = new Situation(firstState_i + 1, firstState_j + 1, start_dir, 0);
Scanner scanner = new Scanner(System.in);
int choice;
@ -316,6 +315,7 @@ public class AppLaser {
} while (choice != 0);
}
else {
Universe universe = new Universe(universe_width + 2, universe_height + 2, start_i + 1, start_j + 1, start_dir);
UserInterface userInterface = new UserInterface(universe);
userInterface.start();
}

BIN
images/horizontalLaser.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 232 B

BIN
images/startBot.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 424 B

BIN
images/startLeft.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 456 B

BIN
images/startRight.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 451 B

BIN
images/startUp.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 475 B

BIN
images/verticalLaser.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 229 B

BIN
images/wall.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 311 B

View file

@ -10,6 +10,7 @@ public class Universe {
private int[][] grid;
private int width, height;
private int start_i, start_j;
private int filled_boxes;
private int nb_mirors;
private String name;
@ -20,6 +21,8 @@ public class Universe {
this.grid = new int[height][width];
this.height = height;
this.width = width;
this.start_i = i_start;
this.start_j = j_start;
this.filled_boxes = 0;
this.nb_mirors = 0;
@ -154,15 +157,11 @@ public class Universe {
}
public void changeUniverseStart(int pos_i, int pos_j, int dir) {
for (int i = 0; i < this.height; i++) {
for (int j = 0; j < this.width; j++) {
if (this.grid[i][j] == 10 || this.grid[i][j] == 11 || this.grid[i][j] == 12|| this.grid[i][j] == 13) {
this.grid[i][j] = 0;
}
}
}
this.grid[this.start_i][start_j] = 0;
this.grid[pos_i][pos_j] = dir;
this.start_i = pos_i;
this.start_j = pos_j;
}
public void addObstacle(int pos_i, int pos_j) {
@ -396,4 +395,11 @@ public class Universe {
public int getHeight() {
return this.height;
}
public int[] getStartCoords() {
int [] tab = new int[2];
tab[0] = this.start_i;
tab[1] = this.start_j;
return tab;
}
}

View file

@ -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);
}
}

View file

@ -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);