maze.pp 13 KB

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