|
|
|
|
|
|
|
|
|
|
|
package praktikum05; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.Random; |
|
|
|
|
|
|
|
|
|
|
|
public class SnakeBot extends Bot{ |
|
|
|
|
|
|
|
|
|
|
|
public SnakeBot(String[] args) { |
|
|
|
|
|
super(args); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
protected char nextMove(View view) throws Exception { |
|
|
|
|
|
|
|
|
|
|
|
char[][] grid = parseView(view); |
|
|
|
|
|
|
|
|
|
|
|
if (containsSymbol(grid, '@')) { |
|
|
|
|
|
return moveTowardsSymbol(grid, '@'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (containsSymbol(grid, '*')) { |
|
|
|
|
|
return moveTowardsSymbol(grid, '*'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return performRandomMove(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
char[][] parseView(View view) { |
|
|
|
|
|
char[][] grid = new char[view.width][view.width]; |
|
|
|
|
|
|
|
|
|
|
|
int index = 0; |
|
|
|
|
|
for (int i = 0; i < view.width; i++) { |
|
|
|
|
|
for (int j = 0; j < view.width; j++) { |
|
|
|
|
|
grid[i][j] = view.data.charAt(index++); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return grid; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean containsSymbol(char[][] grid, char symbol) { |
|
|
|
|
|
for (char[] row : grid) { |
|
|
|
|
|
for (char cell : row) { |
|
|
|
|
|
if (cell == symbol) { |
|
|
|
|
|
return true; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return false; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private char moveTowardsSymbol(char[][] grid, char symbol) throws Exception { |
|
|
|
|
|
|
|
|
|
|
|
return performRandomMove(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private char performRandomMove() throws Exception { |
|
|
|
|
|
Random random = new Random(); |
|
|
|
|
|
int move = random.nextInt(4); |
|
|
|
|
|
|
|
|
|
|
|
return switch (move) { |
|
|
|
|
|
case 0 -> '^'; |
|
|
|
|
|
case 1 -> 'v'; |
|
|
|
|
|
case 2 -> '<'; |
|
|
|
|
|
case 3 -> '>'; |
|
|
|
|
|
default -> throw new Exception("Ungültiger Zug"); |
|
|
|
|
|
}; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
|
|
Bot bot = new SnakeBot(args); |
|
|
|
|
|
bot.run(); |
|
|
|
|
|
} |
|
|
|
|
|
} |