1 ! From http://www.ffconsultancy.com/ocaml/maze/index.html
2 USING: accessors arrays kernel math math.order math.vectors
3 namespaces opengl.demo-support opengl.gl random sequences ui
4 ui.gadgets ui.gadgets.canvas ui.render ;
11 : unvisited? ( cell -- ? ) first2 visited get ?nth ?nth ;
13 : visit ( cell -- ) f swap first2 visited get ?nth ?set-nth ;
15 : choices ( cell -- seq )
16 { { -1 0 } { 1 0 } { 0 -1 } { 0 1 } }
18 [ unvisited? ] filter ;
20 : random-neighbour ( cell -- newcell ) choices random ;
23 first2 [ 0.5 + line-width * ] bi@ glVertex2d ;
25 : (draw-maze) ( cell -- )
28 GL_POINTS [ dup vertex ] do-state
32 dup random-neighbour dup [
33 (draw-maze) (draw-maze)
41 line-width 2 - glLineWidth
42 line-width 2 - glPointSize
43 1.0 1.0 1.0 1.0 glColor4d
44 dup '[ _ t <array> ] replicate visited set
46 { 0 0 } dup vertex (draw-maze)
49 TUPLE: maze < canvas ;
51 : <maze> ( -- gadget ) maze new-canvas ;
53 : n ( gadget -- n ) dim>> first2 min line-width /i ;
55 M: maze layout* delete-canvas-dlist ;
57 M: maze draw-gadget* [ n draw-maze ] draw-canvas ;
59 M: maze pref-dim* drop { 400 400 } ;
61 MAIN-WINDOW: maze-window { { title "Maze" } }