From aefc832ee7e8b801f8b604f74ad84415479ea840 Mon Sep 17 00:00:00 2001 From: safa Date: Thu, 2 Apr 2026 16:22:41 +0400 Subject: [PATCH 1/3] The snake game --- .../safaAlsibani/MoveSnake.java | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 src/main/java/com/agileoracleseval/slitheringeval/safaAlsibani/MoveSnake.java diff --git a/src/main/java/com/agileoracleseval/slitheringeval/safaAlsibani/MoveSnake.java b/src/main/java/com/agileoracleseval/slitheringeval/safaAlsibani/MoveSnake.java new file mode 100644 index 0000000..6b8e5eb --- /dev/null +++ b/src/main/java/com/agileoracleseval/slitheringeval/safaAlsibani/MoveSnake.java @@ -0,0 +1,95 @@ +package com.agileoracleseval.slitheringeval.safaAlsibani; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.LinkedList; +import java.util.Queue; + +public class MoveSnake { + public static void main(String[] args) throws Exception { + + Path file = Path.of("./Data/map.txt"); + String data = Files.readString(file); + String[] lines = data.split("\\R"); + + char[][] grid = new char[lines.length][lines[0].length()]; + for (int i = 0; i < lines.length; i++) { + grid[i] = lines[i].toCharArray(); + } + + // snake body + Queue snake = new LinkedList<>(); + + for (int i = 0; i < grid.length; i++) { + for (int j = 0; j < grid[i].length; j++) { + if (grid[i][j] == 'o') { + snake.add(new int[]{i, j}); + } + } + } + + // head + int[] head = ((LinkedList) snake).getLast(); + + // input + String direction = args[0]; + int steps = Integer.parseInt(args[1]); + + // check invalid direction + if (direction.equals("down")) { + System.out.println("The only open directions are up, left and right"); + return; + } + + // the movement + for (int s = 0; s < steps; s++) { + + int newRow = head[0]; + int newCol = head[1]; + + if (direction.equals("right")) newCol++; + else if (direction.equals("left")) newCol--; + else if (direction.equals("up")) newRow--; + + // wall collision + if (newRow < 0 || newRow >= grid.length || + newCol < 0 || newCol >= grid[0].length) { + System.out.println("Game Over - wall"); + return; + } + + // body collision + for (int[] part : snake) { + if (part[0] == newRow && part[1] == newCol) { + System.out.println("Game Over - Hit itself"); + return; + } + } + + // update snake + int[] tail = snake.peek(); + head = new int[]{newRow, newCol}; + + snake.add(head); + snake.remove(); + + // update grid + grid[tail[0]][tail[1]] = '-'; + grid[newRow][newCol] = 'o'; + + // print + printGrid(grid); + System.out.println(); + } + } + + public static void printGrid(char[][] grid) { + for (int i = 0; i < grid.length; i++) { + for (int j = 0; j < grid[i].length; j++) { + System.out.print(grid[i][j]); + } + System.out.println(); + } + } +} + From f593afda1c1287e63cf15d8e5a489cfa59ecd976 Mon Sep 17 00:00:00 2001 From: safa Date: Thu, 2 Apr 2026 19:15:03 +0400 Subject: [PATCH 2/3] the snake game --- .../slitheringeval/safaAlsibani/MoveSnake.java | 2 +- src/main/resources/map.txt | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 src/main/resources/map.txt diff --git a/src/main/java/com/agileoracleseval/slitheringeval/safaAlsibani/MoveSnake.java b/src/main/java/com/agileoracleseval/slitheringeval/safaAlsibani/MoveSnake.java index 6b8e5eb..7f808e5 100644 --- a/src/main/java/com/agileoracleseval/slitheringeval/safaAlsibani/MoveSnake.java +++ b/src/main/java/com/agileoracleseval/slitheringeval/safaAlsibani/MoveSnake.java @@ -8,7 +8,7 @@ public class MoveSnake { public static void main(String[] args) throws Exception { - Path file = Path.of("./Data/map.txt"); + Path file = Path.of("src/main/resources/map.txt"); String data = Files.readString(file); String[] lines = data.split("\\R"); diff --git a/src/main/resources/map.txt b/src/main/resources/map.txt new file mode 100644 index 0000000..628914c --- /dev/null +++ b/src/main/resources/map.txt @@ -0,0 +1,15 @@ +- - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - +- - - - - o o o o o - - - - - +- - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - +- - - - - - - - - - - - - - - \ No newline at end of file From 2a6abd17d8839250bff40fbda37cd898d5644cf4 Mon Sep 17 00:00:00 2001 From: safa Date: Tue, 14 Apr 2026 14:30:59 +0400 Subject: [PATCH 3/3] updated file --- .../agileoracleseval/slitheringeval/safaAlsibani/MoveSnake.java | 2 +- .../com/agileoracleseval/slitheringeval/safaAlsibani}/map.txt | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename src/main/{resources => java/com/agileoracleseval/slitheringeval/safaAlsibani}/map.txt (100%) diff --git a/src/main/java/com/agileoracleseval/slitheringeval/safaAlsibani/MoveSnake.java b/src/main/java/com/agileoracleseval/slitheringeval/safaAlsibani/MoveSnake.java index 7f808e5..c5e581e 100644 --- a/src/main/java/com/agileoracleseval/slitheringeval/safaAlsibani/MoveSnake.java +++ b/src/main/java/com/agileoracleseval/slitheringeval/safaAlsibani/MoveSnake.java @@ -8,7 +8,7 @@ public class MoveSnake { public static void main(String[] args) throws Exception { - Path file = Path.of("src/main/resources/map.txt"); + Path file = Path.of("src/main/java/com/agileoracleseval/slitheringeval/safaAlsibani/map.txt"); String data = Files.readString(file); String[] lines = data.split("\\R"); diff --git a/src/main/resources/map.txt b/src/main/java/com/agileoracleseval/slitheringeval/safaAlsibani/map.txt similarity index 100% rename from src/main/resources/map.txt rename to src/main/java/com/agileoracleseval/slitheringeval/safaAlsibani/map.txt