v01i061: mazedemo: a maze demo program, Part01/01

Mike Wexler mikew at wyse.wyse.com
Thu Oct 13 04:22:08 AEST 1988


Submitted-by: rhess at pleione.cimshop.com (Richard Hess)
Posting-number: Volume 1, Issue 61
Archive-name: mazedemo/part01

#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of archive 1 (of 1)."
# Contents:  README Imakefile Makefile logo.xbm maze.c maze.man
# Wrapped by mikew at wyse on Wed Oct 12 11:21:00 1988
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'README' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'README'\"
else
echo shar: Extracting \"'README'\" \(846 characters\)
sed "s/^X//" >'README' <<'END_OF_FILE'
X[ maze ]
X
XThis was a SunView demo program which has been hacked to run under X11.  The
Xprogram creates a "random" maze and then solves the maze with graphical
Xfeedback to the user.  After a given maze is solved, another is created.  The
Xmouse can be used to control this process to some degree.
X
X  "Right"  mouse click --> causes the program to exit...  
X  "Left"   mouse click --> causes a new maze pattern to be created...
X  "Middle" mouse click --> toggles the maze off and on (ie. stop/start)...
X
XEnjoy...
X
XRichard Hess	...!uunet!cimshop!rhess		[ enhanced X11 demo... ]
X-------------------------------------------------------------------------------
XDave Lemke	lemke at sun.com			[ original X11 demo... ]
XMartin Weiss	Sun Microsystems		[ original SunView demo... ]
X-------------------------------------------------------------------------------
END_OF_FILE
if test 846 -ne `wc -c <'README'`; then
    echo shar: \"'README'\" unpacked with wrong size!
fi
# end of 'README'
fi
if test -f 'Imakefile' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'Imakefile'\"
else
echo shar: Extracting \"'Imakefile'\" \(54 characters\)
sed "s/^X//" >'Imakefile' <<'END_OF_FILE'
XLOCAL_LIBRARIES = $(XLIB)
X
XSimpleProgramTarget(maze)
X
END_OF_FILE
if test 54 -ne `wc -c <'Imakefile'`; then
    echo shar: \"'Imakefile'\" unpacked with wrong size!
fi
# end of 'Imakefile'
fi
if test -f 'Makefile' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'Makefile'\"
else
echo shar: Extracting \"'Makefile'\" \(4164 characters\)
sed "s/^X//" >'Makefile' <<'END_OF_FILE'
X#
X# Warning: the cpp used on this machine replaces
X# all newlines and multiple tabs/spaces in a macro
X# expansion with a single space.  Imake tries to
X# compensate for this, but is not always
X# successful.
X#
X
X#
X# This makefile is automatically generated by imake... do not modify
X# or you may lose your changes when imake generates makefiles again.
X# Ignore this message if you are not using imake.
X#
X
X            TOP = /usr/new/src/x/x11.2
X             AS = as
X             CC = /bin/cc
X
X            CPP = /lib/cpp
X
X             LD = ld
X           LINT = lint
X        INSTALL = install
X           TAGS = ctags
X             RM = rm -f
X             MV = mv
X             LN = ln -s
X         RANLIB = ranlib
X             AR = ar clq
X             LS = ls
X       LINTOPTS = -axz
X    LINTLIBFLAG = -C
X           MAKE = make
X    STD_DEFINES =
X    CDEBUGFLAGS = -O
X
X        DESTDIR = /usr/new
X
X        PATHSEP = /
X         DEPEND = $(DEPENDSRC)/makedepend
X          IMAKE = $(IMAKESRC)/imake
X            RGB = $(RGBSRC)/rgb
X         CFLAGS = $(CDEBUGFLAGS) $(INCLUDES) $(STD_DEFINES) $(DEFINES)
X       CPPFLAGS = $(INCLUDES) $(STD_DEFINES) $(DEFINES)
X      LINTFLAGS = $(LINTOPTS) $(INCLUDES) $(STD_DEFINES) $(DEFINES) -DLINT
X        LDFLAGS = $(CDEBUGFLAGS) $(SYS_LIBRARIES) $(SYSAUX_LIBRARIES)
X   INSTUIDFLAGS = -m 4755
X   INSTLIBFLAGS = -m 0664
X   INSTINCFLAGS = -m 0444
X   INSTMANFLAGS = -m 0444
X   INSTAPPFLAGS = -m 0444
X
X      USRLIBDIR = $(DESTDIR)/usr/lib
X         BINDIR = $(DESTDIR)/usr/bin/X11
X         LIBDIR = $(USRLIBDIR)/X11
X     LINTLIBDIR = $(USRLIBDIR)/lint
X         INCDIR = $(DESTDIR)/usr/include/X11
X        FONTDIR = $(LIBDIR)/fonts
X         AWMDIR = $(LIBDIR)/awm
X         TWMDIR = $(LIBDIR)/twm
X         UWMDIR = $(LIBDIR)/uwm
X         MANDIR = /usr/man/mann
X    XAPPLOADDIR = $(LIBDIR)/app-defaults
X         ADMDIR = $(DESTDIR)/usr/adm
X
X      CLIENTSRC = $(TOP)/clients
X        DEMOSRC = $(TOP)/demos
X         LIBSRC = $(TOP)/lib
X        FONTSRC = $(TOP)/fonts
X     INCLUDESRC = $(TOP)/X11
X      SERVERSRC = $(TOP)/server
X        UTILSRC = $(TOP)/util
X     EXAMPLESRC = $(TOP)/examples
X     CONTRIBSRC = $(TOP)/contrib
X         DOCSRC = $(TOP)/doc
X      DEPENDSRC = $(UTILSRC)/makedepend
X       IMAKESRC = $(UTILSRC)/imake
X       IRULESRC = $(UTILSRC)/imake.includes
X         RGBSRC = $(UTILSRC)/rgb
X        XLIBSRC = $(LIBSRC)/X
X     TOOLKITSRC = $(LIBSRC)/Xt
X     AWIDGETSRC = $(LIBSRC)/Xaw
X    HPWIDGETSRC = $(CONTRIBSRC)/Xray/src
X     OLDXLIBSRC = $(LIBSRC)/oldX
X   EXTENSIONSRC = $(TOP)/extensions
X        XMANSRC = $(DOCSRC)/Xlib/Xman
X   EXTENSIONLIB = $(EXTENSIONSRC)/lib/libXext.a
X           XLIB = $(USRLIBDIR)/libX11.a
X        OLDXLIB = $(OLDXLIBSRC)/liboldX.a
X       XTOOLLIB = $(TOOLKITSRC)/libXt.a
X         XAWLIB = $(AWIDGETSRC)/libXaw.a
X          XWLIB = $(HPWIDGETSRC)/libXw.a
X       LINTXLIB = $(XLIBSRC)/llib-lX11.ln
X      LINTXTOOL = $(TOOLKITSRC)/llib-lXt.ln
X        LINTXAW = $(AWIDGETSRC)/llib-lXaw.ln
X       INCLUDES = -I/usr/new/usr/include/X11
X      MACROFILE = Sun.macros
X      IMAKE_CMD = $(NEWTOP)$(IMAKE) -TImake.tmpl \
X			-I$(NEWTOP)$(IRULESRC) \
X			-s Makefile
X         RM_CMD = $(RM) *.CKP *.ln *.BAK *.bak *.o core errs ,* *~ *.a \
X			tags TAGS make.log
X
XLOCAL_LIBRARIES = $(XLIB)
X
X OBJS = maze.o
X SRCS = maze.c
X
X PROGRAM = maze
X
Xall:: maze
X
Xmaze: $(OBJS) $(LOCAL_LIBRARIES)
X	$(RM) $@
X	$(CC) -o $@ $(OBJS) $(LOCAL_LIBRARIES) $(LDFLAGS)
X
Xrelink::
X	$(RM) $(PROGRAM)
X	$(MAKE) $(MFLAGS) $(PROGRAM)
X
Xinstall:: maze
X	$(INSTALL) -c $(INSTALLFLAGS) maze $(BINDIR)
X
Xinstall:: maze.man
X	$(INSTALL) -c $(INSTMANFLAGS) maze.man $(MANDIR)/maze.n
X
Xdepend:: $(DEPEND)
X
Xdepend::
X	$(DEPEND) -s "# DO NOT DELETE" -- $(CPPFLAGS) -- $(SRCS)
X
X$(DEPEND):
X	@echo "making $(DEPENDSRC)"; \
X	cd $(DEPENDSRC); $(MAKE)
X
Xclean::
X	$(RM) $(PROGRAM)
X
Xclean::
X	$(RM_CMD) \#*
X
XMakefile:: $(IMAKE)
X
XMakefile:: Imakefile \
X	$(IRULESRC)/Imake.tmpl \
X	$(IRULESRC)/Imake.rules \
X	$(IRULESRC)/$(MACROFILE)
X	-$(RM) Makefile.bak; $(MV) Makefile Makefile.bak
X	$(IMAKE_CMD) -DTOPDIR=$(TOP)
X
X$(IMAKE):
X	@echo "making $(IMAKESRC)"; \
X	cd $(IMAKESRC); $(MAKE)
X
Xtags::
X	$(TAGS) -w *.[ch]
X	$(TAGS) -xw *.[ch] > TAGS
X
Xinstall::
X	@echo "install done"
X
XMakefiles::
X
END_OF_FILE
if test 4164 -ne `wc -c <'Makefile'`; then
    echo shar: \"'Makefile'\" unpacked with wrong size!
