From 261d4f4d16da9f769e70a3db4302e135ec8361ad Mon Sep 17 00:00:00 2001 From: Illia Soloviov <74905269+wav3solo@users.noreply.github.com> Date: Sat, 6 Jan 2024 18:17:24 +0100 Subject: [PATCH] EscapeBot beta version. Max 200 moves. Avg 100 moves. --- .idea/misc.xml | 1 - src/EscapeBot.java | 48 +++++++++++++++++++++------------------------- src/ManualBot.java | 4 ---- 3 files changed, 22 insertions(+), 31 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index fb40be9..b081221 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/src/EscapeBot.java b/src/EscapeBot.java index 0253ef1..e946d10 100644 --- a/src/EscapeBot.java +++ b/src/EscapeBot.java @@ -1,37 +1,33 @@ public class EscapeBot extends Bot{ - boolean rocketDetected = false; - public static void main(String[] args) { + String moves = ""; + private boolean goesForward = true; + public static void main(String[] args) { Bot escapeBot = new EscapeBot(args); escapeBot.run(); } protected EscapeBot(String[] args) { super(args); } - - protected char nextMove(View view) throws Exception { - char[] viewField = view.data.toCharArray(); - - - for (int i = 0; i < viewField.length; i++) { - if(viewField[i] == 'o'){ - rocketDetected = true; - } - } - - if(rocketDetected){ - - }else{ - return randomWalk(); - } - - return 0; + protected char nextMove(View view){ + boolean rocketDetected = view.data.contains("o"); + return rocketDetected ? goToRocket(view) : randomWalk(); + } + private char goToRocket(View view){ + int rowDifference = findRocketRow(view) - 2; + return rowDifference < 0 ? '^' : '<'; + } + private int findRocketRow(View view) { + return view.data.indexOf('o') / 5; } - private char randomWalk() { - return '^'; + if(moves.isEmpty()){ + moves = "^".repeat(28); + moves += (goesForward ? ">^^^^^>" : "<^^^^^<"); + goesForward = !goesForward; + } + char nextMove = moves.charAt(0); + moves = moves.substring(1); + return nextMove; } - - -} - +} \ No newline at end of file diff --git a/src/ManualBot.java b/src/ManualBot.java index 9a82557..0fd75bb 100644 --- a/src/ManualBot.java +++ b/src/ManualBot.java @@ -1,7 +1,6 @@ import java.util.Scanner; public class ManualBot extends Bot{ public static void main(String[] args) { - Bot manualBot = new ManualBot(args); manualBot.run(); } @@ -14,7 +13,6 @@ public class ManualBot extends Bot{ switch(scanner.nextLine().toLowerCase()){ case "w": return '^' ; - case "s": return 'v' ; case "a": @@ -27,7 +25,5 @@ public class ManualBot extends Bot{ break; } return 0; - - } }