improvements

This commit is contained in:
Ninja-Jambon 2024-03-27 14:24:27 +01:00
parent 491cd35136
commit fc18dea7f4
3 changed files with 39 additions and 6 deletions

View file

@ -172,6 +172,13 @@ public class Universe {
else {}
}
public void removeObstacle(int pos_i, int pos_j) {
if (this.grid[pos_i][pos_j] == -1) {
this.grid[pos_i][pos_j] = 0;
}
else {}
}
public int possibleChoices(Situation s) {
int i = s.pos_i;
int j = s.pos_j;

View file

@ -22,31 +22,56 @@ import javax.imageio.ImageIO;
import java.net.URL;
import universe.Universe;
public class Grid extends JPanel {
private int width;
private int height;
private JButton[][] mat;
private int selected;
private Universe universe;
public Grid(int width, int height) {
public Grid(int width, int height, Universe universe) {
super(new GridLayout(height, width));
this.width = width;
this.height = height;
this.selected = 0;
this.universe = universe;
this.mat = new JButton[height][width];
for (int i = 0; i < this.height; i++) {
for (int j = 0; j < this.width; j++) {
this.mat[i][j] = new JButton();
this.mat[i][j].setBackground(Color.WHITE);
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 -> {
System.out.println(coord_i + " " + coord_j);
switch (this.universe.getGrid()[coord_i + 1][coord_j + 1]) {
case 0:
this.mat[coord_i][coord_j].setBackground(Color.RED);
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;
}
//Universe.print(this.universe.getGrid(), this.width + 2, this.height + 2, 4, 4);
});
super.add(this.mat[i][j]);

View file

@ -18,7 +18,7 @@ import javax.imageio.ImageIO;
import java.net.URL;
import universe.*;
import universe.Universe;
public class Window extends JFrame {
@ -96,8 +96,9 @@ public class Window extends JFrame {
int width = Integer.valueOf(JOptionPane.showInputDialog("Choose the width"));
int height = Integer.valueOf(JOptionPane.showInputDialog("Choose the height"));
this.universe.changeUniverseDim(width + 2, height + 2);
this.panel.remove(this.grid);
this.grid = new Grid(width, height);
this.grid = new Grid(width, height, this.universe);
this.panel.add(this.grid);
super.pack();
super.repaint();
@ -111,7 +112,7 @@ public class Window extends JFrame {
this.grid.setSelected(0);
});
this.grid = new Grid(this.universe.getHeight() - 2, this.universe.getWidth() - 2);
this.grid = new Grid(this.universe.getHeight() - 2, this.universe.getWidth() - 2, this.universe);
this.panel.add(grid, BorderLayout.CENTER);
super.setJMenuBar(menuBar);