1 ! Copyright (C) 2017 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
4 USING: accessors arrays assocs calendar colors.constants
5 combinators combinators.short-circuit destructors formatting fry
6 images.loader kernel locals math math.order math.parser
7 namespaces opengl opengl.textures random sequences timers ui
8 ui.commands ui.gadgets ui.gadgets.toolbar ui.gadgets.tracks
9 ui.gadgets.worlds ui.gestures ui.pens.solid ui.render words ;
14 { -1 -1 } { -1 0 } { -1 1 }
16 { 1 -1 } { 1 0 } { 1 1 }
19 SYMBOLS: +flagged+ +question+ +clicked+ ;
21 TUPLE: cell #adjacent mined? state ;
23 : make-cells ( rows cols -- cells )
24 '[ _ [ cell new ] replicate ] replicate ;
26 :: cell-at ( cells row col -- cell/f )
27 row cells ?nth [ col swap ?nth ] [ f ] if* ;
29 : cells-dim ( cells -- rows cols )
30 [ length ] [ first length ] bi ;
32 : unmined-cell ( cells -- cell )
33 f [ dup mined?>> ] [ drop dup random random ] do while nip ;
35 : #mines ( cells -- n )
36 [ [ mined?>> ] count ] map-sum ;
38 : #flagged ( cells -- n )
39 [ [ state>> +flagged+ = ] count ] map-sum ;
41 : #mines-remaining ( cells -- n )
42 [ #mines ] [ #flagged ] bi - ;
44 : place-mines ( cells n -- cells )
45 [ dup unmined-cell t >>mined? drop ] times ;
47 : adjacent-mines ( cells row col -- #mines )
49 first2 [ + ] bi-curry@ bi* cell-at
50 [ mined?>> ] [ f ] if*
51 ] with with with count ;
53 :: each-cell ( ... cells quot: ( ... row col cell -- ... ) -- ... )
55 [| cell col | row col cell quot call ] each-index
58 :: update-counts ( cells -- cells )
59 cells [| row col cell |
60 cells row col adjacent-mines cell #adjacent<<
63 : reset-cells ( cells -- cells )
64 [ cells-dim make-cells ] [ #mines place-mines ] bi update-counts ;
67 [ [ { [ state>> +clicked+ = ] [ mined?>> ] } 1|| ] all? ] all? ;
69 : lost? ( cells -- ? )
70 [ [ { [ state>> +clicked+ = ] [ mined?>> ] } 1&& ] any? ] any? ;
72 : game-over? ( cells -- ? )
73 { [ lost? ] [ won? ] } 1|| ;
75 : new-game? ( cells -- ? )
76 [ [ state>> +clicked+ = ] any? ] any? not ;
80 :: click-cells-around ( cells row col -- )
82 first2 [ row + ] [ col + ] bi* :> ( row' col' )
83 cells row' col' cell-at [
84 { [ mined?>> ] [ state>> +question+ = ] } 1|| [
85 cells row' col' click-cell-at drop
90 :: click-cell-at ( cells row col -- ? )
91 cells row col cell-at [
93 ! first click shouldn't be a mine
95 cells unmined-cell t >>mined? drop f >>mined?
96 cells update-counts drop
99 dup state>> { +clicked+ +flagged+ } member? [ drop f ] [
101 { [ mined?>> not ] [ #adjacent>> 0 = ] } 1&& [
102 cells row col click-cells-around
107 :: mark-cell-at ( cells row col -- ? )
108 cells row col cell-at [
110 { +clicked+ [ +clicked+ ] }
111 { +flagged+ [ +question+ ] }
114 } case >>state drop t
117 TUPLE: grid-gadget < gadget cells timer textures start end ;
119 :: <grid-gadget> ( rows cols mines -- gadget )
122 mines place-mines update-counts >>cells
123 H{ } clone >>textures
124 dup '[ _ relayout-1 ] f 1 seconds <timer> >>timer
125 COLOR: gray <solid> >>interior ;
127 M: grid-gadget graft*
128 [ timer>> start-timer ] [ call-next-method ] bi ;
130 M: grid-gadget ungraft*
133 [ values dispose-each H{ } clone ] change-textures
135 ] [ call-next-method ] bi ;
137 M: grid-gadget pref-dim*
138 cells>> cells-dim [ 32 * ] bi@ swap 58 + 2array ;
140 :: cell-image-path ( cell game-over? -- image-path )
141 game-over? cell mined?>> and [
142 cell state>> +clicked+ = "mineclicked.gif" "mine.gif" ?
146 { +question+ [ "question.gif" ] }
147 { +flagged+ [ game-over? "misflagged.gif" "flagged.gif" ? ] }
152 cell #adjacent>> 0 or number>string
153 "open" ".gif" surround
155 { f [ "blank.gif" ] }
157 ] if "vocab:minesweeper/_resources/" prepend ;
159 : digit-image-path ( ch -- image-path )
160 "vocab:minesweeper/_resources/digit%c.gif" sprintf ;
162 :: smiley-image-path ( won? lost? clicking? -- image-path )
164 { [ lost? ] [ "vocab:minesweeper/_resources/smileylost.gif" ] }
165 { [ won? ] [ "vocab:minesweeper/_resources/smileywon.gif" ] }
166 { [ clicking? ] [ "vocab:minesweeper/_resources/smileyuhoh.gif" ] }
167 [ "vocab:minesweeper/_resources/smiley.gif" ]
170 : cached-texture ( path gadget -- texture )
171 textures>> [ load-image { 0 0 } <texture> ] cache ;
173 :: draw-mines ( n gadget -- )
176 digit-image-path gadget cached-texture
177 { 26 46 } swap draw-scaled-texture
181 :: draw-smiley ( gadget -- )
182 gadget pref-dim first :> width
183 width 2/ 26 - 3 2array [
186 hand-buttons get-global empty? not
187 gadget hand-click-rel second 58 >= and
189 gadget cached-texture { 52 52 } swap draw-scaled-texture
192 :: draw-timer ( n gadget -- )
193 gadget pref-dim first :> width
195 3 swap - 26 * width swap - 3 - 6 2array [
196 digit-image-path gadget cached-texture
197 { 26 46 } swap draw-scaled-texture
201 :: draw-cells ( gadget -- )
202 gadget cells>> game-over? :> game-over?
203 gadget cells>> [| row col cell |
204 col row [ 32 * ] bi@ 58 + 2array [
205 cell game-over? cell-image-path
206 gadget cached-texture
207 { 32 32 } swap draw-scaled-texture
211 :: elapsed-time ( gadget -- n )
213 gadget end>> now or swap time- duration>seconds
216 M: grid-gadget draw-gadget*
218 [ cells>> #mines-remaining ]
226 :: on-click ( gadget -- )
227 gadget hand-rel first2 :> ( w h )
230 gadget pref-dim first 2/ w - abs 26 < and [
231 gadget [ reset-cells ] change-cells
232 f >>start f >>end relayout-1
235 h 58 - w [ 32 /i ] bi@ :> ( row col )
236 gadget cells>> :> cells
238 cells row col click-cell-at [
239 gadget start>> [ now gadget start<< ] unless
240 cells game-over? [ now gadget end<< ] when
246 :: on-mark ( gadget -- )
247 gadget hand-rel first2 :> ( w h )
249 h 58 - w [ 32 /i ] bi@ :> ( row col )
250 gadget cells>> :> cells
252 cells row col mark-cell-at [
253 gadget start>> [ now gadget start<< ] unless
254 cells game-over? [ now gadget end<< ] when
260 : new-game ( gadget rows cols mines -- )
261 [ make-cells ] dip place-mines update-counts >>cells
262 f >>start f >>end relayout-window ;
264 : com-easy ( gadget -- ) 7 7 10 new-game ;
266 : com-medium ( gadget -- ) 15 15 40 new-game ;
268 : com-hard ( gadget -- ) 15 30 99 new-game ;
270 grid-gadget "toolbar" f {
271 { T{ key-down { sym "1" } } com-easy }
272 { T{ key-down { sym "2" } } com-medium }
273 { T{ key-down { sym "3" } } com-hard }
276 grid-gadget "gestures" [
278 { T{ button-down { # 1 } } [ relayout-1 ] }
279 { T{ button-up { # 1 } } [ on-click ] }
280 { T{ button-up { # 3 } } [ on-mark ] }
281 { T{ key-down { sym " " } } [ on-mark ] }
285 TUPLE: minesweeper-gadget < track ;
287 : <minesweeper-gadget> ( -- gadget )
288 vertical minesweeper-gadget new-track
290 [ <toolbar> format-toolbar f track-add ]
293 M: minesweeper-gadget focusable-child* children>> second ;
295 MAIN-WINDOW: run-minesweeper {
296 { title "Minesweeper" }
298 { normal-title-bar close-button minimize-button } }
299 } <minesweeper-gadget> >>gadgets ;