Bots fürs Turnier
This commit is contained in:
parent
73fe05a5ec
commit
7ec35d7888
33
src/praktikum05/RumbleBot.java
Normal file
33
src/praktikum05/RumbleBot.java
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package praktikum05;
|
||||||
|
|
||||||
|
public class RumbleBot extends SnakeBot{
|
||||||
|
private boolean hasFired = false;
|
||||||
|
|
||||||
|
public RumbleBot(String[] args) {
|
||||||
|
super(args);
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
protected char nextMove(View view) throws Exception {
|
||||||
|
char move = super.nextMove(view);
|
||||||
|
|
||||||
|
if (!hasFired && canFire(view)) {
|
||||||
|
hasFired = true;
|
||||||
|
return 'f'; // fire
|
||||||
|
}
|
||||||
|
|
||||||
|
return move;
|
||||||
|
}
|
||||||
|
private boolean canFire(View view) {
|
||||||
|
char[][] grid = parseView(view);
|
||||||
|
|
||||||
|
int roverX = view.width / 2;
|
||||||
|
int roverY = view.width / 2;
|
||||||
|
|
||||||
|
return roverY > 0 && grid[roverY - 1][roverX] == '@';
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Bot bot = new RumbleBot(args);
|
||||||
|
bot.run();
|
||||||
|
}
|
||||||
|
}
|
75
src/praktikum05/SnakeBot.java
Normal file
75
src/praktikum05/SnakeBot.java
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user