1 ! From http://www.ffconsultancy.com/ocaml/maze/index.html
2 USING: sequences namespaces math math.vectors opengl opengl.gl
3 arrays kernel random ui ui.gadgets ui.gadgets.canvas ui.render
4 math.order math.geometry.rect ;
11 : unvisited? ( cell -- ? ) first2 visited get ?nth ?nth ;
13 : ?set-nth ( elt i seq -- )
14 2dup bounds-check? [ set-nth ] [ 3drop ] if ;
16 : visit ( cell -- ) f swap first2 visited get ?nth ?set-nth ;
18 : choices ( cell -- seq )
19 { { -1 0 } { 1 0 } { 0 -1 } { 0 1 } }
21 [ unvisited? ] filter ;
23 : random-neighbour ( cell -- newcell ) choices random ;
26 first2 [ 0.5 + line-width * ] bi@ glVertex2d ;
28 : (draw-maze) ( cell -- )
31 GL_POINTS glBegin dup vertex glEnd
35 dup random-neighbour dup [
36 (draw-maze) (draw-maze)
44 line-width 2 - glLineWidth
45 line-width 2 - glPointSize
46 1.0 1.0 1.0 1.0 glColor4d
47 dup [ drop t <array> ] with map visited set
49 { 0 0 } dup vertex (draw-maze)
52 TUPLE: maze < canvas ;
54 : <maze> ( -- gadget ) maze new-canvas ;
56 : n ( gadget -- n ) rect-dim first2 min line-width /i ;
58 M: maze layout* delete-canvas-dlist ;
60 M: maze draw-gadget* [ n draw-maze ] draw-canvas ;
62 M: maze pref-dim* drop { 400 400 } ;
65 [ <maze> "Maze" open-window ] with-ui ;