View difference between Paste ID: LMSWf5mh and LpzF7mYn
SHOW: | | - or go back to the newest paste.
1
_ms_h  = 9  // board height
2
_ms_w  = 9  // board width
3
_ms_m  = 10 // board mines
4
_ms_th = 9  // temp H
5
_ms_tw = 9  // temp W
6
_ms_tm = 10 // temp M
7-
_ms_fc = 0  // placed flags counter
7+
_ms_fc = 0  // [FLAG] counter
8-
_ms_hc = 0  // hit mines counter
8+
_ms_hc = 0  // [MINE-HIT] counter
9
_ms_sm = 0  // surrounding mines counter
10
_ms_qh = 0  // quick hold toggle
11
_ms_bt = 0  // board tiles (total)
12
_ms_t1 = 0  // timer start (ms)
13-
_ms_u  = 0  // undo counter
13+
14
_ms_ua = 1  // undo toggle
15
_ms_uc = 0  // undo counter
16
_ms_pc = 0  // completion percentage
17
_ms_gm = 0  // game mode: 0 waiting | 1 active | 2 failure | 3 victory
18
19
_ms_mf = "" // board containing mine placement
20-
_ms_mp = "" // supplementary list with mine positions
20+
21-
_ms_hp = "" // supplementary list with hit mine positions
21+
22-
_ms_fp = "" // supplementary list with flag positions
22+
_ms_mp = "" // supplementary list with [MINE] positions
23
_ms_fp = "" // supplementary list with [FLAG] positions
24-
_ms_tq = "" // supplementary list with blank tile queue
24+
_ms_tq = "" // supplementary list with [BLANK] tile queue
25
_ms_tr = "" // supplementary list with tile reveal positions
26
27
28
// ----------------------------------------- //
29
// ---------------- Tile ID ---------------- //
30-
// 0x0     = blank tile
30+
31-
// 0x1-0x8 = number tiles
31+
// 0x0     = [BLANK]
32-
// 0x9     = hit mine tile
32+
// 0x1-0x8 = [NUM#]
33-
// 0xA     = mine tile (win)
33+
// 0x9     = [MINE-HIT]
34-
// 0xB     = mine tile (lose)
34+
// 0xA     = [MINE-WIN]
35-
// 0xC     = flagged tile
35+
// 0xB     = [MINE-LOSE]
36-
// 0xD     = flagged mine tile (win)
36+
// 0xC     = [FLAG]
37-
// 0xE     = flagged mine tile (lose)
37+
// 0xD     = [FLAG-WIN]
38-
// 0xF     = hidden tile
38+
// 0xE     = [FLAG-LOSE]
39
// 0xF     = [HIDDEN]
40
41
42
append = [ $arg1 = (concat (getalias $arg1) $arg2) ]
43
precf  = [ substr $arg1 0 (+ (strstr $arg1 .) (+ $arg2 1)) ]
44
ms     = [ toggleui minesweeper ]
45-
_ms_clock = [
45+
46-
    local n ; n = (mod $arg1 1000)
46+
47-
    format "%1:%2%3" (div $arg1 1000) (if (< $n 100) [? (< $n 10) "00" "0"]) $n
47+
48
    n = (* $_ms_th $_ms_tw)
49
    // calculate board sizes/mines and apply limits
50
    if $arg1 [ _ms_th = (max (min $arg1 24) 9) ]
51
    if $arg2 [ _ms_tw = (max (min $arg2 30) 9) ]
52
    _ms_tm = (max (min (div $n 3) $_ms_tm) (div $n 10))
53
]
54
55
_ms_set_markers = [
56
    // in case of failure, update flag markers
57
    if (= $_ms_gm 2) [
58
        looplist i (listdel $_ms_fp $_ms_mp) [
59
            _ms_tile_open 0xE $i 0
60
        ]
61
    ]
62
    // update still hidden mine tiles
63
    looplist i $_ms_mp [
64
        if (= (at $_ms_cf $i) 0xF) [
65
            _ms_tile_open (case $_ms_gm 2 0xA 3 0xB) $i 0
66
        ]
67
    ]
68
]
69
70
_ms_tile_find = [
71
    // set variables and calculate repeated math
72
    local n r p c1 c2 c3 c4 c5 c6 c7 c8
73
    r = (mod $arg9 $_ms_w) ; p = (- $_ms_w 1)
74
    // calculate positions of adjacent tiles
75
    c1 = (- $arg9 $_ms_w)   // UP
76
    c2 = (+ $arg9 $_ms_w)   // DN
77
    c3 = (- $arg9 1)        // LT
78
    c4 = (+ $arg9 1)        // RT
79
    c5 = (- $arg9 $_ms_w 1) // UP-LT
80
    c6 = (- $arg9 $p)       // UP-RT
81
    c7 = (+ $arg9 $p)       // DN-LT
82
    c8 = (+ $arg9 $_ms_w 1) // DN-RT
83
    // if adjacent tiles fit conditions, execute
84
    if (> $c1 -1)                     $arg1 // UP
85
    if (< $c2 $_ms_bt)                $arg2 // DN
86
    if (> $r 0)                       $arg3 // LT
87-
    local n r  p c1 c2 c3 c4 c5 c6 c7 c8
87+
88-
    n = 0 ; r = (mod $arg9 $_ms_w) ; p = (- $_ms_w 1)
88+
89
    if (&& [> $c6 -1] [< $r $p])      $arg6 // UP-RT
90
    if (&& [< $c7 $_ms_bt] [> $r 0])  $arg7 // DN-LT
91
    if (&& [< $c8 $_ms_bt] [< $r $p]) $arg8 // DN-RT
92
    arg10
93
]
94
95
_ms_tile_expl = [
96
    // add tile to work queue
97
    append _ms_tq $arg1
98
    // execute content as long as list is populated
99
    while [listlen $_ms_tq] [
100
        do [ _ms_tile_find @(loopconcat+ i 1 8 [
101
                result [[ n = $c@@i ; @@@arg2]]
102
        ]) (at $_ms_tq 0) ]
103
        _ms_tq = (sublist $_ms_tq 1)
104
    ]
105
]
106
107
_ms_tile_read = [
108
    local n t
109
    n = (at $_ms_nf $arg1)
110
    case $n 0x9 [
111
        // mark the mine as hit
112
        _ms_tile_open 0x9 $arg1 0
113
        _ms_hc = (+ $_ms_hc 1)
114
        // if lives are enabled, reduce by one, otherwise
115-
        do [ _ms_tile_find @(
115+
        if (&& $_ms_ua $_ms_uc) [ _ms_uc = (- $_ms_uc 1) ] [
116-
            loopconcat+ i 1 8 [result [[
116+
            // game over, undiscovered mines are shown and flag markers updated
117-
                n = $c@@i
117+
118-
                @@@arg2
118+
119-
            ]]]
119+
120-
        ) (at $_ms_tq 0) ]
120+
121
    ] 0x0 [
122
        _ms_tile_open 0x0 $arg1 1
123
        // explore all surrounding tiles until number border
124
        _ms_tile_expl $arg1 [
125-
_ms_tile_view = [
125+
            // make sure the tile is hidden
126
            if (= (at $_ms_cf $n) 0xF) [
127
                // ensure tile is a blank and add to work queue if not there
128
                if (&& [= (at $_ms_nf $n) 0x0] [< (listfind= $_ms_tq $n) 0]) [
129
                    append _ms_tq $n
130
                ]
131-
        append _ms_hp $arg1
131+
                // reveal the tested tile and add to completion counter
132
                _ms_tile_open (at $_ms_nf $n) $n 1
133
            ]
134-
        if (&& $_ms_ua $_ms_u) [ _ms_u = (- $_ms_u 1) ] [
134+
135-
            // game over, board is populated by hit and undiscovered mines and flag marks
135+
136
    // when the completion counter matches the safe tiles, trigger victory and update any hidden mine tiles
137
    if (= $_ms_pc (- $_ms_bt $_ms_m)) [
138
        _ms_gm = 3 ; _ms_t2 = (getmillis)
139
        _ms_set_markers
140
    ]
141
]
142
143
_ms_tile_open = [
144-
            // make sure the tile is not revealed, otherwise skip tile
144+
145
    if (= $_ms_gm 0) [
146-
                // ensure tile is blank and not in work queue, then add it
146+
        _ms_init_game $arg2
147-
                if (&& [= (at $_ms_nf $n) 0x0] [< (indexof $_ms_tq $n) 0]) [
147+
148
        _ms_t1 = (getmillis)
149
    ]
150-
                // reveal the tile around the blank tile and add to completion percentage
150+
151
    _ms_cf = (listsplice $_ms_cf (tohex $arg1) $arg2 1)
152
    if $arg3 [ _ms_pc = (+ $_ms_pc 1) ]
153
]
154
155-
    // in case of board completion, victory
155+
_ms_init_main = [
156
    // initialise board variables
157
    _ms_th = $arg1
158
    _ms_tw = $arg2
159
    _ms_m  = $arg3
160
    _ms_h  = $_ms_th
161
    _ms_w  = $_ms_tw
162-
_ms_make_board = [
162+
163-
    local i n r
163+
164-
    i = 0
164+
    if $_ms_ua [ _ms_uc  = (+ (div $_ms_m 20) 1) ]
165
    _ms_bt = (* $_ms_h $_ms_w) // total tiles
166
    _ms_pc = 0 // percentage counter
167
    _ms_t1 = 0 // timer start
168
    _ms_t2 = 0 // timer stop
169
    _ms_gm = 0 // gamemode: 0/1/2/3 = waiting/active/won/lost
170
    _ms_hc = 0 // [MINE-HIT] counter
171
    _ms_fc = 0 // [FLAG] counter
172
    // initialise boards
173-
    if $_ms_ua [ _ms_u  = (+ (div $_ms_m 20) 1) ]
173+
174
    _ms_mf = (loopconcat i $_ms_bt [result "0"])
175
    _ms_cf = (loopconcat i $_ms_bt [result 0xF])
176
    // initialise supplementary lists
177
    _ms_tq = ""
178
    _ms_mp = ""
179-
    _ms_hc = 0 // mine hit counter
179+
180-
    _ms_fc = 0 // flag counter
180+
181
]
182
183
_ms_init_game = [
184
    local a i r
185
    // find all adjacent tiles of the first-click tile
186
    do [ _ms_tile_find @(loopconcat+ i 1 8 [
187
        result [[ append a $c@@i ]]
188-
    _ms_hp = ""
188+
    ]) $arg1 [ i = 0 ] ]
189
    // initialise the minefield board
190
    while [< $i $_ms_m] [
191-
    // generate the mines board
191+
192
        // avoid writing to a previously picked spot
193
        if (&& [! (at $_ms_mf $r)] [!= $r $arg1] [< (listfind= $a $r) 0]) [
194
            _ms_mf = (listsplice $_ms_mf "1" $r 1)
195-
        if (! (at $_ms_mf $r)) [
195+
196
            i = (+ $i 1)
197
        ]
198
    ]
199
    // initialise the numerical board
200
    loop p $_ms_bt [
201-
    // generate the numbers board
201+
202
            do [ _ms_tile_find @(loopconcat+ i 1 8 [
203
                result [[ n = (+ (at $_ms_mf $c@@i) $n) ]]
204-
            do [ _ms_tile_find @(
204+
            ]) $p [ append _ms_nf (tohex $n) ] ]
205-
                loopconcat+ i 1 8 [result [[
205+
206-
                    n = (+ (at $_ms_mf $c@@i) $n)
206+
207-
                ]]]
207+
208-
            ) $p [ append _ms_nf (tohex $n) ] ]
208+
209
UImenu "minesweeper" [
210
    uieschide 0
211
    uihlist 0.01 [
212
        uivlist 0 [
213
            UIbox "box d d" 0 0.04 [
214
                uiclamp 1 1
215
                uispace 0.01 0 [
216
                    uialign -1
217
                    uihlist 0.01 [
218
                        local n ; n = (- $_ms_m $_ms_fc $_ms_hc)
219
                        uiimage "media/interface/minesweeper/flag.png" 0.032 0.032
220
                        uitext (concatword (? (< $n 0) "^f3") $n) 0.7
221
                    ]
222
                ]
223
                uispace 0.01 0 [
224-
                        uitext (concatword (? (< $n 0) "^f3") $n) 0.65
224+
225
                    uihlist 0.01 [
226
                        local n t
227
                        t = (case $_ms_gm 0 0 1 (- $getmillis $_ms_t1) () (- $_ms_t2 $_ms_t1))
228
                        n = (mod $t 1000)
229
                        uitext (format "%1:%2%3" (div $t 1000) (if (< $n 100) [? (< $n 10) "00" "0"]) $n) 0.7
230-
                        uitext (_ms_clock (
230+
231-
                            case $_ms_gm 0 0 1 (- $getmillis $_ms_t1) () (- $_ms_t2 $_ms_t1)
231+
232-
                        )) 0.65
232+
233
            ]
234
            uispace 0.01 0.001 [
235
                uigroup [
236
                    uigrid $_ms_w 0 0 [
237
                        loop i $_ms_bt [
238-
                uigrid $_ms_w 0 0 [
238+
                            local c ; c = (at $_ms_cf $i)
239-
                    loop i $_ms_bt [
239+
                            uiimage (format "%1media/interface/minesweeper/tiles/%2.png" (
240-
                        local c ;  c = (at $_ms_cf $i)
240+
                                ? (|| [
241-
                        uiimage (format "media/interface/minesweeper/tiles/%1.png" $c) 0.04 0.04 [
241+
                                    && uihover+? [! $uihold] [|| [= $c 0xF] [= $c 0xC]]
242-
                            if (< $_ms_gm 2) [
242+
243-
                                if (uihover?) [
243+
                                    && $_ms_qh [> (listfind= $_ms_tr $i) -1]
244-
                                    p = $i // last 
244+
                                ]) "<mad:1.1/0.8/0.8>"
245-
                                    cond [= $c 0xF] [ // HIDDEN TILE
245+
                            ) (? (&& [= $c 0xF] uihold+?) 0x0 $c)) 0.04 0.04 [
246-
                                        if (uihold?) [
246+
                                if (&& [< $_ms_gm 2] uihover?) [
247-
                                            uioutline 0xFF0000
247+
                                    _ms_pos = $i
248-
                                        ] [ if (uieschold?) [
248+
                                    cases $c 0xF [
249-
                                            uioutline 0xFF0000
249+
                                        uirelease [ _ms_tile_read $i ]
250-
                                        ] [ uioutline 0 ] ]
250+
                                        uiescpress [
251-
                                        uirelease [ _ms_tile_view $i ]
251+
252-
                                        uiescrelease [
252+
253
                                            _ms_fc = (+ $_ms_fc 1)
254
                                        ]
255
                                    ] 0xC [
256
                                        uiescpress [
257-
                                    ] [= $c 0xC] [ // FLAGGED TILE
257+
258-
                                        uioutline (? (uieschold?) 0xFF0000 0)
258+
259-
                                        uiescrelease [
259+
260
                                        ]
261
                                    ]
262
                                ]
263
                            ]
264-
                                    ] [!= $c 0x9] [ // -NOT- HIT MINE TILE
264+
265-
                                        if (|| [uiescpress?] [uialtpress?]) [
265+
266-
                                            _ms_tf = 0 ; o = 0
266+
                    if (&& [< $_ms_gm 2] uihover? [< 0x0 (at $_ms_cf $_ms_pos) 0x9]) [
267-
                                            _ms_tile_expl $i [
267+
                        uitarget 0 0 [
268-
                                                case (at $_ms_cf $n) 0xF [
268+
                            uiclamp 1 1 1 1
269-
                                                    append _ms_tr $n
269+
                            if (|| uialthold? [&& uieschold? uihold?]) [
270-
                                                ] 0xC [
270+
                                local focuslist
271-
                                                    _ms_tf = (+ $_ms_tf 1)
271+
                                _ms_sm = 0
272-
                                                ] 0x9 [
272+
                                _ms_qh = 1
273-
                                                    _ms_tf = (+ $_ms_tf 1)
273+
                                _ms_tile_expl $_ms_pos [
274-
                                                ]
274+
                                    case (at $_ms_cf $n) 0xF [
275-
                                            ]
275+
                                        append focuslist $n
276
                                    ] 0xC [
277-
                                        if (|| [uieschold?] [uialthold?]) [ uioutline 0x00FFFF ]
277+
                                        _ms_sm = (+ $_ms_sm 1)
278-
                                        if (|| [uipress?] [uialtpress?]) [ o = 1 ]
278+
                                    ] 0x9 [
279-
                                        if (|| [uirelease?] [uialtrelease?]) [
279+
                                        _ms_sm = (+ $_ms_sm 1)
280-
                                            if (= $_ms_tf (at $_ms_nf $i)) [
280+
281-
                                                looplist n $_ms_tr [
281+
282-
                                                    if (= (at $_ms_cf $n) 0xF) [ _ms_tile_view $n ]
282+
                                _ms_tr = $focuslist
283-
                                                ]
283+
284-
                                            ]
284+
                            if (|| uialtrelease? [&& $_ms_qh [! $uialthold?] [|| [! $uihold?] [! $uieschold?]]]) [
285-
                                            o = 0
285+
                                local p ; p = (at $_ms_nf $_ms_pos)
286-
                                            uieschold [] [ _ms_tr = "" ]
286+
                                if (&& [= $_ms_sm $p] [< 0x0 $p 0x9]) [
287
                                    looplist n $_ms_tr [
288-
                                        uiescrelease [ _ms_tr = "" ]
288+
                                        if (= (at $_ms_cf $n) 0xF) [ _ms_tile_read $n ]
289
                                    ]
290
                                ]
291-
                                    if (= (getalias p) $i) [ _ms_tr = "" ] [
291+
                                _ms_qh = 0
292-
                                        if (> (indexof $_ms_tr $i) -1) [ uioutline (? $o 0xFF0000 0) ]
292+
293
                        ]
294
                    ] [ _ms_qh = 0 ]
295-
                                uiclamp* 1 1 1 1
295+
296
            ]
297
            UIbox "box d d" 0 0.04 [
298
                uiclamp 1 1
299
                uispace 0.01 0 [
300
                    uialign -1
301
                    uihlist 0.01 [
302
                        uiimage "media/interface/minesweeper/life.png" 0.032 0.032
303
                        uitext (? $_ms_ua (? $_ms_uc $_ms_uc (concatword "^f3" $_ms_uc)) "--") 0.7
304
                    ]
305
                ]
306
                uispace 0.01 0 [
307-
                        uitext (? $_ms_ua (? $_ms_u $_ms_u (concatword "^f3" $_ms_u)) "-") 0.65
307+
308
                    uihlist 0.01 [
309
                        uitext (format "^f%1%2%%" (case $_ms_gm 2 3 3 0 () 7) (
310
                            precf (*f (divf $_ms_pc (- $_ms_bt $_ms_m)) 100) 1
311
                        )) 0.7
312
                        uiimage "media/interface/minesweeper/globe.png" 0.032 0.032
313
                    ]
314
                ]
315-
                        )) 0.65
315+
316
        ]
317
        uivlist 0 [
318
            uiclamp 0 0 1 1
319
            UIbox "box d n n" 0.14 0 [
320
                uivlist 0.02 [
321
                    UIbuttonarea "" 0.14 0.032 [
322
                        uirelease [ _ms_init_main $_ms_th $_ms_tw $_ms_tm ]
323
                        uitext "New Game" 0.6
324
                    ]
325
                    uigrid 2 0.01 0.01 [
326-
                        uirelease [ _ms_make_board $_ms_th $_ms_tw $_ms_tm ]
326+
327
                        uifill 0 0.032 [ uifield _ms_th 3 [ _ms_set_options $_ms_th 0 ] 0.55 style_generic_focus ]
328
                        uiimage "media/interface/minesweeper/width.png" 0.032 0.032
329
                        uifill 0 0.032 [ uifield _ms_tw 3 [ _ms_set_options 0 $_ms_tw ] 0.55 style_generic_focus ]
330
                        uiimage "media/interface/minesweeper/mine.png" 0.032 0.032
331
                        uifill 0 0.032 [ uifield _ms_tm 3 [ _ms_set_options 0 0 ] 0.55 style_generic_focus ]
332
                    ]
333
                    UIbuttonarea "" 0.14 0.032 [
334
                        uirelease [ _ms_ua = (! $_ms_ua) ]
335
                        uihlist 0.01 [
336
                            UIcheckbox $_ms_ua 0.016 0 0
337
                            uitext "Use Lives" 0.55
338
                        ]
339
                    ]
340
                ]
341
            ]
342
            uivlist 0.01 [
343
                UIbox "box d n n" 0.14 0 [
344
                    uiclamp 1 1
345
                    uivlist 0 [
346
                        uiclamp 1 1
347
                        UIbuttonarea "hold2" 0.14 0.032 [
348
                            uirelease [ _ms_init_main 9 9 10 ]
349
                            uitext (concatword (uihover? "^f0" "") "Easy") 0.55
350
                        ]
351
                        UIbuttonarea "hold2" 0.14 0.032 [
352-
                            uirelease [ _ms_make_board 9 9 10 ]
352+
                            uirelease [ _ms_init_main 16 16 40 ]
353-
                            uitext (concatword (? $uihover? "^f0") "Easy") 0.55
353+
                            uitext (concatword (uihover? "^f2" "") "Normal") 0.55
354
                        ]
355
                        UIbuttonarea "hold2" 0.14 0.032 [
356-
                            uirelease [ _ms_make_board 16 16 40 ]
356+
                            uirelease [ _ms_init_main 16 30 80 ]
357-
                            uitext (concatword (? $uihover? "^f2") "Normal") 0.55
357+
                            uitext (concatword (uihover? "^f3" "") "Hard") 0.55
358
                        ]
359
                        uiclamp* 1 1
360-
                            uirelease [ _ms_make_board 16 30 80 ]
360+
361-
                            uitext (concatword (? $uihover? "^f3") "Hard") 0.55
361+
362
                uitext (concat "^f4FPS:" (tabify $getfps 1)) 0.45
363
                uialign- 1 1
364
            ]
365
        ]
366
    ]
367
] [
368
    // if game is running, restore clock on show
369
    if (= $_ms_gm 1) [
370
        _ms_t1 = (- $getmillis $_ms_t2)
371
    ] [ _ms_init_main $_ms_th $_ms_tw $_ms_tm ]
372
] [
373
    // if game is running, suspend clock on hide
374
    if (= $_ms_gm 1) [
375-
    ] [ _ms_make_board $_ms_th $_ms_tw $_ms_tm ]
375+
376
    ]
377
] [] [ uitext "Minesweeper C.S." 0.8 ]