Browse Source

outsource record Move

master
Your Average Code 11 months ago
parent
commit
3f96b89a47
4 changed files with 26 additions and 24 deletions
  1. 7
    8
      src/EscapeBot.java
  2. 7
    8
      src/RumbleBot.java
  3. 8
    8
      src/SnakeBot.java
  4. 4
    0
      src/records/Move.java

+ 7
- 8
src/EscapeBot.java View File

@@ -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) {
}

}

+ 7
- 8
src/RumbleBot.java View File

@@ -1,3 +1,5 @@
import records.Move;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
@@ -133,13 +135,13 @@ 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()));
}
}

@@ -147,7 +149,4 @@ public class RumbleBot extends Bot {
return walkAround(grid);
}

protected record Move(int x, int y, char direction) {
}

}

+ 8
- 8
src/SnakeBot.java View File

@@ -1,3 +1,6 @@
import records.Move;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;

@@ -107,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] == target) 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()));
}
}

@@ -121,7 +124,4 @@ public class SnakeBot extends Bot {
return safeMove(grid);
}

protected record Move(int x, int y, char direction) {
}

}

+ 4
- 0
src/records/Move.java View File

@@ -0,0 +1,4 @@
package records;

public record Move(int x, int y, char direction) {
}

Loading…
Cancel
Save