123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- /*
- * 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“.
- */
-
- import java.util.Random;
-
- public class SnakeBot extends Bot{
- private String moves = "";
- private boolean frontIsBlocked, backIsBlocked, leftIsBlocked, rightIsBlocked, trapped;
- private char previousTurn = '<';
- private int spiralFactor = 1;
- private char spiralDirection = '>';
- private boolean goesForward = true;
- public static void main(String[] args) {
- Bot snakeBot = new SnakeBot(args);
- snakeBot.run();
- }
-
- protected SnakeBot(String[] args) {
- super(args);
- }
-
- protected char nextMove(View view) throws Exception {
- boolean stoneDetected = view.data.contains("@");
- char nextMove;
-
- checkBarriers(view);
-
- nextMove = stoneDetected ? goToStone(view) : walkBySpiral(view);
-
- nextMove = checkMove(nextMove);
-
- saveMove(nextMove);
- return nextMove;
- }
-
- private void checkBarriers(View view) throws Exception {
- int centerCoordinates = view.width / 2;
- int centerIndex = getCharIndexFromCoordinates(view.width, centerCoordinates, centerCoordinates);
-
- int frontIndex = getCharIndexFromCoordinates(view.width, centerCoordinates, centerCoordinates - 1);
- int backIndex = getCharIndexFromCoordinates(view.width, centerCoordinates, centerCoordinates + 1);
- int leftIndex = centerIndex - 1;
- int rightIndex = centerIndex + 1;
-
- char[] obstacles = {'#', '~', 'X', '*'};
-
- frontIsBlocked = containsChar(obstacles, view.data.charAt(frontIndex));
- backIsBlocked = containsChar(obstacles, view.data.charAt(backIndex));
- leftIsBlocked = containsChar(obstacles, view.data.charAt(leftIndex));
- rightIsBlocked = containsChar(obstacles, view.data.charAt(rightIndex));
-
- trapped = frontIsBlocked && backIsBlocked && leftIsBlocked && rightIsBlocked;
-
- if (trapped) {
- Exception trappedException = new Exception("Snake is trapped");
- System.err.println(trappedException.getMessage());
- throw trappedException;
- }
- }
-
- private boolean containsChar(char[] array, char target) {
- for (char c : array) {
- if (c == target) {
- return true;
- }
- }
- return false;
- }
-
- private char checkMove(char move) throws Exception {
- if (frontIsBlocked) {
- resetMovesSequence();
- if (leftIsBlocked) move = '>';
- else if (rightIsBlocked) move = '<';
- else if (previousTurn == '<') move = '>';
- else if (previousTurn == '>') move = '<';
- else {
- Exception invalidTurnException = new Exception("The previous turn was neither '<' nor '>'.");
- System.err.println(invalidTurnException.getMessage());
- throw invalidTurnException;
- }
- }
- return move;
- }
-
- private void saveMove(char move) {
- if (move == '<' || move == '>') {
- previousTurn = move;
- }
- }
-
- private char walkBySpiral(View view) {
- if (moves.isEmpty()) {
- spiralFactor++;
- moves += "^".repeat(view.width * spiralFactor) + spiralDirection + "^".repeat(view.width * spiralFactor) + spiralDirection;
- }
- char nextMove = moves.charAt(0);
- moves = moves.substring(1);
- return nextMove;
- }
-
- private char walkByColumns(View view) {
- if(moves.isEmpty()){
- int min = 10;
- int max = 100;
-
- Random random = new Random();
- int steps = random.nextInt((max - min) + 1) + min;
- //int steps = 32 - view.width;
-
- moves = "^".repeat(steps);
- moves += (goesForward ? ">" + "^".repeat(view.width) + ">" : "<" + "^".repeat(view.width) + "<");
- goesForward = !goesForward;
- }
- char nextMove = moves.charAt(0);
- moves = moves.substring(1);
- return nextMove;
- }
-
- private char goToStone(View view) {
- resetMovesSequence();
-
- int[] frontIndexes = getFrontIndexes(view);
- int[] leftIndexes = getLeftIndexes(view);
- int[] rightIndexes = getRightIndexes(view);
-
- for (int index : frontIndexes) {
- if (view.data.charAt(index) == '@' && view.data.charAt(index + view.width) != '*' && !frontIsBlocked) return '^';
- }
-
- for (int index : leftIndexes) {
- if (view.data.charAt(index) == '@' && view.data.charAt(index + 1) != '*' && !leftIsBlocked) return '<';
- }
-
- for (int index : rightIndexes) {
- if (view.data.charAt(index) == '@' && view.data.charAt(index - 1) != '*' && !rightIsBlocked) return '>';
- }
-
- return '^';
- }
-
- private int[] getLeftIndexes(View view) {
- int center = view.width / 2;
- int[] leftIndexes = new int[center];
- int index = 0;
-
- for (int col = 0; col < center; col++) {
- leftIndexes[index++] = getCharIndexFromCoordinates(view.width, col, center);
- }
-
- return leftIndexes;
- }
-
- private int[] getRightIndexes(View view) {
- int center = view.width / 2;
- int[] rightIndexes = new int[center];
- int index = 0;
-
- for (int col = center + 1; col < view.width; col++) {
- rightIndexes[index++] = getCharIndexFromCoordinates(view.width, col, center);
- }
-
- return rightIndexes;
- }
-
- private int[] getFrontIndexes(View view) {
- int center = view.width / 2;
- int[] frontIndexes = new int[center];
- int index = 0;
-
- for (int row = 0; row < center; row++) {
- frontIndexes[index++] = getCharIndexFromCoordinates(view.width, center, row);
- }
-
- return frontIndexes;
- }
-
- private int getCharIndexFromCoordinates(int width, int x, int y) {
- return width * y + x;
- }
-
- private void resetMovesSequence() {
- moves = "";
- System.out.println("Previous spiralNumber: " + spiralFactor + ". Resetting.");
- spiralFactor = 1;
- spiralDirection = (spiralDirection == '>') ? '<' : '>';
- goesForward = true;
- }
- }
|