diff --git a/src/SuperSnakeBot.java b/src/SuperSnakeBot.java index 88fd667..c42b3bb 100644 --- a/src/SuperSnakeBot.java +++ b/src/SuperSnakeBot.java @@ -1,8 +1,10 @@ +import java.util.Random; + public class SuperSnakeBot extends Bot{ private String moves = ""; 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 boolean goesForward = true; public static void main(String[] args) { @@ -18,36 +20,50 @@ public class SuperSnakeBot extends Bot{ boolean stoneDetected = view.data.contains("@"); char nextMove; - nextMove = stoneDetected ? goToStone(view) : walkByColumns(view); checkBarriers(view); + + nextMove = stoneDetected ? goToStone(view) : walkBySpiral(view); + nextMove = checkMove(nextMove); saveMove(nextMove); return nextMove; } - private void checkBarriers(View view) { + private void checkBarriers(View view) throws Exception { int centerCoordinates = view.width / 2; int centerIndex = getCharIndexFromCoordinates(view.width, centerCoordinates, centerCoordinates); int frontIndex = getCharIndexFromCoordinates(view.width, centerCoordinates, centerCoordinates - 1); + int backIndex = getCharIndexFromCoordinates(view.width, centerCoordinates, centerCoordinates + 1); int leftIndex = centerIndex - 1; int rightIndex = centerIndex + 1; frontIsBlocked = view.data.charAt(frontIndex) == '*'; + backIsBlocked = view.data.charAt(backIndex) == '*'; leftIsBlocked = view.data.charAt(leftIndex) == '*'; 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 { - if(move == '^' && frontIsBlocked){ + if (frontIsBlocked) { 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; } @@ -60,8 +76,8 @@ public class SuperSnakeBot extends Bot{ private char walkBySpiral(View view) { 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); moves = moves.substring(1); @@ -70,7 +86,12 @@ public class SuperSnakeBot extends Bot{ private char walkByColumns(View view) { 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 += (goesForward ? ">" + "^".repeat(view.width) + ">" : "<" + "^".repeat(view.width) + "<"); @@ -89,15 +110,15 @@ public class SuperSnakeBot extends Bot{ int[] rightIndexes = getRightIndexes(view); 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) { - if (view.data.charAt(index) == '@') return '<'; + if (view.data.charAt(index) == '@' && view.data.charAt(index + 1) != '*' && !leftIsBlocked) return '<'; } for (int index : rightIndexes) { - if (view.data.charAt(index) == '@') return '>'; + if (view.data.charAt(index) == '@' && view.data.charAt(index - 1) != '*' && !rightIsBlocked) return '>'; } return '^'; @@ -145,7 +166,8 @@ public class SuperSnakeBot extends Bot{ private void resetMovesSequence() { moves = ""; - spiralNumber = 0; + System.out.println("Previous spiralNumber: " + spiralFactor + ". Resetting."); + spiralFactor = 1; spiralDirection = (spiralDirection == '>') ? '<' : '>'; goesForward = true; }