BotsAI/src/SnakeBot.java
2024-01-07 15:16:41 +01:00

109 lines
3.0 KiB
Java

public class SnakeBot extends Bot{
String moves = "";
private boolean goesForward = true;
private int circleNumber = 0;
private int spiralNumber = 0;
private boolean ignoreStone = false;
public static void main(String[] args) {
Bot snakeBot = new SnakeBot(args);
snakeBot.run();
}
protected SnakeBot(String[] args) {
super(args);
}
//@TODO
protected char nextMove(View view){
boolean stoneDetected = view.data.contains("@");
char nextMove;
nextMove = (stoneDetected && !ignoreStone) ? goToStone(view) : walkBySpiral();
if(nextMove == '^' && view.data.charAt(7) == '*'){
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;
for (char c : view.data.toCharArray()) {
if (c == '*') {
count++;
}
}
return count;
}
private char goToStone(View view){
int rowDifference = findStoneRow(view) - 2;
return rowDifference < 0 ? '^' : '<';
}
private int findStoneRow(View view) {
return view.data.indexOf('@') / 5;
}
private char walkByColumns() {
if(moves.isEmpty()){
moves = "^".repeat(28);
moves += (goesForward ? ">^^^^^>" : "<^^^^^<");
goesForward = !goesForward;
}
char nextMove = moves.charAt(0);
moves = moves.substring(1);
return nextMove;
}
private char walkByCircles(){
if (moves.isEmpty()) {
circleNumber++;
moves += "^".repeat(5) + ">";
int[] steps = {5, 10, 10, 10};
for (int step : steps) {
moves += "^".repeat(step * circleNumber) + ">";
}
moves += "^".repeat(5 * circleNumber) + "<";
}
char nextMove = moves.charAt(0);
moves = moves.substring(1);
return nextMove;
}
private char walkBySpiral(){
if (moves.isEmpty()) {
spiralNumber++;
moves += "^".repeat(5 * spiralNumber) + ">" + "^".repeat(5 * spiralNumber) + ">";
}
char nextMove = moves.charAt(0);
moves = moves.substring(1);
return nextMove;
}
}