Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.12 KB | None | 0 0
  1. package rph.labyrinth.players;
  2.  
  3. import java.awt.Color;
  4. import rph.labyrinth.Coordinate;
  5. import rph.labyrinth.Labyrinth;
  6.  
  7. import rph.labyrinth.Path;
  8. import rph.labyrinth.Player;
  9.  
  10. public class MyPlayer extends Player {
  11.  
  12.     // konstanty pro doporuceny smer
  13.     private final int GOAL = 0; // jsme na miste
  14.     private final int RIGHT = 1;
  15.     private final int LEFT = 2;
  16.     private final int UP = 3;
  17.     private final int DOWN = 4;
  18.     private final int FAIL = 5; // neni kam dal, tudy to nepujde
  19.  
  20.     // staticky counter, pro check jestli nezkousim hledat smer pro jeden bod vic jak 4x
  21.     private static byte counter = 0;
  22.  
  23.     // promenne pro start a cil
  24.     private Coordinate start;
  25.     private Coordinate goal;
  26.  
  27.     // pamet navstivenych bodu
  28.     private boolean[][] discovered;
  29.  
  30.     // pomocne pole
  31.     private int[][] values;
  32.  
  33.     // bludiste
  34.     private int[][] map;
  35.  
  36.     @Override
  37.     protected String getName()
  38.     {
  39.         return "Smart Player";
  40.     }
  41.  
  42.     @Override
  43.     protected Path findPath(int[][] map)
  44.     {
  45.         // inicializace pole map
  46.         this.map = map;
  47.         // inicializace pole discovered
  48.         this.discovered = new boolean[map.length][map[0].length];
  49.  
  50.         // inicializace start a goal bodu, pridani do aktualni cesty
  51.         Path path = new Path(Color.YELLOW);
  52.         for (int i = 0; i < map.length; i++) {
  53.             for (int j = 0; j < map.length; j++) {
  54.                 int cell = map[i][j];
  55.                 if (cell == Labyrinth.START_SQUARE) {
  56.                     this.start = new Coordinate(i, j);
  57.                 } else if (cell == Labyrinth.GOAL_SQUARE) {
  58.                     this.goal = new Coordinate(i, j);
  59.                 }
  60.             }
  61.         }
  62.  
  63.         Fronta fronta = new Fronta();
  64.         fronta.add(this.start, null);
  65.  
  66.         while (!fronta.isEmpty()) {
  67.             Coordinate[] point = fronta.remove();
  68.             if (!this.discovered[point[0].getX()][point[0].getY()]) {
  69.                 this.discovered[point[0].getX()][point[0].getY()] = true;
  70.                 if (point[0].getX() - 1 >= 0 && this.map[point[0].getX() - 1][point[0].getY()] != Labyrinth.WALL_SQUARE) {
  71.                     Coordinate newPoint = new Coordinate(point[0].getX() - 1, point[0].getY());
  72.                     fronta.add(newPoint, point[0]);
  73.                     if (newPoint.equals(this.goal)) {
  74.                         break;
  75.                     }
  76.                 }
  77.                 if (point[0].getX() + 1 < this.map.length && this.map[point[0].getX() + 1][point[0].getY()] != Labyrinth.WALL_SQUARE) {
  78.                     Coordinate newPoint = new Coordinate(point[0].getX() + 1, point[0].getY());
  79.                     fronta.add(newPoint, point[0]);
  80.                     if (newPoint.equals(this.goal)) {
  81.                         break;
  82.                     }
  83.                 }
  84.                 if (point[0].getY() - 1 >= 0 && this.map[point[0].getX()][point[0].getY() - 1] != Labyrinth.WALL_SQUARE) {
  85.                     Coordinate newPoint = new Coordinate(point[0].getX(), point[0].getY() - 1);
  86.                     fronta.add(newPoint, point[0]);
  87.                     if (newPoint.equals(this.goal)) {
  88.                         break;
  89.                     }
  90.                 }
  91.                 if (point[0].getY() + 1 < this.map[0].length && this.map[point[0].getX()][point[0].getY() + 1] != Labyrinth.WALL_SQUARE) {
  92.                     Coordinate newPoint = new Coordinate(point[0].getX(), point[0].getY() + 1);
  93.                     fronta.add(newPoint, point[0]);
  94.                     if (newPoint.equals(this.goal)) {
  95.                         break;
  96.                     }
  97.                 }
  98.             }
  99.         }
  100.  
  101.         if (path.getCoordinates().indexOf(this.goal) == -1) {
  102.             return null;
  103.         }
  104.  
  105.         return path;
  106.     }
  107.    
  108.     // vnitrni trida znazornujici frontu
  109.     private class Fronta
  110.     {
  111.        
  112.         private Prvek first, last;
  113.        
  114.         public Fronta()
  115.         {
  116.             this.first = this.last = null;
  117.         }
  118.    
  119.         public boolean isEmpty()
  120.         {
  121.             return first == null;
  122.         }
  123.  
  124.         public void add(Coordinate coord, Coordinate from)
  125.         {
  126.             Prvek l = this.last;
  127.             this.last = new Prvek(coord, from);
  128.             if (this.isEmpty()) {
  129.                 this.first = this.last;
  130.             } else {
  131.                 l.next = this.last;
  132.             }
  133.         }
  134.        
  135.         public Coordinate[] remove() {
  136.             Coordinate coord = this.first.coord;
  137.             Coordinate from = this.first.from;
  138.             this.first = this.first.next;
  139.             Coordinate[] r = {coord, from};
  140.             return r;
  141.         }
  142.  
  143.         // vnitrni trida znazornujici prvek fronty
  144.         // prvek je reprezentovan dvojci - aktualnim bodem a bodem, ze ktereho jsme se na nej dostali
  145.         private class Prvek
  146.         {
  147.  
  148.             public Coordinate coord;
  149.             public Coordinate from;
  150.             public Prvek next;
  151.  
  152.             public Prvek(Coordinate coord, Coordinate from)
  153.             {
  154.                 this.coord = coord;
  155.                 this.from = from;
  156.             }
  157.  
  158.         }
  159.        
  160.     }
  161.  
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement