BotsAI/src/SnakeBot.java

84 lines
2.4 KiB
Java
Raw Normal View History

import java.util.Random;
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) == '*'){
Random random = new Random();
nextMove = (random.nextInt(2) % 2 == 0) ? '<' : '>';
ignoreStone = true;
}
if(countCollectedStones(view) <= 2) ignoreStone = false;
return nextMove;
}
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;
}
}