maze.pp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. {A demo with some interesting algoritms, and for Graph.
  2. The sources for this game was found on a site that claims to only have
  3. PD stuff with the below header(which was only reindented), and the webmaster
  4. said that everything he published was sent to him with that purpose. We tried
  5. to contact the authors mentioned below via mail over internet, but that
  6. failed. If there is somebody that claims authorship of these programs,
  7. please mail [email protected], and the sources will be removed from our
  8. websites.
  9. ------------------------------------------------------------------------
  10. ORIGINAL Header:
  11. created by Randy Ding July 16,1983 <April 21,1992>
  12. Very small FPC fixes by Marco van de Voort (EgaHi to vgahi), and tried
  13. setting the maze dimensions maxx and maxy to a bigger size.
  14. Won't work, you'll have to update all vars to al least word to increase the
  15. complexity of the grid further. I didn't do it, since 200x200 is already
  16. unreadable to me.
  17. Don't forget the BGIPATH of InitGraph.
  18. }
  19. {$R-} { range checking }
  20. program makemaze;
  21. uses
  22. crt, graph;
  23. const
  24. screenwidth = 640;
  25. screenheight = 480;
  26. minblockwidth = 2;
  27. maxx = 200; { BP: [3 * maxx * maxy] must be less than 65520 (memory segment) }
  28. { FPC: Normally no problem. ( even if you'd use 1600x1200x3< 6MB)}
  29. maxy = 200; { here maxx/maxy about equil to screenwidth/screenheight }
  30. flistsize = maxx*maxy DIV 2; { flist size (fnum max, about 1/3 of maxx * maxy) }
  31. background = black;
  32. gridcolor = green;
  33. solvecolor = white;
  34. rightdir = $01;
  35. updir = $02;
  36. leftdir = $04;
  37. downdir = $08;
  38. unused = $00; { cell types used as flag bits }
  39. frontier = $10;
  40. { reserved = $20; }
  41. tree = $30;
  42. type
  43. frec = record
  44. column, row : byte;
  45. end;
  46. farr = array [1..flistsize] of frec;
  47. cellrec = record
  48. point : word; { pointer to flist record }
  49. flags : byte;
  50. end;
  51. cellarr = array [1..maxx,1..maxy] of cellrec;
  52. {
  53. one byte per cell, flag bits...
  54. 0: right, 1 = barrier removed
  55. 1: top "
  56. 2: left "
  57. 3: bottom "
  58. 5,4: 0,0 = unused cell type
  59. 0,1 = frontier "
  60. 1,1 = tree "
  61. 1,0 = reserved "
  62. 6: (not used)
  63. 7: solve path, 1 = this cell part of solve path
  64. }
  65. var
  66. flist : farr; { list of frontier cells in random order }
  67. cell : ^cellarr; { pointers and flags, on heap }
  68. fnum,
  69. width,
  70. height,
  71. blockwidth,
  72. halfblock,
  73. maxrun : word;
  74. runset : byte;
  75. ch : char;
  76. procedure initbgi;
  77. var
  78. grdriver,
  79. grmode,
  80. errcode : integer;
  81. begin
  82. grdriver := vga;
  83. grmode := vgahi;
  84. initgraph(grdriver, grmode, 'd:\pp\bp\bgi');
  85. errcode:= graphresult;
  86. if errcode <> grok then
  87. begin
  88. CloseGraph;
  89. writeln('Graphics error: ', grapherrormsg(errcode));
  90. halt(1);
  91. end;
  92. end;
  93. function adjust(var x, y : word; d : byte) : boolean;
  94. begin { take x,y to next cell in direction d }
  95. case d of { returns false if new x,y is off grid }
  96. rightdir:
  97. begin
  98. inc (x);
  99. adjust:= x <= width;
  100. end;
  101. updir:
  102. begin
  103. dec (y);
  104. adjust:= y > 0;
  105. end;
  106. leftdir:
  107. begin
  108. dec (x);
  109. adjust:= x > 0;
  110. end;
  111. downdir:
  112. begin
  113. inc (y);
  114. adjust:= y <= height;
  115. end;
  116. end;
  117. end;
  118. procedure remove(x, y : word); { remove a frontier cell from flist }
  119. var
  120. i : word; { done by moving last entry in flist into it's place }
  121. begin
  122. i := cell^[x,y].point; { old pointer }
  123. with flist[fnum] do
  124. cell^[column,row].point := i; { move pointer }
  125. flist[i] := flist[fnum]; { move data }
  126. dec(fnum); { one less to worry about }
  127. end;
  128. procedure add(x, y : word; d : byte); { add a frontier cell to flist }
  129. var
  130. i : byte;
  131. begin
  132. i := cell^[x,y].flags;
  133. case i and $30 of { check cell type }
  134. unused :
  135. begin
  136. cell^[x,y].flags := i or frontier; { change to frontier cell }
  137. inc(fnum); { have one more to worry about }
  138. if fnum > flistsize then
  139. begin { flist overflow error! }
  140. dispose(cell); { clean up memory }
  141. closegraph;
  142. writeln('flist overflow! - To correct, increase "flistsize"');
  143. write('hit return to halt program ');
  144. readln;
  145. halt(1); { exit program }
  146. end;
  147. with flist[fnum] do
  148. begin { copy data into last entry of flist }
  149. column := x;
  150. row := y;
  151. end;
  152. cell^[x,y].point := fnum; { make the pointer point to the new cell }
  153. runset := runset or d; { indicate that a cell in direction d was }
  154. end; { added to the flist }
  155. frontier : runset := runset or d; { allready in flist }
  156. end;
  157. end;
  158. procedure addfront(x, y : word); { change all unused cells around this }
  159. var { base cell to frontier cells }
  160. j, k : word;
  161. d : byte;
  162. begin
  163. remove(x, y); { first remove base cell from flist, it is now }
  164. runset := 0; { part of the tree }
  165. cell^[x,y].flags := cell^[x,y].flags or tree; { change to tree cell }
  166. d := $01; { look in all four directions- $01,$02,$04,$08 }
  167. while d <= $08 do
  168. begin
  169. j := x;
  170. k := y;
  171. if adjust(j, k, d) then
  172. add(j, k, d); { add only if still in bounds }
  173. d := d shl 1; { try next direction }
  174. end;
  175. end;
  176. procedure remline(x, y : word; d : byte); { erase line connecting two blocks }
  177. begin
  178. setcolor(background);
  179. x := (x - 1) * blockwidth;
  180. y := (y - 1) * blockwidth;
  181. case d of
  182. rightdir : line (x + blockwidth, y + 1, x + blockwidth, y + blockwidth - 1);
  183. updir : line (x + 1, y, x + blockwidth - 1, y);
  184. leftdir : line (x, y + 1, x, y + blockwidth - 1);
  185. downdir : line (x + 1, y + blockwidth, x + blockwidth - 1, y + blockwidth);
  186. end;
  187. end;
  188. { erase line and update flags to indicate the barrier has been removed }
  189. procedure rembar(x, y : word; d : byte);
  190. var
  191. d2 : byte;
  192. begin
  193. remline(x, y, d); { erase line }
  194. cell^[x,y].flags := cell^[x,y].flags or d; { show barrier removed dir. d }
  195. d2 := d shl 2; { shift left twice to reverse direction }
  196. if d2 > $08 then
  197. d2 := d2 shr 4; { wrap around }
  198. if adjust(x, y, d) then { do again from adjacent cell back to base cell }
  199. cell^[x,y].flags := cell^[x,y].flags or d2; { skip if out of bounds }
  200. end;
  201. function randomdir : byte; { get a random direction }
  202. begin
  203. case random(4) of
  204. 0 : randomdir := rightdir;
  205. 1 : randomdir := updir;
  206. 2 : randomdir := leftdir;
  207. 3 : randomdir := downdir;
  208. end;
  209. end;
  210. procedure connect(x, y : word); { connect this new branch to the tree }
  211. var { in a random direction }
  212. j, k : word;
  213. d : byte;
  214. found : boolean;
  215. begin
  216. found := false;
  217. while not found do
  218. begin { loop until we find a tree cell to connect to }
  219. j := x;
  220. k := y;
  221. d := randomdir;
  222. if adjust(j, k, d) then
  223. found := cell^[j,k].flags and $30 = tree;
  224. end;
  225. rembar(x, y, d); { remove barrier connecting the cells }
  226. end;
  227. procedure branch(x, y : word); { make a new branch of the tree }
  228. var
  229. runnum : word;
  230. d : byte;
  231. begin
  232. runnum := maxrun; { max number of tree cells to add to a branch }
  233. connect(x, y); { first connect frontier cell to the tree }
  234. addfront(x, y); { convert neighboring unused cells to frontier }
  235. dec(runnum); { number of tree cells left to add to this branch }
  236. while (runnum > 0) and (fnum > 0) and (runset > 0) do
  237. begin
  238. repeat
  239. d := randomdir;
  240. until d and runset > 0; { pick random direction to known frontier }
  241. rembar(x, y, d); { and make it part of the tree }
  242. adjust(x, y, d);
  243. addfront(x, y); { then pick up the neighboring frontier cells }
  244. dec(runnum);
  245. end;
  246. end;
  247. procedure drawmaze;
  248. var
  249. x, y, i : word;
  250. begin
  251. setcolor(gridcolor); { draw the grid }
  252. y := height * blockwidth;
  253. for i := 0 to width do
  254. begin
  255. x := i * blockwidth;
  256. line(x, 0, x, y);
  257. end;
  258. x := width * blockwidth;
  259. for i := 0 to height do
  260. begin
  261. y := i * blockwidth;
  262. line (0, y, x, y);
  263. end;
  264. fillchar(cell^, sizeof(cell^), chr(0)); { zero flags }
  265. fnum := 0; { number of frontier cells in flist }
  266. runset := 0; { directions to known frontier cells from a base cell }
  267. randomize;
  268. x := random(width) + 1; { pick random start cell }
  269. y := random(height) + 1;
  270. add(x, y, rightdir); { direction ignored }
  271. addfront(x, y); { start with 1 tree cell and some frontier cells }
  272. while (fnum > 0) do
  273. with flist[random(fnum) + 1] do
  274. branch(column, row);
  275. end;
  276. procedure dot(x, y, colr : word);
  277. begin
  278. putpixel(blockwidth * x - halfblock, blockwidth * y - halfblock, colr);
  279. end;
  280. procedure solve(x, y, endx, endy : word);
  281. var
  282. j, k : word;
  283. d : byte;
  284. begin
  285. d := rightdir; { starting from left side of maze going right }
  286. while (x <> endx) or (y <> endy) do
  287. begin
  288. if d = $01 then
  289. d := $08
  290. else
  291. d := d shr 1; { look right, hug right wall }
  292. while cell^[x,y].flags and d = 0 do
  293. begin { look for an opening }
  294. d := d shl 1; { if no opening, turn left }
  295. if d > $08 then
  296. d := d shr 4;
  297. end;
  298. j := x;
  299. k := y;
  300. adjust(x, y, d); { go in that direction }
  301. with cell^[j,k] do
  302. begin { turn on dot, off if we were here before }
  303. flags := ((((cell^[x,y].flags xor $80) xor flags) and $80) xor flags);
  304. if flags and $80 <> 0 then
  305. dot(j, k, solvecolor)
  306. else
  307. dot(j, k, background);
  308. end;
  309. end;
  310. dot(endx, endy, solvecolor); { dot last cell on }
  311. end;
  312. procedure mansolve (x,y,endx,endy: word);
  313. var
  314. j, k : word;
  315. d : byte;
  316. ch : char;
  317. begin
  318. ch := ' ';
  319. while ((x <> endx) or (y <> endy)) and (ch <> 'X') and (ch <> #27) do
  320. begin
  321. dot(x, y, solvecolor); { dot man on, show where we are in maze }
  322. ch := upcase(readkey);
  323. dot(x, y, background); { dot man off after keypress }
  324. d := 0;
  325. case ch of
  326. #0:
  327. begin
  328. ch := readkey;
  329. case ch of
  330. #72 : d := updir;
  331. #75 : d := leftdir;
  332. #77 : d := rightdir;
  333. #80 : d := downdir;
  334. end;
  335. end;
  336. 'I' : d := updir;
  337. 'J' : d := leftdir;
  338. 'K' : d := rightdir;
  339. 'M' : d := downdir;
  340. end;
  341. if d > 0 then
  342. begin
  343. j := x;
  344. k := y; { move if no wall and still in bounds }
  345. if (cell^[x,y].flags and d > 0) and adjust(j, k, d) then
  346. begin
  347. x := j;
  348. y := k;
  349. end;
  350. end;
  351. end;
  352. end;
  353. procedure solvemaze;
  354. var
  355. x, y,
  356. endx,
  357. endy : word;
  358. begin
  359. x := 1; { pick random start on left side wall }
  360. y := random(height) + 1;
  361. endx := width; { pick random end on right side wall }
  362. endy := random(height) + 1;
  363. remline(x, y, leftdir); { show start and end by erasing line }
  364. remline(endx, endy, rightdir);
  365. mansolve(x, y, endx, endy); { try it manually }
  366. solve(x, y, endx, endy); { show how when he gives up }
  367. while keypressed do
  368. readkey;
  369. readkey;
  370. end;
  371. procedure getsize;
  372. var
  373. j, k : real;
  374. begin
  375. clrscr;
  376. writeln(' Mind');
  377. writeln(' Over');
  378. writeln(' Maze');
  379. writeln;
  380. writeln(' by Randy Ding');
  381. writeln;
  382. writeln('Use I,J,K,M or arrow keys to walk thru maze,');
  383. writeln('then hit X when you give up!');
  384. repeat
  385. writeln;
  386. write('Maze size: ', minblockwidth, ' (hard) .. 95 (easy) ');
  387. readln(blockwidth);
  388. until (blockwidth >= minblockwidth) and (blockwidth < 96);
  389. writeln;
  390. write('Maximum branch length: 1 easy .. 50 harder, (0 unlimited) ');
  391. readln(maxrun);
  392. if maxrun <= 0 then
  393. maxrun := 65535; { infinite }
  394. j := Real(screenwidth) / blockwidth;
  395. k := Real(screenheight) / blockwidth;
  396. if j = int(j) then
  397. j := j - 1;
  398. if k = int(k) then
  399. k := k - 1;
  400. width := trunc(j);
  401. height := trunc(k);
  402. if (width > maxx) or (height > maxy) then
  403. begin
  404. width := maxx;
  405. height := maxy;
  406. end;
  407. halfblock := blockwidth div 2;
  408. end;
  409. begin
  410. repeat
  411. getsize;
  412. initbgi;
  413. new(cell); { allocate this large array on heap }
  414. drawmaze;
  415. solvemaze;
  416. dispose(cell);
  417. closegraph;
  418. while keypressed do
  419. ch := readkey;
  420. write ('another one? ');
  421. ch := upcase (readkey);
  422. until (ch = 'N') or (ch = #27);
  423. end.
  424. $Log$
  425. Revision 1.2 2000-07-13 11:33:08 michael
  426. + removed logs
  427. }