1 ! From http://www.ffconsultancy.com/ocaml/maze/index.html
2 USING: accessors arrays fry kernel math math.order math.vectors
3 namespaces opengl.gl random sequences ui ui.gadgets
4 ui.gadgets.canvas ui.render ;
11 : unvisited? ( cell -- ? ) first2 visited get ?nth ?nth ;
15 : ?set-nth ( elt n seq -- )
16 2dup bounds-check? [ set-nth-unsafe ] [ 3drop ] if ; inline
20 : visit ( cell -- ) f swap first2 visited get ?nth ?set-nth ;
22 : choices ( cell -- seq )
23 { { -1 0 } { 1 0 } { 0 -1 } { 0 1 } }
25 [ unvisited? ] filter ;
27 : random-neighbour ( cell -- newcell ) choices random ;
30 first2 [ 0.5 + line-width * ] bi@ glVertex2d ;
32 : (draw-maze) ( cell -- )
35 GL_POINTS glBegin dup vertex glEnd
39 dup random-neighbour dup [
40 (draw-maze) (draw-maze)
48 line-width 2 - glLineWidth
49 line-width 2 - glPointSize
50 1.0 1.0 1.0 1.0 glColor4d
51 dup '[ _ t <array> ] replicate visited set
53 { 0 0 } dup vertex (draw-maze)
56 TUPLE: maze < canvas ;
58 : <maze> ( -- gadget ) maze new-canvas ;
60 : n ( gadget -- n ) dim>> first2 min line-width /i ;
62 M: maze layout* delete-canvas-dlist ;
64 M: maze draw-gadget* [ n draw-maze ] draw-canvas ;
66 M: maze pref-dim* drop { 400 400 } ;
68 MAIN-WINDOW: maze-window { { title "Maze" } }