maze.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. i : boolean;
  232. begin
  233. runnum := maxrun; { max number of tree cells to add to a branch }
  234. connect(x, y); { first connect frontier cell to the tree }
  235. addfront(x, y); { convert neighboring unused cells to frontier }
  236. dec(runnum); { number of tree cells left to add to this branch }
  237. while (runnum > 0) and (fnum > 0) and (runset > 0) do
  238. begin
  239. repeat
  240. d := randomdir;
  241. until d and runset > 0; { pick random direction to known frontier }
  242. rembar(x, y, d); { and make it part of the tree }
  243. i := adjust(x, y, d);
  244. addfront(x, y); { then pick up the neighboring frontier cells }
  245. dec(runnum);
  246. end;
  247. end;
  248. procedure drawmaze;
  249. var
  250. x, y, i : word;
  251. begin
  252. setcolor(gridcolor); { draw the grid }
  253. y := height * blockwidth;
  254. for i := 0 to width do
  255. begin
  256. x := i * blockwidth;
  257. line(x, 0, x, y);
  258. end;
  259. x := width * blockwidth;
  260. for i := 0 to height do
  261. begin
  262. y := i * blockwidth;
  263. line (0, y, x, y);
  264. end;
  265. fillchar(cell^, sizeof(cell^), chr(0)); { zero flags }
  266. fnum := 0; { number of frontier cells in flist }
  267. runset := 0; { directions to known frontier cells from a base cell }
  268. randomize;
  269. x := random(width) + 1; { pick random start cell }
  270. y := random(height) + 1;
  271. add(x, y, rightdir); { direction ignored }
  272. addfront(x, y); { start with 1 tree cell and some frontier cells }
  273. while (fnum > 0) do
  274. with flist[random(fnum) + 1] do
  275. branch(column, row);
  276. end;
  277. procedure dot(x, y, colr : word);
  278. begin
  279. putpixel(blockwidth * x - halfblock, blockwidth * y - halfblock, colr);
  280. end;
  281. procedure solve(x, y, endx, endy : word);
  282. var
  283. j, k : word;
  284. d : byte;
  285. i : boolean;
  286. begin
  287. d := rightdir; { starting from left side of maze going right }
  288. while (x <> endx) or (y <> endy) do
  289. begin
  290. if d = $01 then
  291. d := $08
  292. else
  293. d := d shr 1; { look right, hug right wall }
  294. while cell^[x,y].flags and d = 0 do
  295. begin { look for an opening }
  296. d := d shl 1; { if no opening, turn left }
  297. if d > $08 then
  298. d := d shr 4;
  299. end;
  300. j := x;
  301. k := y;
  302. i := adjust(x, y, d); { go in that direction }
  303. with cell^[j,k] do
  304. begin { turn on dot, off if we were here before }
  305. flags := ((((cell^[x,y].flags xor $80) xor flags) and $80) xor flags);
  306. if flags and $80 <> 0 then
  307. dot(j, k, solvecolor)
  308. else
  309. dot(j, k, background);
  310. end;
  311. end;
  312. dot(endx, endy, solvecolor); { dot last cell on }
  313. end;
  314. procedure mansolve (x,y,endx,endy: word);
  315. var
  316. j, k : word;
  317. d : byte;
  318. ch : char;
  319. begin
  320. ch := ' ';
  321. while ((x <> endx) or (y <> endy)) and (ch <> 'X') and (ch <> #27) do
  322. begin
  323. dot(x, y, solvecolor); { dot man on, show where we are in maze }
  324. ch := upcase(readkey);
  325. dot(x, y, background); { dot man off after keypress }
  326. d := 0;
  327. case ch of
  328. #0:
  329. begin
  330. ch := readkey;
  331. case ch of
  332. #72 : d := updir;
  333. #75 : d := leftdir;
  334. #77 : d := rightdir;
  335. #80 : d := downdir;
  336. end;
  337. end;
  338. 'I' : d := updir;
  339. 'J' : d := leftdir;
  340. 'K' : d := rightdir;
  341. 'M' : d := downdir;
  342. end;
  343. if d > 0 then
  344. begin
  345. j := x;
  346. k := y; { move if no wall and still in bounds }
  347. if (cell^[x,y].flags and d > 0) and adjust(j, k, d) then
  348. begin
  349. x := j;
  350. y := k;
  351. end;
  352. end;
  353. end;
  354. end;
  355. procedure solvemaze;
  356. var
  357. x, y,
  358. endx,
  359. endy : word;
  360. ch : char;
  361. begin
  362. x := 1; { pick random start on left side wall }
  363. y := random(height) + 1;
  364. endx := width; { pick random end on right side wall }
  365. endy := random(height) + 1;
  366. remline(x, y, leftdir); { show start and end by erasing line }
  367. remline(endx, endy, rightdir);
  368. mansolve(x, y, endx, endy); { try it manually }
  369. solve(x, y, endx, endy); { show how when he gives up }
  370. while keypressed do
  371. ch := readkey;
  372. ch := readkey;
  373. end;
  374. procedure getsize;
  375. var
  376. j, k : real;
  377. begin
  378. clrscr;
  379. writeln(' Mind');
  380. writeln(' Over');
  381. writeln(' Maze');
  382. writeln;
  383. writeln(' by Randy Ding');
  384. writeln;
  385. writeln('Use I,J,K,M or arrow keys to walk thru maze,');
  386. writeln('then hit X when you give up!');
  387. repeat
  388. writeln;
  389. write('Maze size: ', minblockwidth, ' (hard) .. 95 (easy) ');
  390. readln(blockwidth);
  391. until (blockwidth >= minblockwidth) and (blockwidth < 96);
  392. writeln;
  393. write('Maximum branch length: 1 easy .. 50 harder, (0 unlimited) ');
  394. readln(maxrun);
  395. if maxrun <= 0 then
  396. maxrun := 65535; { infinite }
  397. j := screenwidth / blockwidth;
  398. k := screenheight / blockwidth;
  399. if j = int(j) then
  400. j := j - 1;
  401. if k = int(k) then
  402. k := k - 1;
  403. width := trunc(j);
  404. height := trunc(k);
  405. if (width > maxx) or (height > maxy) then
  406. begin
  407. width := maxx;
  408. height := maxy;
  409. end;
  410. halfblock := blockwidth div 2;
  411. end;
  412. begin
  413. repeat
  414. getsize;
  415. initbgi;
  416. new(cell); { allocate this large array on heap }
  417. drawmaze;
  418. solvemaze;
  419. dispose(cell);
  420. closegraph;
  421. while keypressed do
  422. ch := readkey;
  423. write ('another one? ');
  424. ch := upcase (readkey);
  425. until (ch = 'N') or (ch = #27);
  426. end.