Simplified and generalized checkBarriers()
This commit is contained in:
parent
6c28d6e358
commit
d4b29340bc
@ -1,10 +1,9 @@
|
||||
public class SuperSnakeBot extends Bot{
|
||||
private String moves = "";
|
||||
private char previousTurn = '<';
|
||||
boolean frontIsBlocked;
|
||||
boolean leftIsBlocked;
|
||||
boolean rightIsBlocked;
|
||||
boolean frontIsBlocked, leftIsBlocked, rightIsBlocked;
|
||||
private int spiralNumber = 0;
|
||||
private char spiralDirection = '>';
|
||||
private boolean goesForward = true;
|
||||
public static void main(String[] args) {
|
||||
Bot superSnakeBot = new SuperSnakeBot(args);
|
||||
@ -27,11 +26,13 @@ public class SuperSnakeBot extends Bot{
|
||||
return nextMove;
|
||||
}
|
||||
|
||||
private void checkBarriers(View view){
|
||||
private void checkBarriers(View view) {
|
||||
int centerCoordinates = view.width / 2;
|
||||
int centerIndex = getCharIndexFromCoordinates(view.width, centerCoordinates, centerCoordinates);
|
||||
|
||||
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) == '*';
|
||||
leftIsBlocked = view.data.charAt(leftIndex) == '*';
|
||||
@ -40,7 +41,7 @@ public class SuperSnakeBot extends Bot{
|
||||
|
||||
private char checkMove(char move) throws Exception {
|
||||
if(move == '^' && frontIsBlocked){
|
||||
resetMoves();
|
||||
resetMovesSequence();
|
||||
|
||||
if(leftIsBlocked) move = '>';
|
||||
else if(rightIsBlocked) move = '<';
|
||||
@ -51,7 +52,7 @@ public class SuperSnakeBot extends Bot{
|
||||
return move;
|
||||
}
|
||||
|
||||
private void saveMove(char move){
|
||||
private void saveMove(char move) {
|
||||
if(move == '<' || move == '>'){
|
||||
previousTurn = move;
|
||||
}
|
||||
@ -60,7 +61,7 @@ public class SuperSnakeBot extends Bot{
|
||||
private char walkBySpiral(View view) {
|
||||
if (moves.isEmpty()) {
|
||||
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);
|
||||
moves = moves.substring(1);
|
||||
@ -69,7 +70,9 @@ public class SuperSnakeBot extends Bot{
|
||||
|
||||
private char walkByColumns(View view) {
|
||||
if(moves.isEmpty()){
|
||||
moves = "^".repeat(28);
|
||||
int steps = 32 - view.width;
|
||||
|
||||
moves = "^".repeat(steps);
|
||||
moves += (goesForward ? ">" + "^".repeat(view.width) + ">" : "<" + "^".repeat(view.width) + "<");
|
||||
goesForward = !goesForward;
|
||||
}
|
||||
@ -79,37 +82,71 @@ public class SuperSnakeBot extends Bot{
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
int rightIndex = getCharIndexFromCoordinates(view.width, centerCoordinates + 1, centerCoordinates);
|
||||
int farRightIndex = getCharIndexFromCoordinates(view.width, centerCoordinates + 2, centerCoordinates);
|
||||
|
||||
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 : frontIndexes) {
|
||||
if (view.data.charAt(index) == '@') return '^';
|
||||
}
|
||||
|
||||
for (int index : leftIndexes) {
|
||||
if (view.data.charAt(index) == '@') return '<';
|
||||
}
|
||||
|
||||
for (int index : rightIndexes) {
|
||||
if (view.data.charAt(index) == '@') return '>';
|
||||
}
|
||||
|
||||
return '^';
|
||||
}
|
||||
|
||||
private int getCharIndexFromCoordinates(int width, int x, int y){
|
||||
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) {
|
||||
return width * y + x;
|
||||
}
|
||||
|
||||
private void resetMoves() {
|
||||
private void resetMovesSequence() {
|
||||
moves = "";
|
||||
spiralNumber = 0;
|
||||
spiralDirection = (spiralDirection == '>') ? '<' : '>';
|
||||
goesForward = true;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user