BotsAI/src/EscapeBot.java

66 lines
1.8 KiB
Java
Raw Normal View History

2023-12-14 17:15:23 +01:00
public class EscapeBot extends Bot{
String moves = "";
private boolean goesForward = true;
private int circleNumber = 0;
private int spiralNumber = 0;
2023-12-14 17:15:23 +01:00
public static void main(String[] args) {
2023-12-14 17:15:23 +01:00
Bot escapeBot = new EscapeBot(args);
escapeBot.run();
}
2024-01-07 15:16:41 +01:00
2023-12-14 17:15:23 +01:00
protected EscapeBot(String[] args) {
super(args);
}
protected char nextMove(View view){
boolean rocketDetected = view.data.contains("o");
return rocketDetected ? goToRocket(view) : walkBySpiral();
}
2024-01-07 15:16:41 +01:00
private char goToRocket(View view){
int rowDifference = findRocketRow(view) - 2;
return rowDifference < 0 ? '^' : '<';
}
2024-01-07 15:16:41 +01:00
private int findRocketRow(View view) {
return view.data.indexOf('o') / 5;
2023-12-14 17:15:23 +01:00
}
private char walkByColumns() {
if(moves.isEmpty()){
moves = "^".repeat(28);
moves += (goesForward ? ">^^^^^>" : "<^^^^^<");
goesForward = !goesForward;
}
char nextMove = moves.charAt(0);
moves = moves.substring(1);
return nextMove;
2023-12-14 17:15:23 +01:00
}
2024-01-07 15:16:41 +01:00
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;
}
2024-01-07 15:16:41 +01:00
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;
}
2024-01-07 15:16:41 +01:00
}