Added SuperSnakeBot.java with arg score 30/32
This commit is contained in:
parent
7413a48b58
commit
bd4de54fa7
115
src/SuperSnakeBot.java
Normal file
115
src/SuperSnakeBot.java
Normal file
@ -0,0 +1,115 @@
|
||||
public class SuperSnakeBot extends Bot{
|
||||
private String moves = "";
|
||||
private char previousTurn = '<';
|
||||
boolean frontIsBlocked;
|
||||
boolean leftIsBlocked;
|
||||
boolean rightIsBlocked;
|
||||
private int spiralNumber = 0;
|
||||
private boolean goesForward = true;
|
||||
public static void main(String[] args) {
|
||||
Bot superSnakeBot = new SuperSnakeBot(args);
|
||||
superSnakeBot.run();
|
||||
}
|
||||
|
||||
protected SuperSnakeBot(String[] args) {
|
||||
super(args);
|
||||
}
|
||||
|
||||
protected char nextMove(View view) throws Exception {
|
||||
boolean stoneDetected = view.data.contains("@");
|
||||
char nextMove;
|
||||
|
||||
nextMove = stoneDetected ? goToStone(view) : walkByColumns(view);
|
||||
checkBarriers(view);
|
||||
nextMove = checkMove(nextMove);
|
||||
|
||||
saveMove(nextMove);
|
||||
return nextMove;
|
||||
}
|
||||
|
||||
private void checkBarriers(View view){
|
||||
int centerCoordinates = view.width / 2;
|
||||
int frontIndex = getCharIndexFromCoordinates(view.width, centerCoordinates, centerCoordinates - 1);
|
||||
int leftIndex = getCharIndexFromCoordinates(view.width, centerCoordinates - 1 , centerCoordinates);
|
||||
int rightIndex = getCharIndexFromCoordinates(view.width, centerCoordinates + 1, centerCoordinates);
|
||||
|
||||
frontIsBlocked = view.data.charAt(frontIndex) == '*';
|
||||
leftIsBlocked = view.data.charAt(leftIndex) == '*';
|
||||
rightIsBlocked = view.data.charAt(rightIndex) == '*';
|
||||
}
|
||||
|
||||
private char checkMove(char move) throws Exception {
|
||||
if(move == '^' && frontIsBlocked){
|
||||
resetMoves();
|
||||
|
||||
if(leftIsBlocked) move = '>';
|
||||
else if(rightIsBlocked) move = '<';
|
||||
else if(previousTurn == '<') move = '>';
|
||||
else if(previousTurn == '>') move = '<';
|
||||
else throw new Exception();
|
||||
}
|
||||
return move;
|
||||
}
|
||||
|
||||
private void saveMove(char move){
|
||||
if(move == '<' || move == '>'){
|
||||
previousTurn = move;
|
||||
}
|
||||
}
|
||||
|
||||
private char walkBySpiral(View view) {
|
||||
if (moves.isEmpty()) {
|
||||
spiralNumber++;
|
||||
moves += "^".repeat(view.width * spiralNumber) + ">" + "^".repeat(view.width * spiralNumber) + ">";
|
||||
}
|
||||
char nextMove = moves.charAt(0);
|
||||
moves = moves.substring(1);
|
||||
return nextMove;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
private char goToStone(View view) {
|
||||
resetMoves();
|
||||
|
||||
int centerCoordinates = view.width / 2;
|
||||
|
||||
int leftIndex = getCharIndexFromCoordinates(view.width, centerCoordinates - 1, centerCoordinates);
|
||||
int farLeftIndex = getCharIndexFromCoordinates(view.width, centerCoordinates - 2, centerCoordinates);
|
||||
|
||||
int rightIndex = getCharIndexFromCoordinates(view.width, centerCoordinates + 1, centerCoordinates);
|
||||
int farRightIndex = getCharIndexFromCoordinates(view.width, centerCoordinates + 2, centerCoordinates);
|
||||
|
||||
int frontIndex = getCharIndexFromCoordinates(view.width, centerCoordinates, centerCoordinates - 1);
|
||||
int farFrontIndex = getCharIndexFromCoordinates(view.width, centerCoordinates, centerCoordinates - 2);
|
||||
|
||||
if(view.data.charAt(frontIndex) == '@' || view.data.charAt(farFrontIndex) == '@'){
|
||||
return '^';
|
||||
}else if(view.data.charAt(leftIndex) == '@' || view.data.charAt(farLeftIndex) == '@'){
|
||||
return '<';
|
||||
}else if(view.data.charAt(rightIndex) == '@' || view.data.charAt(farRightIndex) == '@'){
|
||||
return '>';
|
||||
}else {
|
||||
return '^';
|
||||
}
|
||||
}
|
||||
|
||||
private int getCharIndexFromCoordinates(int width, int x, int y){
|
||||
return width * y + x;
|
||||
}
|
||||
|
||||
private void resetMoves() {
|
||||
moves = "";
|
||||
spiralNumber = 0;
|
||||
goesForward = true;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user