Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package rph.labyrinth.players;
- import java.awt.Color;
- import rph.labyrinth.Coordinate;
- import rph.labyrinth.Labyrinth;
- import rph.labyrinth.Path;
- import rph.labyrinth.Player;
- public class MyPlayer extends Player {
- // konstanty pro doporuceny smer
- private final int GOAL = 0; // jsme na miste
- private final int RIGHT = 1;
- private final int LEFT = 2;
- private final int UP = 3;
- private final int DOWN = 4;
- private final int FAIL = 5; // neni kam dal, tudy to nepujde
- // staticky counter, pro check jestli nezkousim hledat smer pro jeden bod vic jak 4x
- private static byte counter = 0;
- // promenne pro start a cil
- private Coordinate start;
- private Coordinate goal;
- // pamet navstivenych bodu
- private boolean[][] discovered;
- // pomocne pole
- private int[][] values;
- // bludiste
- private int[][] map;
- @Override
- protected String getName()
- {
- return "Smart Player";
- }
- @Override
- protected Path findPath(int[][] map)
- {
- // inicializace pole map
- this.map = map;
- // inicializace pole discovered
- this.discovered = new boolean[map.length][map[0].length];
- // inicializace start a goal bodu, pridani do aktualni cesty
- Path path = new Path(Color.YELLOW);
- for (int i = 0; i < map.length; i++) {
- for (int j = 0; j < map.length; j++) {
- int cell = map[i][j];
- if (cell == Labyrinth.START_SQUARE) {
- this.start = new Coordinate(i, j);
- } else if (cell == Labyrinth.GOAL_SQUARE) {
- this.goal = new Coordinate(i, j);
- }
- }
- }
- Fronta fronta = new Fronta();
- fronta.add(this.start, null);
- while (!fronta.isEmpty()) {
- Coordinate[] point = fronta.remove();
- if (!this.discovered[point[0].getX()][point[0].getY()]) {
- this.discovered[point[0].getX()][point[0].getY()] = true;
- if (point[0].getX() - 1 >= 0 && this.map[point[0].getX() - 1][point[0].getY()] != Labyrinth.WALL_SQUARE) {
- Coordinate newPoint = new Coordinate(point[0].getX() - 1, point[0].getY());
- fronta.add(newPoint, point[0]);
- if (newPoint.equals(this.goal)) {
- break;
- }
- }
- if (point[0].getX() + 1 < this.map.length && this.map[point[0].getX() + 1][point[0].getY()] != Labyrinth.WALL_SQUARE) {
- Coordinate newPoint = new Coordinate(point[0].getX() + 1, point[0].getY());
- fronta.add(newPoint, point[0]);
- if (newPoint.equals(this.goal)) {
- break;
- }
- }
- if (point[0].getY() - 1 >= 0 && this.map[point[0].getX()][point[0].getY() - 1] != Labyrinth.WALL_SQUARE) {
- Coordinate newPoint = new Coordinate(point[0].getX(), point[0].getY() - 1);
- fronta.add(newPoint, point[0]);
- if (newPoint.equals(this.goal)) {
- break;
- }
- }
- if (point[0].getY() + 1 < this.map[0].length && this.map[point[0].getX()][point[0].getY() + 1] != Labyrinth.WALL_SQUARE) {
- Coordinate newPoint = new Coordinate(point[0].getX(), point[0].getY() + 1);
- fronta.add(newPoint, point[0]);
- if (newPoint.equals(this.goal)) {
- break;
- }
- }
- }
- }
- if (path.getCoordinates().indexOf(this.goal) == -1) {
- return null;
- }
- return path;
- }
- // vnitrni trida znazornujici frontu
- private class Fronta
- {
- private Prvek first, last;
- public Fronta()
- {
- this.first = this.last = null;
- }
- public boolean isEmpty()
- {
- return first == null;
- }
- public void add(Coordinate coord, Coordinate from)
- {
- Prvek l = this.last;
- this.last = new Prvek(coord, from);
- if (this.isEmpty()) {
- this.first = this.last;
- } else {
- l.next = this.last;
- }
- }
- public Coordinate[] remove() {
- Coordinate coord = this.first.coord;
- Coordinate from = this.first.from;
- this.first = this.first.next;
- Coordinate[] r = {coord, from};
- return r;
- }
- // vnitrni trida znazornujici prvek fronty
- // prvek je reprezentovan dvojci - aktualnim bodem a bodem, ze ktereho jsme se na nej dostali
- private class Prvek
- {
- public Coordinate coord;
- public Coordinate from;
- public Prvek next;
- public Prvek(Coordinate coord, Coordinate from)
- {
- this.coord = coord;
- this.from = from;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement