From a242381a9d7f3bf2bd6848ef408bea49e21bfd27 Mon Sep 17 00:00:00 2001 From: Your Average Code <138674451+UrAvgCode@users.noreply.github.com> Date: Thu, 25 Jan 2024 18:04:53 +0100 Subject: [PATCH] EnemyBot has line of sight --- src/EnemyBot.java | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/EnemyBot.java b/src/EnemyBot.java index 19f753f..a71d700 100644 --- a/src/EnemyBot.java +++ b/src/EnemyBot.java @@ -9,13 +9,16 @@ public class EnemyBot extends Bot { @Override protected char nextMove(View view) { + int size = view.width; String data = view.data .replace('^', '*') .replace('<', '*') .replace('>', '*') .replace('v', '*'); - if (data.contains("*") && random.nextFloat() < 0.8) + char[][] grid = dataToGrid(data, size); + + if (data.contains("*") && isInLineOfSight(grid)) return 'f'; return switch (random.nextInt(10)) { @@ -29,4 +32,21 @@ public class EnemyBot extends Bot { @Override protected void print(View view) { } + + protected char[][] dataToGrid(String data, int size) { + char[][] grid = new char[size][size]; + for (int i = 0; i < data.length(); i++) { + grid[i % size][i / size] = data.charAt(i); + } + return grid; + } + + protected boolean isInLineOfSight(char[][] grid) { + int size = grid.length; + for (int y = size / 2; y > 0; y--) { + if (grid[size / 2][y] == '*') + return true; + } + return false; + } }