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

public class SnakeBot extends Bot{ public class SnakeBot extends Bot{
String moves = ""; String moves = "";
private int spiralNumber = 0; private int spiralNumber = 0;
private boolean ignoreStone = false;
private boolean ignoreStones = false;
public static void main(String[] args) { public static void main(String[] args) {
Bot snakeBot = new SnakeBot(args); Bot snakeBot = new SnakeBot(args);
snakeBot.run(); snakeBot.run();
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(view);
nextMove = (stoneDetected && !ignoreStones) ? goToStone(view) : walkBySpiral(view);


int centerCoordinateOfView = view.width / 2; int centerCoordinateOfView = view.width / 2;
int frontCellIndex = calculateCharIndexFromCoordinates(view.width, centerCoordinateOfView, centerCoordinateOfView - 1); int frontCellIndex = calculateCharIndexFromCoordinates(view.width, centerCoordinateOfView, centerCoordinateOfView - 1);


if(nextMove == '^' && view.data.charAt(frontCellIndex) == '*'){ if(nextMove == '^' && view.data.charAt(frontCellIndex) == '*'){
nextMove = (countCollectedStonesLeft(view) <= countCollectedStonesRight(view)) ? '<' : '>'; 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; 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) { private int countCollectedStonesLeft(View view) {
int[] leftSide = generateLeftSideArray(view.width); int[] leftSide = generateLeftSideArray(view.width);
int stones = 0; int stones = 0;
int count = 0; int count = 0;


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

Loading…
Cancel
Save