home *** CD-ROM | disk | FTP | other *** search
-
- // MAZE.SCR -- Generate and display a random maze -- Version 1.00
- //
- // Script program for MUL - the Maximus User Language
- // MUL is (C) Copyright 1990-93 by CodeLand Australia
- //
- // Based on code placed in the Public Domain by Jonathan Guthrie.
- //
- // USAGE: MUL -pMaze or
- // MUL -pMaze <Cols> <Rows>
-
- char *banner = "MAZE v1.00"; // Script banner
- char *aborts = "\nKeyboard ABORT\n"; // Keyboard abort
-
- int CSIZ=19; // Default columns dimension
- int RSIZ=4; // Default rows dimension
- // int CSIZ=39; // Default columns dimension
- // int RSIZ=8; // Default rows dimension
-
- char SCH = 0x20; // Space display character
- //char MCH = '#'; // Ascii Display character
- char MCH = 0xDB; // IBM graphic display character
- //char MCH = 0xB2; // IBM graphic display character
-
- int array[(CSIZ+2)*(RSIZ+2)]; // Maze array
-
- int UP=1; // Maze generation constant
- int DN=2; // Maze generation constant
- int LT=4; // Maze generation constant
- int RT=8; // Maze generation constant
- int keybreak=0; // Keyboard abort
- int rotor; // Rotor display flag
-
- main (int argc, char *arg1, char *arg2) // Main program
- {
- int r, c, base;
- long search[CSIZ*RSIZ+((CSIZ*RSIZ)/2)];
-
- getcmdline (argc,arg1,arg2); // Get the command line
-
- // Announce
- printf ("\n%s - Generate a %d column by %d row Maze\n",banner,CSIZ,RSIZ);
-
- // Setup the maze array
- for (c=0;c<CSIZ+1;c++) {
- array[c*(RSIZ+2)]=-1; array[RSIZ+1+c*(RSIZ+2)]=-1;
- }
- for (r=1;r<RSIZ+1;r++) {
- array[r]=-1; array[r+(CSIZ+1)*(RSIZ+2)]=-1;
- for (c=1;c<CSIZ+1;c++) array[r+c*(RSIZ+2)]=0;
- }
-
- srand (SysTime ()); // Setup the random generator
- r=rand ()%RSIZ+1; c=rand ()%CSIZ+1;
- base=addelem (0,search,r,c);
- array[r+c*(RSIZ+2)]=RT+RT; // Not a valid value
-
- printf ("\nGenerating the maze .. "); // Notify
-
- while (0<base) { // Create the maze
- r=rand ()%base;
- c=search[r]; search[r]=search[--base];
- r=c%(CSIZ*RSIZ); c=c/(CSIZ*RSIZ);
- openwall (r,c);
- base=addelem (base,search,r,c);
- if (keyabort ()) break;
- }
-
- if(!keybreak) writemaze (); // Display the maze
-
- saybibi (); // Was it good for you too?
- }
-
- addelem (int base, long *search, int row, int col)
- {
- if (0==array[row-1+col*(RSIZ+2)]) {
- search[base++]=row+col*(CSIZ*RSIZ)-1;
- array[row-1+col*(RSIZ+2)]=-DN;
- }
- else if(0>array[row-1+col*(RSIZ+2)])
- array[row-1+col*(RSIZ+2)]=array[row-1+col*(RSIZ+2)]-DN;
-
- if(0==array[row+1+col*(RSIZ+2)]) {
- search[base++]=row+col*(CSIZ*RSIZ)+1;
- array[row+1+col*(RSIZ+2)]=-UP;
- }
- else if(0>array[row+1+col*(RSIZ+2)])
- array[row+1+col*(RSIZ+2)]=array[row+1+col*(RSIZ+2)]-UP;
-
- if(0==array[row+(col-1)*(RSIZ+2)]) {
- search[base++]=row+col*(CSIZ*RSIZ)-(CSIZ*RSIZ);
- array[row+(col-1)*(RSIZ+2)]=-RT;
- }
- else if(0>array[row+(col-1)*(RSIZ+2)])
- array[row+(col-1)*(RSIZ+2)]=array[row+(col-1)*(RSIZ+2)]-RT;
-
- if(0==array[row+(col+1)*(RSIZ+2)]) {
- search[base++]=row+col*(CSIZ*RSIZ)+(CSIZ*RSIZ);
- array[row+(col+1)*(RSIZ+2)]=-LT;
- }
- else if(0>array[row+(col+1)*(RSIZ+2)])
- array[row+(col+1)*(RSIZ+2)]=array[row+(col+1)*(RSIZ+2)]-LT;
-
- return base;
- }
-
- openwall (int row, int col)
- {
- int directions, max, direction, temprow, tempcol, temp, back;
-
- directions=-array[row+col*(RSIZ+2)];
- max=0;
-
- if (And (directions,UP)) {
- temp=rand ();
- if (temp>max) {
- max=temp; direction=UP; back=DN;
- temprow=row-1; tempcol=col;
- }
- }
-
- if(And (directions,DN)) {
- temp=rand ();
- if(temp>max) {
- max=temp; direction=DN; back=UP;
- temprow=row+1; tempcol=col;
- }
- }
-
- if(And (directions,LT)) {
- temp=rand ();
- if(temp>max) {
- max=temp; direction=LT; back=RT;
- temprow=row; tempcol=col-1;
- }
- }
-
- if(And (directions,RT)) {
- temp=rand ();
- if(temp>max) {
- max=temp; direction=RT; back=LT;
- temprow=row; tempcol=col+1;
- }
- }
-
- array[row+col*(RSIZ+2)]=direction;
- array[temprow+tempcol*(RSIZ+2)]=array[temprow+tempcol*(RSIZ+2)]+back;
- }
-
- // Write the maze to screen
- writemaze ()
- {
- int r, c;
-
- printf (" \n\n"); // Rotor cleanup
- array[RSIZ+CSIZ*(RSIZ+2)]=DN; // Lower door
-
- for (c=1;c<CSIZ+1;++c) { // Display top line
- if(c!=1) printf ("%c%c",MCH,MCH);
- else printf ("%c%c",MCH,SCH); // Upper door
- }
- printf("%c\n",MCH);
-
- for (r=1;r<RSIZ+1;++r) { // Display maze body
- putch (MCH);
- for (c=1;c<CSIZ+1;++c) {
- putch (SCH);
- if (And (array[r+c*(RSIZ+2)],RT)) putch (SCH);
- else putch (MCH);
- }
- putch ('\n');
-
- for (c=1;c<CSIZ+1;++c) {
- putch (MCH);
- if (And (array[r+c*(RSIZ+2)],DN)) putch (SCH);
- else putch (MCH);
- }
- printf ("%c\n",MCH);
- }
- }
-
- // Display the rotor 8-)
- showrotor ()
- {
- if (++rotor>8) rotor=1;
-
- if (rotor==2) puts ("-\b");
- else if (rotor==4) puts ("\\\b");
- else if (rotor==6) puts ("|\b");
- else if (rotor==8) puts ("/\b");
- }
-
- // Check for keyboard abort
- keyabort ()
- {
- if (kbhit ()) { // Abort if key hit
- getch (); // Get the character
- putch (' '); // Rotor cleanup
- puts (aborts); // Acknowledge abort
- ++keybreak; // Flag an abort
- return 1; // Exit play loop
- }
-
- showrotor (); // Display activity
- return 0;
- }
-
- // Get the command line
- getcmdline (int argc, char *a1, char *a2)
- {
- // Load command line maze size options
-
- if (argc) CSIZ=atoi (a1); // Maze columns dimension
- if (argc>1) RSIZ=atoi (a2); // Maze rows dimension
-
- if (CSIZ<2 || CSIZ>39) CSIZ=39; // Bounds check
- if (RSIZ<2 || RSIZ>60) CSIZ=8; // Bounds check
- }
-
- // Byebye
- saybibi ()
- {
- puts ("\nMaze done!\n");
- }
-
- // End of script
-
-