|
|
|
|
|
|
|
|
import java.util.Random; |
|
|
|
|
|
|
|
|
|
|
|
public class SnakeBot extends Bot{ |
|
|
public class SnakeBot extends Bot{ |
|
|
String moves = ""; |
|
|
String moves = ""; |
|
|
private boolean goesForward = true; |
|
|
private boolean goesForward = true; |
|
|
|
|
|
|
|
|
Bot snakeBot = new SnakeBot(args); |
|
|
Bot snakeBot = new SnakeBot(args); |
|
|
snakeBot.run(); |
|
|
snakeBot.run(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
protected SnakeBot(String[] args) { |
|
|
protected SnakeBot(String[] args) { |
|
|
super(args); |
|
|
super(args); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
protected char nextMove(View view){ |
|
|
protected char nextMove(View view){ |
|
|
boolean stoneDetected = view.data.contains("@"); |
|
|
boolean stoneDetected = view.data.contains("@"); |
|
|
char nextMove; |
|
|
char nextMove; |
|
|
nextMove = stoneDetected && !ignoreStone ? goToStone(view) : walkBySpiral(); |
|
|
|
|
|
|
|
|
nextMove = (stoneDetected && !ignoreStone) ? goToStone(view) : walkBySpiral(); |
|
|
|
|
|
|
|
|
if(nextMove == '^' && view.data.charAt(7) == '*'){ |
|
|
if(nextMove == '^' && view.data.charAt(7) == '*'){ |
|
|
nextMove = (countCollectedStonesLeft(view) <= countCollectedStonesRight(view)) ? '<' : '>'; |
|
|
nextMove = (countCollectedStonesLeft(view) <= countCollectedStonesRight(view)) ? '<' : '>'; |
|
|
|
|
|
|
|
|
if(countCollectedStones(view) <= 2) ignoreStone = false; |
|
|
if(countCollectedStones(view) <= 2) ignoreStone = false; |
|
|
return nextMove; |
|
|
return nextMove; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private int countCollectedStonesLeft(View view) { |
|
|
private int countCollectedStonesLeft(View view) { |
|
|
int[] leftStones = {0, 1, 5, 6, 10, 11, 15, 16, 20, 21}; |
|
|
int[] leftStones = {0, 1, 5, 6, 10, 11, 15, 16, 20, 21}; |
|
|
int stones = 0; |
|
|
int stones = 0; |
|
|
for (int stone: leftStones) { |
|
|
|
|
|
|
|
|
for (int stone : leftStones) { |
|
|
if(view.data.charAt(stone) == '*'){ |
|
|
if(view.data.charAt(stone) == '*'){ |
|
|
stones++; |
|
|
stones++; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
return stones; |
|
|
return stones; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private int countCollectedStonesRight(View view) { |
|
|
private int countCollectedStonesRight(View view) { |
|
|
int[] rightStones = {3, 4, 8, 9, 13, 14, 18, 19, 23, 24}; |
|
|
int[] rightStones = {3, 4, 8, 9, 13, 14, 18, 19, 23, 24}; |
|
|
int stones = 0; |
|
|
int stones = 0; |
|
|
for (int stone: rightStones) { |
|
|
|
|
|
|
|
|
for (int stone : rightStones) { |
|
|
if(view.data.charAt(stone) == '*'){ |
|
|
if(view.data.charAt(stone) == '*'){ |
|
|
stones++; |
|
|
stones++; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
return stones; |
|
|
return stones; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private int countCollectedStones(View view) { |
|
|
private int countCollectedStones(View view) { |
|
|
int count = 0; |
|
|
int count = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
return count; |
|
|
return count; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private char goToStone(View view){ |
|
|
private char goToStone(View view){ |
|
|
int rowDifference = findStoneRow(view) - 2; |
|
|
int rowDifference = findStoneRow(view) - 2; |
|
|
return rowDifference < 0 ? '^' : '<'; |
|
|
return rowDifference < 0 ? '^' : '<'; |
|
|
|
|
|
|
|
|
private int findStoneRow(View view) { |
|
|
private int findStoneRow(View view) { |
|
|
return view.data.indexOf('@') / 5; |
|
|
return view.data.indexOf('@') / 5; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private char walkByColumns() { |
|
|
private char walkByColumns() { |
|
|
if(moves.isEmpty()){ |
|
|
if(moves.isEmpty()){ |
|
|
moves = "^".repeat(28); |
|
|
moves = "^".repeat(28); |
|
|
|
|
|
|
|
|
moves = moves.substring(1); |
|
|
moves = moves.substring(1); |
|
|
return nextMove; |
|
|
return nextMove; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private char walkByCircles(){ |
|
|
private char walkByCircles(){ |
|
|
if (moves.isEmpty()) { |
|
|
if (moves.isEmpty()) { |
|
|
circleNumber++; |
|
|
circleNumber++; |
|
|
|
|
|
|
|
|
moves = moves.substring(1); |
|
|
moves = moves.substring(1); |
|
|
return nextMove; |
|
|
return nextMove; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private char walkBySpiral(){ |
|
|
private char walkBySpiral(){ |
|
|
if (moves.isEmpty()) { |
|
|
if (moves.isEmpty()) { |
|
|
spiralNumber++; |
|
|
spiralNumber++; |