From 53482674e64044dd0a1a8ed55b94dba4a1f1c89f Mon Sep 17 00:00:00 2001 From: Your Average Code <138674451+UrAvgCode@users.noreply.github.com> Date: Thu, 25 Jan 2024 16:01:26 +0100 Subject: [PATCH] simplify markFiringLines --- src/RumbleBot.java | 59 ++++++++++++++++++---------------------------- 1 file changed, 23 insertions(+), 36 deletions(-) diff --git a/src/RumbleBot.java b/src/RumbleBot.java index d8b643b..8c3c9be 100644 --- a/src/RumbleBot.java +++ b/src/RumbleBot.java @@ -54,47 +54,29 @@ public class RumbleBot extends Bot { int size = grid.length; for (int x = 0; x < size; x++) { for (int y = 0; y < size; y++) { - - if (grid[x][y] == 'v') { - int scanY = y; - while (scanY < size && !obstacles.contains("" + grid[x][y])) { - if (grid[x][scanY] == '.') - grid[x][scanY] = '*'; - scanY++; - } + switch (grid[x][y]) { + case 'v' -> markLine(grid, x, y, 0, 1); + case '^' -> markLine(grid, x, y, 0, -1); + case '>' -> markLine(grid, x, y, 1, 0); + case '<' -> markLine(grid, x, y, -1, 0); } - - if (grid[x][y] == '^') { - int scanY = y; - while (scanY >= 0 && !obstacles.contains("" + grid[x][y])) { - if (grid[x][scanY] == '.') - grid[x][scanY] = '*'; - scanY--; - } - } - - if (grid[x][y] == '>') { - int scanX = x; - while (scanX < size && !obstacles.contains("" + grid[x][y])) { - if (grid[scanX][y] == '.') - grid[scanX][y] = '*'; - scanX++; - } - } - - if (grid[x][y] == '<') { - int scanX = x; - while (scanX >= 0 && !obstacles.contains("" + grid[x][y])) { - if (grid[scanX][y] == '.') - grid[scanX][y] = '*'; - scanX--; - } - } - } } } + private void markLine(char[][] grid, int x, int y, int dx, int dy) { + int size = grid.length; + x += dx; + y += dy; + while (x >= 0 && x < size && y >= 0 && y < size && !obstacles.contains("" + grid[x][y])) { + if (grid[x][y] == '.') { + grid[x][y] = '*'; + } + x += dx; + y += dy; + } + } + protected boolean isInLineOfSight(char[][] grid) { int size = grid.length; for (int y = size / 2; y > 0; y--) { @@ -136,6 +118,11 @@ public class RumbleBot extends Bot { int size = grid.length; int start = size / 2; + for (int y = 0; y < size; y++) { + for (char[] chars : grid) System.err.print(chars[y]); + System.err.println(); + } + boolean[][] visited = new boolean[size][size]; Queue queue = new LinkedList<>();