From fc18dea7f44dbcd749bb905048c70151fea26177 Mon Sep 17 00:00:00 2001 From: Ninja-Jambon Date: Wed, 27 Mar 2024 14:24:27 +0100 Subject: [PATCH] improvements --- universe/Universe.java | 7 +++++++ userInterface/Grid.java | 31 ++++++++++++++++++++++++++++--- userInterface/Window.java | 7 ++++--- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/universe/Universe.java b/universe/Universe.java index a100ec2..4c3bd78 100644 --- a/universe/Universe.java +++ b/universe/Universe.java @@ -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; diff --git a/userInterface/Grid.java b/userInterface/Grid.java index 9fc19f3..f214ce0 100644 --- a/userInterface/Grid.java +++ b/userInterface/Grid.java @@ -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]); diff --git a/userInterface/Window.java b/userInterface/Window.java index 2cad948..7378677 100644 --- a/userInterface/Window.java +++ b/userInterface/Window.java @@ -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);