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_mp = "" // supplement list with mine positions
- _ms_fp = "" // supplement list with flag positions
- _ms_nf = "" // board containing tile numbers
- _ms_cf = "" // board of completion (displayed)
- _ms_tq = "" // supplement list with tile reveal queue
- _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_open = [
- 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_cf = (listsplice $_ms_cf "R" $arg1 1)
- // 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
- looplist i $_ms_mp [
- if (!=s (at $_ms_cf $i) "R") [
- _ms_cf = (listsplice $_ms_cf "X" $i 1)
- ]
- ]
- _ms_gm = 2
- _ms_t2 = (getmillis)
- ]
- ] "R" [
- // do nothing here, simply skip
- ] "F" [
- // do nothing here, simply skip
- ] "0" [
- // explore all surrounding tiles until number border
- append _ms_tq $arg1
- while [listlen $_ms_tq] [
- do [ _ms_tile_find @(
- loopconcat i 8 [result [[
- // make sure the tile is not revealed, otherwise skip tile
- if (=s (at $_ms_cf $c@@(+ $i 1)) "H") [
- // make sure tile is blank and not in work queue, then add it
- if (&& [=s (at $_ms_nf $c@@@@(+ $i 1)) "0"] [< (indexof $_ms_tq $c@@@@(+ $i 1)) 0]) [
- append _ms_tq $c@@@@(+ $i 1)
- ]
- // reveal the tile around the blank tile and add to completion percentage
- _ms_cf = (listsplice $_ms_cf (at $_ms_nf $c@@@(+ $i 1)) $c@@@(+ $i 1) 1)
- _ms_pc = (+ $_ms_pc 1)
- ]
- ]]]
- ) (at $_ms_tq 0) ]
- // remove used tile from work queue
- _ms_tq = (sublist $_ms_tq 1)
- ]
- ] () [
- // reveal the tile and add to completion percentage
- _ms_cf = (listsplice $_ms_cf $n $arg1 1)
- _ms_pc = (+ $_ms_pc 1)
- ]
- // in case of board completion, victory
- if (= $_ms_pc (- $_ms_bt $_ms_m)) [
- _ms_gm = 3 ; _ms_t2 = (getmillis)
- ]
- ]
- ]
- _ms_make_board = [
- // Initialise variables
- local i n r
- _ms_th = $arg1 ; _ms_tw = $arg2 ; _ms_m = $arg3
- _ms_h = $_ms_th ; _ms_w = $_ms_tw ; _ms_tm = $_ms_m
- _ms_bt = (* $_ms_h $_ms_w) ; _ms_pc = 0; i = 0
- _ms_um = (+ (div $_ms_m 20) 1); _ms_u = $_ms_um
- _ms_t1 = (getmillis) ; _ms_t2 = 0 ; _ms_tq = ""
- _ms_gm = 1 ; _ms_mp = "" ; _ms_fp = "" ; _ms_nf = ""
- // Initialise boards
- _ms_mf = (loopconcat i $_ms_bt [result "0"])
- _ms_cf = (loopconcat i $_ms_bt [result "H"])
- // Begin randomly placing mines
- while [< $i $_ms_m] [
- r = (rnd $_ms_bt)
- // Make sure it's not 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 complete board with numbers
- 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)) [
- _ms_tile_open @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) [
- guistrut 0.5
- guilist [
- guilist [
- guialign 0 [ guitext "" "minesweeper/resize_h.png" ]
- guifield _ms_th 2 [ _ms_th = (max (min $_ms_th 24) 9) ]
- ]
- guilist [
- guialign 0 [ guitext "" "minesweeper/resize_w.png" ]
- guifield _ms_tw 2 [ _ms_tw = (max (min $_ms_tw 30) 9) ]
- ]
- guistrut 0.5
- guilist [
- guialign 0 [ guitext "" "minesweeper/mine_raw.png" ]
- guifield _ms_tm 3 [ _ms_tm = (max (min $_ms_tm (div (* $_ms_th $_ms_tw) 3)) 10) ]
- ]
- ]
- 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
- //]
- //guibutton "^f6Show Board" [ _ms_cf = $_ms_nf ] 0
- ]
- ]
- ]
- ] 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement