very major improvements
This commit is contained in:
parent
3068c5ba94
commit
9afffd4297
8 changed files with 112 additions and 16 deletions
BIN
images/crossLaser.png
Normal file
BIN
images/crossLaser.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 298 B |
BIN
images/miror1.png
Normal file
BIN
images/miror1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 494 B |
BIN
images/miror2.png
Normal file
BIN
images/miror2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 520 B |
BIN
images/miror3.png
Normal file
BIN
images/miror3.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 502 B |
BIN
images/miror4.png
Normal file
BIN
images/miror4.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 517 B |
|
@ -79,6 +79,14 @@ public class Universe {
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 5:
|
case 5:
|
||||||
|
System.out.printf(" /");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 6:
|
||||||
|
System.out.printf(" \\");
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 7:
|
||||||
System.out.printf(" \\");
|
System.out.printf(" \\");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -295,16 +303,26 @@ public class Universe {
|
||||||
this.grid[i][j] = 3;
|
this.grid[i][j] = 3;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ((c == 3 && d == 10) || (c == 3 && d == 11) || (c == 2 && d == 12) || (c == 2 && d == 13)) {
|
if ((c == 3 && d == 10) || (c == 2 && d == 13)) {
|
||||||
this.grid[i][j] = 4;
|
this.grid[i][j] = 4;
|
||||||
this.filled_boxes ++;
|
this.filled_boxes ++;
|
||||||
this.nb_mirors ++;
|
this.nb_mirors ++;
|
||||||
}
|
}
|
||||||
if ((c == 2 && d == 10) || (c == 2 && d == 11) || (c == 3 && d == 12) || (c == 3 && d == 13)) {
|
if ((c == 2 && d == 12) || (c == 3 && d == 11)) {
|
||||||
this.grid[i][j] = 5;
|
this.grid[i][j] = 5;
|
||||||
this.filled_boxes ++;
|
this.filled_boxes ++;
|
||||||
this.nb_mirors ++;
|
this.nb_mirors ++;
|
||||||
}
|
}
|
||||||
|
if ((c == 2 && d == 10) || (c == 3 && d == 12)) {
|
||||||
|
this.grid[i][j] = 6;
|
||||||
|
this.filled_boxes ++;
|
||||||
|
this.nb_mirors ++;
|
||||||
|
}
|
||||||
|
if ((c == 2 && d == 11) || (c == 3 && d == 13)) {
|
||||||
|
this.grid[i][j] = 7;
|
||||||
|
this.filled_boxes ++;
|
||||||
|
this.nb_mirors ++;
|
||||||
|
}
|
||||||
|
|
||||||
// changing the position of the situation
|
// changing the position of the situation
|
||||||
|
|
||||||
|
@ -340,7 +358,7 @@ public class Universe {
|
||||||
this.grid[i][j] = 1;
|
this.grid[i][j] = 1;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (this.grid[i][j] == 4 || this.grid[i][j] == 5) {this.nb_mirors--;}
|
if (this.grid[i][j] == 4 || this.grid[i][j] == 5 || this.grid[i][j] == 6 || this.grid[i][j] == 7) {this.nb_mirors--;}
|
||||||
this.filled_boxes--;
|
this.filled_boxes--;
|
||||||
this.grid[i][j] = 0;
|
this.grid[i][j] = 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,6 +32,7 @@ public class Grid extends JPanel {
|
||||||
private JButton[][] mat;
|
private JButton[][] mat;
|
||||||
private int selected;
|
private int selected;
|
||||||
private Universe universe;
|
private Universe universe;
|
||||||
|
private int button_width, button_height;
|
||||||
|
|
||||||
public Grid(int width, int height, Universe universe) {
|
public Grid(int width, int height, Universe universe) {
|
||||||
super(new GridLayout(height, width));
|
super(new GridLayout(height, width));
|
||||||
|
@ -39,6 +40,8 @@ public class Grid extends JPanel {
|
||||||
this.height = height;
|
this.height = height;
|
||||||
this.selected = 0;
|
this.selected = 0;
|
||||||
this.universe = universe;
|
this.universe = universe;
|
||||||
|
this.button_width = 600 / this.height;
|
||||||
|
this.button_height = button_width;
|
||||||
|
|
||||||
this.mat = new JButton[height][width];
|
this.mat = new JButton[height][width];
|
||||||
|
|
||||||
|
@ -46,7 +49,7 @@ public class Grid extends JPanel {
|
||||||
for (int j = 0; j < this.width; j++) {
|
for (int j = 0; j < this.width; j++) {
|
||||||
this.mat[i][j] = new JButton();
|
this.mat[i][j] = new JButton();
|
||||||
|
|
||||||
this.mat[i][j].setPreferredSize(new Dimension(Math.max(50, 600 / this.height), Math.max(50, (this.width * 600 / this.height) / this.width)));
|
this.mat[i][j].setPreferredSize(new Dimension(this.button_width, this.button_height));
|
||||||
|
|
||||||
final int coord_i = i;
|
final int coord_i = i;
|
||||||
final int coord_j = j;
|
final int coord_j = j;
|
||||||
|
@ -118,7 +121,7 @@ public class Grid extends JPanel {
|
||||||
Image photo;
|
Image photo;
|
||||||
switch (this.universe.getGrid()[i + 1][j + 1]) {
|
switch (this.universe.getGrid()[i + 1][j + 1]) {
|
||||||
case -1:
|
case -1:
|
||||||
photo = new ImageIcon(this.getClass().getResource("../images/wall.png")).getImage();
|
photo = new ImageIcon(this.getClass().getResource("../images/wall.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH);
|
||||||
this.mat[i][j].setIcon(new ImageIcon(photo));
|
this.mat[i][j].setIcon(new ImageIcon(photo));
|
||||||
break;
|
break;
|
||||||
case 0:
|
case 0:
|
||||||
|
@ -126,39 +129,47 @@ public class Grid extends JPanel {
|
||||||
this.mat[i][j].setIcon(null);
|
this.mat[i][j].setIcon(null);
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
photo = new ImageIcon(this.getClass().getResource("../images/verticalLaser.png")).getImage();
|
photo = new ImageIcon(this.getClass().getResource("../images/verticalLaser.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH);
|
||||||
this.mat[i][j].setIcon(new ImageIcon(photo));
|
this.mat[i][j].setIcon(new ImageIcon(photo));
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
photo = new ImageIcon(this.getClass().getResource("../images/horizontalLaser.png")).getImage();
|
photo = new ImageIcon(this.getClass().getResource("../images/horizontalLaser.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH);
|
||||||
this.mat[i][j].setIcon(new ImageIcon(photo));
|
this.mat[i][j].setIcon(new ImageIcon(photo));
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
photo = new ImageIcon(this.getClass().getResource("../images/wall.png")).getImage();
|
photo = new ImageIcon(this.getClass().getResource("../images/crossLaser.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH);
|
||||||
this.mat[i][j].setIcon(new ImageIcon(photo));
|
this.mat[i][j].setIcon(new ImageIcon(photo));
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
photo = new ImageIcon(this.getClass().getResource("../images/wall.png")).getImage();
|
photo = new ImageIcon(this.getClass().getResource("../images/miror1.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH);
|
||||||
this.mat[i][j].setIcon(new ImageIcon(photo));
|
this.mat[i][j].setIcon(new ImageIcon(photo));
|
||||||
break;
|
break;
|
||||||
case 5:
|
case 5:
|
||||||
photo = new ImageIcon(this.getClass().getResource("../images/wall.png")).getImage();
|
photo = new ImageIcon(this.getClass().getResource("../images/miror2.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH);
|
||||||
|
this.mat[i][j].setIcon(new ImageIcon(photo));
|
||||||
|
break;
|
||||||
|
case 6:
|
||||||
|
photo = new ImageIcon(this.getClass().getResource("../images/miror3.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH);
|
||||||
|
this.mat[i][j].setIcon(new ImageIcon(photo));
|
||||||
|
break;
|
||||||
|
case 7:
|
||||||
|
photo = new ImageIcon(this.getClass().getResource("../images/miror4.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH);
|
||||||
this.mat[i][j].setIcon(new ImageIcon(photo));
|
this.mat[i][j].setIcon(new ImageIcon(photo));
|
||||||
break;
|
break;
|
||||||
case 10:
|
case 10:
|
||||||
photo = new ImageIcon(this.getClass().getResource("../images/startUp.png")).getImage();
|
photo = new ImageIcon(this.getClass().getResource("../images/startUp.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH);
|
||||||
this.mat[i][j].setIcon(new ImageIcon(photo));
|
this.mat[i][j].setIcon(new ImageIcon(photo));
|
||||||
break;
|
break;
|
||||||
case 11:
|
case 11:
|
||||||
photo = new ImageIcon(this.getClass().getResource("../images/startBot.png")).getImage();
|
photo = new ImageIcon(this.getClass().getResource("../images/startBot.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH);
|
||||||
this.mat[i][j].setIcon(new ImageIcon(photo));
|
this.mat[i][j].setIcon(new ImageIcon(photo));
|
||||||
break;
|
break;
|
||||||
case 12:
|
case 12:
|
||||||
photo = new ImageIcon(this.getClass().getResource("../images/startRight.png")).getImage();
|
photo = new ImageIcon(this.getClass().getResource("../images/startRight.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH);
|
||||||
this.mat[i][j].setIcon(new ImageIcon(photo));
|
this.mat[i][j].setIcon(new ImageIcon(photo));
|
||||||
break;
|
break;
|
||||||
case 13:
|
case 13:
|
||||||
photo = new ImageIcon(this.getClass().getResource("../images/startLeft.png")).getImage();
|
photo = new ImageIcon(this.getClass().getResource("../images/startLeft.png")).getImage().getScaledInstance(this.button_width, this.button_height, Image.SCALE_SMOOTH);
|
||||||
this.mat[i][j].setIcon(new ImageIcon(photo));
|
this.mat[i][j].setIcon(new ImageIcon(photo));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -207,6 +218,8 @@ 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);
|
||||||
|
|
||||||
printUniverse();
|
printUniverse();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,13 @@ import java.awt.GridBagConstraints;
|
||||||
import java.awt.GridLayout;
|
import java.awt.GridLayout;
|
||||||
import java.awt.Insets;
|
import java.awt.Insets;
|
||||||
|
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.event.*;
|
import java.awt.event.*;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
@ -91,6 +98,65 @@ public class Window extends JFrame {
|
||||||
menuBar.add(aideMenu);
|
menuBar.add(aideMenu);
|
||||||
menuBar.add(toolsMenu);
|
menuBar.add(toolsMenu);
|
||||||
|
|
||||||
|
nouveauItem.addActionListener(e -> {
|
||||||
|
this.universe.changeUniverseDim(5, 5);
|
||||||
|
this.universe.changeUniverseStart(1, 1, 11);
|
||||||
|
this.panel.remove(this.grid);
|
||||||
|
this.grid = new Grid(3, 3, this.universe);
|
||||||
|
this.panel.add(this.grid);
|
||||||
|
super.pack();
|
||||||
|
super.repaint();
|
||||||
|
});
|
||||||
|
|
||||||
|
enregistrerItem.addActionListener(e -> {
|
||||||
|
String name = JOptionPane.showInputDialog("Choose the universe name");
|
||||||
|
this.universe.save(name);
|
||||||
|
});
|
||||||
|
|
||||||
|
ouvrirItem.addActionListener(e -> {
|
||||||
|
String message = "Choose the universe among those : ";
|
||||||
|
|
||||||
|
Set<String> files = Stream.of(new File("./saves").listFiles()).filter(file -> !file.isDirectory()).map(File::getName).collect(Collectors.toSet());
|
||||||
|
|
||||||
|
for (String element : files) {
|
||||||
|
message += "\n- " + element.replace(".txt", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
String name = JOptionPane.showInputDialog(message);
|
||||||
|
|
||||||
|
try {
|
||||||
|
BufferedReader reader = new BufferedReader(new FileReader("./saves/" + name + ".txt"));
|
||||||
|
int universe_height = Integer.valueOf(reader.readLine());
|
||||||
|
int universe_width = Integer.valueOf(reader.readLine());
|
||||||
|
int start_dir = Integer.valueOf(reader.readLine());
|
||||||
|
int start_i = Integer.valueOf(reader.readLine());
|
||||||
|
int start_j = Integer.valueOf(reader.readLine());
|
||||||
|
|
||||||
|
|
||||||
|
this.universe.changeUniverseDim(universe_width + 2, universe_height + 2);
|
||||||
|
this.universe.changeUniverseStart(start_i, start_j, start_dir);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
int pos1 = Integer.valueOf(reader.readLine());
|
||||||
|
int pos2 = Integer.valueOf(reader.readLine());
|
||||||
|
|
||||||
|
this.universe.addObstacle(pos1, pos2);
|
||||||
|
}
|
||||||
|
catch (Exception error) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.panel.remove(this.grid);
|
||||||
|
this.grid = new Grid(universe_width, universe_height, this.universe);
|
||||||
|
this.panel.add(this.grid);
|
||||||
|
super.pack();
|
||||||
|
super.repaint();
|
||||||
|
}
|
||||||
|
catch (Exception error) {}
|
||||||
|
});
|
||||||
|
|
||||||
regles.addActionListener(e -> {
|
regles.addActionListener(e -> {
|
||||||
JOptionPane.showMessageDialog(this, "Définissez la taille du plateau ainsi que l'orientation du laser, enfin ajoutez des obstacles et laissez le programme trouver le bon chemin !");
|
JOptionPane.showMessageDialog(this, "Définissez la taille du plateau ainsi que l'orientation du laser, enfin ajoutez des obstacles et laissez le programme trouver le bon chemin !");
|
||||||
});
|
});
|
||||||
|
@ -119,7 +185,6 @@ public class Window extends JFrame {
|
||||||
this.grid.solve();
|
this.grid.solve();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
reset.addActionListener(e -> {
|
reset.addActionListener(e -> {
|
||||||
this.grid.reset();
|
this.grid.reset();
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue