Browse Source

Bots fürs Turnier

master
ecenurkeles 10 months ago
parent
commit
7ec35d7888
2 changed files with 108 additions and 0 deletions
  1. 33
    0
      src/praktikum05/RumbleBot.java
  2. 75
    0
      src/praktikum05/SnakeBot.java

+ 33
- 0
src/praktikum05/RumbleBot.java View File

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
- 0
src/praktikum05/SnakeBot.java View File

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…
Cancel
Save