From 7413a48b584b399cc9ff4af0dc6c952d35672cd5 Mon Sep 17 00:00:00 2001 From: Illia Soloviov <74905269+wav3solo@users.noreply.github.com> Date: Sun, 7 Jan 2024 22:31:01 +0100 Subject: [PATCH] Added goToNearbyStone and isStoneNearby to SnakeBot.java --- src/SnakeBot.java | 56 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 7 deletions(-) diff --git a/src/SnakeBot.java b/src/SnakeBot.java index 00bf479..f594f83 100644 --- a/src/SnakeBot.java +++ b/src/SnakeBot.java @@ -13,6 +13,7 @@ public class SnakeBot extends Bot{ String moves = ""; private int spiralNumber = 0; + private boolean goesForward = true; private boolean ignoreStones = false; public static void main(String[] args) { Bot snakeBot = new SnakeBot(args); @@ -22,7 +23,8 @@ public class SnakeBot extends Bot{ protected SnakeBot(String[] args) { super(args); } - //@TODO: find a better way to avoid collectedStones + //@TODO: find a better way to avoid collected stones + //@TODO: find a better way to collect stones protected char nextMove(View view) { boolean stoneDetected = view.data.contains("@"); char nextMove; @@ -35,18 +37,47 @@ public class SnakeBot extends Bot{ nextMove = (countCollectedStonesLeft(view) <= countCollectedStonesRight(view)) ? '<' : '>'; ignoreStones = true; } - /* - * @TODO: - * Avoiding endless loops due to trying to collect two stones at once. - * Problem: stones located near to each other are ignored. - * Tested: 20 tries; avgScore = 25.45/32; minScore = 19; maxScore = 29; trapped 1/20. - */ + if(countUncollectedStones(view) > 1) ignoreStones = true; if(countCollectedStones(view) <= 2 && countUncollectedStones(view) <= 1) ignoreStones = false; + if(isStoneNearby(view)){ + nextMove = goToNearbyStone(view); + } + return nextMove; } + private boolean isStoneNearby(View view){ + int centerCoordinateOfView = view.width / 2; + int frontCellIndex = calculateCharIndexFromCoordinates(view.width, centerCoordinateOfView, centerCoordinateOfView - 1); + int leftCellIndex = calculateCharIndexFromCoordinates(view.width, centerCoordinateOfView - 1, centerCoordinateOfView); + int rightCellIndex = calculateCharIndexFromCoordinates(view.width, centerCoordinateOfView + 1, centerCoordinateOfView); + + boolean stoneIsInFront = view.data.charAt(frontCellIndex) == '@'; + boolean stoneIsOnTheLeft = view.data.charAt(leftCellIndex) == '@'; + boolean stoneIsOneTheRight = view.data.charAt(rightCellIndex) == '@'; + + return stoneIsInFront || stoneIsOnTheLeft || stoneIsOneTheRight; + } + + private char goToNearbyStone(View view){ + int centerCoordinateOfView = view.width / 2; + int frontCellIndex = calculateCharIndexFromCoordinates(view.width, centerCoordinateOfView, centerCoordinateOfView - 1); + int leftCellIndex = calculateCharIndexFromCoordinates(view.width, centerCoordinateOfView - 1, centerCoordinateOfView); + int rightCellIndex = calculateCharIndexFromCoordinates(view.width, centerCoordinateOfView + 1, centerCoordinateOfView); + + if(view.data.charAt(frontCellIndex) == '@') return '^'; + if(view.data.charAt(leftCellIndex) == '@') return '<'; + if(view.data.charAt(rightCellIndex) == '@') return '>'; + return 0; + } + + private void resetMoves() { + moves = ""; + spiralNumber = 0; + } + private int countUncollectedStones(View view) { int count = 0; @@ -131,4 +162,15 @@ public class SnakeBot extends Bot{ return rightStones; } + + 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; + } }