fi
# end of 'Makefile'
fi
if test -f 'logo.xbm' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'logo.xbm'\"
else
echo shar: Extracting \"'logo.xbm'\" \(3275 characters\)
sed "s/^X//" >'logo.xbm' <<'END_OF_FILE'
X#define logo_width 64
X#define logo_height 64
Xstatic char logo_bits[] = {
X   0xff, 0xff, 0xff, 0x3f, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f,
X   0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0xe0, 0xff, 0xff, 0xff,
X   0xff, 0xff, 0xff, 0x03, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x03,
X   0x80, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0xff, 0xff, 0xff,
X   0xff, 0xff, 0xff, 0x81, 0x00, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xbf, 0x01,
X   0x01, 0xfc, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x03, 0x02, 0xf8, 0xff, 0xff,
X   0xff, 0xff, 0x0f, 0x02, 0x04, 0xf0, 0xff, 0xff, 0xff, 0xff, 0x07, 0x04,
X   0x08, 0xe0, 0xff, 0xff, 0xff, 0xff, 0x0b, 0x08, 0x10, 0xc0, 0xff, 0xff,
X   0xff, 0xff, 0x11, 0x10, 0x20, 0x80, 0xff, 0xff, 0xff, 0xff, 0x20, 0x20,
X   0x40, 0x00, 0xff, 0xff, 0xff, 0x7f, 0x40, 0x40, 0x80, 0x00, 0xff, 0xff,
X   0xff, 0x7f, 0x80, 0x80, 0x00, 0x81, 0xfc, 0xff, 0xff, 0x9f, 0x00, 0x01,
X   0x01, 0x42, 0xf8, 0xff, 0xff, 0x0f, 0x01, 0x02, 0x02, 0x24, 0xf0, 0xff,
X   0xff, 0x07, 0x02, 0x04, 0x04, 0x18, 0xe0, 0xff, 0xff, 0x03, 0x04, 0x08,
X   0x08, 0x08, 0xd0, 0xff, 0xff, 0x01, 0x0a, 0x10, 0x10, 0x04, 0x88, 0xff,
X   0xff, 0x00, 0x11, 0x20, 0x30, 0x02, 0x04, 0xff, 0x7f, 0x80, 0x20, 0x40,
X   0x60, 0x01, 0x02, 0xfe, 0x3f, 0x40, 0x40, 0x00, 0xe0, 0x00, 0x01, 0xff,
X   0x1f, 0x20, 0xe0, 0x00, 0x60, 0x80, 0x80, 0xff, 0x0f, 0x10, 0xf0, 0x01,
X   0x30, 0x40, 0xc0, 0xf8, 0x07, 0x08, 0x18, 0x03, 0x10, 0x20, 0x20, 0xe0,
X   0x03, 0x04, 0x04, 0x04, 0x08, 0x10, 0x10, 0xc0, 0x01, 0x02, 0x02, 0x18,
X   0x06, 0x08, 0x08, 0x80, 0x01, 0x01, 0x01, 0xf8, 0x07, 0x04, 0x04, 0x80,
X   0x80, 0x80, 0x00, 0xf0, 0x03, 0x02, 0x02, 0x00, 0x40, 0x40, 0x00, 0xf0,
X   0x03, 0x01, 0x01, 0x00, 0x00, 0x20, 0x20, 0xf0, 0x03, 0x80, 0x00, 0x01,
X   0x00, 0x10, 0x10, 0xf0, 0x03, 0x40, 0x80, 0x00, 0x01, 0x08, 0x08, 0xf8,
X   0x07, 0x20, 0x40, 0x80, 0x01, 0x04, 0x04, 0x18, 0x06, 0x10, 0x20, 0x80,
X   0x03, 0x02, 0x02, 0x04, 0x08, 0x08, 0x10, 0xc0, 0x8f, 0x01, 0x01, 0x02,
X   0x30, 0x06, 0x08, 0xe0, 0xff, 0x80, 0x00, 0x03, 0xe0, 0x03, 0x04, 0xf0,
X   0x7f, 0x40, 0x80, 0x01, 0xc0, 0x01, 0x02, 0xf8, 0x3f, 0x20, 0xc0, 0x01,
X   0x80, 0x00, 0x01, 0xfc, 0x7f, 0x10, 0xa0, 0x81, 0x00, 0x81, 0x00, 0xfe,
X   0xff, 0x08, 0x10, 0x03, 0x01, 0x42, 0x00, 0xff, 0xff, 0x05, 0x08, 0x02,
X   0x02, 0x24, 0x80, 0xff, 0xff, 0x03, 0x04, 0x04, 0x04, 0x18, 0xc0, 0xff,
X   0xff, 0x07, 0x0a, 0x08, 0x08, 0x10, 0xe0, 0xff, 0xff, 0x0f, 0x11, 0x10,
X   0x10, 0x20, 0xf0, 0xff, 0xff, 0x9f, 0x20, 0x20, 0x20, 0x40, 0xf8, 0xff,
X   0xff, 0x7f, 0x40, 0x40, 0x40, 0x80, 0xfc, 0xff, 0xff, 0x7f, 0x80, 0x80,
X   0x80, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x01, 0x01, 0x01, 0xff, 0xff,
X   0xff, 0xff, 0x01, 0x02, 0x02, 0x82, 0xff, 0xff, 0xff, 0xff, 0x03, 0x04,
X   0x04, 0xc4, 0xff, 0xff, 0xff, 0xff, 0x07, 0x08, 0x08, 0xe8, 0xff, 0xff,
X   0xff, 0xff, 0x0f, 0x10, 0x10, 0xf0, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x20,
X   0x20, 0xf8, 0xff, 0xff, 0xff, 0xff, 0x3f, 0x40, 0x60, 0xfc, 0xff, 0xff,
X   0xff, 0xff, 0x7f, 0x80, 0xc0, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
X   0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0xc0, 0xff, 0xff, 0xff,
X   0xff, 0xff, 0xff, 0x03, 0xe0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07,
X   0xe0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0xf0, 0xff, 0xff, 0xff,
X   0xff, 0xff, 0xff, 0x3f, 0xfc, 0xff, 0xff, 0xff};
END_OF_FILE
if test 3275 -ne `wc -c <'logo.xbm'`; then
    echo shar: \"'logo.xbm'\" unpacked with wrong size!
