Browse Source

Added goToNearbyStone and isStoneNearby to SnakeBot.java

testing_N
Illia Soloviov 4 months ago
parent
commit
7413a48b58
1 changed files with 49 additions and 7 deletions
  1. 49
    7
      src/SnakeBot.java

+ 49
- 7
src/SnakeBot.java View File

@@ -13,6 +13,7 @@
public class SnakeBot extends Bot{
String moves = "";
private int spiralNumber = 0;
private boolean goesForward = true;
private boolean ignoreStones = false;
public static void main(String[] args) {
Bot snakeBot = new SnakeBot(args);
@@ -22,7 +23,8 @@ public class SnakeBot extends Bot{
protected SnakeBot(String[] args) {
super(args);
}
//@TODO: find a better way to avoid collectedStones
//@TODO: find a better way to avoid collected stones
//@TODO: find a better way to collect stones
protected char nextMove(View view) {
boolean stoneDetected = view.data.contains("@");
char nextMove;
@@ -35,18 +37,47 @@ public class SnakeBot extends Bot{
nextMove = (countCollectedStonesLeft(view) <= countCollectedStonesRight(view)) ? '<' : '>';
ignoreStones = true;
}
/*
* @TODO:
* Avoiding endless loops due to trying to collect two stones at once.
* Problem: stones located near to each other are ignored.
* Tested: 20 tries; avgScore = 25.45/32; minScore = 19; maxScore = 29; trapped 1/20.
*/

if(countUncollectedStones(view) > 1) ignoreStones = true;
if(countCollectedStones(view) <= 2 && countUncollectedStones(view) <= 1) ignoreStones = false;

if(isStoneNearby(view)){
nextMove = goToNearbyStone(view);
}

return nextMove;
}

private boolean isStoneNearby(View view){
int centerCoordinateOfView = view.width / 2;
int frontCellIndex = calculateCharIndexFromCoordinates(view.width, centerCoordinateOfView, centerCoordinateOfView - 1);
int leftCellIndex = calculateCharIndexFromCoordinates(view.width, centerCoordinateOfView - 1, centerCoordinateOfView);
int rightCellIndex = calculateCharIndexFromCoordinates(view.width, centerCoordinateOfView + 1, centerCoordinateOfView);

boolean stoneIsInFront = view.data.charAt(frontCellIndex) == '@';
boolean stoneIsOnTheLeft = view.data.charAt(leftCellIndex) == '@';
boolean stoneIsOneTheRight = view.data.charAt(rightCellIndex) == '@';

return stoneIsInFront || stoneIsOnTheLeft || stoneIsOneTheRight;
}

private char goToNearbyStone(View view){
int centerCoordinateOfView = view.width / 2;
int frontCellIndex = calculateCharIndexFromCoordinates(view.width, centerCoordinateOfView, centerCoordinateOfView - 1);
int leftCellIndex = calculateCharIndexFromCoordinates(view.width, centerCoordinateOfView - 1, centerCoordinateOfView);
int rightCellIndex = calculateCharIndexFromCoordinates(view.width, centerCoordinateOfView + 1, centerCoordinateOfView);

if(view.data.charAt(frontCellIndex) == '@') return '^';
if(view.data.charAt(leftCellIndex) == '@') return '<';
if(view.data.charAt(rightCellIndex) == '@') return '>';
return 0;
}

private void resetMoves() {
moves = "";
spiralNumber = 0;
}

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

@@ -131,4 +162,15 @@ public class SnakeBot extends Bot{

return rightStones;
}

private char walkByColumns(View view) {
if(moves.isEmpty()){
moves = "^".repeat(28);
moves += (goesForward ? ">" + "^".repeat(view.width) + ">" : "<" + "^".repeat(view.width) + "<");
goesForward = !goesForward;
}
char nextMove = moves.charAt(0);
moves = moves.substring(1);
return nextMove;
}
}

Loading…
Cancel
Save