Advertisement
Guest User

MazeActor.java

a guest
Jun 26th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. package de.marcbeckhäuser.maze;
  2.  
  3. import java.awt.*;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. import ch.aplu.jgamegrid.*;
  8. import ch.aplu.util.*;
  9.  
  10. public class MazeActor extends Actor {
  11.    
  12.     private final Location startLocation = new Location(0, 1);
  13.     private final Location exitLocation;
  14.     private List<Location> visitedLocations;
  15.     private Location previousLoc = startLocation;
  16.     private GameGrid gamegrid;
  17.  
  18.     public MazeActor(GameGrid gamegrid) {
  19.         exitLocation = new Location(gamegrid.getNbHorzCells() - 1, gamegrid.getNbVertCells() - 2);
  20.         visitedLocations = new ArrayList<Location>();
  21.        
  22.         this.gamegrid = gamegrid;
  23.     }
  24.  
  25.     public void startSearch() {
  26.         TextActor distanceMark = new TextActor("");
  27.         gamegrid.addActor(distanceMark, new Location(0, 0));
  28.         gamegrid.setPaintOrder(MazeActor.class, TextActor.class);
  29.         searchWay(startLocation, 0);
  30.     }
  31.  
  32.     public void act() {
  33.         Monitor.wakeUp();
  34.     }
  35.  
  36.     private void searchWay(Location loc, int distance) {
  37.         Monitor.putSleep();
  38.         if (visitedLocations.contains(exitLocation) || visitedLocations.contains(loc))
  39.             return;
  40.         else {
  41.             visitedLocations.add(loc);
  42.             setLocationFacing(loc);
  43.             if (loc.equals(exitLocation)) {
  44.                 return;
  45.             } else {
  46.                 TextActor distanceMark = new TextActor("" + distance);
  47.                 distanceMark.setLocationOffset(new Point(-7, 0));
  48.                 gamegrid.addActorNoRefresh(distanceMark, loc);
  49.  
  50.                 if (canMove(new Location(loc.x, loc.y - 1))) // UP
  51.                     searchWay(new Location(loc.x, loc.y - 1), distance + 1);
  52.                 if (canMove(new Location(loc.x - 1, loc.y))) // LEFT
  53.                     searchWay(new Location(loc.x - 1, loc.y), distance + 1);
  54.                 if (canMove(new Location(loc.x, loc.y + 1))) // DOWN
  55.                     searchWay(new Location(loc.x, loc.y + 1), distance + 1);
  56.                 if (canMove(new Location(loc.x + 1, loc.y))) // RIGHT
  57.                     searchWay(new Location(loc.x + 1, loc.y), distance + 1);
  58.  
  59.                 if (!visitedLocations.contains(exitLocation)) {
  60.                     gamegrid.removeActorsAt(loc, TextActor.class);
  61.                     TextActor wrongMark = new TextActor("x", Color.red, Color.white, new Font("TimesNewRoman", Font.PLAIN, 12));
  62.                     distanceMark.setLocationOffset(new Point(-8, 0));
  63.                     gamegrid.addActorNoRefresh(wrongMark, loc);
  64.                     setLocationFacing(loc);
  65.                 }
  66.             }
  67.         }
  68.     }
  69.  
  70.     private void setLocationFacing(Location loc) {
  71.         setDirection(previousLoc.getCompassDirectionTo(loc));
  72.         previousLoc = loc;
  73.         setLocation(loc);
  74.     }
  75.  
  76.     private boolean canMove(Location location) {
  77.         Color c = getBackground().getColor(location);
  78.         return (!c.equals(Color.black));
  79.     }
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement