Added bool trapped

This commit is contained in:
Illia Soloviov 2024-01-11 16:03:23 +01:00
parent d4b29340bc
commit d041826164

View File

@ -1,8 +1,10 @@
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; boolean frontIsBlocked, backIsBlocked, leftIsBlocked, rightIsBlocked, trapped;
private int spiralNumber = 0; 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) {
@ -18,36 +20,50 @@ public class SuperSnakeBot extends Bot{
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 = '>';
if(leftIsBlocked) move = '>'; else if (rightIsBlocked) move = '<';
else if(rightIsBlocked) move = '<'; else if (previousTurn == '<') move = '>';
else if(previousTurn == '<') move = '>'; else if (previousTurn == '>') move = '<';
else if(previousTurn == '>') move = '<'; else {
else throw new Exception(); Exception invalidTurnException = new Exception("The previous turn was neither '<' nor '>'.");
System.err.println(invalidTurnException.getMessage());
throw invalidTurnException;
}
} }
return move; return move;
} }
@ -60,8 +76,8 @@ public class SuperSnakeBot extends Bot{
private char walkBySpiral(View view) { private char walkBySpiral(View view) {
if (moves.isEmpty()) { if (moves.isEmpty()) {
spiralNumber++; spiralFactor++;
moves += "^".repeat(view.width * spiralNumber) + spiralDirection + "^".repeat(view.width * spiralNumber) + spiralDirection; 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);
@ -70,7 +86,12 @@ public class SuperSnakeBot extends Bot{
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) + "<");
@ -89,15 +110,15 @@ public class SuperSnakeBot extends Bot{
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 '^';
@ -145,7 +166,8 @@ public class SuperSnakeBot extends Bot{
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;
} }