From e7422c031154c4a5d09b030fe9ad3320f1fdc938 Mon Sep 17 00:00:00 2001 From: Lukian LEIZOUR Date: Wed, 28 Feb 2024 18:39:36 +0100 Subject: [PATCH] optimizations and comments --- AppLaser.java | 48 ++++++++++++++++++++++-------------------------- Universe.java | 49 +++++++++++-------------------------------------- 2 files changed, 33 insertions(+), 64 deletions(-) diff --git a/AppLaser.java b/AppLaser.java index 16ed2d6..3e06a46 100644 --- a/AppLaser.java +++ b/AppLaser.java @@ -1,39 +1,35 @@ // Antoine CRETUAL, Lukian LEIZOUR, 14/02/2024 public class AppLaser { - public static void main(String [] args) { - int start_i = 3; - int start_j = 1; - int start_dir = 10; - int firstState_i = 0; - int firstState_j = 0; - if (start_dir == 10) { - firstState_i = start_i - 1; - firstState_j = start_j; - } - else if (start_dir == 11) { - firstState_i = start_i + 1; - firstState_j = start_j; - } - else if (start_dir == 12) { - firstState_i = start_i; - firstState_j = start_j + 1; - } - else if (start_dir == 13) { - firstState_i = start_i; - firstState_j = start_j - 1; - } + // definitions of the starting position and the universe dimensions + int start_i = 3; + int start_j = 1; + int start_dir = 11; + int universe_width = 6; + int universe_height = 6; + int firstState_i = start_i; + int firstState_j = start_j; + + if (start_dir == 10) firstState_i = start_i - 1; + else if (start_dir == 11) firstState_i = start_i + 1; + else if (start_dir == 12) firstState_j = start_j + 1; + else if (start_dir == 13) firstState_j = start_j - 1; + + // creation of the stack, the universe and the first state Stack stack = new Stack (); + Universe universe = new Universe(universe_width + 2, universe_height + 2, start_i, start_j, start_dir); + Situation currentState = new Situation(firstState_i, firstState_j, start_dir, 0); - Universe universe = new Universe(8, 8, start_i, start_j, start_dir); + // obstacles creation - Situation previousState, currentState = new Situation(firstState_i, firstState_j, start_dir, 0); - - //universe.addObstacle(4, 4); + universe.addObstacle(2, 3); + universe.addObstacle(3, 3); + universe.addObstacle(2, 4); + universe.addObstacle(3, 4); universe.print(); diff --git a/Universe.java b/Universe.java index cef67f3..8d05109 100644 --- a/Universe.java +++ b/Universe.java @@ -4,42 +4,35 @@ public class Universe { // Atributes private int[][] grid; - private int length, height; + private int width, height; private int boxes_to_fill; // Constructors - public Universe(int length, int height, int i_start, int j_start, int dir_start) { - this.grid = new int[height][length]; + public Universe(int width, int height, int i_start, int j_start, int dir_start) { + this.grid = new int[height][width]; this.height = height; - this.length = length; + this.width = width; + this.boxes_to_fill = (width - 2) * (height - 2) - 1; int i, j; for (i = 1; i < this.height - 1; i++) { - for (j = 1; j < this.length - 1; j++) { + for (j = 1; j < this.width - 1; j++) { this.grid[i][j] = 0; } } for (i = 0; i < this.height; i++) { this.grid[i][0] = -1; - this.grid[i][length - 1] = -1; + this.grid[i][width - 1] = -1; } - for (j = 0; j < this.length; j++) { + for (j = 0; j < this.width; j++) { this.grid[0][j] = -1; this.grid[height - 1][j] = -1; } this.grid[i_start][j_start] = dir_start; - - this.boxes_to_fill = 0; - - for (i = 1; i < this.height - 1; i++) { - for (j = 1; j < this.length - 1; j++) { - if (this.grid[i][j] == 0) this.boxes_to_fill++; - } - } } // Methods @@ -47,6 +40,7 @@ public class Universe { public void addObstacle(int pos_i, int pos_j) { if (this.grid[pos_i][pos_j] == 0) { this.grid[pos_i][pos_j] = -1; + this.boxes_to_fill--; } else {} } @@ -151,23 +145,7 @@ public class Universe { int d = s.direction; int c = possibleChoices(s); - // adding the miror - - /*switch (c) { - case 1: - this.grid[i][j] = 1; - break; - - case 2: - this.grid[i][j] = 2; - break; - - case 3: - this.grid[i][j] = 3; - break; - - default: - }*/ + // new status of the box if (c == 1 && (d == 10 || d == 11)) this.grid[i][j] = 1; if (c == 1 && (d == 12 || d == 13)) this.grid[i][j] = 2; @@ -207,14 +185,10 @@ public class Universe { return this.boxes_to_fill - 1 == 0; } - /*public Situation restore(Situation current, Situation prev) { - - }*/ - public void print() { int i, j; for (i = 0; i < this.height; i++) { - for (j = 0; j < this.length; j++) { + for (j = 0; j < this.width; j++) { switch (this.grid[i][j]) { case -1: System.out.printf(" X"); @@ -258,7 +232,6 @@ public class Universe { default: System.out.printf("%3d", this.grid[i][j]); - } } System.out.print("\n");