From 3ace2a44d9c67009aac207bae7cdc2ec034d9d84 Mon Sep 17 00:00:00 2001 From: Illia Soloviov <74905269+wav3solo@users.noreply.github.com> Date: Sat, 6 Jan 2024 23:47:07 +0100 Subject: [PATCH] Added stones count in SnakeBot.java --- src/SnakeBot.java | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/SnakeBot.java b/src/SnakeBot.java index 5da9612..9e2485a 100644 --- a/src/SnakeBot.java +++ b/src/SnakeBot.java @@ -20,13 +20,32 @@ public class SnakeBot extends Bot{ nextMove = stoneDetected && !ignoreStone ? goToStone(view) : walkBySpiral(); if(nextMove == '^' && view.data.charAt(7) == '*'){ - Random random = new Random(); - nextMove = (random.nextInt(2) % 2 == 0) ? '<' : '>'; + nextMove = (countCollectedStonesLeft(view) <= countCollectedStonesRight(view)) ? '<' : '>'; ignoreStone = true; } if(countCollectedStones(view) <= 2) ignoreStone = false; return nextMove; } + private int countCollectedStonesLeft(View view) { + int[] leftStones = {0, 1, 5, 6, 10, 11, 15, 16, 20, 21}; + int stones = 0; + for (int stone: leftStones) { + if(view.data.charAt(stone) == '*'){ + stones++; + } + } + return stones; + } + private int countCollectedStonesRight(View view) { + int[] rightStones = {3, 4, 8, 9, 13, 14, 18, 19, 23, 24}; + int stones = 0; + for (int stone: rightStones) { + if(view.data.charAt(stone) == '*'){ + stones++; + } + } + return stones; + } private int countCollectedStones(View view) { int count = 0;