ebooksgratis.com

See also ebooksgratis.com: no banners, no cookies, totally FREE.

CLASSICISTRANIERI HOME PAGE - YOUTUBE CHANNEL
Privacy Policy Cookie Policy Terms and Conditions
Image:Maze.png - Wikipedia, the free encyclopedia

Image:Maze.png

From Wikipedia, the free encyclopedia

Maze, generated by my algorithm; Source edited for clarity by quin/10-24-06

import java.awt.*;
import java.applet.*;
  
/*
 * For the benefit of novice programmers, I've listed
 * the various bitmasks used to the best of my ability
 * (I don't use Java, I'm assuming it uses standard coordinates where 0,0 is the top left)
 * I've commented on the drawing code as well
 *
 *  1: Wall above
 *  2: Wall below
 *  4: Wall left
 *  8: Wall right
 * 16: queued to be added
 * 32: "in" the maze
 *
 *  - quin/10-24-06
 */

public class Maze extends Applet {
   int maze[][];
   
   public void init () {
       int x, y, n, d;
       int dx[] = { 0, 0, -1, 1 };
       int dy[] = { -1, 1, 0, 0 };
       int todo[] = new int[400], todonum = 0;
  
       /* We want to create a maze on a grid. */
       maze = new int[20][20];
  
       /* We start with a grid full of walls. */
       for (x = 0; x < 20; ++x)
           for (y = 0; y < 20; ++y)
               if (x == 0 || x == 19 || y == 0 || y == 19) {
                   maze[x][y] = 32;
               } else {
                   maze[x][y] = 63;
               }
  
       /* Select any square of the grid, to start with. */
       x = (int) (1 + Math.random () * 18);
       y = (int) (1 + Math.random () * 18);
  
       /* Mark this square as connected to the maze. */
       maze[x][y] &= ~48;
  
       /* Remember the surrounding squares, as we will */
       for (d = 0; d < 4; ++d)
           if ((maze[x + dx[d]][y + dy[d]] & 16) != 0) {
               /* want to connect them to the maze. */

               /* alternately, you could use a struct to store the two integers
                * this would result in easier to read code, though not as speedy
                * of course, if you were worried about speed, you wouldn't be using Java
                * you could also use a single integer which represents (x + y * width)
                * this would actually be faster than the current approach
                * - quin/10-24-06
                *    Actually, the former wouldn't work in Java- there's no such thing as a
                *    struct. It's a class or nothing, I'm afraid.
                *    - Jae Armstrong/23-03-07
                */
                
               todo[todonum++] = ((x + dx[d]) << 16) | (y + dy[d]);
               maze[x + dx[d]][y + dy[d]] &= ~16;
           }
  
       /* We won't be finished until all is connected. */
       while (todonum > 0) {
           /* We select one of the squares next to the maze. */
           n = (int) (Math.random () * todonum);
           x = todo[n] >> 16; /* the top 2 bytes of the data */
           y = todo[n] & 65535; /* the bottom 2 bytes of the data */
  
           /* We will connect it, so remove it from the queue. */
           todo[n] = todo[--todonum];
  
           /* Select a direction, which leads to the maze. */
           do
               d = (int) (Math.random () * 4);
           while ((maze[x + dx[d]][y + dy[d]] & 32) != 0);
  
           /* Connect this square to the maze. */
           maze[x][y] &= ~((1 << d) | 32);
           maze[x + dx[d]][y + dy[d]] &= ~(1 << (d ^ 1));
  
           /* Remember the surrounding squares, which aren't */
           for (d = 0; d < 4; ++d)
               if ((maze[x + dx[d]][y + dy[d]] & 16) != 0) {
                   
                   /* connected to the maze, and aren't yet queued to be. */
                   todo[todonum++] = ((x + dx[d]) << 16) | (y + dy[d]);
                   maze[x + dx[d]][y + dy[d]] &= ~16;
               }
           /* Repeat until finished. */
       }
       
       /* One may want to add an entrance and exit. */
       maze[1][1] &= ~1; 
       maze[18][18] &= ~2;
   }
  
   public void paint (Graphics g) {
       int x, y;
  
       for (x = 1; x < 19; ++x)
           for (y = 1; y < 19; ++y) {
               if ((maze[x][y] & 1) != 0) /* This cell has a top wall */
                   g.drawLine (x * 10, y * 10, x * 10 + 10, y * 10);
               if ((maze[x][y] & 2) != 0) /* This cell has a bottom wall */
                   g.drawLine (x * 10, y * 10 + 10, x * 10 + 10, y * 10 + 10);
               if ((maze[x][y] & 4) != 0) /* This cell has a left wall */
                   g.drawLine (x * 10, y * 10, x * 10, y * 10 + 10);
               if ((maze[x][y] & 8) != 0) /* This cell has a right wall */
                   g.drawLine (x * 10 + 10, y * 10, x * 10 + 10, y * 10 + 10);
           }
   }
}

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeDimensionsUserComment
current13:54, 28 October 2007200×200 (443 B)McLoaf (Talk | contribs)
16:01, 3 June 2003200×200 (3 KB)Cyp (Talk | contribs) (Maze, generated by my algorithm.)

The following pages on the English Wikipedia link to this file (pages on other projects are not listed):


aa - ab - af - ak - als - am - an - ang - ar - arc - as - ast - av - ay - az - ba - bar - bat_smg - bcl - be - be_x_old - bg - bh - bi - bm - bn - bo - bpy - br - bs - bug - bxr - ca - cbk_zam - cdo - ce - ceb - ch - cho - chr - chy - co - cr - crh - cs - csb - cu - cv - cy - da - de - diq - dsb - dv - dz - ee - el - eml - en - eo - es - et - eu - ext - fa - ff - fi - fiu_vro - fj - fo - fr - frp - fur - fy - ga - gan - gd - gl - glk - gn - got - gu - gv - ha - hak - haw - he - hi - hif - ho - hr - hsb - ht - hu - hy - hz - ia - id - ie - ig - ii - ik - ilo - io - is - it - iu - ja - jbo - jv - ka - kaa - kab - kg - ki - kj - kk - kl - km - kn - ko - kr - ks - ksh - ku - kv - kw - ky - la - lad - lb - lbe - lg - li - lij - lmo - ln - lo - lt - lv - map_bms - mdf - mg - mh - mi - mk - ml - mn - mo - mr - mt - mus - my - myv - mzn - na - nah - nap - nds - nds_nl - ne - new - ng - nl - nn - no - nov - nrm - nv - ny - oc - om - or - os - pa - pag - pam - pap - pdc - pi - pih - pl - pms - ps - pt - qu - quality - rm - rmy - rn - ro - roa_rup - roa_tara - ru - rw - sa - sah - sc - scn - sco - sd - se - sg - sh - si - simple - sk - sl - sm - sn - so - sr - srn - ss - st - stq - su - sv - sw - szl - ta - te - tet - tg - th - ti - tk - tl - tlh - tn - to - tpi - tr - ts - tt - tum - tw - ty - udm - ug - uk - ur - uz - ve - vec - vi - vls - vo - wa - war - wo - wuu - xal - xh - yi - yo - za - zea - zh - zh_classical - zh_min_nan - zh_yue - zu -