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..6b6538ef --- /dev/null +++ b/src/main/java/org/example/fromrazan/EscapeMaze.java @@ -0,0 +1,188 @@ +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(); + + // 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 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..10890a23 --- /dev/null +++ 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