Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- _ms_clock = [
- strreplace (concat (
- div $arg1 1000
- ) (
- padnum (padnum (mod $arg1 1000) 10) 100
- )) " " ":"
- ]
- padnum = [ concatword (? (< $arg1 $arg2) 0) $arg1 ]
- append = [ $arg1 = (concat (getalias $arg1) $arg2) ]
- precf = [ substr $arg1 0 (+ (strstr $arg1 ".") (+ $arg2 1)) ]
- _ms_h = 9 // board height
- _ms_w = 9 // board width
- _ms_m = 10 // board mines
- _ms_th = 9 // temp H
- _ms_tw = 9 // temp W
- _ms_tm = 10 // temp M
- _ms_bt = 0 // board tiles (total)
- _ms_t1 = 0 // timer start (ms)
- _ms_t2 = 0 // timer end (ms)
- _ms_ua = 1 // undo toggle
- _ms_u = 0 // undo counter
- _ms_um = 0 // undo max
- _ms_pc = 0 // completion percentage
- _ms_gm = 0 // game mode: 0 waiting; 1 active; 2 failure ; 3 victory
- _ms_mf = "" // board containing mine placement
- _ms_nf = "" // board containing tile numbers
- _ms_cf = "" // board of completion (displayed)
- _ms_mp = "" // supplementary list with mine positions
- _ms_fp = "" // supplementary list with flag positions
- _ms_tq = "" // supplementary list with tile reveal queue
- // ----------------------------------------- //
- // ---------------- Tile ID ---------------- //
- // ----------------------------------------- //
- // 0 = blank tile
- // 1-8 = number tiles
- // X = mine tile (failure)
- // HX = mine tile (victory)
- // R = hit mine tile
- // F = flagged tile
- // FX = flagged mine tile (success)
- // FF = flagged mine tile (failure)
- _ms_set_options = [
- local n
- n = (* $_ms_th $_ms_tw)
- // calculate board sizes/mines and apply limits
- if $arg1 [ _ms_th = (max (min $arg1 24) 9) ]
- if $arg2 [ _ms_tw = (max (min $arg2 30) 9) ]
- _ms_tm = (max (min (div $n 3) $_ms_tm) (div $n 10))
- ]
- _ms_set_markers = [
- // in case of failure, update flag markers
- if (= $_ms_gm 2) [
- looplist i $_ms_fp [
- if (=s (at $_ms_nf $i) "X") [
- _ms_tile_open "FX" $i 0
- ] [ _ms_tile_open "FF" $i 0 ]
- ]
- ]
- // update mine markers, discounting flagged ones
- looplist i $_ms_mp [
- if (< (indexof "R FX FF" (at $_ms_cf $i)) 0) [
- case $_ms_gm 2 [
- _ms_tile_open "X" $i 0
- ] 3 [
- _ms_tile_open "HX" $i 0
- ]
- ]
- ]
- ]
- _ms_tile_open = [
- // reveal # tile at given position and add to completion counter if needed
- _ms_cf = (listsplice $_ms_cf $arg1 $arg2 1)
- if $arg3 [ _ms_pc = (+ $_ms_pc 1) ]
- ]
- _ms_tile_find = [
- local n c1 c2 c3 c4 c5 c6 c7 c8
- n = 0
- c1 = (- $arg9 $_ms_w) // UP
- c2 = (+ $arg9 $_ms_w) // DN
- c3 = (- $arg9 1) // LT
- c4 = (+ $arg9 1) // RT
- c5 = (- $arg9 (+ $_ms_w 1)) // UP-LT
- c6 = (- $arg9 (- $_ms_w 1)) // UP-RT
- c7 = (+ $arg9 (- $_ms_w 1)) // DN-LT
- c8 = (+ $arg9 (+ $_ms_w 1)) // DN-RT
- if (> $c1 -1) $arg1 // UP
- if (< $c2 $_ms_bt) $arg2 // DN
- if (> (mod $arg9 $_ms_w) 0) $arg3 // LT
- if (< (mod $arg9 $_ms_w) (- $_ms_w 1)) $arg4 // RT
- if (&& [> $c5 -1] [> (mod $arg9 $_ms_w) 0]) $arg5 // UP-LT
- if (&& [> $c6 -1] [< (mod $arg9 $_ms_w) (- $_ms_w 1)]) $arg6 // UP-RT
- if (&& [< $c7 $_ms_bt] [> (mod $arg9 $_ms_w) 0]) $arg7 // DN-LT
- if (&& [< $c8 $_ms_bt] [< (mod $arg9 $_ms_w) (- $_ms_w 1)]) $arg8 // DN-RT
- arg10
- ]
- _ms_tile_view = [
- if (&& [= $_ms_gm 1] [=s (at $_ms_cf $arg1) "H"]) [
- local n t
- n = (at $_ms_nf $arg1)
- cases $n "X" [
- // mark the mine as hit
- _ms_tile_open "R" $arg1 0
- // if lives are enabled, reduce by one, otherwise
- if (&& $_ms_ua $_ms_u) [ _ms_u = (- $_ms_u 1) ] [
- // game over, board is populated by hit and undiscovered mines and flag marks
- _ms_gm = 2
- _ms_t2 = (getmillis)
- _ms_set_markers
- ]
- ] "R" [
- // do nothing here, simply skip
- ] "F" [
- // do nothing here, simply skip
- ] "0" [
- _ms_tile_open "0" $arg1 1
- append _ms_tq $arg1
- // explore all surrounding tiles until number border
- while [listlen $_ms_tq] [
- do [ _ms_tile_find @(
- loopconcat i 8 [result [[
- n = $c@@(+ $i 1)
- // make sure the tile is not revealed, otherwise skip tile
- if (=s (at $_ms_cf $n) "H") [
- // ensure tile is blank and not in work queue, then add it
- if (&& [=s (at $_ms_nf $n) "0"] [< (indexof $_ms_tq $n) 0]) [
- append _ms_tq $n
- ]
- // reveal the tile around the blank tile and add to completion percentage
- _ms_tile_open (at $_ms_nf $n) $n 1
- ]
- ]]]
- ) (at $_ms_tq 0) ]
- // remove used tile from work queue
- _ms_tq = (sublist $_ms_tq 1)
- ]
- ] () [ _ms_tile_open $n $arg1 1 ]
- // in case of board completion, victory
- if (= $_ms_pc (- $_ms_bt $_ms_m)) [
- _ms_gm = 3 ; _ms_t2 = (getmillis)
- _ms_set_markers
- ]
- ]
- ]
- _ms_make_board = [
- local i n r
- i = 0
- // initialise board variables
- _ms_th = $arg1
- _ms_tw = $arg2
- _ms_m = $arg3
- _ms_h = $_ms_th
- _ms_w = $_ms_tw
- _ms_tm = $_ms_m
- // initialise game variables
- _ms_bt = (* $_ms_h $_ms_w)
- _ms_um = (+ (div $_ms_m 20) 1)
- _ms_u = $_ms_um
- _ms_pc = 0
- _ms_t1 = 0
- _ms_t2 = 0
- _ms_gm = 0
- // initialise boards
- _ms_nf = ""
- _ms_mf = (loopconcat i $_ms_bt [result "0"])
- _ms_cf = (loopconcat i $_ms_bt [result "H"])
- // initialise supplementary lists
- _ms_tq = ""
- _ms_mp = ""
- _ms_fp = ""
- // generate the mines board
- while [< $i $_ms_m] [
- r = (rnd $_ms_bt)
- // avoid writing to a previously picked spot
- if (! (at $_ms_mf $r)) [
- _ms_mf = (listsplice $_ms_mf "1" $r 1)
- append _ms_mp $r
- i = (+ $i 1)
- ]
- ]
- // generate the numbers board
- loop y $_ms_bt [
- if (at $_ms_mf $y) [ append _ms_nf "X" ] [
- do [ _ms_tile_find @(
- loopconcat i 8 [result [[
- n = (+ (at $_ms_mf $c@@(+ $i 1)) $n)
- ]]]
- ) $y [ append _ms_nf $n ] ]
- ]
- ]
- ]
- newgui minesweeper [
- guistayopen [
- guititle (format "^f7Completed:^f%1 %2%% ^f7-- Time: ^f2%3" (
- result (case $_ms_gm 0 4 1 2 2 3 3 0)
- ) (
- absf (precf (*f (divf $_ms_pc (- $_ms_bt $_ms_m)) 100) 1)
- ) (
- _ms_clock (case $_ms_gm 0 0 1 (- (getmillis) $_ms_t1) () (- $_ms_t2 $_ms_t1))
- ))
- guistrut 0.5
- guilist [
- guilist [
- loop y $_ms_h [
- guilist [
- loop x $_ms_w [
- n = (+ (* $y $_ms_w) $x)
- guiimage (format "packages/icons/minesweeper/%1.png" (at $_ms_cf $n)) [
- if $test [ // placeholder for right-click flag marks
- cond (=s (at $_ms_cf @@n) "H") [
- _ms_tile_open "F" @@@n 0
- append _ms_fp @@@n
- ] (=s (at $_ms_cf @@n) "F") [
- _ms_tile_open "H" @@@n 0
- _ms_fp = (listdel $_ms_fp @@@n)
- ]
- ] [
- if (= $_ms_gm 0) [
- _ms_gm = 1
- _ms_t1 = (getmillis)
- _ms_t2 = 0
- ]
- _ms_tile_view @@n
- ]
- ] 0.5 0
- ]
- ]
- ]
- ]
- if (!=s $_ms_cf "") [ guistrut 1.5 ]
- guilist [
- guibutton "New Minefield" [
- _ms_make_board $_ms_th $_ms_tw $_ms_tm
- ] 0
- guitext (format "^f7Lives: ^f2%1/%2" $_ms_u $_ms_um) 0
- //if (! $_ms_gm) [
- guicheckbox test test
- guistrut 0.5
- guilist [
- guilist [
- guialign 0 [ guitext "" "minesweeper/resize_h.png" ]
- guifield _ms_th 2 [ _ms_set_options $_ms_th 0 ]
- ]
- guilist [
- guialign 0 [ guitext "" "minesweeper/resize_w.png" ]
- guifield _ms_tw 2 [ _ms_set_options 0 $_ms_tw ]
- ]
- guistrut 0.5
- guilist [
- guialign 0 [ guitext "" "minesweeper/mine_raw.png" ]
- guifield _ms_tm 3 [ _ms_set_options 0 0 ]
- ]
- ]
- guistrut 1
- guititle "^f2Difficulty"
- guistrut 0.5
- guibutton "Beginner" [ _ms_make_board 9 9 10 ] 0
- guibutton "Intermediate" [ _ms_make_board 16 16 40 ] 0
- guibutton "Expert" [ _ms_make_board 16 30 80 ] 0
- //]
- ]
- ]
- ]
- ] 0 [ _ms_make_board $_ms_th $_ms_tw $_ms_tm ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement