Browse Source

Simplified and generalized checkBarriers()

testing_N
Illia Soloviov 11 months ago
parent
commit
d4b29340bc
1 changed files with 66 additions and 29 deletions
  1. 66
    29
      src/SuperSnakeBot.java

+ 66
- 29
src/SuperSnakeBot.java View File

public class SuperSnakeBot extends Bot{ public class SuperSnakeBot extends Bot{
private String moves = ""; private String moves = "";
private char previousTurn = '<'; private char previousTurn = '<';
boolean frontIsBlocked;
boolean leftIsBlocked;
boolean rightIsBlocked;
boolean frontIsBlocked, leftIsBlocked, rightIsBlocked;
private int spiralNumber = 0; private int spiralNumber = 0;
private char spiralDirection = '>';
private boolean goesForward = true; private boolean goesForward = true;
public static void main(String[] args) { public static void main(String[] args) {
Bot superSnakeBot = new SuperSnakeBot(args); Bot superSnakeBot = new SuperSnakeBot(args);
return nextMove; return nextMove;
} }


private void checkBarriers(View view){
private void checkBarriers(View view) {
int centerCoordinates = view.width / 2; int centerCoordinates = view.width / 2;
int centerIndex = getCharIndexFromCoordinates(view.width, centerCoordinates, centerCoordinates);

int frontIndex = getCharIndexFromCoordinates(view.width, centerCoordinates, centerCoordinates - 1); int frontIndex = getCharIndexFromCoordinates(view.width, centerCoordinates, centerCoordinates - 1);
int leftIndex = getCharIndexFromCoordinates(view.width, centerCoordinates - 1 , centerCoordinates);
int rightIndex = getCharIndexFromCoordinates(view.width, centerCoordinates + 1, centerCoordinates);
int leftIndex = centerIndex - 1;
int rightIndex = centerIndex + 1;


frontIsBlocked = view.data.charAt(frontIndex) == '*'; frontIsBlocked = view.data.charAt(frontIndex) == '*';
leftIsBlocked = view.data.charAt(leftIndex) == '*'; leftIsBlocked = view.data.charAt(leftIndex) == '*';


private char checkMove(char move) throws Exception { private char checkMove(char move) throws Exception {
if(move == '^' && frontIsBlocked){ if(move == '^' && frontIsBlocked){
resetMoves();
resetMovesSequence();


if(leftIsBlocked) move = '>'; if(leftIsBlocked) move = '>';
else if(rightIsBlocked) move = '<'; else if(rightIsBlocked) move = '<';
return move; return move;
} }


private void saveMove(char move){
private void saveMove(char move) {
if(move == '<' || move == '>'){ if(move == '<' || move == '>'){
previousTurn = move; previousTurn = move;
} }
private char walkBySpiral(View view) { private char walkBySpiral(View view) {
if (moves.isEmpty()) { if (moves.isEmpty()) {
spiralNumber++; spiralNumber++;
moves += "^".repeat(view.width * spiralNumber) + ">" + "^".repeat(view.width * spiralNumber) + ">";
moves += "^".repeat(view.width * spiralNumber) + spiralDirection + "^".repeat(view.width * spiralNumber) + spiralDirection;
} }
char nextMove = moves.charAt(0); char nextMove = moves.charAt(0);
moves = moves.substring(1); moves = moves.substring(1);


private char walkByColumns(View view) { private char walkByColumns(View view) {
if(moves.isEmpty()){ if(moves.isEmpty()){
moves = "^".repeat(28);
int steps = 32 - view.width;

moves = "^".repeat(steps);
moves += (goesForward ? ">" + "^".repeat(view.width) + ">" : "<" + "^".repeat(view.width) + "<"); moves += (goesForward ? ">" + "^".repeat(view.width) + ">" : "<" + "^".repeat(view.width) + "<");
goesForward = !goesForward; goesForward = !goesForward;
} }
} }


private char goToStone(View view) { private char goToStone(View view) {
resetMoves();
resetMovesSequence();


int centerCoordinates = view.width / 2;
int[] frontIndexes = getFrontIndexes(view);
int[] leftIndexes = getLeftIndexes(view);
int[] rightIndexes = getRightIndexes(view);


int leftIndex = getCharIndexFromCoordinates(view.width, centerCoordinates - 1, centerCoordinates);
int farLeftIndex = getCharIndexFromCoordinates(view.width, centerCoordinates - 2, centerCoordinates);
for (int index : frontIndexes) {
if (view.data.charAt(index) == '@') return '^';
}


int rightIndex = getCharIndexFromCoordinates(view.width, centerCoordinates + 1, centerCoordinates);
int farRightIndex = getCharIndexFromCoordinates(view.width, centerCoordinates + 2, centerCoordinates);
for (int index : leftIndexes) {
if (view.data.charAt(index) == '@') return '<';
}


int frontIndex = getCharIndexFromCoordinates(view.width, centerCoordinates, centerCoordinates - 1);
int farFrontIndex = getCharIndexFromCoordinates(view.width, centerCoordinates, centerCoordinates - 2);

if(view.data.charAt(frontIndex) == '@' || view.data.charAt(farFrontIndex) == '@'){
return '^';
}else if(view.data.charAt(leftIndex) == '@' || view.data.charAt(farLeftIndex) == '@'){
return '<';
}else if(view.data.charAt(rightIndex) == '@' || view.data.charAt(farRightIndex) == '@'){
return '>';
}else {
return '^';
for (int index : rightIndexes) {
if (view.data.charAt(index) == '@') return '>';
}

return '^';
}

private int[] getLeftIndexes(View view) {
int center = view.width / 2;
int[] leftIndexes = new int[center];
int index = 0;

for (int col = 0; col < center; col++) {
leftIndexes[index++] = getCharIndexFromCoordinates(view.width, col, center);
} }

return leftIndexes;
}

private int[] getRightIndexes(View view) {
int center = view.width / 2;
int[] rightIndexes = new int[center];
int index = 0;

for (int col = center + 1; col < view.width; col++) {
rightIndexes[index++] = getCharIndexFromCoordinates(view.width, col, center);
}

return rightIndexes;
}

private int[] getFrontIndexes(View view) {
int center = view.width / 2;
int[] frontIndexes = new int[center];
int index = 0;

for (int row = 0; row < center; row++) {
frontIndexes[index++] = getCharIndexFromCoordinates(view.width, center, row);
}

return frontIndexes;
} }


private int getCharIndexFromCoordinates(int width, int x, int y){
private int getCharIndexFromCoordinates(int width, int x, int y) {
return width * y + x; return width * y + x;
} }


private void resetMoves() {
private void resetMovesSequence() {
moves = ""; moves = "";
spiralNumber = 0; spiralNumber = 0;
spiralDirection = (spiralDirection == '>') ? '<' : '>';
goesForward = true; goesForward = true;
} }
} }

Loading…
Cancel
Save