simplify markFiringLines

This commit is contained in:
Your Average Code 2024-01-25 16:01:26 +01:00
parent 8c40d7bacb
commit 53482674e6

View File

@ -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<Move> queue = new LinkedList<>();