diff --git a/src/EscapeBot.java b/src/EscapeBot.java index 8d0fbb2..e0352ba 100644 --- a/src/EscapeBot.java +++ b/src/EscapeBot.java @@ -29,7 +29,7 @@ public class EscapeBot extends Bot{ } protected char nextMove(View view){ boolean rocketDetected = view.data.contains("o"); - return rocketDetected ? goToRocket(view) : walkBySpiral(); + return rocketDetected ? goToRocket(view) : walkBySpiral(view); } private char goToRocket(View view){ @@ -38,13 +38,13 @@ public class EscapeBot extends Bot{ } private int findRocketRow(View view) { - return view.data.indexOf('o') / 5; + return view.data.indexOf('o') / view.width; } - private char walkBySpiral(){ + private char walkBySpiral(View view){ if (moves.isEmpty()) { spiralNumber++; - moves += "^".repeat(5 * spiralNumber) + ">" + "^".repeat(5 * spiralNumber) + ">"; + moves += "^".repeat(view.width * spiralNumber) + ">" + "^".repeat(view.width * spiralNumber) + ">"; } char nextMove = moves.charAt(0); moves = moves.substring(1); diff --git a/src/SnakeBot.java b/src/SnakeBot.java index 4f31d22..bc3c7e4 100644 --- a/src/SnakeBot.java +++ b/src/SnakeBot.java @@ -26,9 +26,12 @@ public class SnakeBot extends Bot{ protected char nextMove(View view){ boolean stoneDetected = view.data.contains("@"); char nextMove; - nextMove = (stoneDetected && !ignoreStone) ? goToStone(view) : walkBySpiral(); + nextMove = (stoneDetected && !ignoreStone) ? goToStone(view) : walkBySpiral(view); - if(nextMove == '^' && view.data.charAt(7) == '*'){ + int centerCoordinateOfView = view.width / 2; + int frontCellIndex = calculateCharIndexFromCoordinates(view.width, centerCoordinateOfView, centerCoordinateOfView - 1); + + if(nextMove == '^' && view.data.charAt(frontCellIndex) == '*'){ nextMove = (countCollectedStonesLeft(view) <= countCollectedStonesRight(view)) ? '<' : '>'; ignoreStone = true; } @@ -37,23 +40,19 @@ public class SnakeBot extends Bot{ } private int countCollectedStonesLeft(View view) { - int[] leftStones = {0, 1, 5, 6, 10, 11, 15, 16, 20, 21}; + int[] leftSide = generateLeftSideArray(view.width); int stones = 0; - for (int stone : leftStones) { - if(view.data.charAt(stone) == '*'){ - stones++; - } + for (int cellIndex : leftSide) { + if(view.data.charAt(cellIndex) == '*') stones++; } return stones; } private int countCollectedStonesRight(View view) { - int[] rightStones = {3, 4, 8, 9, 13, 14, 18, 19, 23, 24}; + int[] rightSide = generateRightSideArray(view.width); int stones = 0; - for (int stone : rightStones) { - if(view.data.charAt(stone) == '*'){ - stones++; - } + for (int cellIndex : rightSide) { + if(view.data.charAt(cellIndex) == '*') stones++; } return stones; } @@ -70,21 +69,51 @@ public class SnakeBot extends Bot{ } private char goToStone(View view){ - int rowDifference = findStoneRow(view) - 2; + int rowDifference = findStoneRow(view) - (view.width / 2); return rowDifference < 0 ? '^' : '<'; } private int findStoneRow(View view) { - return view.data.indexOf('@') / 5; + return view.data.indexOf('@') / view.width; } - private char walkBySpiral(){ + private char walkBySpiral(View view){ if (moves.isEmpty()) { spiralNumber++; - moves += "^".repeat(5 * spiralNumber) + ">" + "^".repeat(5 * spiralNumber) + ">"; + moves += "^".repeat(view.width * spiralNumber) + ">" + "^".repeat(view.width * spiralNumber) + ">"; } char nextMove = moves.charAt(0); moves = moves.substring(1); return nextMove; } + + 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; + } }