home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / BURKS / LANGUAGE / JAVA / NOTES / SOURCE / maze.jav < prev    next >
Text File  |  1996-12-20  |  18KB  |  438 lines

  1. /*********************************************************************************
  2.  
  3.    Creates a random maze, then solves it by finding a path from the
  4.    upper left corner to the lower right corner.  (After doing
  5.    one maze, it waits a while then starts over by creating a
  6.    new random maze.)
  7.    
  8.    This applet is cutomizable using the following parameters in
  9.    its <applet> tag:
  10.    
  11.         name            default value    meaning
  12.        ------           ---------------  --------------------------------------
  13.         rows                     21         number of rows in maze (must be odd)
  14.         columns                  21         number of columns in maze (must be odd)
  15.         border                    0         width of colored border around maze
  16.                                               (extra space after making equal-sized
  17.                                                rows and columns is also part of the border)
  18.         sleepTime              5000         pause, in milliseconds between
  19.                                                solving one maze and creating
  20.                                                another
  21.         speed                     3         integer between 1 and 5 specifying
  22.                                                how fast the a maze is created
  23.                                                and solved.  (1 is fastest.)
  24.         wallColor             black  
  25.         emptyColor      128 128 255
  26.         pathColor           200 0 0      
  27.         visitedColor  same as empty
  28.         borderColor           white
  29.         
  30.     Parameter names are case sensative.  Parameters that specify
  31.     colors can be given by one of the predefined color names
  32.     (black, white, red, blue, green, cyan, magenta, yellow, pink
  33.     orange, gray, lightGray, darkGray) or as an RGB color specified
  34.     by three integers between 0 and 255.  Color names are NOT
  35.     case sensative.
  36.  
  37. BY:  David Eck
  38.      Department of Mathematics and Computer Science
  39.      Hobart and William Smith Colleges
  40.      Geneva, NY   14456
  41.      
  42.      E-mail:  eck@hws.edu
  43.      
  44.  
  45. NOTE:  YOU CAN DO ANYTHING YOU WANT WITH THIS CODE AND APPLET, EXCEPT
  46.        CLAIM CREDIT FOR THEM (such as by trying to copyright the code
  47.        your self).
  48.  
  49. **************************************************************************/
  50.  
  51.  
  52. import java.awt.*;
  53.  
  54. public class Maze extends java.applet.Applet implements Runnable {
  55.  
  56.     int[][] maze;  // Description of state of maze.  The value of maze[i][j]
  57.                    // is one of the constants wallCode, pathcode, emptyCode,
  58.                    // or visitedCode.  (Value can also be negative, temporarily,
  59.                    // inside createMaze().)
  60.                    //    A maze is made up of walls and corridors.  maze[i][j]
  61.                    // is either part of a wall or part of a corridor.  A cell
  62.                    // cell that is part of a cooridor is represented by pathCode
  63.                    // if it is part of the current path through the maze, by
  64.                    // visitedCode if it has already been explored without finding
  65.                    // a solution, and by emptyCode if it has not yet been explored.
  66.  
  67.     final static int backgroundCode = 0;
  68.     final static int wallCode = 1;
  69.     final static int pathCode = 2;
  70.     final static int emptyCode = 3;
  71.     final static int visitedCode = 4;
  72.     
  73.        // the next six items are set up in init(), and can be specified 
  74.        // using applet parameters
  75.  
  76.     Color[] color = new Color[5];  // colors associated with the preceding 5 constants;
  77.     int rows = 21;          // number of rows of cells in maze, including a wall around edges
  78.     int columns = 21;       // number of columns of cells in maze, including a wall around edges
  79.     int border = 0;         // minimum number of pixels between maze and edge of applet
  80.     int sleepTime = 5000;   // wait time after solving one maze before making another
  81.     int speedSleep = 50;    // short delay between steps in making and solving maze
  82.     
  83.     Thread mazeThread;   // thread for creating and solving maze
  84.     Graphics me = null;  // graphics context for applet; created in checkSize()
  85.     
  86.     int width = -1;   // width of applet, to be set by checkSize()
  87.     int height = -1;  // height of applet, to be set by checkSize()
  88.  
  89.     int totalWidth;   // width of applet, minus border area (set in checkSize())
  90.     int totalHeight;  // height of applet, minus border area (set in checkSize())
  91.     int left;         // left edge of maze, allowing for border (set in checkSize())
  92.     int top;          // top edge of maze, allowing for border (set in checkSize())
  93.     
  94.     boolean mazeExists = false;  // set to true when maze[][] is valid; used in
  95.                                  // redrawMaze(); set to true in createMaze(), and
  96.                                  // reset to false in run()
  97.  
  98.  
  99.  
  100.     Integer getIntParam(String paramName) {
  101.           // Utility routine for reading an applet param which is an integer.
  102.           // Returns null if there is no such param, or if the value is not
  103.           // a legal integer.
  104.        String param = getParameter(paramName);
  105.        if (param == null)
  106.           return null;
  107.        int i;
  108.        try {
  109.           i = Integer.parseInt(param);
  110.        }
  111.        catch (NumberFormatException e) {
  112.           return null;
  113.        }
  114.        return new Integer(i);
  115.     }
  116.     
  117.     Color getColorParam(String paramName) {
  118.           // Utility routine for reading an applet param which is a color.
  119.           // Returns null if there is no such param, or if the value is not
  120.           // a legal color.  Colors can be specified as three integers,
  121.           // separated by spaces, giving RGB components in the range 0 to 255;
  122.           // the standard Java color names are also acceptable.
  123.        String param = getParameter(paramName);
  124.        if (param == null || param.length() == 0)
  125.           return null;
  126.        if (Character.isDigit(param.charAt(0))) {  // try to parse RGB color
  127.           int r=0,g=0,b=0;
  128.           int pos=0;
  129.           int d=0;
  130.           int len=param.length();
  131.           while (pos < len && Character.isDigit(param.charAt(pos)) && r < 255) {
  132.               d = Character.digit(param.charAt(pos),10);
  133.               r = 10*r + d;
  134.               pos++;
  135.           }
  136.           if (r > 255)
  137.              return null;
  138.           while (pos < len && !Character.isDigit(param.charAt(pos)))
  139.              pos++;
  140.           if (pos >= len)
  141.              return null;
  142.           while (pos < len && Character.isDigit(param.charAt(pos)) && g < 255) {
  143.               d = Character.digit(param.charAt(pos),10);
  144.               g = 10*g + d;
  145.               pos++;
  146.           }
  147.           if (g > 255)
  148.              return null;
  149.           while (pos < len && !Character.isDigit(param.charAt(pos)))
  150.              pos++;
  151.           if (pos >= len)
  152.              return null;
  153.           while (pos < len && Character.isDigit(param.charAt(pos)) && b < 255) {
  154.               d = Character.digit(param.charAt(pos),10);
  155.               b = 10*b + d;
  156.               pos++;
  157.           }
  158.           if (b > 255)
  159.              return null;
  160.           return new Color(r,g,b);          
  161.        }
  162.        if (param.equalsIgnoreCase("black"))
  163.           return Color.black;
  164.        if (param.equalsIgnoreCase("white"))
  165.           return Color.white;
  166.        if (param.equalsIgnoreCase("red"))
  167.           return Color.red;
  168.        if (param.equalsIgnoreCase("green"))
  169.           return Color.green;
  170.        if (param.equalsIgnoreCase("blue"))
  171.           return Color.blue;
  172.        if (param.equalsIgnoreCase("yellow"))
  173.           return Color.yellow;
  174.        if (param.equalsIgnoreCase("cyan"))
  175.           return Color.cyan;
  176.        if (param.equalsIgnoreCase("magenta"))
  177.           return Color.magenta;
  178.        if (param.equalsIgnoreCase("pink"))
  179.           return Color.pink;
  180.        if (param.equalsIgnoreCase("orange"))
  181.           return Color.orange;
  182.        if (param.equalsIgnoreCase("gray"))
  183.           return Color.gray;
  184.        if (param.equalsIgnoreCase("darkgray"))
  185.           return Color.darkGray;
  186.        if (param.equalsIgnoreCase("lightgray"))
  187.           return Color.lightGray;
  188.        return null;  // param is not a legal color
  189.     }
  190.  
  191.