BotsAI/src/SnakeBot.java

91 lines
3.0 KiB
Java
Raw Normal View History

/*
* SnakeBot.
* Eine weitere wichtige Funktion des Rovers ist es, Gesteinsproben zu sammeln.
* Interessante Steine sind im Scan mit einem @ gekennzeichnet.
* Mit jeder aufgesammelten Gesteinsprobe wird an den Rover ein Wagen angehängt, der zukünftig hinter dem Rover mitgezogen wird.
* Die Wagen sind im Scan mit * zu identifizieren.
* Vorsicht: fährt der Rover in einen Wagen, ist er schwer beschädigt und kann keine weiteren Steine mehr sammeln.
* Sie können Ihre Implementierung wieder testen mit:
* docker run --rm -p 63187:63187 mediaeng/bots snakes
* Für diese Funktion wird am 7.2.24 in einer gemeinsamen Arena mit allen Teams des Jahrgangs ein Wettbewerb durchgeführt.
* Die besten acht Teams qualifizieren sich für die Königsdisziplin Rumble.
*/
public class SnakeBot extends Bot{
String moves = "";
private int spiralNumber = 0;
private boolean ignoreStone = false;
public static void main(String[] args) {
Bot snakeBot = new SnakeBot(args);
snakeBot.run();
}
2024-01-07 15:16:41 +01:00
protected SnakeBot(String[] args) {
super(args);
}
//@TODO: find a better way to avoid collectedStones
protected char nextMove(View view){
boolean stoneDetected = view.data.contains("@");
char nextMove;
2024-01-07 15:16:41 +01:00
nextMove = (stoneDetected && !ignoreStone) ? goToStone(view) : walkBySpiral();
if(nextMove == '^' && view.data.charAt(7) == '*'){
2024-01-06 23:47:07 +01:00
nextMove = (countCollectedStonesLeft(view) <= countCollectedStonesRight(view)) ? '<' : '>';
ignoreStone = true;
}
if(countCollectedStones(view) <= 2) ignoreStone = false;
return nextMove;
}
2024-01-07 15:16:41 +01:00
2024-01-06 23:47:07 +01:00
private int countCollectedStonesLeft(View view) {
int[] leftStones = {0, 1, 5, 6, 10, 11, 15, 16, 20, 21};
int stones = 0;
2024-01-07 15:16:41 +01:00
for (int stone : leftStones) {
2024-01-06 23:47:07 +01:00
if(view.data.charAt(stone) == '*'){
stones++;
}
}
return stones;
}
2024-01-07 15:16:41 +01:00
2024-01-06 23:47:07 +01:00
private int countCollectedStonesRight(View view) {
int[] rightStones = {3, 4, 8, 9, 13, 14, 18, 19, 23, 24};
int stones = 0;
2024-01-07 15:16:41 +01:00
for (int stone : rightStones) {
2024-01-06 23:47:07 +01:00
if(view.data.charAt(stone) == '*'){
stones++;
}
}
return stones;
}
2024-01-07 15:16:41 +01:00
private int countCollectedStones(View view) {
int count = 0;
for (char c : view.data.toCharArray()) {
if (c == '*') {
count++;
}
}
return count;
}
2024-01-07 15:16:41 +01:00
private char goToStone(View view){
int rowDifference = findStoneRow(view) - 2;
return rowDifference < 0 ? '^' : '<';
}
private int findStoneRow(View view) {
return view.data.indexOf('@') / 5;
}
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;
}
}