Advertisement
Guest User

Maze.java

a guest
Jan 21st, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.43 KB | None | 0 0
  1.  
  2. import info.gridworld.grid.Location;
  3. import java.awt.Color;
  4. import java.util.*;
  5.  
  6. public class Maze extends AbstractMaze
  7. {
  8.     private static final Color WALL = Color.BLACK;
  9.     private static final Color PATH = Color.WHITE;
  10.     private static final Color BEGIN = Color.RED;
  11.     private static final Color COMPLETED = Color.MAGENTA;
  12.     private static final Color END = Color.GREEN;
  13.  
  14.     public Maze()
  15.     {
  16.         super();
  17.     }
  18.  
  19.     @Override
  20.     public void populateMaze()
  21.     {
  22.         int temp = (int)(Math.random() * getGrid().getNumRows());
  23.         int temp2 = (int)(Math.random() * getGrid().getNumRows());
  24.  
  25.         for(int row = 1; row < getGrid().getNumRows() - 1; row++)
  26.         {
  27.             for(int col = 1; col < getGrid().getNumCols() - 1; col++)
  28.             {
  29.                 getGrid().put(new Location(row, col), Math.random() < Math.random()? WALL : PATH);
  30.             }
  31.         }
  32.  
  33.         for(int x = 0; x <getGrid().getNumRows(); x++)
  34.         {
  35.             getGrid().put(new Location(x,0), WALL);
  36.             getGrid().put(new Location(x, getGrid().getNumRows() - 1 ), WALL);
  37.         }
  38.  
  39.         for(int x = 1; x <getGrid().getNumCols(); x++)
  40.         {
  41.             getGrid().put(new Location(getGrid().getNumCols() - 1 , x), WALL);
  42.             getGrid().put(new Location(0 , x), WALL);
  43.         }
  44.  
  45.         getGrid().put(new Location(temp, 0), BEGIN);
  46.         getGrid().put(new Location(temp2, getGrid().getNumCols() - 1), END);
  47.     }
  48.  
  49.     @Override
  50.     public boolean solveMaze()
  51.     {
  52.         if(done(findFinish()))
  53.         {
  54.             return true;
  55.         }
  56.  
  57.         boolean solve = false;
  58.         Deque<Location> store = new LinkedList<>();
  59.  
  60.         store.push(getStart());
  61.  
  62.         while(!store.isEmpty() && !solve)
  63.         {
  64.           Location pos = store.peek();
  65.           ArrayList<Location> moves = getNext(pos);
  66.           if(moves.size() != 0)
  67.           {
  68.               Location change = moves.get(0);
  69.               add(change, COMPLETED);
  70.               store.push(change);
  71.               if (done(findFinish()))
  72.               {
  73.                   solve = true;
  74.               }
  75.           }
  76.           else
  77.           {
  78.               store.pop();
  79.           }
  80.         }
  81.  
  82.       return solve;
  83.     }
  84.  
  85.     private Location getStart()
  86.     {
  87.         for(Location location : getGrid().getOccupiedLocations())
  88.         {
  89.             if(getGrid().get(location).equals(BEGIN))
  90.             {
  91.                 return location;
  92.             }
  93.         }
  94.         return null;
  95.     }
  96.  
  97.     private ArrayList<Location> getNext(Location temp)
  98.     {
  99.         ArrayList<Location> ans = new ArrayList<>();
  100.         for (Location location : getGrid().getValidAdjacentLocations(temp))
  101.         {
  102.             if(getGrid().get(location).equals(PATH))
  103.                 ans.add(location);
  104.         }
  105.         return ans;
  106.     }
  107.  
  108.     private boolean done(Location lo)
  109.     {
  110.         for(Location location : getGrid().getValidAdjacentLocations(findFinish()))
  111.         {
  112.             if(getGrid().get(location).equals(COMPLETED))
  113.             {
  114.                 return true;
  115.             }
  116.         }
  117.         return false;
  118.     }
  119.  
  120.     private Location findFinish()
  121.     {
  122.         for(Location location : getGrid().getOccupiedLocations())
  123.         {
  124.             if(getGrid().get(location).equals(END))
  125.             {
  126.                 return location;
  127.             }
  128.         }
  129.         return null;
  130.     }
  131.  
  132.  
  133.  
  134.  
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement