Browse Source

Removed "magic numbers" by adding generateLeftSideArray and generateRightSideArray to SnakeBot.java. Added calculateCharIndexFromCoordinates function.

testing_N
Illia Soloviov 11 months ago
parent
commit
7bfbd6812e
2 changed files with 49 additions and 20 deletions
  1. 4
    4
      src/EscapeBot.java
  2. 45
    16
      src/SnakeBot.java

+ 4
- 4
src/EscapeBot.java View File

} }
protected char nextMove(View view){ protected char nextMove(View view){
boolean rocketDetected = view.data.contains("o"); boolean rocketDetected = view.data.contains("o");
return rocketDetected ? goToRocket(view) : walkBySpiral();
return rocketDetected ? goToRocket(view) : walkBySpiral(view);
} }


private char goToRocket(View view){ private char goToRocket(View view){
} }


private int findRocketRow(View view) { 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()) { if (moves.isEmpty()) {
spiralNumber++; spiralNumber++;
moves += "^".repeat(5 * spiralNumber) + ">" + "^".repeat(5 * spiralNumber) + ">";
moves += "^".repeat(view.width * spiralNumber) + ">" + "^".repeat(view.width * spiralNumber) + ">";
} }
char nextMove = moves.charAt(0); char nextMove = moves.charAt(0);
moves = moves.substring(1); moves = moves.substring(1);

+ 45
- 16
src/SnakeBot.java View File

protected char nextMove(View view){ protected char nextMove(View view){
boolean stoneDetected = view.data.contains("@"); boolean stoneDetected = view.data.contains("@");
char nextMove; 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)) ? '<' : '>'; nextMove = (countCollectedStonesLeft(view) <= countCollectedStonesRight(view)) ? '<' : '>';
ignoreStone = true; ignoreStone = true;
} }
} }


private int countCollectedStonesLeft(View view) { 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; 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; return stones;
} }


private int countCollectedStonesRight(View view) { 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; 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; return stones;
} }
} }


private char goToStone(View view){ private char goToStone(View view){
int rowDifference = findStoneRow(view) - 2;
int rowDifference = findStoneRow(view) - (view.width / 2);
return rowDifference < 0 ? '^' : '<'; return rowDifference < 0 ? '^' : '<';
} }


private int findStoneRow(View view) { 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()) { if (moves.isEmpty()) {
spiralNumber++; spiralNumber++;
moves += "^".repeat(5 * spiralNumber) + ">" + "^".repeat(5 * spiralNumber) + ">";
moves += "^".repeat(view.width * spiralNumber) + ">" + "^".repeat(view.width * spiralNumber) + ">";
} }
char nextMove = moves.charAt(0); char nextMove = moves.charAt(0);
moves = moves.substring(1); moves = moves.substring(1);
return nextMove; 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;
}
} }

Loading…
Cancel
Save