Simplified and generalized checkBarriers()
This commit is contained in:
parent
6c28d6e358
commit
d4b29340bc
@ -1,10 +1,9 @@
|
|||||||
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 frontIsBlocked, leftIsBlocked, rightIsBlocked;
|
||||||
boolean leftIsBlocked;
|
|
||||||
boolean 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);
|
||||||
@ -27,11 +26,13 @@ public class SuperSnakeBot extends Bot{
|
|||||||
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 leftIndex = centerIndex - 1;
|
||||||
int rightIndex = getCharIndexFromCoordinates(view.width, centerCoordinates + 1, centerCoordinates);
|
int rightIndex = centerIndex + 1;
|
||||||
|
|
||||||
frontIsBlocked = view.data.charAt(frontIndex) == '*';
|
frontIsBlocked = view.data.charAt(frontIndex) == '*';
|
||||||
leftIsBlocked = view.data.charAt(leftIndex) == '*';
|
leftIsBlocked = view.data.charAt(leftIndex) == '*';
|
||||||
@ -40,7 +41,7 @@ public class SuperSnakeBot extends Bot{
|
|||||||
|
|
||||||
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 = '<';
|
||||||
@ -51,7 +52,7 @@ public class SuperSnakeBot extends Bot{
|
|||||||
return move;
|
return move;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void saveMove(char move){
|
private void saveMove(char move) {
|
||||||
if(move == '<' || move == '>'){
|
if(move == '<' || move == '>'){
|
||||||
previousTurn = move;
|
previousTurn = move;
|
||||||
}
|
}
|
||||||
@ -60,7 +61,7 @@ public class SuperSnakeBot extends Bot{
|
|||||||
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);
|
||||||
@ -69,7 +70,9 @@ public class SuperSnakeBot extends Bot{
|
|||||||
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
@ -79,37 +82,71 @@ public class SuperSnakeBot extends Bot{
|
|||||||
}
|
}
|
||||||
|
|
||||||
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);
|
for (int index : frontIndexes) {
|
||||||
int farLeftIndex = getCharIndexFromCoordinates(view.width, centerCoordinates - 2, centerCoordinates);
|
if (view.data.charAt(index) == '@') return '^';
|
||||||
|
|
||||||
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 : 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;
|
return width * y + x;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void resetMoves() {
|
private void resetMovesSequence() {
|
||||||
moves = "";
|
moves = "";
|
||||||
spiralNumber = 0;
|
spiralNumber = 0;
|
||||||
|
spiralDirection = (spiralDirection == '>') ? '<' : '>';
|
||||||
goesForward = true;
|
goesForward = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user