fi
# end of 'logo.xbm'
fi
if test -f 'maze.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'maze.c'\"
else
echo shar: Extracting \"'maze.c'\" \(18555 characters\)
sed "s/^X//" >'maze.c' <<'END_OF_FILE'
X/******************************************************************************
X * [ maze ] ...
X *
X * modified:  [ 10-4-88 ]  Richard Hess    ...!uunet!cimshop!rhess  
X *              [ Revised primary execution loop within main()...
X *              [ Extended X event handler, check_events()...
X * modified:  [ 1-29-88 ]  Dave Lemke      lemke at sun.com  
X *              [ Hacked for X11...
X *              [  Note the word "hacked" -- this is extremely ugly, but at 
X *              [   least it does the job.  NOT a good programming example 
X *              [   for X.
X * original:  [ 6/21/85 ]  Martin Weiss    Sun Microsystems  [ SunView ]
X *
X ******************************************************************************
X Copyright 1988 by Sun Microsystems, Inc. Mountain View, CA.
X  
X All Rights Reserved
X  
X Permission to use, copy, modify, and distribute this software and its
X documentation for any purpose and without fee is hereby granted, 
X provided that the above copyright notice appear in all copies and that
X both that copyright notice and this permission notice appear in 
X supporting documentation, and that the names of Sun or MIT not be
X used in advertising or publicity pertaining to distribution of the
X software without specific prior written permission. Sun and M.I.T. 
X make no representations about the suitability of this software for 
X any purpose. It is provided "as is" without any express or implied warranty.
X 
X SUN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
X ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
X PURPOSE. IN NO EVENT SHALL SUN BE LIABLE FOR ANY SPECIAL, INDIRECT
X OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
X OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 
X OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
X OR PERFORMANCE OF THIS SOFTWARE.
X *****************************************************************************/
X
X#include  <sys/types.h>
X#include  <stdio.h>
X#include  <X11/Xlib.h>
X#include  <X11/Xutil.h>
X
X#define LOGOSIZE	7
X#define MIN_MAZE_SIZE	3
X#define MAX_MAZE_SIZE_X	205
X#define MAX_MAZE_SIZE_Y	205
X
X#define MOVE_LIST_SIZE  (MAX_MAZE_SIZE_X * MAX_MAZE_SIZE_Y)
X  
X#define WALL_TOP	0x8000
X#define WALL_RIGHT	0x4000
X#define WALL_BOTTOM	0x2000
X#define WALL_LEFT	0x1000
X  
X#define DOOR_IN_TOP	0x800
X#define DOOR_IN_RIGHT	0x400
X#define DOOR_IN_BOTTOM	0x200
X#define DOOR_IN_LEFT	0x100
X#define DOOR_IN_ANY	0xF00
X  
X#define DOOR_OUT_TOP	0x80
X#define DOOR_OUT_RIGHT	0x40
X#define DOOR_OUT_BOTTOM	0x20
X#define DOOR_OUT_LEFT	0x10
X  
X#define START_SQUARE	0x2
X#define END_SQUARE	0x1
X  
X#define SQ_SIZE_X	10
X#define SQ_SIZE_Y       10
X  
X#define NUM_RANDOM      100
X  
X#define	BORDERWIDTH     2
X#define	border_x        (0)
X#define	border_y        (0)
X#define	MIN_W	        200
X#define	MIN_H	        200
X#define	DEF_W	        500
X#define	DEF_H	        500
X  
X#define	get_random(x)	(random() % (x))
X  
Xstatic int logo_x, logo_y;
X
X#include  "logo.xbm"
X
Xstatic long int randnum[NUM_RANDOM];
Xstatic u_short maze[MAX_MAZE_SIZE_X][MAX_MAZE_SIZE_Y];
X
Xstatic struct {
X  u_char x;
X  u_char y;
X  u_char dir;
X} move_list[MOVE_LIST_SIZE], save_path[MOVE_LIST_SIZE], path[MOVE_LIST_SIZE];
X
Xstatic int maze_size_x, maze_size_y;
Xstatic int sqnum, cur_sq_x, cur_sq_y, path_length;
Xstatic int start_x, start_y, start_dir, end_x, end_y, end_dir;
Xstatic int maze_restart_flag, random_index;
X
XDisplay	*dpy;
XWindow	win;
XWindow	iwin;
XGC	gc, cgc;
XXWindowAttributes win_attr;
Xint	screen;
Xchar	*defgeo = "=500x500+10+10" ;
Xlong	background;
XPixmap	logo_map;
Xint	reverse = 0;
X
Xint	width = DEF_W, height = DEF_H ;
Xint	x = 10, y = 10, restart = 0, stop = 1, state = 1;
X
X
Xmain(argc,argv)                                               /* main module */
X     int argc;
X     char **argv;
X{
X  extern int	optind;
X  extern char	*optarg;
X  char	*display = NULL;
X  char	*geo = NULL;
X  char	*cmd;
X  char	c;
X  extern char	*getenv();
X  int	screen_saver = 0;
X  Pixmap	backmap, bdrmap;
X  XSizeHints size_hints;
X  XWindowAttributes	wind_info;
X
X  cmd = argv[0];
X  while ((c = getopt(argc, argv, "rSd:g:")) != EOF)
X    switch(c)	{
X      
X    case 'S':
X      screen_saver = 1;
X      break;
X    case 'd':
X      display = optarg;
X      break;
X    case 'g':
X      geo = optarg;
X      break;
X    case 'r':
X      reverse = 1;
X      break;
X    case '?':
X      usage(cmd);
X      exit(0);
X    }
X  
X  if ((dpy = XOpenDisplay(display)) == NULL)	{
X    fprintf(stderr, "Can\'t open display: %s\n",
X	    (display ? display : getenv("DISPLAY")));
X    exit(0);
X  }
X  screen = DefaultScreen(dpy);
X  
X  if (screen_saver)	{
X    width = DisplayWidth(dpy, screen) - 2 * BORDERWIDTH;
X    height = DisplayHeight(dpy, screen) - 2 * BORDERWIDTH;
X    x = 0; y = 0;
X  }
X  else	{
X    int flags;
X    
X    if (geo == NULL)
X      geo = defgeo;
X    flags = XParseGeometry(geo, &x, &y, &width, &height); 
X    if ((flags & XValue) && (flags & XNegative))
X      x += DisplayWidth(dpy, screen) - width;
X    if ((flags & YValue) && (flags & YNegative))
X      y += DisplayHeight(dpy, screen) - height;
X  }
X
X  if (reverse)
X    background = BlackPixel(dpy, screen) ;
X  else
X    background = WhitePixel(dpy, screen) ;
X
X  win = XCreateSimpleWindow(dpy, RootWindow(dpy, screen), x, y, width,
X			    height, BORDERWIDTH, 1, background );
X  
X  set_maze_sizes(width, height);
X  XSelectInput(dpy, win,
X	       ExposureMask | ButtonPressMask | StructureNotifyMask );
X  
X  gc = XCreateGC(dpy, win, 0, 0);
X  cgc = XCreateGC(dpy, win, 0, 0);
X  
X  if (reverse)	{
X      XSetForeground(dpy, gc, WhitePixel(dpy, screen));
X      XSetBackground(dpy, gc, BlackPixel(dpy, screen));
X      XSetForeground(dpy, cgc, BlackPixel(dpy, screen)); 
X      XSetBackground(dpy, cgc, WhitePixel(dpy, screen)); 
X      XSetWindowBackground(dpy, win, BlackPixel(dpy, screen));
X  }
X  else {
X      XSetForeground(dpy, gc, BlackPixel(dpy, screen));
X      XSetBackground(dpy, gc, WhitePixel(dpy, screen));
X      XSetForeground(dpy, cgc, WhitePixel(dpy, screen)); 
X      XSetBackground(dpy, cgc, BlackPixel(dpy, screen)); 
X      XSetWindowBackground(dpy, win, WhitePixel(dpy, screen)); 
X    }
X  
X  if  (!(logo_map = XCreateBitmapFromData(dpy, win, logo_bits,
X					  logo_width, logo_height))) {
X    fprintf(stderr, "Can't create logo pixmap\n");
X    exit (1);
X  }
X  size_hints.flags =  USPosition | USSize | PMinSize ;
X  size_hints.x = x;
X  size_hints.y = y;
X  size_hints.width = width;
X  size_hints.height = height;
X  size_hints.min_width = MIN_W;
X  size_hints.min_height = MIN_H;
X  
X  XSetStandardProperties(dpy, win, "Xmaze", "Xmaze", logo_map,
X			 argv, argc, &size_hints);
X  XMapWindow(dpy, win);
X  srandom(getpid());
X
X  while (1) {                            /* primary execution loop [ rhess ] */
X    if (check_events()) continue ;
X    if (restart || stop) goto pop;
X    switch (state) {
X    case 1:
X      initialize_maze();
X      break;
X    case 2:
X      XClearWindow(dpy, win);
X      draw_maze_border();
X      break;
X    case 3:
X      create_maze();
X      break;
X    case 4:
X      XFlush(dpy);
X      sleep(2);
X      break;
X    case 5:
X      solve_maze();
X      break;
X    default:
X      XFlush(dpy) ;
X      sleep(4) ;
X      state = 0 ;
X      break;
X    }
X    state = ++state ;
X  pop:
X    if (restart) {
X      restart = 0 ;
X      stop = 0 ;
X      state = 1 ;
X      XGetWindowAttributes(dpy, win, &win_attr);
X      width = win_attr.width ;
X      height = win_attr.height ;
X      set_maze_sizes(width, height);
X      XClearWindow(dpy, win);
X      XFlush(dpy) ;
X    }
X  }
X}
X
X
Xcheck_events()                                  /* X event handler [ rhess ] */
X{
X  XEvent	e;
X
X  while (XPending(dpy))	{
X    XNextEvent(dpy, &e);
X    switch (e.type) {
X    case ButtonPress:
X      switch (e.xbutton.button) {
X      case 3:
X	XFreeGC(dpy, gc);
X	XFreeGC(dpy, cgc);
X	XDestroyWindow(dpy, win);
X	XCloseDisplay(dpy);
X	exit(0);
X	break;
X      case 2:
X	stop = !stop ;
X	if (state == 5) state = 4 ;
X	else {
X	  restart = 1;
X	  stop = 0;
X	}
X	break;
X      default:
X	restart = 1 ;
X	stop = 0 ;
X	break;
X      }
X      break;
X    case ConfigureNotify:
X      restart = 1;
X      break;
X    case UnmapNotify:
X      stop = 1;
X      XClearWindow(dpy, win);
X      XFlush(dpy);
X      break;
X    case Expose:
X      restart = 1;
X      break;
X    }
X    return(1);
X  }
X  return(0);
X}	  
X
X
Xusage(cmd)
X     char	*cmd;
X{
X  fprintf(stderr, "usage: %s -S -r [-g geometry] [-d display]\n", cmd);
X}
X
X
Xset_maze_sizes(width, height)
X{
X  maze_size_x = width / SQ_SIZE_X;
X  maze_size_y = height / SQ_SIZE_Y;
X  
X}
X
X
Xinitialize_maze()         /* draw the surrounding wall and start/end squares */
X{
X  register int i, j, wall;
X  
X  /* initialize all squares */
X  for ( i=0; i<maze_size_x; i++) {
X    for ( j=0; j<maze_size_y; j++) {
X      maze[i][j] = 0;
X    }
X  }
X  
X  /* top wall */
X  for ( i=0; i<maze_size_x; i++ ) {
X    maze[i][0] |= WALL_TOP;
X  }
X  
X  /* right wall */
X  for ( j=0; j<maze_size_y; j++ ) {
X    maze[maze_size_x-1][j] |= WALL_RIGHT;
X  }
X  
X  /* bottom wall */
X  for ( i=0; i<maze_size_x; i++ ) {
X    maze[i][maze_size_y-1] |= WALL_BOTTOM;
X  }
X  
X  /* left wall */
X  for ( j=0; j<maze_size_y; j++ ) {
X    maze[0][j] |= WALL_LEFT;
X  }
X  
X  /* set start square */
X  wall = get_random(4);
X  switch (wall) {
X  case 0:	
X    i = get_random(maze_size_x);
X    j = 0;
X    break;
X  case 1:	
X    i = maze_size_x - 1;
X    j = get_random(maze_size_y);
X    break;
X  case 2:	
X    i = get_random(maze_size_x);
X    j = maze_size_y - 1;
X    break;
X  case 3:	
X    i = 0;
X    j = get_random(maze_size_y);
X    break;
X  }
X  maze[i][j] |= START_SQUARE;
X  maze[i][j] |= ( DOOR_IN_TOP >> wall );
X  maze[i][j] &= ~( WALL_TOP >> wall );
X  cur_sq_x = i;
X  cur_sq_y = j;
X  start_x = i;
X  start_y = j;
X  start_dir = wall;
X  sqnum = 0;
X  
X  /* set end square */
X  wall = (wall + 2)%4;
X  switch (wall) {
X  case 0:
X    i = get_random(maze_size_x);
X    j = 0;
X    break;
X  case 1:
X    i = maze_size_x - 1;
X    j = get_random(maze_size_y);
X    break;
X  case 2:
X    i = get_random(maze_size_x);
X    j = maze_size_y - 1;
X    break;
X  case 3:
X    i = 0;
X    j = get_random(maze_size_y);
X    break;
X  }
X  maze[i][j] |= END_SQUARE;
X  maze[i][j] |= ( DOOR_OUT_TOP >> wall );
X  maze[i][j] &= ~( WALL_TOP >> wall );
X  end_x = i;
X  end_y = j;
X  end_dir = wall;
X  
X  /* set logo */
X  if ( (maze_size_x > 15) && (maze_size_y > 15) ) {
X    logo_x = get_random(maze_size_x - LOGOSIZE - 6) + 3;
X    logo_y = get_random(maze_size_y - LOGOSIZE - 6) + 3;
X    
X    for (i=0; i<LOGOSIZE; i++) {
X      for (j=0; j<LOGOSIZE; j++) {
X	maze[logo_x + i][logo_y + j] |= DOOR_IN_TOP;
X      }
X    }
X  }
X  else
X    logo_y = logo_x = -1;
X}
X
X
Xcreate_maze()             /* create a maze layout given the intiialized maze */
X{
X  register int i, newdoor;
X  
X  do {
X    move_list[sqnum].x = cur_sq_x;
X    move_list[sqnum].y = cur_sq_y;
X    move_list[sqnum].dir = newdoor;
X    while ( ( newdoor = choose_door() ) == -1 ) { /* pick a door */
X      if ( backup() == -1 ) { /* no more doors ... backup */
X	return; /* done ... return */
X      }
X    }
X    
X    /* mark the out door */
X    maze[cur_sq_x][cur_sq_y] |= ( DOOR_OUT_TOP >> newdoor );
X    
X    switch (newdoor) {
X    case 0: cur_sq_y--;
X      break;
X    case 1: cur_sq_x++;
X      break;
X    case 2: cur_sq_y++;
X      break;
X    case 3: cur_sq_x--;
X      break;
X    }
X    sqnum++;
X    
X    /* mark the in door */
X    maze[cur_sq_x][cur_sq_y] |= ( DOOR_IN_TOP >> ((newdoor+2)%4) );
X    
X    /* if end square set path length and save path */
X    if ( maze[cur_sq_x][cur_sq_y] & END_SQUARE ) {
X      path_length = sqnum;
X      for ( i=0; i<path_length; i++) {
X	save_path[i].x = move_list[i].x;
X	save_path[i].y = move_list[i].y;
X	save_path[i].dir = move_list[i].dir;
X      }
X    }
X    
X  } while (1);
X  
X}
X
X
Xchoose_door()                                            /* pick a new path */
X{
X  int candidates[3];
X  register int num_candidates;
X  
X  num_candidates = 0;
X  
X topwall:
X  /* top wall */
X  if ( maze[cur_sq_x][cur_sq_y] & DOOR_IN_TOP )
X    goto rightwall;
X  if ( maze[cur_sq_x][cur_sq_y] & DOOR_OUT_TOP )
X    goto rightwall;
X  if ( maze[cur_sq_x][cur_sq_y] & WALL_TOP )
X    goto rightwall;
X  if ( maze[cur_sq_x][cur_sq_y - 1] & DOOR_IN_ANY ) {
X    maze[cur_sq_x][cur_sq_y] |= WALL_TOP;
X    maze[cur_sq_x][cur_sq_y - 1] |= WALL_BOTTOM;
X    draw_wall(cur_sq_x, cur_sq_y, 0);
X    goto rightwall;
X  }
X  candidates[num_candidates++] = 0;
X  
X rightwall:
X  /* right wall */
X  if ( maze[cur_sq_x][cur_sq_y] & DOOR_IN_RIGHT )
X    goto bottomwall;
X  if ( maze[cur_sq_x][cur_sq_y] & DOOR_OUT_RIGHT )
X    goto bottomwall;
X  if ( maze[cur_sq_x][cur_sq_y] & WALL_RIGHT )
X    goto bottomwall;
X  if ( maze[cur_sq_x + 1][cur_sq_y] & DOOR_IN_ANY ) {
X    maze[cur_sq_x][cur_sq_y] |= WALL_RIGHT;
X    maze[cur_sq_x + 1][cur_sq_y] |= WALL_LEFT;
X    draw_wall(cur_sq_x, cur_sq_y, 1);
X    goto bottomwall;
X  }
X  candidates[num_candidates++] = 1;
X  
X bottomwall:
X  /* bottom wall */
X  if ( maze[cur_sq_x][cur_sq_y] & DOOR_IN_BOTTOM )
X    goto leftwall;
X  if ( maze[cur_sq_x][cur_sq_y] & DOOR_OUT_BOTTOM )
X    goto leftwall;
X  if ( maze[cur_sq_x][cur_sq_y] & WALL_BOTTOM )
X    goto leftwall;
X  if ( maze[cur_sq_x][cur_sq_y + 1] & DOOR_IN_ANY ) {
X    maze[cur_sq_x][cur_sq_y] |= WALL_BOTTOM;
X    maze[cur_sq_x][cur_sq_y + 1] |= WALL_TOP;
X    draw_wall(cur_sq_x, cur_sq_y, 2);
X    goto leftwall;
X  }
X  candidates[num_candidates++] = 2;
X  
X leftwall:
X  /* left wall */
X  if ( maze[cur_sq_x][cur_sq_y] & DOOR_IN_LEFT )
X    goto donewall;
X  if ( maze[cur_sq_x][cur_sq_y] & DOOR_OUT_LEFT )
X    goto donewall;
X  if ( maze[cur_sq_x][cur_sq_y] & WALL_LEFT )
X    goto donewall;
X  if ( maze[cur_sq_x - 1][cur_sq_y] & DOOR_IN_ANY ) {
X    maze[cur_sq_x][cur_sq_y] |= WALL_LEFT;
X    maze[cur_sq_x - 1][cur_sq_y] |= WALL_RIGHT;
X    draw_wall(cur_sq_x, cur_sq_y, 3);
X    goto donewall;
X  }
X  candidates[num_candidates++] = 3;
X  
X donewall:
X  if (num_candidates == 0)
X    return ( -1 );
X  if (num_candidates == 1)
X    return ( candidates[0] );
X  return ( candidates[ get_random(num_candidates) ] );
X  
X}
X
X
Xbackup()                                                  /* back up a move */
X{
X  sqnum--;
X  cur_sq_x = move_list[sqnum].x;
X  cur_sq_y = move_list[sqnum].y;
X  return ( sqnum );
X}
X
X
Xdraw_maze_border()                                  /* draw the maze outline */
X{
X  register int i, j;
X  
X  
X  for ( i=0; i<maze_size_x; i++) {
X    if ( maze[i][0] & WALL_TOP ) {
X      XDrawLine(dpy, win, gc,
X		border_x + SQ_SIZE_X * i,
X		border_y,
X		border_x + SQ_SIZE_X * (i+1),
X		border_y);
X    }
X    if ((maze[i][maze_size_y - 1] & WALL_BOTTOM)) {
X      XDrawLine(dpy, win, gc,
X		border_x + SQ_SIZE_X * i,
X		border_y + SQ_SIZE_Y * (maze_size_y),
X		border_x + SQ_SIZE_X * (i+1),
X		border_y + SQ_SIZE_Y * (maze_size_y));
X    }
X  }
X  for ( j=0; j<maze_size_y; j++) {
X    if ( maze[maze_size_x - 1][j] & WALL_RIGHT ) {
X      XDrawLine(dpy, win, gc,
X		border_x + SQ_SIZE_X * maze_size_x,
X		border_y + SQ_SIZE_Y * j,
X		border_x + SQ_SIZE_X * maze_size_x,
X		border_y + SQ_SIZE_Y * (j+1));
X    }
X    if ( maze[0][j] & WALL_LEFT ) {
X      XDrawLine(dpy, win, gc,
X		border_x,
X		border_y + SQ_SIZE_Y * j,
X		border_x,
X		border_y + SQ_SIZE_Y * (j+1));
X    }
X  }
X  
X  if (logo_x != -1) {
X    XCopyPlane(dpy, logo_map, win, gc,
X	       0, 0, logo_width, logo_height,
X	       border_x + 3 + SQ_SIZE_X * logo_x,
X	       border_y + 3 + SQ_SIZE_Y * logo_y, 1);
X  }
X  
X  draw_solid_square( start_x, start_y, start_dir, gc);
X  draw_solid_square( end_x, end_y, end_dir, gc);
X}
X
X
Xdraw_wall(i, j, dir)                                   /* draw a single wall */
X     int i, j, dir;
X{
X  switch (dir) {
X  case 0:
X    XDrawLine(dpy, win, gc,
X	      border_x + SQ_SIZE_X * i, 
X	      border_y + SQ_SIZE_Y * j,
X	      border_x + SQ_SIZE_X * (i+1), 
X	      border_y + SQ_SIZE_Y * j);
X    break;
X  case 1:
X    XDrawLine(dpy, win, gc,
X	      border_x + SQ_SIZE_X * (i+1), 
X	      border_y + SQ_SIZE_Y * j,
X	      border_x + SQ_SIZE_X * (i+1), 
X	      border_y + SQ_SIZE_Y * (j+1));
X    break;
X  case 2:
X    XDrawLine(dpy, win, gc,
X	      border_x + SQ_SIZE_X * i, 
X	      border_y + SQ_SIZE_Y * (j+1),
X	      border_x + SQ_SIZE_X * (i+1), 
X	      border_y + SQ_SIZE_Y * (j+1));
X    break;
X  case 3:
X    XDrawLine(dpy, win, gc,
X	      border_x + SQ_SIZE_X * i, 
X	      border_y + SQ_SIZE_Y * j,
X	      border_x + SQ_SIZE_X * i, 
X	      border_y + SQ_SIZE_Y * (j+1));
X    break;
X  }
X}
X
X
Xdraw_solid_square(i, j, dir, gc)          /* draw a solid square in a square */
X     register int i, j, dir;
X     GC	gc;
X{
X  switch (dir) {
X  case 0: XFillRectangle(dpy, win, gc,
X			 border_x + 3 + SQ_SIZE_X * i, 
X			 border_y - 3 + SQ_SIZE_Y * j, 
X			 SQ_SIZE_X - 6, SQ_SIZE_Y);
X    break;
X  case 1: XFillRectangle(dpy, win, gc,
X			 border_x + 3 + SQ_SIZE_X * i, 
X			 border_y + 3 + SQ_SIZE_Y * j, 
X			 SQ_SIZE_X, SQ_SIZE_Y - 6);
X    break;
X  case 2: XFillRectangle(dpy, win, gc,
X			 border_x + 3 + SQ_SIZE_X * i, 
X			 border_y + 3 + SQ_SIZE_Y * j, 
X			 SQ_SIZE_X - 6, SQ_SIZE_Y);
X    break;
X  case 3: XFillRectangle(dpy, win, gc,
X			 border_x - 3 + SQ_SIZE_X * i, 
X			 border_y + 3 + SQ_SIZE_Y * j, 
X			 SQ_SIZE_X, SQ_SIZE_Y - 6);
X    break;
X  }
X  XFlush(dpy);
X#ifdef	notdef
X  (void) check_events();
X#endif
X}
X
X
Xsolve_maze()                             /* solve it with graphical feedback */
X{
X  int i;
X  
X  
X  /* plug up the surrounding wall */
X  maze[start_x][start_y] |= (WALL_TOP >> start_dir);
X  maze[end_x][end_y] |= (WALL_TOP >> end_dir);
X  
X  /* initialize search path */
X  i = 0;
X  path[i].x = end_x;
X  path[i].y = end_y;
X  path[i].dir = -1;
X  
X  /* do it */
X  while (1) {
X    if ( ++path[i].dir >= 4 ) {
X      i--;
X      draw_solid_square( (int)(path[i].x), (int)(path[i].y), 
X			(int)(path[i].dir), cgc);
X    }
X    else if ( ! (maze[path[i].x][path[i].y] & 
X		 (WALL_TOP >> path[i].dir))  && 
X	     ( (i == 0) || ( (path[i].dir != 
X			      (path[i-1].dir+2)%4) ) ) ) {
X      enter_square(i);
X      i++;
X      if ( maze[path[i].x][path[i].y] & START_SQUARE ) {
X	return;
X      }
X    } 
X    if (check_events()) return;
X    /* Abort solve on expose - cheapo repaint strategy */
X  }
X} 
X
X
Xenter_square(n)                            /* move into a neighboring square */
X     int n;
X{
X  draw_solid_square( (int)path[n].x, (int)path[n].y, 
X		    (int)path[n].dir, gc);
X  
X  path[n+1].dir = -1;
X  switch (path[n].dir) {
X  case 0: path[n+1].x = path[n].x;
X    path[n+1].y = path[n].y - 1;
X    break;
X  case 1: path[n+1].x = path[n].x + 1;
X    path[n+1].y = path[n].y;
X    break;
X  case 2: path[n+1].x = path[n].x;
X    path[n+1].y = path[n].y + 1;
X    break;
X  case 3: path[n+1].x = path[n].x - 1;
X    path[n+1].y = path[n].y;
X    break;
X  }
X}
X
X/* ----<eof> */
END_OF_FILE
if test 18555 -ne `wc -c <'maze.c'`; then
    echo shar: \"'maze.c'\" unpacked with wrong size!
