Compare commits
12
Commits
f3d09aabb6
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3f96b89a47 | ||
|
|
a3de94a4f8 | ||
|
|
59514963eb | ||
|
|
13955b7b29 | ||
|
|
6391e9d149 | ||
|
|
4e4b2c6d08 | ||
|
|
e408032f5d | ||
|
|
a242381a9d | ||
|
|
9c555c9cde | ||
|
|
53482674e6 | ||
|
|
8c40d7bacb | ||
|
|
49fffe2a27 |
No files matched your search
@@ -46,6 +46,7 @@ public abstract class Bot implements Runnable {
|
||||
|
||||
protected void print(View view) {
|
||||
view.print();
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
// Diese Methode ermittelt den nächsten Zug des Bots. Sie wird von der
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
import java.util.Random;
|
||||
|
||||
public class DummyBot extends Bot {
|
||||
protected final Random random = new Random();
|
||||
|
||||
protected DummyBot(String[] args) {
|
||||
super(args);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected char nextMove(View view) {
|
||||
String data = view.data
|
||||
.replace('^', '*')
|
||||
.replace('<', '*')
|
||||
.replace('>', '*')
|
||||
.replace('v', '*');
|
||||
|
||||
if (data.contains("*") && random.nextFloat() < 0.8)
|
||||
return 'f';
|
||||
|
||||
return switch (random.nextInt(10)) {
|
||||
case 1 -> '<';
|
||||
case 2 -> '>';
|
||||
case 3 -> 'v';
|
||||
default -> '^';
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void print(View view) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import java.util.Random;
|
||||
|
||||
public class EnemyBot extends Bot {
|
||||
protected final Random random = new Random();
|
||||
|
||||
protected EnemyBot(String[] args) {
|
||||
super(args);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected char nextMove(View view) {
|
||||
int size = view.width;
|
||||
String data = view.data
|
||||
.replace('^', '*')
|
||||
.replace('<', '*')
|
||||
.replace('>', '*')
|
||||
.replace('v', '*');
|
||||
|
||||
char[][] grid = dataToGrid(data, size);
|
||||
|
||||
if (data.contains("*") && isInLineOfSight(grid))
|
||||
return 'f';
|
||||
|
||||
return switch (random.nextInt(10)) {
|
||||
case 1 -> '<';
|
||||
case 2 -> '>';
|
||||
case 3 -> 'v';
|
||||
default -> '^';
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void print(View view) {
|
||||
}
|
||||
|
||||
protected char[][] dataToGrid(String data, int size) {
|
||||
char[][] grid = new char[size][size];
|
||||
for (int i = 0; i < data.length(); i++) {
|
||||
grid[i % size][i / size] = data.charAt(i);
|
||||
}
|
||||
return grid;
|
||||
}
|
||||
|
||||
protected boolean isInLineOfSight(char[][] grid) {
|
||||
int size = grid.length;
|
||||
for (int y = size / 2; y > 0; y--) {
|
||||
if (grid[size / 2][y] == '*')
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+7
-8
@@ -1,3 +1,5 @@
|
||||
import records.Move;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
@@ -80,13 +82,13 @@ public class EscapeBot extends Bot {
|
||||
|
||||
while (!queue.isEmpty()) {
|
||||
Move move = queue.poll();
|
||||
if (move.x < 0 || move.x >= size || move.y < 0 || move.y >= size || visited[move.x][move.y]) continue;
|
||||
visited[move.x][move.y] = true;
|
||||
if (obstacles.contains("" + grid[move.x][move.y])) continue;
|
||||
if (grid[move.x][move.y] == 'o') return move.direction;
|
||||
if (move.x() < 0 || move.x() >= size || move.y() < 0 || move.y() >= size || visited[move.x()][move.y()]) continue;
|
||||
visited[move.x()][move.y()] = true;
|
||||
if (obstacles.contains("" + grid[move.x()][move.y()])) continue;
|
||||
if (grid[move.x()][move.y()] == 'o') return move.direction();
|
||||
|
||||
for (int[] direction : directions) {
|
||||
queue.add(new Move(move.x + direction[0], move.y + direction[1], move.direction));
|
||||
queue.add(new Move(move.x() + direction[0], move.y() + direction[1], move.direction()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,7 +96,4 @@ public class EscapeBot extends Bot {
|
||||
return safeMove(grid);
|
||||
}
|
||||
|
||||
protected record Move(int x, int y, char direction) {
|
||||
}
|
||||
|
||||
}
|
||||
+76
-48
@@ -1,17 +1,23 @@
|
||||
import records.Move;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
import java.util.Random;
|
||||
|
||||
public class RumbleBot extends Bot {
|
||||
protected final static String obstacles = "~#X*";
|
||||
protected final static String targets = "v^<>";
|
||||
protected boolean offByOne = true;
|
||||
protected int currentStepCount = 0;
|
||||
protected final static String players = "v^<>";
|
||||
protected final static String targets = players + "o";
|
||||
|
||||
protected Random random = new Random();
|
||||
protected int steps = 0;
|
||||
|
||||
public static void main(String[] args) {
|
||||
if(args.length == 0) {
|
||||
DummyBot dummy = new DummyBot(args);
|
||||
new Thread(dummy).start();
|
||||
if (args.length == 0) {
|
||||
String[] dummyArgs = {"localhost", "63187"};
|
||||
for (int i = 0; i < 5; i++) {
|
||||
new Thread(new EnemyBot(dummyArgs)).start();
|
||||
}
|
||||
}
|
||||
|
||||
Bot bot = new RumbleBot(args);
|
||||
@@ -24,18 +30,14 @@ public class RumbleBot extends Bot {
|
||||
|
||||
@Override
|
||||
protected char nextMove(View view) {
|
||||
System.out.println();
|
||||
|
||||
int size = view.width;
|
||||
String data = view.data;
|
||||
char[][] grid = dataToGrid(data, size);
|
||||
|
||||
if (data.contains("v") || data.contains("^") || data.contains("<") || data.contains(">")) {
|
||||
if (isInLineOfSight(grid)) {
|
||||
return 'f';
|
||||
} else {
|
||||
return breadthFirstSearch(grid);
|
||||
}
|
||||
if (isInLineOfSight(grid)) {
|
||||
return 'f';
|
||||
} else if (data.contains("v") || data.contains("^") || data.contains("<") || data.contains(">")) {
|
||||
return breadthFirstSearch(grid);
|
||||
} else {
|
||||
return walkAround(grid);
|
||||
}
|
||||
@@ -44,55 +46,84 @@ public class RumbleBot extends Bot {
|
||||
protected char[][] dataToGrid(String data, int size) {
|
||||
char[][] grid = new char[size][size];
|
||||
for (int i = 0; i < data.length(); i++) {
|
||||
grid[i % size][i / size] = data.charAt(i);
|
||||
char c = data.charAt(i);
|
||||
if (Character.isUpperCase(c) && c != 'X') c = 'A';
|
||||
grid[i % size][i / size] = c;
|
||||
}
|
||||
return markFiringLines(grid);
|
||||
return grid;
|
||||
}
|
||||
|
||||
protected char[][] markFiringLines(char[][] grid) {
|
||||
return grid;
|
||||
protected void markFiringLines(char[][] grid) {
|
||||
int[][] dir = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
|
||||
int size = grid.length;
|
||||
|
||||
for (int x = 0; x < size; x++) {
|
||||
for (int y = 0; y < size; y++) {
|
||||
|
||||
switch (grid[x][y]) {
|
||||
case '^' -> markLine(grid, x, y, dir[0], '*', false);
|
||||
case 'v' -> markLine(grid, x, y, dir[1], '*', false);
|
||||
case '>' -> markLine(grid, x, y, dir[2], '*', false);
|
||||
case '<' -> markLine(grid, x, y, dir[3], '*', false);
|
||||
}
|
||||
|
||||
if (players.contains("" + grid[x][y])) {
|
||||
for (int[] direction : dir) {
|
||||
markLine(grid, x, y, direction, 'o', true);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void markLine(char[][] grid, int x, int y, int[] dir, char c, boolean block) {
|
||||
int size = grid.length;
|
||||
x += dir[0];
|
||||
y += dir[1];
|
||||
while (x >= 0 && x < size && y >= 0 && y < size && !obstacles.contains("" + grid[x][y])) {
|
||||
if (grid[x][y] == 'A' && block) break;
|
||||
if (grid[x][y] == '.') {
|
||||
grid[x][y] = c;
|
||||
}
|
||||
x += dir[0];
|
||||
y += dir[1];
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean isInLineOfSight(char[][] grid) {
|
||||
int size = grid.length;
|
||||
for (int y = size / 2; y > 0; y--) {
|
||||
for (int y = size / 2; y >= 0; y--) {
|
||||
if (players.contains("" + grid[size / 2][y]))
|
||||
return true;
|
||||
|
||||
if (obstacles.contains("" + grid[size / 2][y]))
|
||||
break;
|
||||
|
||||
if (targets.contains("" + grid[size / 2][y]))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
protected char walkAround(char[][] grid) {
|
||||
int size = grid.length;
|
||||
if (steps == 0) {
|
||||
currentStepCount = (currentStepCount + 1) % 10;
|
||||
if (offByOne) {
|
||||
currentStepCount++;
|
||||
}
|
||||
offByOne = !offByOne;
|
||||
steps = currentStepCount;
|
||||
return '>';
|
||||
steps = random.nextInt(20);
|
||||
return random.nextBoolean() ? '<' : '>';
|
||||
} else {
|
||||
steps--;
|
||||
return safeMove(grid);
|
||||
}
|
||||
}
|
||||
|
||||
protected char safeMove(char[][] grid) {
|
||||
int size = grid.length;
|
||||
if (obstacles.contains("" + grid[size / 2][size / 2 - 1])) {
|
||||
return '<';
|
||||
} else {
|
||||
return '^';
|
||||
return obstacles.contains("" + grid[size / 2][size / 2 - 1]) ? '<' : '^';
|
||||
}
|
||||
}
|
||||
|
||||
protected char breadthFirstSearch(char[][] grid) {
|
||||
markFiringLines(grid);
|
||||
int size = grid.length;
|
||||
int start = size / 2;
|
||||
|
||||
for (int y = 0; y < size; y++) {
|
||||
for (char[] chars : grid) System.err.print(chars[y]);
|
||||
System.err.println();
|
||||
}
|
||||
|
||||
boolean[][] visited = new boolean[size][size];
|
||||
Queue<Move> queue = new LinkedList<>();
|
||||
|
||||
@@ -104,21 +135,18 @@ public class RumbleBot extends Bot {
|
||||
|
||||
while (!queue.isEmpty()) {
|
||||
Move move = queue.poll();
|
||||
if (move.x < 0 || move.x >= size || move.y < 0 || move.y >= size || visited[move.x][move.y]) continue;
|
||||
visited[move.x][move.y] = true;
|
||||
if (obstacles.contains("" + grid[move.x][move.y])) continue;
|
||||
if (targets.contains("" + grid[move.x][move.y])) return move.direction;
|
||||
if (move.x() < 0 || move.x() >= size || move.y() < 0 || move.y() >= size || visited[move.x()][move.y()]) continue;
|
||||
visited[move.x()][move.y()] = true;
|
||||
if (obstacles.contains("" + grid[move.x()][move.y()])) continue;
|
||||
if (targets.contains("" + grid[move.x()][move.y()])) return move.direction();
|
||||
|
||||
for (int[] direction : directions) {
|
||||
queue.add(new Move(move.x + direction[0], move.y + direction[1], move.direction));
|
||||
queue.add(new Move(move.x() + direction[0], move.y() + direction[1], move.direction()));
|
||||
}
|
||||
}
|
||||
|
||||
System.err.println("No path found");
|
||||
return safeMove(grid);
|
||||
}
|
||||
|
||||
protected record Move(int x, int y, char direction) {
|
||||
return walkAround(grid);
|
||||
}
|
||||
|
||||
}
|
||||
+20
-14
@@ -1,15 +1,26 @@
|
||||
import records.Move;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
public class SnakeBot extends Bot {
|
||||
protected final static String obstacles = "~#X*";
|
||||
protected final static char target = '@';
|
||||
protected boolean offByOne = true;
|
||||
protected int currentStepCount = 0;
|
||||
protected int steps = 0;
|
||||
|
||||
public static void main(String[] args) {
|
||||
Bot bot = new SnakeBot(args);
|
||||
bot.run();
|
||||
if (args.length == 0) {
|
||||
String[] dummyArgs = {"localhost", "63187"};
|
||||
for (int i = 0; i < 5; i++) {
|
||||
new Thread(new SnakeBot(dummyArgs)).start();
|
||||
}
|
||||
}
|
||||
|
||||
Bot bot = new SnakeBot(args);
|
||||
bot.run();
|
||||
}
|
||||
|
||||
protected SnakeBot(String[] args) {
|
||||
@@ -72,10 +83,8 @@ public class SnakeBot extends Bot {
|
||||
int rightStars = 0;
|
||||
for (int x = 0; x < size / 2; x++) {
|
||||
for (int y = 0; y < size; y++) {
|
||||
if (grid[x][y] == '*')
|
||||
leftStars++;
|
||||
if (grid[size - x - 1][y] == '*')
|
||||
rightStars++;
|
||||
if (grid[x][y] == '*') leftStars++;
|
||||
if (grid[size - x - 1][y] == '*') rightStars++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,13 +110,13 @@ public class SnakeBot extends Bot {
|
||||
|
||||
while (!queue.isEmpty()) {
|
||||
Move move = queue.poll();
|
||||
if (move.x < 0 || move.x >= size || move.y < 0 || move.y >= size || visited[move.x][move.y]) continue;
|
||||
visited[move.x][move.y] = true;
|
||||
if (obstacles.contains("" + grid[move.x][move.y])) continue;
|
||||
if (grid[move.x][move.y] == '@') return move.direction;
|
||||
if (move.x() < 0 || move.x() >= size || move.y() < 0 || move.y() >= size || visited[move.x()][move.y()]) continue;
|
||||
visited[move.x()][move.y()] = true;
|
||||
if (obstacles.contains("" + grid[move.x()][move.y()])) continue;
|
||||
if (grid[move.x()][move.y()] == target) return move.direction();
|
||||
|
||||
for (int[] direction : directions) {
|
||||
queue.add(new Move(move.x + direction[0], move.y + direction[1], move.direction));
|
||||
queue.add(new Move(move.x() + direction[0], move.y() + direction[1], move.direction()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,7 +124,4 @@ public class SnakeBot extends Bot {
|
||||
return safeMove(grid);
|
||||
}
|
||||
|
||||
protected record Move(int x, int y, char direction) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package records;
|
||||
|
||||
public record Move(int x, int y, char direction) {
|
||||
}
|
||||
Reference in new issue
Block a user