From bd4de54fa7edbe97dec6cc610b4adcf9f6dfbd1a Mon Sep 17 00:00:00 2001 From: Illia Soloviov <74905269+wav3solo@users.noreply.github.com> Date: Mon, 8 Jan 2024 02:03:58 +0100 Subject: [PATCH] Added SuperSnakeBot.java with arg score 30/32 --- src/SuperSnakeBot.java | 115 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 src/SuperSnakeBot.java diff --git a/src/SuperSnakeBot.java b/src/SuperSnakeBot.java new file mode 100644 index 0000000..6655656 --- /dev/null +++ b/src/SuperSnakeBot.java @@ -0,0 +1,115 @@ +public class SuperSnakeBot extends Bot{ + private String moves = ""; + private char previousTurn = '<'; + boolean frontIsBlocked; + boolean leftIsBlocked; + boolean rightIsBlocked; + private int spiralNumber = 0; + private boolean goesForward = true; + public static void main(String[] args) { + Bot superSnakeBot = new SuperSnakeBot(args); + superSnakeBot.run(); + } + + protected SuperSnakeBot(String[] args) { + super(args); + } + + protected char nextMove(View view) throws Exception { + boolean stoneDetected = view.data.contains("@"); + char nextMove; + + nextMove = stoneDetected ? goToStone(view) : walkByColumns(view); + checkBarriers(view); + nextMove = checkMove(nextMove); + + saveMove(nextMove); + return nextMove; + } + + private void checkBarriers(View view){ + int centerCoordinates = view.width / 2; + int frontIndex = getCharIndexFromCoordinates(view.width, centerCoordinates, centerCoordinates - 1); + int leftIndex = getCharIndexFromCoordinates(view.width, centerCoordinates - 1 , centerCoordinates); + int rightIndex = getCharIndexFromCoordinates(view.width, centerCoordinates + 1, centerCoordinates); + + frontIsBlocked = view.data.charAt(frontIndex) == '*'; + leftIsBlocked = view.data.charAt(leftIndex) == '*'; + rightIsBlocked = view.data.charAt(rightIndex) == '*'; + } + + private char checkMove(char move) throws Exception { + if(move == '^' && frontIsBlocked){ + resetMoves(); + + if(leftIsBlocked) move = '>'; + else if(rightIsBlocked) move = '<'; + else if(previousTurn == '<') move = '>'; + else if(previousTurn == '>') move = '<'; + else throw new Exception(); + } + return move; + } + + private void saveMove(char move){ + if(move == '<' || move == '>'){ + previousTurn = move; + } + } + + private char walkBySpiral(View view) { + if (moves.isEmpty()) { + spiralNumber++; + moves += "^".repeat(view.width * spiralNumber) + ">" + "^".repeat(view.width * spiralNumber) + ">"; + } + char nextMove = moves.charAt(0); + moves = moves.substring(1); + return nextMove; + } + + private char walkByColumns(View view) { + if(moves.isEmpty()){ + moves = "^".repeat(28); + moves += (goesForward ? ">" + "^".repeat(view.width) + ">" : "<" + "^".repeat(view.width) + "<"); + goesForward = !goesForward; + } + char nextMove = moves.charAt(0); + moves = moves.substring(1); + return nextMove; + } + + private char goToStone(View view) { + resetMoves(); + + int centerCoordinates = view.width / 2; + + 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 '^'; + } + } + + private int getCharIndexFromCoordinates(int width, int x, int y){ + return width * y + x; + } + + private void resetMoves() { + moves = ""; + spiralNumber = 0; + goesForward = true; + } +}