From af253263879263b81033807817ab1fa9aeb8dab7 Mon Sep 17 00:00:00 2001 From: Razansalam Date: Sun, 5 Apr 2026 10:07:39 +0400 Subject: [PATCH 1/4] Escape Maze --- .../org/example/fromrazan/EscapeMaze.java | 137 ++++++++++++++++++ .../org/example/fromrazan/Slithering.java | 4 + .../java/org/example/fromrazan/Snake.java | 104 +++++++++++++ .../fromrazan/TestEvaluation/MoveSnacke.java | 125 ++++++++++++++++ .../example/fromrazan/TestEvaluation/map.txt | 0 src/main/java/org/example/fromrazan/file.java | 4 + src/main/java/org/example/fromrazan/map.txt | 0 src/main/java/org/example/fromrazan/maze.txt | 0 8 files changed, 374 insertions(+) create mode 100644 src/main/java/org/example/fromrazan/EscapeMaze.java create mode 100644 src/main/java/org/example/fromrazan/Slithering.java create mode 100644 src/main/java/org/example/fromrazan/Snake.java create mode 100644 src/main/java/org/example/fromrazan/TestEvaluation/MoveSnacke.java create mode 100644 src/main/java/org/example/fromrazan/TestEvaluation/map.txt create mode 100644 src/main/java/org/example/fromrazan/file.java create mode 100644 src/main/java/org/example/fromrazan/map.txt create mode 100644 src/main/java/org/example/fromrazan/maze.txt diff --git a/src/main/java/org/example/fromrazan/EscapeMaze.java b/src/main/java/org/example/fromrazan/EscapeMaze.java new file mode 100644 index 00000000..4965ced1 --- /dev/null +++ b/src/main/java/org/example/fromrazan/EscapeMaze.java @@ -0,0 +1,137 @@ +package org.example.fromrazan; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; + + + + public class EscapeMaze { + + public static void main(String[] args) throws InterruptedException { + char[][] maze = loadAndGetMaze(); + int[] initialPlayerPosition = getPLayerLocation(maze); + ArrayList listOfMoves = new ArrayList<>(); + + // Predefined list of moves + listOfMoves.add(new int[]{9, 2}); + listOfMoves.add(new int[]{8, 2}); + listOfMoves.add(new int[]{7, 2}); + listOfMoves.add(new int[]{6, 2}); + listOfMoves.add(new int[]{5, 2}); + listOfMoves.add(new int[]{4, 2}); + listOfMoves.add(new int[]{4, 3}); + listOfMoves.add(new int[]{4, 4}); + + System.out.printf("Location of @ is (%d,%d)\n", initialPlayerPosition[0], initialPlayerPosition[1]); + + System.out.println("Before the change:"); + displayMaze(maze); + + // Processing + int[] currPlayerPosition = initialPlayerPosition; + + for (int[] currMove: listOfMoves) { + Thread.sleep(2000); + currPlayerPosition = makeMove(maze, currPlayerPosition, currMove); + + printEmptyLines(); + displayMaze(maze); + } + } + + public static int[] makeMove(char[][] maze, int[] sourcePosition, int[] targetPosition) { + char valueOnTargetLocation = maze[targetPosition[0]-1][targetPosition[1]-1]; + + // Check if target location is valid + if (valueOnTargetLocation == '0') { + System.out.println("Found a valid way forward"); + + // Mark the source location as tracked + maze[sourcePosition[0] - 1][sourcePosition[1] - 1] = '-'; + + // Move the player + maze[targetPosition[0] - 1][targetPosition[1] - 1] = '@'; + + return new int[]{targetPosition[0],targetPosition[1]}; + + + } else { + System.out.println("No way forward"); + + //stay in the same position + return sourcePosition; + } + + } + + public static void displayMaze(char[][] maze) { + for(int row=0; row < maze.length; row++) { + for(int col=0; col < maze[row].length; col++) { + System.out.printf("%c ", maze[row][col]); + } + System.out.println(); + } + } + + public static char[][] loadAndGetMaze() { + char[][] maze; + Path mazePath = null; + + try { + mazePath = Path.of(EscapeMaze.class.getResource(relativeMazePath).toURI()); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } + + try { + String fileContent = Files.readString(mazePath); + String[] linesOfFile = fileContent.split("\\R"); + + int lineLength = linesOfFile[0].trim().length(); + + maze = new char[linesOfFile.length][lineLength]; // Load it in 2D Array or Array of Arrays + + for (int row = 0; row < linesOfFile.length; row++) { + char[] currRow = linesOfFile[row].toCharArray(); + // System.out.printf("%s\n", linesOfFile[row]); + + for (int col = 0; col < currRow.length; col++) { + maze[row][col] = currRow[col]; + } + } + + } catch (IOException e) { + throw new RuntimeException(e); + } + return maze; + } + + public static int[] getPLayerLocation(char[][] maze) { + int[] location = new int[2]; + for (int row=0; row < maze.length; row++) { + for (int col=0; col < maze[row].length; col++) { + if (maze[row][col] == '@') { + location[0] = row + 1; + location[1] = col + 1; + } + } + } + return location; + } + + + public static void printEmptyLines() { + for (int count=0; count<18; count++) { + System.out.println(); + } + } + + public static final String relativeMazePath = "maze.txt"; + } + + + + diff --git a/src/main/java/org/example/fromrazan/Slithering.java b/src/main/java/org/example/fromrazan/Slithering.java new file mode 100644 index 00000000..6b565611 --- /dev/null +++ b/src/main/java/org/example/fromrazan/Slithering.java @@ -0,0 +1,4 @@ +package org.example.fromrazan; + +public class Slithering { +} diff --git a/src/main/java/org/example/fromrazan/Snake.java b/src/main/java/org/example/fromrazan/Snake.java new file mode 100644 index 00000000..a3f81794 --- /dev/null +++ b/src/main/java/org/example/fromrazan/Snake.java @@ -0,0 +1,104 @@ +package org.example.fromrazan; + +import java.util.ArrayList; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.LinkedList; + +public class Snacke { + public static void main(String[] args) + throws Exception { + + + Path mapPath = Path.of("src/main/java/org/example/fromrazan/TestEvaluation/map.txt"); + //read file + String snacke = Files.readString(mapPath); + + //split lines + + String[] linesOfFile = snacke.split("\n"); + int lineLength = linesOfFile[0].trim().split(" ").length; + + // char[][] map----> easy for print and audit + char[][] map = new char[linesOfFile.length][lineLength]; + + //reading the text data , fill map + + for (int r = 0; r < linesOfFile.length; r++) { + String[] parts = linesOfFile[r].trim().split(" "); + + for (int c = 0; c < parts.length; c++) { + map[r][c] = parts[c].charAt(0); + } + } + //print map + System.out.println(" Start : "); + PrintMap(map); + + //snake body + ArrayList snackes = new ArrayList<>(); + for (int r = 0; r < map.length; r++) { + for (int c = 0; c < lineLength; c++) { + if (map[r][c] == 'o') { + snackes.add(new int[]{r, c}); + + } + } + } + //tail ----> head + //snake movment + //calculate the new head + int mr = 0; + int mc = 1; + int steps = 1; + + + for (int s = 0; s < steps; s++) { + int[] head = snackes.get(snackes.size() -1); + + int newRow = head[0] + mr; + int newCol = head[1] + mc; + + //direction control + + String direction = "up"; + + if (direction.equals("up")) { + newRow--; + } else if (direction.equals("down")) { + newRow++; + } else if (direction.equals("right")) { + newCol++; + } else { + System.out.println("invalid"); + return; + } + //add head + snackes.add(new int[]{newRow, newCol}); + + //remove tail + int[] tail = snackes.remove(0); + map[tail[0]][tail[1]] = '-'; + + //update map + map[newRow][newCol] = 'o'; + map[tail[0]][tail[1]] = '-'; + + } + + //after move print the map + System.out.println("after move print the map: "); + PrintMap(map); + } + + public static void PrintMap(char[][] map) { + for (int r = 0; r < map.length; r++) { + + for (int c = 0; c < map[0].length; c++) { + System.out.print(map[r][c] + " "); + } + System.out.println(); + + } + } +} diff --git a/src/main/java/org/example/fromrazan/TestEvaluation/MoveSnacke.java b/src/main/java/org/example/fromrazan/TestEvaluation/MoveSnacke.java new file mode 100644 index 00000000..0d0d917f --- /dev/null +++ b/src/main/java/org/example/fromrazan/TestEvaluation/MoveSnacke.java @@ -0,0 +1,125 @@ +package org.example.fromrazan.TestEvaluation; + +import java.util.LinkedList; +import java.nio.file.Files; +import java.nio.file.Path; + + + public class MoveSnake { + public static void main(String [] args) + throws Exception{ + + + //file directory + + Path mapPath = Path.of("src/main/java/org/example/fromrazan/TestEvaluation/map.txt"); + + //read file + + String snake = Files.readString(mapPath); + //split lines + String [] linesOfFile = snake.split("\n"); + int lineLength = linesOfFile[0].trim().split(" ").length; + + // char[][] map----> easy for print and audit + + + char[][] map = new char[linesOfFile.length][lineLength]; + + + //reading the text data , fill map + + for (int r = 0; r < linesOfFile.length; r++){ + String[] parts = linesOfFile[r].trim().split(" "); + + for (int c = 0; c < parts.length; c++){ + map[r][c] = parts[c].charAt(0); + } + } + + //print map + System.out.println(" Start : "); + PrintMap(map); + + //snake body + LinkedList snakes = new LinkedList<>(); + for (int r = 0; r < map.length; r++){ + for(int c = 0; c < lineLength; c++) { + if (map[r][c] == 'o') { + snakes.add(new int[]{r, c}); + + + } + } + } + + //tail ----> head + //snake movment + //calculate the new head + int mr = 0; + int mc = 1; + int steps = 1; + + //move snake + for (int s = 0; s < steps; s++) { + + //get head + int[] head = snakes.getFirst(); + + int newRow = head[0] + mr; + int newCol = head[1] + mc; + + // direction control + switch (snake){ + case "up": + newRow++; + break; + + case "down": + newRow--; + break; + + case "right": + newCol++; + break; + + default: + System.out.println("error "); + return; + + + } + //add head + snakes.addFirst(new int[]{newRow, newCol}); + + //remove tail + int[] tail = snakes.removeLast(); + + //update map + map[newRow][newCol] = 'o'; + map[tail[0]][tail[1]] = '-'; + + } + //after move print the map + System.out.println("after move print the map: "); + PrintMap(map); + } + + public static void PrintMap(char[][] map){ + for (int r = 0; r < map.length; r++){ + + for (int c = 0; c < map[0].length; c++){ + System.out.print(map[r][c] + " "); + } + System.out.println(); + } + } + + } + + + + + + + diff --git a/src/main/java/org/example/fromrazan/TestEvaluation/map.txt b/src/main/java/org/example/fromrazan/TestEvaluation/map.txt new file mode 100644 index 00000000..e69de29b diff --git a/src/main/java/org/example/fromrazan/file.java b/src/main/java/org/example/fromrazan/file.java new file mode 100644 index 00000000..0863b10b --- /dev/null +++ b/src/main/java/org/example/fromrazan/file.java @@ -0,0 +1,4 @@ +package org.example.fromrazan; + +public class file { +} diff --git a/src/main/java/org/example/fromrazan/map.txt b/src/main/java/org/example/fromrazan/map.txt new file mode 100644 index 00000000..e69de29b diff --git a/src/main/java/org/example/fromrazan/maze.txt b/src/main/java/org/example/fromrazan/maze.txt new file mode 100644 index 00000000..e69de29b From 86ba8a9697c14de5a05447b0edd8f8d852056c6c Mon Sep 17 00:00:00 2001 From: Razansalam Date: Wed, 8 Apr 2026 09:43:07 +0400 Subject: [PATCH 2/4] maze --- .../org/example/fromrazan/Slithering.java | 4 - .../java/org/example/fromrazan/Snake.java | 104 --------------- .../fromrazan/TestEvaluation/MoveSnacke.java | 125 ------------------ .../example/fromrazan/TestEvaluation/map.txt | 0 src/main/java/org/example/fromrazan/file.java | 4 - src/main/java/org/example/fromrazan/map.txt | 0 src/main/java/org/example/fromrazan/maze.txt | 10 ++ 7 files changed, 10 insertions(+), 237 deletions(-) delete mode 100644 src/main/java/org/example/fromrazan/Slithering.java delete mode 100644 src/main/java/org/example/fromrazan/Snake.java delete mode 100644 src/main/java/org/example/fromrazan/TestEvaluation/MoveSnacke.java delete mode 100644 src/main/java/org/example/fromrazan/TestEvaluation/map.txt delete mode 100644 src/main/java/org/example/fromrazan/file.java delete mode 100644 src/main/java/org/example/fromrazan/map.txt diff --git a/src/main/java/org/example/fromrazan/Slithering.java b/src/main/java/org/example/fromrazan/Slithering.java deleted file mode 100644 index 6b565611..00000000 --- a/src/main/java/org/example/fromrazan/Slithering.java +++ /dev/null @@ -1,4 +0,0 @@ -package org.example.fromrazan; - -public class Slithering { -} diff --git a/src/main/java/org/example/fromrazan/Snake.java b/src/main/java/org/example/fromrazan/Snake.java deleted file mode 100644 index a3f81794..00000000 --- a/src/main/java/org/example/fromrazan/Snake.java +++ /dev/null @@ -1,104 +0,0 @@ -package org.example.fromrazan; - -import java.util.ArrayList; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.LinkedList; - -public class Snacke { - public static void main(String[] args) - throws Exception { - - - Path mapPath = Path.of("src/main/java/org/example/fromrazan/TestEvaluation/map.txt"); - //read file - String snacke = Files.readString(mapPath); - - //split lines - - String[] linesOfFile = snacke.split("\n"); - int lineLength = linesOfFile[0].trim().split(" ").length; - - // char[][] map----> easy for print and audit - char[][] map = new char[linesOfFile.length][lineLength]; - - //reading the text data , fill map - - for (int r = 0; r < linesOfFile.length; r++) { - String[] parts = linesOfFile[r].trim().split(" "); - - for (int c = 0; c < parts.length; c++) { - map[r][c] = parts[c].charAt(0); - } - } - //print map - System.out.println(" Start : "); - PrintMap(map); - - //snake body - ArrayList snackes = new ArrayList<>(); - for (int r = 0; r < map.length; r++) { - for (int c = 0; c < lineLength; c++) { - if (map[r][c] == 'o') { - snackes.add(new int[]{r, c}); - - } - } - } - //tail ----> head - //snake movment - //calculate the new head - int mr = 0; - int mc = 1; - int steps = 1; - - - for (int s = 0; s < steps; s++) { - int[] head = snackes.get(snackes.size() -1); - - int newRow = head[0] + mr; - int newCol = head[1] + mc; - - //direction control - - String direction = "up"; - - if (direction.equals("up")) { - newRow--; - } else if (direction.equals("down")) { - newRow++; - } else if (direction.equals("right")) { - newCol++; - } else { - System.out.println("invalid"); - return; - } - //add head - snackes.add(new int[]{newRow, newCol}); - - //remove tail - int[] tail = snackes.remove(0); - map[tail[0]][tail[1]] = '-'; - - //update map - map[newRow][newCol] = 'o'; - map[tail[0]][tail[1]] = '-'; - - } - - //after move print the map - System.out.println("after move print the map: "); - PrintMap(map); - } - - public static void PrintMap(char[][] map) { - for (int r = 0; r < map.length; r++) { - - for (int c = 0; c < map[0].length; c++) { - System.out.print(map[r][c] + " "); - } - System.out.println(); - - } - } -} diff --git a/src/main/java/org/example/fromrazan/TestEvaluation/MoveSnacke.java b/src/main/java/org/example/fromrazan/TestEvaluation/MoveSnacke.java deleted file mode 100644 index 0d0d917f..00000000 --- a/src/main/java/org/example/fromrazan/TestEvaluation/MoveSnacke.java +++ /dev/null @@ -1,125 +0,0 @@ -package org.example.fromrazan.TestEvaluation; - -import java.util.LinkedList; -import java.nio.file.Files; -import java.nio.file.Path; - - - public class MoveSnake { - public static void main(String [] args) - throws Exception{ - - - //file directory - - Path mapPath = Path.of("src/main/java/org/example/fromrazan/TestEvaluation/map.txt"); - - //read file - - String snake = Files.readString(mapPath); - //split lines - String [] linesOfFile = snake.split("\n"); - int lineLength = linesOfFile[0].trim().split(" ").length; - - // char[][] map----> easy for print and audit - - - char[][] map = new char[linesOfFile.length][lineLength]; - - - //reading the text data , fill map - - for (int r = 0; r < linesOfFile.length; r++){ - String[] parts = linesOfFile[r].trim().split(" "); - - for (int c = 0; c < parts.length; c++){ - map[r][c] = parts[c].charAt(0); - } - } - - //print map - System.out.println(" Start : "); - PrintMap(map); - - //snake body - LinkedList snakes = new LinkedList<>(); - for (int r = 0; r < map.length; r++){ - for(int c = 0; c < lineLength; c++) { - if (map[r][c] == 'o') { - snakes.add(new int[]{r, c}); - - - } - } - } - - //tail ----> head - //snake movment - //calculate the new head - int mr = 0; - int mc = 1; - int steps = 1; - - //move snake - for (int s = 0; s < steps; s++) { - - //get head - int[] head = snakes.getFirst(); - - int newRow = head[0] + mr; - int newCol = head[1] + mc; - - // direction control - switch (snake){ - case "up": - newRow++; - break; - - case "down": - newRow--; - break; - - case "right": - newCol++; - break; - - default: - System.out.println("error "); - return; - - - } - //add head - snakes.addFirst(new int[]{newRow, newCol}); - - //remove tail - int[] tail = snakes.removeLast(); - - //update map - map[newRow][newCol] = 'o'; - map[tail[0]][tail[1]] = '-'; - - } - //after move print the map - System.out.println("after move print the map: "); - PrintMap(map); - } - - public static void PrintMap(char[][] map){ - for (int r = 0; r < map.length; r++){ - - for (int c = 0; c < map[0].length; c++){ - System.out.print(map[r][c] + " "); - } - System.out.println(); - } - } - - } - - - - - - - diff --git a/src/main/java/org/example/fromrazan/TestEvaluation/map.txt b/src/main/java/org/example/fromrazan/TestEvaluation/map.txt deleted file mode 100644 index e69de29b..00000000 diff --git a/src/main/java/org/example/fromrazan/file.java b/src/main/java/org/example/fromrazan/file.java deleted file mode 100644 index 0863b10b..00000000 --- a/src/main/java/org/example/fromrazan/file.java +++ /dev/null @@ -1,4 +0,0 @@ -package org.example.fromrazan; - -public class file { -} diff --git a/src/main/java/org/example/fromrazan/map.txt b/src/main/java/org/example/fromrazan/map.txt deleted file mode 100644 index e69de29b..00000000 diff --git a/src/main/java/org/example/fromrazan/maze.txt b/src/main/java/org/example/fromrazan/maze.txt index e69de29b..10890a23 100644 --- a/src/main/java/org/example/fromrazan/maze.txt +++ b/src/main/java/org/example/fromrazan/maze.txt @@ -0,0 +1,10 @@ +1111111111 +@000000001 +1111101111 +1001100001 +1010101101 +1010000101 +1011110101 +1000010111 +111101000E +1111111111 \ No newline at end of file From 8275e0305a5704b15330cefd6bd410a33a66dbef Mon Sep 17 00:00:00 2001 From: Razansalam Date: Tue, 14 Apr 2026 14:57:44 +0400 Subject: [PATCH 3/4] fix error --- src/main/java/org/example/fromrazan/EscapeMaze.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/org/example/fromrazan/EscapeMaze.java b/src/main/java/org/example/fromrazan/EscapeMaze.java index 4965ced1..33cacd61 100644 --- a/src/main/java/org/example/fromrazan/EscapeMaze.java +++ b/src/main/java/org/example/fromrazan/EscapeMaze.java @@ -110,6 +110,7 @@ public static char[][] loadAndGetMaze() { } public static int[] getPLayerLocation(char[][] maze) { + int[] location = new int[2]; for (int row=0; row < maze.length; row++) { for (int col=0; col < maze[row].length; col++) { From 629b1b9a7d128176c8549a7240024c8328170bf9 Mon Sep 17 00:00:00 2001 From: Razansalam Date: Wed, 10 Jun 2026 08:58:16 +0400 Subject: [PATCH 4/4] add validation --- .../org/example/fromrazan/EscapeMaze.java | 304 ++++++++++-------- 1 file changed, 177 insertions(+), 127 deletions(-) diff --git a/src/main/java/org/example/fromrazan/EscapeMaze.java b/src/main/java/org/example/fromrazan/EscapeMaze.java index 33cacd61..6b6538ef 100644 --- a/src/main/java/org/example/fromrazan/EscapeMaze.java +++ b/src/main/java/org/example/fromrazan/EscapeMaze.java @@ -8,131 +8,181 @@ - public class EscapeMaze { - - public static void main(String[] args) throws InterruptedException { - char[][] maze = loadAndGetMaze(); - int[] initialPlayerPosition = getPLayerLocation(maze); - ArrayList listOfMoves = new ArrayList<>(); - - // Predefined list of moves - listOfMoves.add(new int[]{9, 2}); - listOfMoves.add(new int[]{8, 2}); - listOfMoves.add(new int[]{7, 2}); - listOfMoves.add(new int[]{6, 2}); - listOfMoves.add(new int[]{5, 2}); - listOfMoves.add(new int[]{4, 2}); - listOfMoves.add(new int[]{4, 3}); - listOfMoves.add(new int[]{4, 4}); - - System.out.printf("Location of @ is (%d,%d)\n", initialPlayerPosition[0], initialPlayerPosition[1]); - - System.out.println("Before the change:"); - displayMaze(maze); - - // Processing - int[] currPlayerPosition = initialPlayerPosition; - - for (int[] currMove: listOfMoves) { - Thread.sleep(2000); - currPlayerPosition = makeMove(maze, currPlayerPosition, currMove); - - printEmptyLines(); - displayMaze(maze); - } - } - - public static int[] makeMove(char[][] maze, int[] sourcePosition, int[] targetPosition) { - char valueOnTargetLocation = maze[targetPosition[0]-1][targetPosition[1]-1]; - - // Check if target location is valid - if (valueOnTargetLocation == '0') { - System.out.println("Found a valid way forward"); - - // Mark the source location as tracked - maze[sourcePosition[0] - 1][sourcePosition[1] - 1] = '-'; - - // Move the player - maze[targetPosition[0] - 1][targetPosition[1] - 1] = '@'; - - return new int[]{targetPosition[0],targetPosition[1]}; - - - } else { - System.out.println("No way forward"); - - //stay in the same position - return sourcePosition; - } - - } - - public static void displayMaze(char[][] maze) { - for(int row=0; row < maze.length; row++) { - for(int col=0; col < maze[row].length; col++) { - System.out.printf("%c ", maze[row][col]); - } - System.out.println(); - } - } - - public static char[][] loadAndGetMaze() { - char[][] maze; - Path mazePath = null; - - try { - mazePath = Path.of(EscapeMaze.class.getResource(relativeMazePath).toURI()); - } catch (URISyntaxException e) { - throw new RuntimeException(e); - } - - try { - String fileContent = Files.readString(mazePath); - String[] linesOfFile = fileContent.split("\\R"); - - int lineLength = linesOfFile[0].trim().length(); - - maze = new char[linesOfFile.length][lineLength]; // Load it in 2D Array or Array of Arrays - - for (int row = 0; row < linesOfFile.length; row++) { - char[] currRow = linesOfFile[row].toCharArray(); - // System.out.printf("%s\n", linesOfFile[row]); - - for (int col = 0; col < currRow.length; col++) { - maze[row][col] = currRow[col]; - } - } - - } catch (IOException e) { - throw new RuntimeException(e); - } - return maze; - } - - public static int[] getPLayerLocation(char[][] maze) { - - int[] location = new int[2]; - for (int row=0; row < maze.length; row++) { - for (int col=0; col < maze[row].length; col++) { - if (maze[row][col] == '@') { - location[0] = row + 1; - location[1] = col + 1; - } - } - } - return location; - } - - - public static void printEmptyLines() { - for (int count=0; count<18; count++) { - System.out.println(); - } - } - - public static final String relativeMazePath = "maze.txt"; - } - - - +public class EscapeMaze { + + public static void main(String[] args) throws InterruptedException { + char[][] maze = loadAndGetMaze(); + + // Validate the maze before doing anything + if (!validateMaze(maze)) { + System.out.println("Maze validation failed. Exiting."); + return; + } + + int[] initialPlayerPosition = getPLayerLocation(maze); + ArrayList listOfMoves = new ArrayList<>(); + + // Predefined list of moves + listOfMoves.add(new int[]{9, 2}); + listOfMoves.add(new int[]{8, 2}); + listOfMoves.add(new int[]{7, 2}); + listOfMoves.add(new int[]{6, 2}); + listOfMoves.add(new int[]{5, 2}); + listOfMoves.add(new int[]{4, 2}); + listOfMoves.add(new int[]{4, 3}); + listOfMoves.add(new int[]{4, 4}); + + System.out.printf("Location of @ is (%d,%d)\n", initialPlayerPosition[0], initialPlayerPosition[1]); + + System.out.println("Before the change:"); + displayMaze(maze); + + // Processing + int[] currPlayerPosition = initialPlayerPosition; + + for (int[] currMove: listOfMoves) { + Thread.sleep(2000); + currPlayerPosition = makeMove(maze, currPlayerPosition, currMove); + + printEmptyLines(); + displayMaze(maze); + } + } + + public static boolean validateMaze(char[][] maze) { + if (maze == null || maze.length == 0) { + System.out.println("Validation error: Maze is empty."); + return false; + } + + int playerCount = 0; + int exitCount = 0; + int expectedColumns = maze[0].length; + + for (int row = 0; row < maze.length; row++) { + // Check consistent row length + if (maze[row].length != expectedColumns) { + System.out.printf("Validation error: Row %d has inconsistent length.\n", row + 1); + return false; + } + + for (int col = 0; col < maze[row].length; col++) { + char cell = maze[row][col]; + switch (cell) { + case '@' -> playerCount++; + case 'E' -> exitCount++; + case '0', '1' -> { /* valid characters */ } + default -> { + System.out.printf( + "Validation error: Invalid character '%c' at (%d, %d).\n", + cell, row + 1, col + 1); + return false; + } + } + } + } + + if (playerCount != 1) { + System.out.printf("Validation error: Expected 1 player '@', found %d.\n", playerCount); + return false; + } + + if (exitCount != 1) { + System.out.printf("Validation error: Expected 1 exit 'E', found %d.\n", exitCount); + return false; + } + + System.out.println("Maze validation passed."); + return true; + } + + public static int[] makeMove(char[][] maze, int[] sourcePosition, int[] targetPosition) { + char valueOnTargetLocation = maze[targetPosition[0]-1][targetPosition[1]-1]; + + // Check if target location is valid + if (valueOnTargetLocation == '0') { + System.out.println("Found a valid way forward"); + + // Mark the source location as tracked + maze[sourcePosition[0] - 1][sourcePosition[1] - 1] = '-'; + + // Move the player + maze[targetPosition[0] - 1][targetPosition[1] - 1] = '@'; + + return new int[]{targetPosition[0],targetPosition[1]}; + + + } else { + System.out.println("No way forward"); + + //stay in the same position + return sourcePosition; + } + + } + + public static void displayMaze(char[][] maze) { + for(int row=0; row < maze.length; row++) { + for(int col=0; col < maze[row].length; col++) { + System.out.printf("%c ", maze[row][col]); + } + System.out.println(); + } + } + + public static char[][] loadAndGetMaze() { + char[][] maze; + Path mazePath = null; + + try { + mazePath = Path.of(EscapeMaze.class.getResource(relativeMazePath).toURI()); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } + + try { + String fileContent = Files.readString(mazePath); + String[] linesOfFile = fileContent.split("\\R"); + + int lineLength = linesOfFile[0].trim().length(); + + maze = new char[linesOfFile.length][lineLength]; // Load it in 2D Array or Array of Arrays + + for (int row = 0; row < linesOfFile.length; row++) { + char[] currRow = linesOfFile[row].toCharArray(); + // System.out.printf("%s\n", linesOfFile[row]); + + for (int col = 0; col < currRow.length; col++) { + maze[row][col] = currRow[col]; + } + } + + } catch (IOException e) { + throw new RuntimeException(e); + } + return maze; + } + + public static int[] getPLayerLocation(char[][] maze) { + + int[] location = new int[2]; + for (int row=0; row < maze.length; row++) { + for (int col=0; col < maze[row].length; col++) { + if (maze[row][col] == '@') { + location[0] = row + 1; + location[1] = col + 1; + } + } + } + return location; + } + + + public static void printEmptyLines() { + for (int count=0; count<18; count++) { + System.out.println(); + } + } + public static final String relativeMazePath = "maze.txt"; +} \ No newline at end of file