2024-01-07 17:48:14 +01:00
|
|
|
/*
|
|
|
|
* 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“.
|
|
|
|
*/
|
2024-01-06 20:26:01 +01:00
|
|
|
public class SnakeBot extends Bot{
|
2024-01-06 21:51:53 +01:00
|
|
|
String moves = "";
|
|
|
|
private int spiralNumber = 0;
|
2024-01-07 22:31:01 +01:00
|
|
|
private boolean goesForward = true;
|
2024-01-07 19:38:17 +01:00
|
|
|
private boolean ignoreStones = false;
|
2024-01-06 20:26:01 +01:00
|
|
|
public static void main(String[] args) {
|
2024-01-06 21:51:53 +01:00
|
|
|
Bot snakeBot = new SnakeBot(args);
|
|
|
|
snakeBot.run();
|
2024-01-06 20:26:01 +01:00
|
|
|
}
|
2024-01-07 15:16:41 +01:00
|
|
|
|
2024-01-06 20:26:01 +01:00
|
|
|
protected SnakeBot(String[] args) {
|
|
|
|
super(args);
|
|
|
|
}
|
2024-01-07 22:31:01 +01:00
|
|
|
//@TODO: find a better way to avoid collected stones
|
|
|
|
//@TODO: find a better way to collect stones
|
2024-01-07 19:16:31 +01:00
|
|
|
protected char nextMove(View view) {
|
2024-01-06 21:51:53 +01:00
|
|
|
boolean stoneDetected = view.data.contains("@");
|
|
|
|
char nextMove;
|
2024-01-07 19:38:17 +01:00
|
|
|
nextMove = (stoneDetected && !ignoreStones) ? goToStone(view) : walkBySpiral(view);
|
2024-01-06 21:51:53 +01:00
|
|
|
|
2024-01-07 19:09:30 +01:00
|
|
|
int centerCoordinateOfView = view.width / 2;
|
|
|
|
int frontCellIndex = calculateCharIndexFromCoordinates(view.width, centerCoordinateOfView, centerCoordinateOfView - 1);
|
|
|
|
|
|
|
|
if(nextMove == '^' && view.data.charAt(frontCellIndex) == '*'){
|
2024-01-06 23:47:07 +01:00
|
|
|
nextMove = (countCollectedStonesLeft(view) <= countCollectedStonesRight(view)) ? '<' : '>';
|
2024-01-07 19:38:17 +01:00
|
|
|
ignoreStones = true;
|
2024-01-06 21:51:53 +01:00
|
|
|
}
|
2024-01-07 22:31:01 +01:00
|
|
|
|
2024-01-07 19:38:17 +01:00
|
|
|
if(countUncollectedStones(view) > 1) ignoreStones = true;
|
|
|
|
if(countCollectedStones(view) <= 2 && countUncollectedStones(view) <= 1) ignoreStones = false;
|
|
|
|
|
2024-01-07 22:31:01 +01:00
|
|
|
if(isStoneNearby(view)){
|
|
|
|
nextMove = goToNearbyStone(view);
|
|
|
|
}
|
|
|
|
|
2024-01-06 21:51:53 +01:00
|
|
|
return nextMove;
|
|
|
|
}
|
2024-01-07 15:16:41 +01:00
|
|
|
|
2024-01-07 22:31:01 +01:00
|
|
|
private boolean isStoneNearby(View view){
|
|
|
|
int centerCoordinateOfView = view.width / 2;
|
|
|
|
int frontCellIndex = calculateCharIndexFromCoordinates(view.width, centerCoordinateOfView, centerCoordinateOfView - 1);
|
|
|
|
int leftCellIndex = calculateCharIndexFromCoordinates(view.width, centerCoordinateOfView - 1, centerCoordinateOfView);
|
|
|
|
int rightCellIndex = calculateCharIndexFromCoordinates(view.width, centerCoordinateOfView + 1, centerCoordinateOfView);
|
|
|
|
|
|
|
|
boolean stoneIsInFront = view.data.charAt(frontCellIndex) == '@';
|
|
|
|
boolean stoneIsOnTheLeft = view.data.charAt(leftCellIndex) == '@';
|
|
|
|
boolean stoneIsOneTheRight = view.data.charAt(rightCellIndex) == '@';
|
|
|
|
|
|
|
|
return stoneIsInFront || stoneIsOnTheLeft || stoneIsOneTheRight;
|
|
|
|
}
|
|
|
|
|
|
|
|
private char goToNearbyStone(View view){
|
|
|
|
int centerCoordinateOfView = view.width / 2;
|
|
|
|
int frontCellIndex = calculateCharIndexFromCoordinates(view.width, centerCoordinateOfView, centerCoordinateOfView - 1);
|
|
|
|
int leftCellIndex = calculateCharIndexFromCoordinates(view.width, centerCoordinateOfView - 1, centerCoordinateOfView);
|
|
|
|
int rightCellIndex = calculateCharIndexFromCoordinates(view.width, centerCoordinateOfView + 1, centerCoordinateOfView);
|
|
|
|
|
|
|
|
if(view.data.charAt(frontCellIndex) == '@') return '^';
|
|
|
|
if(view.data.charAt(leftCellIndex) == '@') return '<';
|
|
|
|
if(view.data.charAt(rightCellIndex) == '@') return '>';
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void resetMoves() {
|
|
|
|
moves = "";
|
|
|
|
spiralNumber = 0;
|
|
|
|
}
|
|
|
|
|
2024-01-07 19:38:17 +01:00
|
|
|
private int countUncollectedStones(View view) {
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
for (char c : view.data.toCharArray()) {
|
|
|
|
if (c == '@') count++;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2024-01-06 23:47:07 +01:00
|
|
|
private int countCollectedStonesLeft(View view) {
|
2024-01-07 19:09:30 +01:00
|
|
|
int[] leftSide = generateLeftSideArray(view.width);
|
2024-01-06 23:47:07 +01:00
|
|
|
int stones = 0;
|
2024-01-07 19:09:30 +01:00
|
|
|
for (int cellIndex : leftSide) {
|
|
|
|
if(view.data.charAt(cellIndex) == '*') stones++;
|
2024-01-06 23:47:07 +01:00
|
|
|
}
|
|
|
|
return stones;
|
|
|
|
}
|
2024-01-07 15:16:41 +01:00
|
|
|
|
2024-01-06 23:47:07 +01:00
|
|
|
private int countCollectedStonesRight(View view) {
|
2024-01-07 19:09:30 +01:00
|
|
|
int[] rightSide = generateRightSideArray(view.width);
|
2024-01-06 23:47:07 +01:00
|
|
|
int stones = 0;
|
2024-01-07 19:09:30 +01:00
|
|
|
for (int cellIndex : rightSide) {
|
|
|
|
if(view.data.charAt(cellIndex) == '*') stones++;
|
2024-01-06 23:47:07 +01:00
|
|
|
}
|
|
|
|
return stones;
|
|
|
|
}
|
2024-01-07 15:16:41 +01:00
|
|
|
|
2024-01-06 21:51:53 +01:00
|
|
|
private int countCollectedStones(View view) {
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
for (char c : view.data.toCharArray()) {
|
2024-01-07 19:38:17 +01:00
|
|
|
if (c == '*') count++;
|
2024-01-06 21:51:53 +01:00
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
2024-01-07 15:16:41 +01:00
|
|
|
|
2024-01-07 19:16:31 +01:00
|
|
|
private char goToStone(View view) {
|
2024-01-07 19:09:30 +01:00
|
|
|
int rowDifference = findStoneRow(view) - (view.width / 2);
|
2024-01-06 21:51:53 +01:00
|
|
|
return rowDifference < 0 ? '^' : '<';
|
|
|
|
}
|
|
|
|
|
|
|
|
private int findStoneRow(View view) {
|
2024-01-07 19:09:30 +01:00
|
|
|
return view.data.indexOf('@') / view.width;
|
2024-01-06 21:51:53 +01:00
|
|
|
}
|
2024-01-07 15:16:41 +01:00
|
|
|
|
2024-01-07 19:16:31 +01:00
|
|
|
private char walkBySpiral(View view) {
|
2024-01-06 21:51:53 +01:00
|
|
|
if (moves.isEmpty()) {
|
|
|
|
spiralNumber++;
|
2024-01-07 19:09:30 +01:00
|
|
|
moves += "^".repeat(view.width * spiralNumber) + ">" + "^".repeat(view.width * spiralNumber) + ">";
|
2024-01-06 21:51:53 +01:00
|
|
|
}
|
|
|
|
char nextMove = moves.charAt(0);
|
|
|
|
moves = moves.substring(1);
|
|
|
|
return nextMove;
|
2024-01-06 20:26:01 +01:00
|
|
|
}
|
2024-01-07 19:09:30 +01:00
|
|
|
|
|
|
|
private int calculateCharIndexFromCoordinates(int width, int x, int y){
|
|
|
|
return width * y + x;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int[] generateLeftSideArray(int sideLength) {
|
|
|
|
int[] leftStones = new int[sideLength / 2 * sideLength];
|
|
|
|
int index = 0;
|
|
|
|
|
|
|
|
for (int row = 0; row < sideLength; row++) {
|
|
|
|
for (int col = 0; col < sideLength / 2; col++) {
|
|
|
|
leftStones[index++] = row * sideLength + col;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return leftStones;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int[] generateRightSideArray(int sideLength) {
|
|
|
|
int[] rightStones = new int[sideLength / 2 * sideLength];
|
|
|
|
int index = 0;
|
|
|
|
|
|
|
|
for (int row = 0; row < sideLength; row++) {
|
|
|
|
for (int col = (sideLength / 2) + 1; col < sideLength; col++) {
|
|
|
|
rightStones[index++] = row * sideLength + col;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rightStones;
|
|
|
|
}
|
2024-01-07 22:31:01 +01:00
|
|
|
|
|
|
|
private char walkByColumns(View view) {
|
|
|
|
if(moves.isEmpty()){
|
|
|
|
moves = "^".repeat(28);
|
|
|
|
moves += (goesForward ? ">" + "^".repeat(view.width) + ">" : "<" + "^".repeat(view.width) + "<");
|
|
|
|
goesForward = !goesForward;
|
|
|
|
}
|
|
|
|
char nextMove = moves.charAt(0);
|
|
|
|
moves = moves.substring(1);
|
|
|
|
return nextMove;
|
|
|
|
}
|
2024-01-06 20:26:01 +01:00
|
|
|
}
|