fi
# end of 'maze.c'
fi
if test -f 'maze.man' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'maze.man'\"
else
echo shar: Extracting \"'maze.man'\" \(2538 characters\)
sed "s/^X//" >'maze.man' <<'END_OF_FILE'
X.TH MAZE 1 "5 October 1988" "X Version 11"
X.SH NAME
Xmaze - an automated maze program... [ demo ][ X11 ]
X.SH SYNTAX
X.B
Xmaze 
X[
X.B \-S
X] [
X.B \-r
X] [
X.B \-g 
X.I geometry
X] [
X.B \-d 
X.I display
X]
X.PP
X.SH DESCRIPTION
XThe \fImaze\fP program creates a "random" maze and then solves it with 
Xgraphical feedback. 
X.sp 1
X.B Command Options
X.IP "\fB\-S\fP"
XFull screen window option...
X.IP "\fB\-r\fP"
XReverse video option...
X.IP "\fB\-g\fP \fIgeometry\fP"
XSpecifies the window geometry to be used...
X.IP "\fB\-d\fP \fIdisplay\fP"
XSpecifies the display to be used...
X.PP
XThe following lists the current functionality of various mouse button clicks;
X.IP "\fBLeftButton\fP"
XClears the window and restarts maze...
X.IP "\fB\MiddleButton\fP"
XToggles the maze program, 
Xfirst click -> \fIstop\fP, 
Xsecond click -> \fIcontinue\fP...
X.IP "\fB\RightButton\fP"
XKills maze...
X.PP 
X.SH LIMATIONS
XNo color support...
X.br
XExpose events force a restart of maze...
X.br
XCurrently, mouse actions are based on "raw" values [ Button1, Button2 and 
XButton3 ] from the ButtonPress event... 
X.br
X[ doesn't use pointer mapping ]
X.SH COPYRIGHT
X.PP
XCopyright 1988 by Sun Microsystems, Inc. Mountain View, CA.
X.PP  
XAll Rights Reserved
X.PP
XPermission to use, copy, modify, and distribute this software and its
Xdocumentation for any purpose and without fee is hereby granted, provided that
Xthe above copyright notice appear in all copies and that both that copyright
Xnotice and this permission notice appear in supporting documentation, and that
Xthe names of Sun or MIT not be used in advertising or publicity pertaining to
Xdistribution of the software without specific prior written permission. Sun
Xand M.I.T.  make no representations about the suitability of this software for
Xany purpose. It is provided "as is" without any express or implied warranty.
X.PP
XSUN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
XIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN
XNO EVENT SHALL SUN BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
XDAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
XWHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
XOUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
X.SH AUTHOR(s)
X.nf
XRichard Hess	[ X11 extensions ]  	{...}!uunet!cimshop!rhess
X  Consilium, Mountain View, CA
XDave Lemke	[ X11 version ]		lemke at sun.COM
X  Sun MicroSystems, Mountain View, CA
XMartin Weiss	[ SunView version ]
X  Sun MicroSystems, Mountain View, CA
X.fi
END_OF_FILE
if test 2538 -ne `wc -c <'maze.man'`; then
    echo shar: \"'maze.man'\" unpacked with wrong size!
fi
# end of 'maze.man'
fi
echo shar: End of archive 1 \(of 1\).
cp /dev/null ark1isdone
MISSING=""
for I in 1 ; do
    if test ! -f ark${I}isdone ; then
	MISSING="${MISSING} ${I}"
    fi
done
if test "${MISSING}" = "" ; then
    echo You have the archive.
    rm -f ark[1-9]isdone
else
    echo You still need to unpack the following archives:
    echo "        " ${MISSING}
fi
##  End of shell archive.
exit 0
-- 
Mike Wexler(wyse!mikew)    Phone: (408)433-1000 x1330
Moderator of comp.sources.x



More information about the Comp.sources.x mailing list