Advertisement
Guest User

maze.class

a guest
Nov 7th, 2012
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.99 KB | None | 0 0
  1. import se.lth.cs.ptdc.window.SimpleWindow;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.jar.*;
  5. import java.io.*;
  6. import java.util.Scanner;
  7.  
  8. /**
  9.  * Describes a maze. The maze definition is read from a file.
  10.  *
  11.  */
  12. public class Maze {
  13. /** Where the maze files are. */
  14. private static final String JAR_NAME = "cs_eda016.jar";
  15. /** Entry and exit coordinates. */
  16. private int xEntry, yEntry, xExit, yExit;
  17. /** The walls. */
  18. private ArrayList<Wall> walls;
  19. /** The width of a wall. */
  20. private static final int WALL_WIDTH = 6;
  21.  
  22. /**
  23.  * Creates a maze with number nbr. The maze definition is read from the file
  24.  * maze<nbr>.maze, which must be in cs_eda016.jar, which must be on the
  25.  * class path.
  26.  *
  27.  * @param nbr
  28.  *            the number of the maze
  29.  */
  30. public Maze(int nbr) {
  31.     walls = new ArrayList<Wall>();
  32.     create(nbr);
  33. }
  34.  
  35. /**
  36.  * Draws the maze in a window.
  37.  *
  38.  * @param w
  39.  *            the SimpleWindow to draw in
  40.  */
  41. public void draw(SimpleWindow w) {
  42.     int savedLineWidth = w.getLineWidth();
  43.     w.setLineWidth(WALL_WIDTH - 2);
  44.     java.awt.Color savedColor = w.getLineColor();
  45.     w.setLineColor(java.awt.Color.BLUE);
  46.     for (int i = 0; i < walls.size(); i++) {
  47.         Wall wall = walls.get(i);
  48.         if (wall.y1 == wall.y2) {
  49.             w.moveTo(wall.left + 1, wall.y1);
  50.             w.lineTo(wall.right - 1, wall.y2);
  51.         } else {
  52.             w.moveTo(wall.x1, wall.top + 1);
  53.             w.lineTo(wall.x2, wall.bottom - 1);
  54.         }
  55.     }
  56.     w.setLineColor(savedColor);
  57.     w.setLineWidth(savedLineWidth);
  58. }
  59.  
  60. /**
  61.  * Returns the x coordinate of the entry point.
  62.  *
  63.  * @return the x coordinate
  64.  */
  65. public int getXEntry() {
  66.     return xEntry + WALL_WIDTH / 2;
  67. }
  68.  
  69. /**
  70.  * Returns the y coordinate of the entry point.
  71.  *
  72.  * @return the y coordinate
  73.  */
  74. public int getYEntry() {
  75.     return yEntry;
  76. }
  77.  
  78. /**
  79.  * Checks if the point x,y is at (or near) the exit.
  80.  *
  81.  * @param x
  82.  *            the x coordinate
  83.  * @param y
  84.  *            the y coordinate
  85.  * @return true if the point is at the exit, false otherwise
  86.  */
  87. public boolean atExit(int x, int y) {
  88.     return Math.abs(x - xExit) <= WALL_WIDTH && y == yExit
  89.             || Math.abs(y - yExit) <= WALL_WIDTH && x == xExit;
  90. }
  91.  
  92. /**
  93.  * Check if you, when you're in x,y and heading in a given direction, have a
  94.  * wall directly to the left.
  95.  *
  96.  * @param direction
  97.  *            the direction (0, 90, 180, 270 degrees)
  98.  * @param x
  99.  *            the x coordinate
  100.  * @param y
  101.  *            the y coordinate
  102.  * @return true if there's a wall directly to the left, false otherwise
  103.  */
  104. public boolean wallAtLeft(int direction, int x, int y) {
  105.     return inWall(direction + 90, x, y);
  106. }
  107.  
  108. /**
  109.  * Check if you, when you're in x,y and heading in a given direction, have a
  110.  * wall directly in front.
  111.  *
  112.  * @param direction
  113.  *            the direction (0, 90, 180, 270 degrees)
  114.  * @param x
  115.  *            the x coordinate
  116.  * @param y
  117.  *            the y coordinate
  118.  * @return true if there's a wall directly in front, false otherwise
  119.  */
  120. public boolean wallInFront(int direction, int x, int y) {
  121.     return inWall(direction, x, y);
  122. }
  123.  
  124. // -------------------------------------
  125.  
  126. private class Wall {
  127.     int x1, y1, x2, y2; // coordinates
  128.     int top, bottom, left, right; // coordinates for outline of the wall
  129.  
  130.     /** create a wall, width wide, from x1,y1 to x2,y2 */
  131.     public Wall(int width, int x1, int y1, int x2, int y2) {
  132.         this.x1 = x1;
  133.         this.y1 = y1;
  134.         this.x2 = x2;
  135.         this.y2 = y2;
  136.         int minx = Math.min(x1, x2);
  137.         int maxx = Math.max(x1, x2);
  138.         int miny = Math.min(y1, y2);
  139.         int maxy = Math.max(y1, y2);
  140.         top = miny - width / 2;
  141.         bottom = maxy + width / 2;
  142.         left = minx - width / 2;
  143.         right = maxx + width / 2;
  144.     }
  145.  
  146.     public boolean touches(int x, int y) {
  147.         return (x >= left && x <= right && y >= top && y <= bottom);
  148.     }
  149. }
  150.  
  151. private boolean inWall(int dir, int x, int y) {
  152.     dir = dir % 360;
  153.     int nextX = x + (int) Math.round(Math.cos(dir * Math.PI / 180));
  154.     int nextY = y - (int) Math.round(Math.sin(dir * Math.PI / 180));
  155.     int i = 0;
  156.     while (i < walls.size() && !walls.get(i).touches(nextX, nextY)) {
  157.         i++;
  158.     }
  159.     return i < walls.size();
  160. }
  161.  
  162. private void create(int nbr) {
  163.     JarInputStream jin = getStream(JAR_NAME);
  164.     if (jin == null) {
  165.         System.err.println("Maze error: could not find " + JAR_NAME);
  166.         System.exit(1);
  167.     }
  168.  
  169.     String mazeFileName = "maze" + nbr + ".maze";
  170.     JarEntry entry = null;
  171.     try {
  172.         entry = jin.getNextJarEntry();
  173.         while (entry != null && !entry.getName().endsWith(mazeFileName)) {
  174.             entry = jin.getNextJarEntry();
  175.         }
  176.     } catch (IOException e) {
  177.     }
  178.     if (entry == null) {
  179.         System.err.println("Maze error: could not find " + mazeFileName
  180.                 + " in " + JAR_NAME);
  181.         System.exit(1);
  182.     }
  183.  
  184.     /*
  185.      * Read the maze file: xExit, yExit, xEntry, yEntry then x1, y1, x2, y2
  186.      * for each wall
  187.      */
  188.     Scanner sc = new Scanner(jin);
  189.     try {
  190.         xExit = sc.nextInt();
  191.         yExit = sc.nextInt();
  192.         xEntry = sc.nextInt();
  193.         yEntry = sc.nextInt();
  194.         while (sc.hasNextInt()) {
  195.             int x1 = sc.nextInt();
  196.             int y1 = sc.nextInt();
  197.             int x2 = sc.nextInt();
  198.             int y2 = sc.nextInt();
  199.             walls.add(new Wall(WALL_WIDTH, x1, y1, x2, y2));
  200.         }
  201.     } catch (Exception e) {
  202.         System.err.println("Maze error: cannot read " + mazeFileName);
  203.         System.err.println(e);
  204.         System.exit(1);
  205.     }
  206. }
  207.  
  208. /*
  209.  * Find and open a jar file with the name jarFileName, which is somewhere on
  210.  * the class path.
  211.  */
  212. private JarInputStream getStream(String jarFileName) {
  213.     /*
  214.      * The following should work, but doesn't:
  215.      *
  216.      * return new
  217.      * JarInputStream(this.getClassLoader().getResourceAsStream(jarFileName
  218.      * ));
  219.      *
  220.      * So we must search manually.
  221.      */
  222.  
  223.     String fullpath = (System.getProperty("java.class.path")
  224.             + File.pathSeparator + System.getProperty("java.library.path"));
  225.     Scanner sc = new Scanner(fullpath).useDelimiter(File.pathSeparator);
  226.     File f = null;
  227.     while (f == null && sc.hasNext()) {
  228.         String name = sc.next();
  229.         if (name.endsWith(JAR_NAME)) {
  230.             f = new File(name);
  231.         } else if (!name.endsWith(".jar")) { // name is a directory,
  232.                                                 // hopefully f =
  233.             new File(name + File.separator + JAR_NAME);
  234.         }
  235.         if (f != null && !f.exists()) {
  236.             f = null;
  237.         }
  238.     }
  239.     if (f == null) {
  240.         return null;
  241.     }
  242.     try {
  243.         return new JarInputStream(new FileInputStream(f));
  244.     } catch (FileNotFoundException e1) {
  245.         System.out.println(e1);
  246.         return null;
  247.     } catch (IOException e2) {
  248.         System.out.println(e2);
  249.         return null;
  250.     }
  251.  
  252. }
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement