From 3068c5ba94852687ab597fe24f21c21905ed5c7e Mon Sep 17 00:00:00 2001 From: Lukian LEIZOUR Date: Thu, 28 Mar 2024 12:00:57 +0100 Subject: [PATCH] major improvements --- AppLaser.java | 8 +- images/horizontalLaser.png | Bin 0 -> 232 bytes images/startBot.png | Bin 0 -> 424 bytes images/startLeft.png | Bin 0 -> 456 bytes images/startRight.png | Bin 0 -> 451 bytes images/startUp.png | Bin 0 -> 475 bytes images/verticalLaser.png | Bin 0 -> 229 bytes images/wall.png | Bin 0 -> 311 bytes universe/Universe.java | 22 +++-- userInterface/Grid.java | 170 ++++++++++++++++++++++++++++++++++--- userInterface/Window.java | 20 ++++- 11 files changed, 190 insertions(+), 30 deletions(-) create mode 100644 images/horizontalLaser.png create mode 100644 images/startBot.png create mode 100644 images/startLeft.png create mode 100644 images/startRight.png create mode 100644 images/startUp.png create mode 100644 images/verticalLaser.png create mode 100644 images/wall.png diff --git a/AppLaser.java b/AppLaser.java index b37f5c2..4ef30f9 100644 --- a/AppLaser.java +++ b/AppLaser.java @@ -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 stack = new Stack (); - 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 stack = new Stack (); + 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(); } diff --git a/images/horizontalLaser.png b/images/horizontalLaser.png new file mode 100644 index 0000000000000000000000000000000000000000..0eb7a42c3035d9e8df840feaca0c79aee8f6f437 GIT binary patch literal 232 zcmeAS@N?(olHy`uVBq!ia0vp^Mj*_=1SBWM%0B~AjKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBugD~Uq{1quc!DLSt$B+p3w^s}W8yo~!4sLf8Wn}!fcLB4Y ze^cX0y%#G*FPyx4OZl)?;?xaZ5vps1!Z=sAgdPHNuT|e;-ahZKl*JmOHJesW|6$*i zh@obKmvUrvy~)8zPi_9To&j3Nmm1-j=IhI#1!QvoF$i1=o(!TqUHx3vIVCg!0Pz(IBm9K^v!ZEe+9Gy6y8sGr?E0?^^V|J#j%RGEBQLSvWuWZ{nvc%X?>+ zDt^k+-OYNvuJOvo+iCwZ&fb|axlgXCKQVf(^0Y59k*{XGc&&6#Ve^a^-|y`TNi(^4 zPPcyInY=}R)eU3SW@gNH?KHaT(yrq*(bA{n*rEkz(iib{-!a M>FVdQ&MBb@05rp@`~Uy| literal 0 HcmV?d00001 diff --git a/images/startLeft.png b/images/startLeft.png new file mode 100644 index 0000000000000000000000000000000000000000..a40ccc3629c68fb891907561503bb21470b0d01a GIT binary patch literal 456 zcmeAS@N?(olHy`uVBq!ia0vp^Mj*_=1SBWM%0B~AjKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBugD~Uq{1quc4bGk}jv*25XKzNYJ7mD)dhrmCRdU)L#VO34 zzd9aW@yj{>yS=;R%F@eX2cK?z|66E%d7aF=&-(U~eu-~E^R3FUiBqqK2}C}f)UTkt zKPkZCRc@GIuXFe{uTHaI`AI1YJJUBGb&{Fc`l+#Xj=6XH?Q_h@&d8A4@2*@J#ddWzYh$Ie-`hE(K2pQJ$`TF6*2UngDVzy+Z&1 literal 0 HcmV?d00001 diff --git a/images/startRight.png b/images/startRight.png new file mode 100644 index 0000000000000000000000000000000000000000..108a4275a90fa1b1323e6c53f1e3477dca9ec454 GIT binary patch literal 451 zcmeAS@N?(olHy`uVBq!ia0vp^Mj*_=1SBWM%0B~AjKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBugD~Uq{1quc4R)R`jv*25XK%diYcddMP5i>QBl-Od?*q-b z9;$24T%Y;tU*t)(*OL>sNyz`+r~m)@=6@yMe}(AZIqLJ8>C^8+%dUP~uJdHd#)uQj z%9|!c9!pi6-E+=~Es~8pGu2aP$)v!D+uN>&-#?l9b;@?#XDbdxUEdlLH)+px!?h8Y zXQropG~al2M(UTT+fOz{#&C#h&2QRFU=B|n`)q_cRJ0OENiUiR7(vgn-hI zNP}|KDMyzmBsTT4EME6o=NQZJ*cz=F^LC#|aw{-vU~gg;juT^UYEHBfo?~fz_R-1* zAwvDE$B$Hs^|QE7Gf=en`9h`pJnLsx9!@@aoAi$h6L$1i9<2O4;i!7k$=L>49XI}_ zeO#RICd`LBsBY7b?cFDt+F}-~Pc$u1>^#o6__=j=eM_NMPkQ3|xl?r?M@gJda^sJ* zi<4N_`tgjxt5-UQSFGO5vv}P(qn=$>r?Ch`ozzOYay7Co@Ou5ZUx!j8i^CU1)@R+e sW4yLaJjoz;<#u4O^QA_3ruq6ZXaU(AKnwzxf+vG0Pgg&ebxsLQ0JT86OaK4? literal 0 HcmV?d00001 diff --git a/images/verticalLaser.png b/images/verticalLaser.png new file mode 100644 index 0000000000000000000000000000000000000000..6e9a01b0ee50fb523b6c7fd68e596c9e1a6995c5 GIT binary patch literal 229 zcmeAS@N?(olHy`uVBq!ia0vp^20(1Y!2~3yt~h@iNHG=%xjQkeJ16rJ$YDu$^mSxl z*x1kgCy^D%=PdAuEM{QfI}E~%$MaXD00k2~T^vIq+~1yC$lDMg!0LEQ!BOGGe(_fe zI{2AdZ?fG6|Ujwv@ gFEzq5&DWPf3&`dGVi33#JQ+lJy85}Sb4q9e0HxVTZ~y=R literal 0 HcmV?d00001 diff --git a/images/wall.png b/images/wall.png new file mode 100644 index 0000000000000000000000000000000000000000..6b1825e7aeb8c8edca8c2a64a6333e4ec0d6304b GIT binary patch literal 311 zcmeAS@N?(olHy`uVBq!ia0vp^Mj*_=1SBWM%0B~AjKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBugD~Uq{1quc!A+hnjv*25Z*LlM9X8-;O}xM7`m2l>*)>W= zo3y=7N@rzO{1;;9`Bfn1d~0>e(@3SH?&lwK-o;5LpLsepuS({8YG3Bw@_K*6$cJaH zx%-$z?#|hC$#>f66T1W>JyH}Wwz`N3ff!TMm)`gOE_Pb)?9+&E3a`VFq~K~?r%lr< zp1dt0YEh1Pro4`>tC&wzO6IPY1;rn(iHEg5lHX*{>MJ?TfNTyR27yb#lR=cHtDnm{r-UW|-HdB? literal 0 HcmV?d00001 diff --git a/universe/Universe.java b/universe/Universe.java index 4c3bd78..c7bfc2d 100644 --- a/universe/Universe.java +++ b/universe/Universe.java @@ -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; + } } diff --git a/userInterface/Grid.java b/userInterface/Grid.java index f214ce0..154330e 100644 --- a/userInterface/Grid.java +++ b/userInterface/Grid.java @@ -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 stack = new Stack (); + 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); + } } \ No newline at end of file diff --git a/userInterface/Window.java b/userInterface/Window.java index 7378677..f3e7708 100644 --- a/userInterface/Window.java +++ b/userInterface/Window.java @@ -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);