|
|
|
|
|
|
|
|
|
|
|
import java.util.Random; |
|
|
|
|
|
|
|
|
public class SuperSnakeBot extends Bot{ |
|
|
public class SuperSnakeBot extends Bot{ |
|
|
private String moves = ""; |
|
|
private String moves = ""; |
|
|
private char previousTurn = '<'; |
|
|
private char previousTurn = '<'; |
|
|
boolean frontIsBlocked, leftIsBlocked, rightIsBlocked; |
|
|
|
|
|
private int spiralNumber = 0; |
|
|
|
|
|
|
|
|
boolean frontIsBlocked, backIsBlocked, leftIsBlocked, rightIsBlocked, trapped; |
|
|
|
|
|
private int spiralFactor = 1; |
|
|
private char spiralDirection = '>'; |
|
|
private char spiralDirection = '>'; |
|
|
private boolean goesForward = true; |
|
|
private boolean goesForward = true; |
|
|
public static void main(String[] args) { |
|
|
public static void main(String[] args) { |
|
|
|
|
|
|
|
|
boolean stoneDetected = view.data.contains("@"); |
|
|
boolean stoneDetected = view.data.contains("@"); |
|
|
char nextMove; |
|
|
char nextMove; |
|
|
|
|
|
|
|
|
nextMove = stoneDetected ? goToStone(view) : walkByColumns(view); |
|
|
|
|
|
checkBarriers(view); |
|
|
checkBarriers(view); |
|
|
|
|
|
|
|
|
|
|
|
nextMove = stoneDetected ? goToStone(view) : walkBySpiral(view); |
|
|
|
|
|
|
|
|
nextMove = checkMove(nextMove); |
|
|
nextMove = checkMove(nextMove); |
|
|
|
|
|
|
|
|
saveMove(nextMove); |
|
|
saveMove(nextMove); |
|
|
return nextMove; |
|
|
return nextMove; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void checkBarriers(View view) { |
|
|
|
|
|
|
|
|
private void checkBarriers(View view) throws Exception { |
|
|
int centerCoordinates = view.width / 2; |
|
|
int centerCoordinates = view.width / 2; |
|
|
int centerIndex = getCharIndexFromCoordinates(view.width, centerCoordinates, centerCoordinates); |
|
|
int centerIndex = getCharIndexFromCoordinates(view.width, centerCoordinates, centerCoordinates); |
|
|
|
|
|
|
|
|
int frontIndex = getCharIndexFromCoordinates(view.width, centerCoordinates, centerCoordinates - 1); |
|
|
int frontIndex = getCharIndexFromCoordinates(view.width, centerCoordinates, centerCoordinates - 1); |
|
|
|
|
|
int backIndex = getCharIndexFromCoordinates(view.width, centerCoordinates, centerCoordinates + 1); |
|
|
int leftIndex = centerIndex - 1; |
|
|
int leftIndex = centerIndex - 1; |
|
|
int rightIndex = centerIndex + 1; |
|
|
int rightIndex = centerIndex + 1; |
|
|
|
|
|
|
|
|
frontIsBlocked = view.data.charAt(frontIndex) == '*'; |
|
|
frontIsBlocked = view.data.charAt(frontIndex) == '*'; |
|
|
|
|
|
backIsBlocked = view.data.charAt(backIndex) == '*'; |
|
|
leftIsBlocked = view.data.charAt(leftIndex) == '*'; |
|
|
leftIsBlocked = view.data.charAt(leftIndex) == '*'; |
|
|
rightIsBlocked = view.data.charAt(rightIndex) == '*'; |
|
|
rightIsBlocked = view.data.charAt(rightIndex) == '*'; |
|
|
|
|
|
trapped = frontIsBlocked && backIsBlocked && leftIsBlocked && rightIsBlocked; |
|
|
|
|
|
|
|
|
|
|
|
if (trapped) { |
|
|
|
|
|
Exception trappedException = new Exception("Snake is trapped"); |
|
|
|
|
|
System.err.println(trappedException.getMessage()); |
|
|
|
|
|
throw trappedException; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private char checkMove(char move) throws Exception { |
|
|
private char checkMove(char move) throws Exception { |
|
|
if(move == '^' && frontIsBlocked){ |
|
|
|
|
|
|
|
|
if (frontIsBlocked) { |
|
|
resetMovesSequence(); |
|
|
resetMovesSequence(); |
|
|
|
|
|
|
|
|
if(leftIsBlocked) move = '>'; |
|
|
|
|
|
else if(rightIsBlocked) move = '<'; |
|
|
|
|
|
else if(previousTurn == '<') move = '>'; |
|
|
|
|
|
else if(previousTurn == '>') move = '<'; |
|
|
|
|
|
else throw new Exception(); |
|
|
|
|
|
|
|
|
if (leftIsBlocked) move = '>'; |
|
|
|
|
|
else if (rightIsBlocked) move = '<'; |
|
|
|
|
|
else if (previousTurn == '<') move = '>'; |
|
|
|
|
|
else if (previousTurn == '>') move = '<'; |
|
|
|
|
|
else { |
|
|
|
|
|
Exception invalidTurnException = new Exception("The previous turn was neither '<' nor '>'."); |
|
|
|
|
|
System.err.println(invalidTurnException.getMessage()); |
|
|
|
|
|
throw invalidTurnException; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
return move; |
|
|
return move; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private char walkBySpiral(View view) { |
|
|
private char walkBySpiral(View view) { |
|
|
if (moves.isEmpty()) { |
|
|
if (moves.isEmpty()) { |
|
|
spiralNumber++; |
|
|
|
|
|
moves += "^".repeat(view.width * spiralNumber) + spiralDirection + "^".repeat(view.width * spiralNumber) + spiralDirection; |
|
|
|
|
|
|
|
|
spiralFactor++; |
|
|
|
|
|
moves += "^".repeat(view.width * spiralFactor) + spiralDirection + "^".repeat(view.width * spiralFactor) + 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()){ |
|
|
int steps = 32 - view.width; |
|
|
|
|
|
|
|
|
int min = 10; |
|
|
|
|
|
int max = 100; |
|
|
|
|
|
|
|
|
|
|
|
Random random = new Random(); |
|
|
|
|
|
int steps = random.nextInt((max - min) + 1) + min; |
|
|
|
|
|
//int steps = 32 - view.width; |
|
|
|
|
|
|
|
|
moves = "^".repeat(steps); |
|
|
moves = "^".repeat(steps); |
|
|
moves += (goesForward ? ">" + "^".repeat(view.width) + ">" : "<" + "^".repeat(view.width) + "<"); |
|
|
moves += (goesForward ? ">" + "^".repeat(view.width) + ">" : "<" + "^".repeat(view.width) + "<"); |
|
|
|
|
|
|
|
|
int[] rightIndexes = getRightIndexes(view); |
|
|
int[] rightIndexes = getRightIndexes(view); |
|
|
|
|
|
|
|
|
for (int index : frontIndexes) { |
|
|
for (int index : frontIndexes) { |
|
|
if (view.data.charAt(index) == '@') return '^'; |
|
|
|
|
|
|
|
|
if (view.data.charAt(index) == '@' && view.data.charAt(index + view.width) != '*' && !frontIsBlocked) return '^'; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
for (int index : leftIndexes) { |
|
|
for (int index : leftIndexes) { |
|
|
if (view.data.charAt(index) == '@') return '<'; |
|
|
|
|
|
|
|
|
if (view.data.charAt(index) == '@' && view.data.charAt(index + 1) != '*' && !leftIsBlocked) return '<'; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
for (int index : rightIndexes) { |
|
|
for (int index : rightIndexes) { |
|
|
if (view.data.charAt(index) == '@') return '>'; |
|
|
|
|
|
|
|
|
if (view.data.charAt(index) == '@' && view.data.charAt(index - 1) != '*' && !rightIsBlocked) return '>'; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return '^'; |
|
|
return '^'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void resetMovesSequence() { |
|
|
private void resetMovesSequence() { |
|
|
moves = ""; |
|
|
moves = ""; |
|
|
spiralNumber = 0; |
|
|
|
|
|
|
|
|
System.out.println("Previous spiralNumber: " + spiralFactor + ". Resetting."); |
|
|
|
|
|
spiralFactor = 1; |
|
|
spiralDirection = (spiralDirection == '>') ? '<' : '>'; |
|
|
spiralDirection = (spiralDirection == '>') ? '<' : '>'; |
|
|
goesForward = true; |
|
|
goesForward = true; |
|
|
} |
|
|
} |