Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.82 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.awt.Graphics2D;
  4. import java.awt.Shape;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.geom.Line2D;
  8. import java.awt.geom.Rectangle2D;
  9. import java.util.ArrayList;
  10. import java.util.BitSet;
  11. import java.util.Random;
  12.  
  13. import javax.swing.BorderFactory;
  14. import javax.swing.JButton;
  15. import javax.swing.JCheckBox;
  16. import javax.swing.JComponent;
  17. import javax.swing.JFrame;
  18. import javax.swing.JPanel;
  19.  
  20. /**
  21. * An instance of this class is a maze, displayed in a window, which can be navigated.
  22. * <p>
  23. * When a Maze object is created,
  24. * a pseudorandom maze based on a certain integer "seed" is generated.
  25. * The seed is printed so that you can generate the same maze again if you wish
  26. * (this is very useful for debugging).
  27. * <p>
  28. * Navigation of the maze begins in the northwest corner.
  29. * The goal is to reach the southeast corner of the maze.
  30. * The path taken is drawn as a red line.
  31. */
  32. public class Maze extends JFrame
  33. {
  34. /**
  35. * Constant representing the direction "east".
  36. */
  37. public final static int EAST = 0;
  38.  
  39. /**
  40. * Constant representing the direction "north".
  41. */
  42. public final static int NORTH = 1;
  43.  
  44. /**
  45. * Constant representing the direction "south".
  46. */
  47. public final static int SOUTH = 2;
  48.  
  49. /**
  50. * Constant representing the direction "west".
  51. */
  52. public final static int WEST = 3;
  53.  
  54. /* CONSTANTS (NO MAGIC NUMBERS) */
  55. private static final long serialVersionUID = 1L;
  56. final static int FRAMEDIMENSIONS = 600;
  57. final static int MAZEDIMENSIONS = 500;
  58. final static int NUMBEROFCELLS = 30;
  59. final static int MAZEOFFSET = 30;
  60. final static int ERROR = 4;
  61. final static int SLEEPTIME = 5;
  62. final static double CELLWIDTH = MAZEDIMENSIONS / NUMBEROFCELLS;
  63. final static int COMPONENTOFFSET = 20;
  64. Random rgen;
  65. JButton solve = null;
  66. static Character c = null;
  67. /* PATH GRAPHICS */
  68. final static float PATHLENGTH = .5f;
  69. final static float PATHWIDTH = .1f;
  70. final static float LENGTHFIX = (float) (.1 * CELLWIDTH);
  71. Cell prevRectCell = null;
  72. Cell currentRectCell = null;
  73.  
  74. /* OTHER */
  75. private Cell[][] mazeRepresentation = new Cell[NUMBEROFCELLS][NUMBEROFCELLS];
  76. private static JComponent jc = null;
  77.  
  78.  
  79. private void setUnvisited()
  80. {
  81. for (int i = 0; i < NUMBEROFCELLS; i++)
  82. {
  83. for (int j = 0; j < NUMBEROFCELLS; j++)
  84. {
  85. mazeRepresentation[i][j].visited = false;
  86. }
  87. }
  88. }
  89.  
  90. private void setRectangles(int x, int y, int dir)
  91. {
  92. if (currentRectCell != null && prevRectCell != null)
  93. {
  94. prevRectCell.initializeRectangles();
  95. currentRectCell.initializeRectangles();
  96. if (dir == NORTH || dir == SOUTH)
  97. {
  98. if (dir == NORTH)
  99. {
  100. prevRectCell.setTopDrawable();
  101. currentRectCell.setBotDrawable();
  102. }
  103. else
  104. {
  105. prevRectCell.setBotDrawable();
  106. currentRectCell.setTopDrawable();
  107. }
  108. }
  109. else if (dir == WEST || dir == EAST)
  110. {
  111. if (dir == WEST)
  112. {
  113. prevRectCell.setLeftHorizDrawable();
  114. currentRectCell.setRightHorizDrawable();
  115. }
  116. else
  117. {
  118. prevRectCell.setRightHorizDrawable();
  119. currentRectCell.setLeftHorizDrawable();
  120. }
  121. }
  122. else
  123. {
  124. System.err.println("error in maze generation method");
  125. }
  126. }
  127. }
  128.  
  129. private void generateMaze()
  130. {
  131. generateMazeHelper(0, 0);
  132. jc.repaint(0);
  133. }
  134.  
  135. private void generateMazeHelper(int x, int y)
  136. {
  137. Cell startingCell = mazeRepresentation[x][y];
  138. startingCell.setVisited();
  139. prevRectCell = startingCell;
  140. ArrayList<Cell> neighbors = findNeighbors(x, y);
  141. while (neighbors != null && neighbors.size() > 0)
  142. {
  143. int randomNum = rgen.nextInt(neighbors.size());
  144. Cell chosen = neighbors.get(randomNum);
  145. if (chosen.getVisited() == false)
  146. {
  147. removeWall(startingCell, chosen);
  148. try
  149. {
  150. Thread.sleep(SLEEPTIME);
  151. } catch (InterruptedException e)
  152. {
  153. e.printStackTrace();
  154. }
  155. }
  156. neighbors.remove(randomNum);
  157. if (chosen.getVisited() == false)
  158. {
  159. currentRectCell = chosen;
  160. // ///////////////////////
  161. // setRectangles(x,y);
  162. // ///////////////////////
  163. generateMazeHelper(chosen.x, chosen.y);
  164. prevRectCell = startingCell;
  165. // prevRectCell.col = Color.gray;
  166. }
  167. }
  168. }
  169.  
  170. private void removeWall(Cell from, Cell to)
  171. {
  172. int dir = direction(from, to);
  173. if (dir == ERROR)
  174. {
  175. System.err.println("removeWall error");
  176. }
  177. int a = from.x;
  178. int b = from.y;
  179. if(dir == EAST)
  180. {
  181. removeEastWall(a, b);
  182. }
  183. if (dir == WEST)
  184. {
  185. removeWestWall(a, b);
  186. }
  187. if (dir == NORTH)
  188. {
  189. removeNorthWall(a, b);
  190. }
  191. if (dir == SOUTH)
  192. {
  193. removeSouthWall(a, b);
  194. }
  195. }
  196.  
  197. private ArrayList<Cell> findNeighbors(int x, int y)
  198. {
  199. if (x < 0 || x > NUMBEROFCELLS - 1)
  200. {
  201. return null;
  202. }
  203. if (y < 0 || y > NUMBEROFCELLS - 1)
  204. {
  205. return null;
  206. }
  207.  
  208. ArrayList<Cell> list = new ArrayList<Cell>();
  209. int a = x - 1;
  210. int b = y;
  211. if (a >= 0 && mazeRepresentation[a][b].getVisited() == false)
  212. {
  213. list.add(mazeRepresentation[a][b]);
  214. }
  215. a = x + 1;
  216. b = y;
  217. if (a < NUMBEROFCELLS && mazeRepresentation[a][b].getVisited() == false)
  218. {
  219. list.add(mazeRepresentation[a][b]);
  220. }
  221.  
  222. a = x;
  223. b = y + 1;
  224. if (b < NUMBEROFCELLS && mazeRepresentation[a][b].getVisited() == false)
  225. {
  226. list.add(mazeRepresentation[a][b]);
  227. }
  228. a = x;
  229. b = y - 1;
  230. if (b >= 0 && mazeRepresentation[a][b].getVisited() == false)
  231. {
  232. list.add(mazeRepresentation[a][b]);
  233. }
  234. return list;
  235. }
  236.  
  237. private int direction(Cell from, Cell to)
  238. {
  239. int x = to.x - from.x;
  240. int y = to.y - from.y;
  241. if (x == 0 && y == 0)
  242. {
  243. System.err
  244. .println("The cells for the direction method are the same");
  245. }
  246. if (x > 0)
  247. {
  248. return EAST;
  249. } else if (x < 0)
  250. {
  251. return WEST;
  252. } else if (y < 0)
  253. {
  254. return NORTH;
  255. } else if (y > 0)
  256. {
  257. return SOUTH;
  258. } else
  259. {
  260. System.err.println("Something is wrong with the direction method");
  261. return ERROR;
  262. }
  263. }
  264.  
  265. private void removeEastWall(int x, int y)
  266. {
  267. if (x < 0 || y < 0)
  268. {
  269. return;
  270. }
  271. if (x > NUMBEROFCELLS - 1 || y > NUMBEROFCELLS - 1)
  272. {
  273. return;
  274. }
  275. mazeRepresentation[x][y].setEast(null);
  276. if (x != NUMBEROFCELLS - 1)
  277. {
  278. mazeRepresentation[x + 1][y].setWest(null);
  279. }
  280. this.reDrawMaze();
  281. }
  282.  
  283. private void removeSouthWall(int x, int y)
  284. {
  285. if (x < 0 || y < 0)
  286. {
  287. return;
  288. }
  289. if (x > NUMBEROFCELLS - 1 || y > NUMBEROFCELLS - 1)
  290. {
  291. return;
  292. }
  293. mazeRepresentation[x][y].setSouth(null);
  294. if (y != NUMBEROFCELLS - 1)
  295. {
  296. mazeRepresentation[x][y + 1].setNorth(null);
  297. }
  298. this.reDrawMaze();
  299. }
  300.  
  301. private void removeWestWall(int x, int y)
  302. {
  303. if (x < 0 || y < 0)
  304. {
  305. return;
  306. }
  307. if (x > NUMBEROFCELLS - 1 || y > NUMBEROFCELLS - 1)
  308. {
  309. return;
  310. }
  311. mazeRepresentation[x][y].setWest(null);
  312. if (x > 0)
  313. {
  314. mazeRepresentation[x - 1][y].setEast(null);
  315. }
  316. this.reDrawMaze();
  317. }
  318.  
  319. private void removeNorthWall(int x, int y)
  320. {
  321. if (x < 0 || y < 0)
  322. {
  323. return;
  324. }
  325. if (x > NUMBEROFCELLS - 1 || y > NUMBEROFCELLS - 1)
  326. {
  327. return;
  328. }
  329. mazeRepresentation[x][y].setNorth(null);
  330. if (y > 0)
  331. {
  332. mazeRepresentation[x][y - 1].setSouth(null);
  333. }
  334. this.reDrawMaze();
  335. }
  336.  
  337. private void reDrawMaze()
  338. {
  339. jc.repaint(0);
  340. }
  341.  
  342. private void initializeLines()
  343. {
  344. for (int i = 0; i < NUMBEROFCELLS; i++)
  345. {
  346. for (int j = 0; j < NUMBEROFCELLS; j++)
  347. {
  348. mazeRepresentation[i][j] = new Cell((byte)i, (byte)j);
  349. Cell c = mazeRepresentation[i][j];
  350. if (j != 0)
  351. {
  352. c.setNorth(mazeRepresentation[i][j - 1].getSouth());
  353. } else
  354. {
  355. c.setNorth(new Line2D.Double(i * CELLWIDTH, j * CELLWIDTH,
  356. (i + 1) * CELLWIDTH, j * CELLWIDTH));
  357. }
  358. if (i != 0)
  359. {
  360. c.setWest(mazeRepresentation[i - 1][j].getEast());
  361. } else
  362. {
  363. c.setWest(new Line2D.Double(i * CELLWIDTH, j * CELLWIDTH, i
  364. * CELLWIDTH, (j + 1) * CELLWIDTH));
  365. }
  366. c.setEast(new Line2D.Double((i + 1) * CELLWIDTH, j * CELLWIDTH,
  367. (i + 1) * CELLWIDTH, (j + 1) * CELLWIDTH));
  368. c.setSouth(new Line2D.Double(i * CELLWIDTH,
  369. (j + 1) * CELLWIDTH, (i + 1) * CELLWIDTH, (j + 1)
  370. * CELLWIDTH));
  371.  
  372. }
  373. }
  374. }
  375.  
  376. private void resetMaze()
  377. {
  378. for(int i = 0; i < NUMBEROFCELLS;i++)
  379. {
  380. for(int j = 0; j < NUMBEROFCELLS;j++)
  381. {
  382. Cell c = mazeRepresentation[i][j];
  383. c.values.clear();
  384. c.visited = false;
  385. }
  386. }
  387. jc.repaint(0);
  388. }
  389. private void addPaintSurface()
  390. {
  391. jc = new PaintSurface();
  392. jc.setBounds(MAZEOFFSET, MAZEOFFSET, MAZEDIMENSIONS + 1,
  393. MAZEDIMENSIONS + 1);
  394. this.add(jc);
  395. }
  396.  
  397. /**
  398. * This method is responsible for moving east in the maze.
  399. * It will do nothing if there is a wall in that direction.
  400. */
  401. public void moveEast(){ c.moveEast(); }
  402. /**
  403. * This method is responsible for moving south in the maze.
  404. * It will do nothing if there is a wall in that direction.
  405. */
  406. public void moveSouth(){ c.moveSouth(); }
  407. /**
  408. * This method is responsible for moving north in the maze.
  409. * It will do nothing if there is a wall in that direction.
  410. */
  411. public void moveNorth(){ c.moveNorth(); }
  412. /**
  413. * This method is responsible for moving west in the maze.
  414. * It will do nothing if there is a wall in that direction.
  415. */
  416. public void moveWest(){ c.moveWest(); }
  417.  
  418. /**
  419. * This method is used to get the current direction, the maze character is facing.
  420. * @return Returns type int
  421. */
  422. int getDirection(){ return c.direction; }
  423. /**
  424. * This method is used to set the current direction, the maze character is facing.
  425. * @param dir
  426. */
  427. void setDirection(int dir){ c.direction = dir; }
  428.  
  429.  
  430. /**
  431. * Checks to see if there is a wall adjacent to the current position.
  432. * @param direction Direction in which to check for a wall.
  433. * Must be one of the following:
  434. * Maze.NORTH,
  435. * Maze.SOUTH,
  436. * Maze.EAST,
  437. * Maze.WEST.
  438. * @return True if there is a wall, false if not.
  439. */
  440. public boolean wallPresent(int direction){ return c.wallPresent(direction); }
  441.  
  442. /**
  443. * Determines whether goal (southeast corner of maze) has been reached.
  444. * @return True if goal has NOT been reached, false if it has been reached.
  445. */
  446. public boolean goalIsNotReached(){
  447. try
  448. {
  449. Thread.sleep(SLEEPTIME);
  450. } catch (InterruptedException e)
  451. {
  452. e.printStackTrace();
  453. }
  454. if(c.x != NUMBEROFCELLS -1 || c.y != NUMBEROFCELLS -1){
  455. return true;
  456. }
  457. return false;
  458. }
  459.  
  460. /**
  461. * Create a randomly generated maze (based on an arbitrarily chosen seed).
  462. * The seed used will be printed to the screen so you can reuse it later.
  463. */
  464. public Maze()
  465. {
  466. this((int) System.currentTimeMillis());
  467. }
  468.  
  469. /**
  470. * Create a maze using the specified seed.
  471. * @param seed An integer used to start the pseudorandom number generation process.
  472. */
  473. public Maze(int seed)
  474. {
  475. System.out.println("Using random seed: " + seed);
  476. rgen = new Random(seed);
  477. this.setBounds(100, 100, FRAMEDIMENSIONS, FRAMEDIMENSIONS);
  478. this.setTitle("Maze");
  479. this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  480. this.setLayout(null);
  481. initializeLines();
  482. addPaintSurface();
  483.  
  484. this.validate();
  485. this.setResizable(false);
  486. this.setVisible(true);
  487. generateMaze();
  488. c = new Character(mazeRepresentation);
  489. /*JPanel options = new JPanel();
  490. options.setLayout(null);
  491. options.setBounds(MAZEOFFSET+MAZEDIMENSIONS, MAZEOFFSET, MAZEDIMENSIONS/2, MAZEDIMENSIONS);
  492. options.setBorder(BorderFactory.createLineBorder(Color.black));
  493. JCheckBox animate = new JCheckBox("Animation");
  494. solve = new JButton("Solve");
  495. solve.setBounds((MAZEDIMENSIONS/4)-45,MAZEDIMENSIONS-(MAZEDIMENSIONS/10),90,30);
  496. solve.addActionListener(this);
  497. animate.setBounds(COMPONENTOFFSET,COMPONENTOFFSET,100,30);
  498. options.add(solve);
  499. options.add(animate);
  500. this.add(options);*/
  501. }
  502.  
  503. /***************************
  504. **THE PAINT SURFACE CLASS**
  505. ***************************/
  506.  
  507. private class PaintSurface extends JComponent
  508. {
  509. private static final long serialVersionUID = 1L;
  510.  
  511. private void myDraw(Graphics2D g2, Shape s)
  512. {
  513. if (s == null)
  514. {
  515. return;
  516. }
  517. g2.draw(s);
  518. }
  519.  
  520. private void drawPath(Graphics2D g2, Cell c, boolean cArg)
  521. {
  522. if (prevRectCell != null && currentRectCell != null)
  523. {
  524. Color col = null;
  525. if (c.getBotDrawable() && (c.getBotVertColor() == cArg))
  526. {
  527. col = (c.getBotVertColor() == false) ? Color.red: Color.gray;
  528. g2.setPaint(col);
  529. g2.fill(c.botVertRect);
  530. myDraw(g2, c.botVertRect);
  531. }
  532. if (c.getTopDrawable() && (c.getTopVertColor() == cArg))
  533. {
  534. col = (c.getTopVertColor() == false) ? Color.red: Color.gray;
  535. g2.setPaint(col);
  536. g2.fill(c.topVertRect);
  537. myDraw(g2, c.topVertRect);
  538. }
  539. if (c.getLeftHorizDrawable() && (c.getLeftHorizColor() == cArg))
  540. {
  541. col = (c.getLeftHorizColor() == false) ? Color.red: Color.gray;
  542. g2.setPaint(col);
  543. g2.fill(c.leftHorizRect);
  544. myDraw(g2, c.leftHorizRect);
  545. }
  546. if (c.getRightHorizDrawable() && (c.getRightHorizColor() == cArg))
  547. {
  548. col = (c.getRightHorizColor() == false) ? Color.red: Color.gray;
  549. g2.setPaint(col);
  550. g2.fill(c.rightHorizRect);
  551. myDraw(g2, c.rightHorizRect);
  552. }
  553.  
  554. }
  555. g2.setPaint(Color.black);
  556. }
  557.  
  558. public void paint(Graphics g)
  559. {
  560. Graphics2D g2 = (Graphics2D) g;
  561. g2.setPaint(Color.black);
  562. Cell c = null;
  563. for (int i = 0; i < NUMBEROFCELLS; i++)
  564. {
  565. for (int j = 0; j < NUMBEROFCELLS; j++)
  566. {
  567. c = mazeRepresentation[i][j];
  568. myDraw(g2, c.getEast());
  569. myDraw(g2, c.getWest());
  570. myDraw(g2, c.getNorth());
  571. myDraw(g2, c.getSouth());
  572. // TODO warp
  573. drawPath(g2, c, true);//true will color gray
  574. drawPath(g2, c, false);// false will color red
  575. }
  576.  
  577. }
  578. }
  579. }
  580.  
  581. private class Character
  582. {
  583. int x;
  584. int y;
  585. Cell[][] maze = null;
  586. int direction = SOUTH;
  587.  
  588. private Character(Cell[][] maze)
  589. {
  590. x = 0;
  591. y = 0;
  592. setUnvisited();
  593. this.maze = maze;
  594. }
  595.  
  596. private void traversal()
  597. {
  598. setUnvisited();
  599. traversalHelper(0, 0, NUMBEROFCELLS - 1, NUMBEROFCELLS - 1);
  600. jc.repaint(0);
  601. }
  602.  
  603. private boolean traversalHelper(int x, int y, int goalx, int goaly)
  604. {
  605. if (x == goalx && y == goaly)
  606. {
  607. return true;
  608. }
  609. Cell startingCell = maze[x][y];
  610.  
  611. startingCell.setVisited();
  612. prevRectCell = startingCell;
  613. Cell prev = null;
  614. Cell curr = null;
  615. int dir = -77;
  616. ArrayList<Integer> neighbors = findCells(startingCell);
  617. for (int i = 0; i < neighbors.size(); i++)
  618. {
  619. int direction = neighbors.get(i);
  620. Cell chosen = getNextCell(startingCell, direction);
  621. if (chosen.getVisited() == false)
  622. {
  623. //prevRectCell.col = Color.red;
  624. if (chosen.getVisited() == false)
  625. {
  626. currentRectCell = chosen;
  627. // ///////////////////////
  628. dir = direction(prevRectCell, currentRectCell);
  629. setRectangles(x, y, dir);
  630. // ///////////////////////
  631. jc.repaint(0);
  632. try
  633. {
  634. Thread.sleep(SLEEPTIME);
  635. } catch (InterruptedException e)
  636. {
  637. e.printStackTrace();
  638. }
  639.  
  640. if (traversalHelper(chosen.x, chosen.y, goalx, goaly))
  641. {
  642. return true;
  643. } else
  644. {
  645. prevRectCell = startingCell;
  646. prev = prevRectCell;
  647. curr = chosen;
  648. /*************************************************************************/
  649. if (prev != null && curr != null)
  650. {
  651. if (dir == NORTH || dir == SOUTH)
  652. {
  653. if (dir == NORTH)
  654. {
  655. prev.setTopVertRectGray();
  656. curr.setBotVertRectGray();
  657. } else
  658. {
  659. prev.setBotVertRectGray();
  660. curr.setTopVertRectGray();
  661. }
  662. } else if (dir == WEST || dir == EAST)
  663. {
  664. if (dir == WEST)
  665. {
  666. prev.setLeftHorizRectGray();
  667. curr.setRightHorizRectGray();
  668. } else
  669. {
  670. prev.setRightHorizRectGray();
  671. curr.setLeftHorizRectGray();
  672. }
  673. } else
  674. {
  675. System.err.println("prev " + prev.x + " , "
  676. + prev.y + " curr ");
  677. }
  678. }
  679. /*************************************************************************/
  680. }
  681. }
  682. }
  683. }
  684. // prevRectCell.col = Color.gray;
  685. jc.repaint(0);
  686. try
  687. {
  688. Thread.sleep(SLEEPTIME);
  689. } catch (InterruptedException e)
  690. {
  691. e.printStackTrace();
  692. }
  693. return false;
  694. }
  695.  
  696.  
  697.  
  698. private void moveEast()
  699. {
  700. if (x < NUMBEROFCELLS - 1 && ! wallPresent(EAST))
  701. {
  702. prevRectCell = mazeRepresentation[x][y];
  703. prevRectCell.setVisited();
  704. currentRectCell = mazeRepresentation[x + 1][y];
  705. x++;
  706. setRectangles(x, y, EAST);
  707. jc.repaint(0);
  708. }
  709. }
  710.  
  711. private void moveWest()
  712. {
  713.  
  714. if (x > 0 && ! wallPresent(WEST))
  715. {
  716. prevRectCell = mazeRepresentation[x][y];
  717. prevRectCell.setVisited();
  718. currentRectCell = mazeRepresentation[x - 1][y];
  719. x--;
  720. setRectangles(x, y, WEST);
  721. jc.repaint(0);
  722. }
  723. }
  724.  
  725. private void moveSouth()
  726. {
  727. if (y < NUMBEROFCELLS - 1 && ! wallPresent(SOUTH))
  728. {
  729. prevRectCell = mazeRepresentation[x][y];
  730. prevRectCell.setVisited();
  731. currentRectCell = mazeRepresentation[x][y + 1];
  732. y++;
  733. setRectangles(x, y, SOUTH);
  734. jc.repaint(0);
  735. }
  736. }
  737.  
  738. private void moveNorth()
  739. {
  740. if (y > 0 && ! wallPresent(NORTH))
  741. {
  742. prevRectCell = mazeRepresentation[x][y];
  743. prevRectCell.setVisited();
  744. currentRectCell = mazeRepresentation[x][y - 1];
  745. y--;
  746. setRectangles(x, y, NORTH);
  747. jc.repaint(0);
  748. }
  749. }
  750.  
  751. private boolean wallPresent(int arg)
  752. {
  753. if (arg == EAST)
  754. {
  755. if (mazeRepresentation[x][y].east == null)
  756. {
  757. return false;
  758. }
  759. return true;
  760. } else if (arg == WEST)
  761. {
  762. if (mazeRepresentation[x][y].west == null)
  763. {
  764. return false;
  765. }
  766. return true;
  767. } else if (arg == NORTH)
  768. {
  769. if (mazeRepresentation[x][y].north == null)
  770. {
  771. return false;
  772. }
  773. return true;
  774. } else
  775. {
  776. if (mazeRepresentation[x][y].south == null)
  777. {
  778. return false;
  779. }
  780. return true;
  781. }
  782. }
  783.  
  784. private Cell getNextCell(Cell from, int direction)
  785. {
  786. int x = from.x;
  787. int y = from.y;
  788. if (direction == EAST)
  789. {
  790. return maze[x + 1][y];
  791. }
  792. if (direction == WEST)
  793. {
  794. return maze[x - 1][y];
  795. }
  796. if (direction == NORTH)
  797. {
  798. return maze[x][y - 1];
  799. }
  800. if (direction == SOUTH)
  801. {
  802. return maze[x][y + 1];
  803. }
  804. return null;
  805. }
  806.  
  807. private ArrayList<Integer> findCells(Cell currentCell)
  808. {
  809. if (currentCell == null)
  810. {
  811. return null;
  812. }
  813. ArrayList<Integer> list = new ArrayList<Integer>();
  814. if (currentCell.north == null)
  815. {
  816. list.add(NORTH);
  817. }
  818. if (currentCell.east == null)
  819. {
  820. list.add(EAST);
  821. }
  822. if (currentCell.west == null)
  823. {
  824. list.add(WEST);
  825. }
  826. if (currentCell.south == null)
  827. {
  828. list.add(SOUTH);
  829. }
  830. return list;
  831. }
  832. }
  833.  
  834. private class Cell
  835. {
  836. byte x;
  837. byte y;
  838. private Line2D.Double north = null;
  839. private Line2D.Double south = null;
  840. private Line2D.Double east = null;
  841. private Line2D.Double west = null;
  842. Shape botVertRect = null;
  843. Shape topVertRect = null;
  844. Shape leftHorizRect = null;
  845. Shape rightHorizRect = null;
  846.  
  847. BitSet values =null;
  848.  
  849.  
  850.  
  851. boolean visited = false;
  852.  
  853. private Cell(byte x, byte y)
  854. {
  855. this.x = x;
  856. this.y = y;
  857. values = new BitSet(8);
  858. }
  859.  
  860. private void initializeRectangles()
  861. {
  862. topVertRect = new Rectangle2D.Double((x * CELLWIDTH)
  863. + ((CELLWIDTH / 2) - (CELLWIDTH * PATHWIDTH)), y
  864. * CELLWIDTH, CELLWIDTH * PATHWIDTH, PATHLENGTH * CELLWIDTH);
  865. botVertRect = new Rectangle2D.Double((x * CELLWIDTH)
  866. + ((CELLWIDTH / 2) - (CELLWIDTH * PATHWIDTH)), y
  867. * CELLWIDTH + (CELLWIDTH / 2) - LENGTHFIX, CELLWIDTH
  868. * PATHWIDTH, PATHLENGTH * CELLWIDTH + LENGTHFIX);
  869. leftHorizRect = new Rectangle2D.Double(x * CELLWIDTH, y * CELLWIDTH
  870. + ((CELLWIDTH / 2) - (CELLWIDTH * PATHWIDTH)), PATHLENGTH
  871. * CELLWIDTH, CELLWIDTH * PATHWIDTH);
  872. rightHorizRect = new Rectangle2D.Double(x * CELLWIDTH
  873. + (CELLWIDTH / 2), y * CELLWIDTH
  874. + ((CELLWIDTH / 2) - (CELLWIDTH * PATHWIDTH)), PATHLENGTH
  875. * CELLWIDTH, CELLWIDTH * PATHWIDTH);
  876. }
  877.  
  878. private Line2D.Double getNorth(){ return north; }
  879. private Line2D.Double getSouth(){ return south; }
  880. private Line2D.Double getEast(){ return east; }
  881. private Line2D.Double getWest(){ return west; }
  882. private boolean getVisited(){ return visited; }
  883. private boolean getBotVertColor(){ return values.get(0); }
  884. private boolean getTopVertColor(){ return values.get(1); }
  885. private boolean getLeftHorizColor(){ return values.get(2); }
  886. private boolean getRightHorizColor(){ return values.get(3); }
  887. private boolean getBotDrawable(){ return values.get(4); }
  888. private boolean getTopDrawable(){ return values.get(5); }
  889. private boolean getLeftHorizDrawable(){ return values.get(6); }
  890. private boolean getRightHorizDrawable(){ return values.get(7); }
  891.  
  892. private void setNorth(Line2D.Double wall){ north = wall; }
  893. private void setSouth(Line2D.Double wall){ south = wall; }
  894. private void setEast(Line2D.Double wall){ east = wall; }
  895. private void setWest(Line2D.Double wall){ west = wall; }
  896. private void setVisited(){ visited = true; }
  897. private void setBotVertRectGray(){ values.flip(0); }
  898. private void setTopVertRectGray(){ values.flip(1); }
  899. private void setLeftHorizRectGray(){ values.flip(2); }
  900. private void setRightHorizRectGray(){ values.flip(3); }
  901. private void setBotDrawable(){ values.set(4); }
  902. private void setTopDrawable(){ values.set(5); }
  903. private void setLeftHorizDrawable(){ values.set(6); }
  904. private void setRightHorizDrawable(){ values.set(7); }
  905. }
  906.  
  907.  
  908. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement