! From http://www.ffconsultancy.com/ocaml/maze/index.html
-USING: accessors arrays fry kernel math math.order math.vectors
-namespaces opengl.gl random sequences ui ui.gadgets
-ui.gadgets.canvas ui.render ;
+USING: accessors arrays kernel math math.order math.vectors
+namespaces opengl.demo-support opengl.gl random sequences ui
+ui.gadgets ui.gadgets.canvas ui.gestures ui.render ;
IN: maze
CONSTANT: line-width 8
: unvisited? ( cell -- ? ) first2 visited get ?nth ?nth ;
-<PRIVATE
-
-: ?set-nth ( elt n seq -- )
- 2dup bounds-check? [ set-nth-unsafe ] [ 3drop ] if ; inline
-
-PRIVATE>
-
: visit ( cell -- ) f swap first2 visited get ?nth ?set-nth ;
: choices ( cell -- seq )
[ v+ ] with map
[ unvisited? ] filter ;
-: random-neighbour ( cell -- newcell ) choices random ;
+: random-neighbor ( cell -- newcell ) choices random ;
: vertex ( pair -- )
first2 [ 0.5 + line-width * ] bi@ glVertex2d ;
: (draw-maze) ( cell -- )
dup vertex
glEnd
- GL_POINTS glBegin dup vertex glEnd
+ GL_POINTS [ dup vertex ] do-state
GL_LINE_STRIP glBegin
dup vertex
dup visit
- dup random-neighbour dup [
+ dup random-neighbor [
(draw-maze) (draw-maze)
] [
- 2drop
+ drop
glEnd
GL_LINE_STRIP glBegin
- ] if ;
+ ] if* ;
: draw-maze ( n -- )
line-width 2 - glLineWidth
line-width 2 - glPointSize
1.0 1.0 1.0 1.0 glColor4d
dup '[ _ t <array> ] replicate visited set
- GL_LINE_STRIP glBegin
- { 0 0 } dup vertex (draw-maze)
- glEnd ;
+ GL_LINE_STRIP [
+ { 0 0 } dup vertex (draw-maze)
+ ] do-state ;
TUPLE: maze < canvas ;
M: maze pref-dim* drop { 400 400 } ;
+M: maze handle-gesture
+ over T{ button-down { # 1 } } =
+ [ nip relayout f ] [ call-next-method ] if ;
+
MAIN-WINDOW: maze-window { { title "Maze" } }
<maze> >>gadgets ;
-
-MAIN: maze-window