Browse Source

EscapeBot using walkBySpiral. Avg moves 54.

testing_N
Illia Soloviov 4 months ago
parent
commit
1bf3ea159d
1 changed files with 29 additions and 2 deletions
  1. 29
    2
      src/EscapeBot.java

+ 29
- 2
src/EscapeBot.java View File

public class EscapeBot extends Bot{ public class EscapeBot extends Bot{
String moves = ""; String moves = "";
private boolean goesForward = true; private boolean goesForward = true;
private int circleNumber = 0;
private int spiralNumber = 0;


public static void main(String[] args) { public static void main(String[] args) {
Bot escapeBot = new EscapeBot(args); Bot escapeBot = new EscapeBot(args);
} }
protected char nextMove(View view){ protected char nextMove(View view){
boolean rocketDetected = view.data.contains("o"); boolean rocketDetected = view.data.contains("o");
return rocketDetected ? goToRocket(view) : randomWalk();
return rocketDetected ? goToRocket(view) : walkBySpiral();
} }
private char goToRocket(View view){ private char goToRocket(View view){
int rowDifference = findRocketRow(view) - 2; int rowDifference = findRocketRow(view) - 2;
private int findRocketRow(View view) { private int findRocketRow(View view) {
return view.data.indexOf('o') / 5; return view.data.indexOf('o') / 5;
} }
private char randomWalk() {
private char walkByColumns() {
if(moves.isEmpty()){ if(moves.isEmpty()){
moves = "^".repeat(28); moves = "^".repeat(28);
moves += (goesForward ? ">^^^^^>" : "<^^^^^<"); moves += (goesForward ? ">^^^^^>" : "<^^^^^<");
moves = moves.substring(1); moves = moves.substring(1);
return nextMove; return nextMove;
} }
private char WalkByCircles(){
if (moves.isEmpty()) {
circleNumber++;
moves += "^".repeat(5) + ">";
int[] steps = {5, 10, 10, 10};

for (int step : steps) {
moves += "^".repeat(step * circleNumber) + ">";
}

moves += "^".repeat(5 * circleNumber) + "<";
}
char nextMove = moves.charAt(0);
moves = moves.substring(1);
return nextMove;
}
private char walkBySpiral(){
if (moves.isEmpty()) {
spiralNumber++;
moves += "^".repeat(5 * spiralNumber) + ">" + "^".repeat(5 * spiralNumber) + ">";
}
char nextMove = moves.charAt(0);
moves = moves.substring(1);
return nextMove;
}
} }

Loading…
Cancel
Save