diff --git a/src/main/java/com/agileoracleseval/slitheringeval/ibrahim_alrahbi/SnakeMove/SnakeMove.java b/src/main/java/com/agileoracleseval/slitheringeval/ibrahim_alrahbi/SnakeMove/SnakeMove.java index 307383a..425b12a 100644 --- a/src/main/java/com/agileoracleseval/slitheringeval/ibrahim_alrahbi/SnakeMove/SnakeMove.java +++ b/src/main/java/com/agileoracleseval/slitheringeval/ibrahim_alrahbi/SnakeMove/SnakeMove.java @@ -5,7 +5,6 @@ import java.util.LinkedList; import java.util.Scanner; - public class SnakeMove { public static void main(String[] argumentsInput) throws IOException { @@ -14,7 +13,7 @@ public static void main(String[] argumentsInput) throws IOException { // Validate Arguments if (argumentsInput.length == 0) { System.out.println("Error: Must run via command line with: java MoveSnake.java "); - return; // Stop the program + return; } if (argumentsInput.length > 2) { @@ -55,10 +54,12 @@ public static void main(String[] argumentsInput) throws IOException { // Part 2. Read file and split each row immediately - // Reading the file & Splitting ArrayList rowsData = new ArrayList<>(); + // [ADDED] A variable to store the snake order line if it exists in the file + String snakeOrderLine = null; + File mapFile = new File("src/main/java/com/agileoracleseval/slitheringeval/ibrahim_alrahbi/SnakeMove/map"); - System.out.println(new File("src/main/java/com/agileoracleseval/slitheringeval/ibrahim_alrahbi/SnakeMove/map").getAbsolutePath()); + System.out.println(mapFile.getAbsolutePath()); try { Scanner fileReader = new Scanner(mapFile); @@ -67,11 +68,16 @@ public static void main(String[] argumentsInput) throws IOException { while (fileReader.hasNextLine()) { // removes extra spaces at the beginning and end of the line String line = fileReader.nextLine().trim(); - - // This checks if line is not empty. + // This checks if line is not empty if (!line.isEmpty()) { - String[] parts = line.split(" "); // Split lines by spaces; ease to access - rowsData.add(parts); + // [ADDED] Check if this line is the snake order line (starts with "(") + // If yes, save it separately instead of adding it to the map rows + if (line.startsWith("(")) { + snakeOrderLine = line; + } else { + String[] parts = line.split(" "); // Split lines by spaces; ease to access + rowsData.add(parts); + } } } fileReader.close(); @@ -83,16 +89,13 @@ public static void main(String[] argumentsInput) throws IOException { // Part 2: Validate map.txt content & Create 2D Array - // Rows int rows = rowsData.size(); if (rows == 0) { System.out.println("Error: Map file is empty."); return; } - // Columns int columns = rowsData.get(0).length; - // Check if n*n & n>=15 if (rows < 15 || columns < 15) { System.out.println("Error: Map must be at least 15x15."); @@ -105,7 +108,6 @@ public static void main(String[] argumentsInput) throws IOException { return; } } - // Validate symbols - & o if there are another symbols for (int row = 0; row < rows; row++) { for (int col = 0; col < columns; col++) { @@ -116,16 +118,16 @@ public static void main(String[] argumentsInput) throws IOException { } } } - // Create 2D Array & filling char[][] map = new char[rows][columns]; for (int row = 0; row < rows; row++) { for (int col = 0; col < columns; col++) { - map[row][col] = rowsData.get(row)[col].charAt(0); // + map[row][col] = rowsData.get(row)[col].charAt(0); } } - // Printing the grid map.txt + + System.out.println("\nThe map:"); for (int row = 0; row < rows; row++) { for (int col = 0; col < columns; col++) { System.out.print(map[row][col] + " "); @@ -137,18 +139,39 @@ public static void main(String[] argumentsInput) throws IOException { // Locating the Snake in the Grid LinkedList snake = new LinkedList<>(); - for (int row = 0; row < rows; row++) { - for (int col = 0; col < columns; col++) { - if (map[row][col] == 'o') { - snake.add(new int[]{row, col}); + // [ADDED] If the snake order line exists, read the snake order from it + // This gives us the correct tail-to-head order for any snake shape + if (snakeOrderLine != null) { + // [ADDED] Each point looks like (row,col) and they are separated by spaces + // For example: (2,3) (2,4) (2,5) + String[] points = snakeOrderLine.split(" "); + for (String point : points) { + // [ADDED] Remove the "(" and ")" and split by "," + point = point.replace("(", "").replace(")", ""); + String[] snakeLocation = point.split(","); + int r = Integer.parseInt(snakeLocation[0].trim()); + int c = Integer.parseInt(snakeLocation[1].trim()); + snake.add(new int[]{r, c}); + } + + } else { + // [ADDED] First time running: no order line exists yet, so fall back to + // the original scan method to at least find the snake segments + for (int row = 0; row < rows; row++) { + for (int col = 0; col < columns; col++) { + if (map[row][col] == 'o') { + snake.add(new int[]{row, col}); + } } } } + if (snake.isEmpty()) { System.out.println("No snake found in map.txt!"); return; } + // Part.3 Moving the Snake // Repeating Number of Steps Entered for (int step = 0; step < steps; step++) { @@ -189,24 +212,46 @@ public static void main(String[] argumentsInput) throws IOException { map[tail[0]][tail[1]] = '-'; } + // Part.4 Save map.txt to file & Print it - BufferedWriter fileWrite = new BufferedWriter(new FileWriter("src/main/java/com/agileoracleseval/slitheringeval/ibrahim_alrahbi/SnakeMove/map")); + BufferedWriter fileWrite = new BufferedWriter(new FileWriter( + "src/main/java/com/agileoracleseval/slitheringeval/ibrahim_alrahbi/SnakeMove/map")); + for (int row = 0; row < rows; row++) { for (int col = 0; col < columns; col++) { fileWrite.write(map[row][col] + " "); } fileWrite.newLine(); } + + // [ADDED] After saving the map, write the updated snake order on the extra line + // This way, the next run will read the correct order from this line + StringBuilder snakeOrderBuilder = new StringBuilder(); + for (int i = 0; i < snake.size(); i++) { + int[] segment = snake.get(i); + snakeOrderBuilder.append("(").append(segment[0]).append(",").append(segment[1]).append(")"); + // [ADDED] Add a space between points but not after the last one + if (i < snake.size() - 1) { + snakeOrderBuilder.append(" "); + } + } + fileWrite.write(snakeOrderBuilder.toString()); + fileWrite.newLine(); + // [ADDED] End of snake order line writing + fileWrite.close(); // Print map.txt - System.out.println("Updated Map:"); + System.out.println("\nUpdated Map:"); for (int row = 0; row < rows; row++) { for (int col = 0; col < columns; col++) { System.out.print(map[row][col] + " "); } System.out.println(); } + + // [ADDED] Print the snake order so the user can see it in the console too + System.out.println("Snake order (tail to head): " + snakeOrderBuilder.toString()); } // Validate directions (Inside Map or Not Part of Snake) diff --git a/src/main/java/com/agileoracleseval/slitheringeval/ibrahim_alrahbi/SnakeMove/map b/src/main/java/com/agileoracleseval/slitheringeval/ibrahim_alrahbi/SnakeMove/map index fd7c204..b4ece38 100644 --- a/src/main/java/com/agileoracleseval/slitheringeval/ibrahim_alrahbi/SnakeMove/map +++ b/src/main/java/com/agileoracleseval/slitheringeval/ibrahim_alrahbi/SnakeMove/map @@ -3,13 +3,13 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - o o o o o - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - o - - - -- - - - - - - - - - - o - - - -- - - - - - - - - - - o - - - -- - - - - - - - - - o o - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - +- - - - - - - - - - - - - - -