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

import records.Move;

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




while (!queue.isEmpty()) { while (!queue.isEmpty()) {
Move move = queue.poll(); 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) { 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()));
} }
} }


return safeMove(grid); return safeMove(grid);
} }


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

} }

+ 7
- 8
src/RumbleBot.java View File

import records.Move;

import java.util.LinkedList; import java.util.LinkedList;
import java.util.Queue; import java.util.Queue;
import java.util.Random; import java.util.Random;


while (!queue.isEmpty()) { while (!queue.isEmpty()) {
Move move = queue.poll(); 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) { 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()));
} }
} }


return walkAround(grid); return walkAround(grid);
} }


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

} }

+ 8
- 8
src/SnakeBot.java View File

import records.Move;

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




while (!queue.isEmpty()) { while (!queue.isEmpty()) {
Move move = queue.poll(); 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) { 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()));
} }
} }


return safeMove(grid); return safeMove(grid);
} }


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

} }

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

package records;

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

Loading…
Cancel
Save