Browse Source

Added countUncollectedStones for avoiding endless loops.

testing_N
Illia Soloviov 11 months ago
parent
commit
2d6def3c29
1 changed files with 21 additions and 7 deletions
  1. 21
    7
      src/SnakeBot.java

+ 21
- 7
src/SnakeBot.java View File

@@ -13,7 +13,7 @@
public class SnakeBot extends Bot{
String moves = "";
private int spiralNumber = 0;
private boolean ignoreStone = false;
private boolean ignoreStones = false;
public static void main(String[] args) {
Bot snakeBot = new SnakeBot(args);
snakeBot.run();
@@ -26,19 +26,35 @@ public class SnakeBot extends Bot{
protected char nextMove(View view) {
boolean stoneDetected = view.data.contains("@");
char nextMove;
nextMove = (stoneDetected && !ignoreStone) ? goToStone(view) : walkBySpiral(view);
nextMove = (stoneDetected && !ignoreStones) ? goToStone(view) : walkBySpiral(view);

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;
ignoreStones = true;
}
if(countCollectedStones(view) <= 2) ignoreStone = false;
/*
* @TODO:
* Avoiding endless loops due to trying to collect two stones at once.
* Problem: stones located near to each other are ignored.
*/
if(countUncollectedStones(view) > 1) ignoreStones = true;
if(countCollectedStones(view) <= 2 && countUncollectedStones(view) <= 1) ignoreStones = false;

return nextMove;
}

private int countUncollectedStones(View view) {
int count = 0;

for (char c : view.data.toCharArray()) {
if (c == '@') count++;
}
return count;
}

private int countCollectedStonesLeft(View view) {
int[] leftSide = generateLeftSideArray(view.width);
int stones = 0;
@@ -61,9 +77,7 @@ public class SnakeBot extends Bot{
int count = 0;

for (char c : view.data.toCharArray()) {
if (c == '*') {
count++;
}
if (c == '*') count++;
}
return count;
}

Loading…
Cancel
Save