Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- _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_tu = 1 // temp U
- _ms_fc = 0 // placed flags counter
- _ms_hc = 0 // hit mines counter
- _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_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_hp = "" // supplementary list with hit mine positions
- _ms_fp = "" // supplementary list with flag positions
- _ms_tr = "" // supplementary list with tile reveal positions
- _ms_tq = "" // supplementary list with blank tile queue
- // ----------------------------------------- //
- // ---------------- Tile ID ---------------- //
- // ----------------------------------------- //
- // 0 = blank tile
- // 1-8 = number tiles
- // 9 = mine tile (failure)
- // 10 = mine tile (victory)
- // 11 = hit mine tile
- // 12 = flagged tile
- // 13 = flagged mine tile (success)
- // 14 = flagged mine tile (failure)
- // 15 = hidden tile
- append = [ $arg1 = (concat (getalias $arg1) $arg2) ]
- precf = [ substr $arg1 0 (+ (strstr $arg1 .) (+ $arg2 1)) ]
- ms = [ toggleui minesweeper ]
- _ms_clock = [
- local n
- n = (mod $arg1 1000)
- format "%1:%2%3" (div $arg1 1000) (if (< $n 100) [? (< $n 10) "00" "0"]) $n
- ]
- _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 (= (at $_ms_nf $i) "9") [
- _ms_tile_open "13" $i 0
- ] [ _ms_tile_open "14" $i 0 ]
- ]
- ]
- // update mine markers, discounting flagged ones
- looplist i $_ms_mp [
- case $_ms_gm 2 [
- if (< (indexof "11 13 14" (at $_ms_cf $i)) 0) [
- _ms_tile_open "9" $i 0
- ]
- ] 3 [
- if (< (indexof "11 12" (at $_ms_cf $i)) 0) [
- _ms_tile_open "10" $i 0
- ]
- ]
- ]
- ]
- _ms_tile_open = [
- // start the game if still in waiting phase
- if (= $_ms_gm 0) [
- _ms_gm = 1
- _ms_t1 = (getmillis)
- ]
- // 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 = [
- // set variables and calculate repeated math
- local n r p c1 c2 c3 c4 c5 c6 c7 c8
- n = 0 ; r = (mod $arg9 $_ms_w) ; p = (- $_ms_w 1)
- // calculate positions of adjacent tiles
- 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 $p) // UP-RT
- c7 = (+ $arg9 $p) // DN-LT
- c8 = (+ $arg9 $_ms_w 1) // DN-RT
- // if adjacent tiles fit conditions, execute
- if (> $c1 -1) $arg1 // UP
- if (< $c2 $_ms_bt) $arg2 // DN
- if (> $r 0) $arg3 // LT
- if (< $r $p) $arg4 // RT
- if (&& [> $c5 -1] [> $r 0]) $arg5 // UP-LT
- if (&& [> $c6 -1] [< $r $p]) $arg6 // UP-RT
- if (&& [< $c7 $_ms_bt] [> $r 0]) $arg7 // DN-LT
- if (&& [< $c8 $_ms_bt] [< $r $p]) $arg8 // DN-RT
- arg10
- ]
- _ms_tile_expl = [
- // add tile to work queue
- append _ms_tq $arg1
- // execute content as long as list is populated
- while [listlen $_ms_tq] [
- do [ _ms_tile_find @(
- loopconcat+ i 1 8 [result [[
- n = $c@@i
- @@@arg2
- ]]]
- ) (at $_ms_tq 0) ]
- _ms_tq = (sublist $_ms_tq 1)
- ]
- ]
- _ms_tile_view = [
- local n t
- n = (at $_ms_nf $arg1)
- case $n "9" [
- // mark the mine as hit
- _ms_tile_open "11" $arg1 0
- append _ms_hp $arg1
- _ms_hc = (+ $_ms_hc 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
- _ms_gm = 2
- _ms_t2 = (getmillis)
- _ms_set_markers
- ]
- ] "0" [
- _ms_tile_open "0" $arg1 1
- // explore all surrounding tiles until number border
- _ms_tile_expl $arg1 [
- // make sure the tile is not revealed, otherwise skip tile
- if (= (at $_ms_cf $n) "15") [
- // ensure tile is blank and not in work queue, then add it
- if (&& [= (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
- ]
- ]
- ] () [ _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_ua = $_ms_tu
- if $_ms_ua [ _ms_u = (+ (div $_ms_m 20) 1) ]
- _ms_bt = (* $_ms_h $_ms_w)
- _ms_pc = 0
- _ms_t1 = 0
- _ms_t2 = 0
- _ms_gm = 0
- _ms_hc = 0
- _ms_fc = 0
- // initialise boards
- _ms_nf = ""
- _ms_mf = (loopconcat i $_ms_bt [result "0"])
- _ms_cf = (loopconcat i $_ms_bt [result "15"])
- // initialise supplementary lists
- _ms_tq = ""
- _ms_mp = ""
- _ms_hp = ""
- _ms_fp = ""
- _ms_tr = ""
- // 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 p $_ms_bt [
- if (at $_ms_mf $p) [ append _ms_nf "9" ] [
- do [ _ms_tile_find @(
- loopconcat+ i 1 8 [result [[
- n = (+ (at $_ms_mf $c@@i) $n)
- ]]]
- ) $p [ append _ms_nf $n ] ]
- ]
- ]
- ]
- // styles for the difficulty buttons
- _style_button_ms0 = [
- uihover [
- uicolor 0xF0E8E0
- uihold [ uimodvgradient 0x4DFF20 0x279910 ] [
- uimodvgradient 0x279910 0x4DFF20
- ]
- uioutline $_c_line
- ]
- ]
- _style_button_ms1 = [
- uihover [
- uicolor 0xF0E8E0
- uihold [ uimodvgradient 0xFFE020 0x998010 ] [
- uimodvgradient 0x998010 0xFFE020
- ]
- uioutline $_c_line
- ]
- ]
- _style_button_ms2 = [
- uihover [
- uicolor 0xF0E8E0
- uihold [ uimodvgradient 0xFF2020 0x991010 ] [
- uimodvgradient 0x991010 0xFF2020
- ]
- uioutline $_c_line
- ]
- ]
- ui_menu "minesweeper" [
- uieschide (> $_ms_gm 1)
- uihlist 0.01 [
- uivlist 0 [
- ui_title [
- uiclamp 1 1
- uispace 0.01 0 [
- uialign -1
- uihlist 0.01 [
- local n ; n = (- $_ms_m $_ms_fc $_ms_hc)
- uiimage "media/texture/minesweeper/flag.png" 0.028 0.028
- uitext (concatword (? (< $n 0) "^f3") $n) 0.7
- ]
- ]
- uispace 0.01 0 [
- uialign 1
- uihlist 0.01 [
- uitext (_ms_clock (
- case $_ms_gm 0 0 1 (- $getmillis $_ms_t1) () (- $_ms_t2 $_ms_t1)
- )) 0.7
- uiimage "media/texture/minesweeper/clock.png" 0.028 0.028
- ]
- ]
- ] 0 0.032
- uivlist 0 [ loop y $_ms_h [
- uihlist 0 [ loop x $_ms_w [
- n = (+ (* $y $_ms_w) $x)
- uiimage (format "media/texture/minesweeper/tiles/%1.png" (at $_ms_cf $n)) 0.04 0.04 [
- if (< $_ms_gm 2) [
- if (uihover?) [
- p = $n
- cond [= (at $_ms_cf $n) "15"] [
- if (uihold?) [
- uioutline 0xFF0000
- ] [ if (uieschold?) [
- uioutline 0xFF0000
- ] [ uioutline 0 ] ]
- uirelease [ _ms_tile_view $n ]
- uiescrelease [
- _ms_tile_open "12" $n 0
- append _ms_fp $n
- _ms_fc = (+ $_ms_fc 1)
- ]
- ] [= (at $_ms_cf $n) "12"] [
- uioutline (? (uieschold?) 0xFF0000 0)
- uiescrelease [
- _ms_tile_open "15" $n 0
- _ms_fp = (listdel $_ms_fp $n)
- _ms_fc = (- $_ms_fc 1)
- ]
- ] [!= (at $_ms_cf $n) "11"] [
- if (|| [uiescpress?] [uialtpress?]) [
- _ms_tf = 0 ; o = 0
- _ms_tile_expl $n [
- case (at $_ms_cf $n) "15" [
- append _ms_tr $n
- ] "12" [
- _ms_tf = (+ $_ms_tf 1)
- ] "11" [
- _ms_tf = (+ $_ms_tf 1)
- ]
- ]
- ]
- if (|| [uieschold?] [uialthold?]) [ uioutline 0x00FFFF ]
- if (|| [uipress?] [uialtpress?]) [ o = 1 ]
- if (|| [uirelease?] [uialtrelease?]) [
- if (= $_ms_tf (at $_ms_nf $n)) [
- looplist i $_ms_tr [
- if (= (at $_ms_cf $i) "15") [ _ms_tile_view $i ]
- ]
- ]
- o = 0
- uieschold [] [ _ms_tr = "" ]
- ]
- uiescrelease [ _ms_tr = "" ]
- ]
- ] [
- if (= (getalias p) $n) [ _ms_tr = "" ] [
- if (> (indexof $_ms_tr $n) -1) [ uioutline (? $o 0xFF0000 0) ]
- ]
- ]
- uiclamp* 1 1 1 1
- ]
- ]
- ] ]
- ] ]
- ui_title [
- uiclamp 1 1
- uispace 0.01 0 [
- uialign -1
- uihlist 0.01 [
- uiimage "media/texture/minesweeper/life.png" 0.028 0.028
- uitext (? $_ms_ua (? $_ms_u $_ms_u (concatword "^f3" $_ms_u)) "-") 0.7
- ]
- ]
- uispace 0.01 0 [
- uialign 1
- uihlist 0.01 [
- uitext (format "^f%1%2%%" (case $_ms_gm 2 3 3 0 () 7) (
- precf (*f (divf $_ms_pc (- $_ms_bt $_ms_m)) 100) 1
- )) 0.7
- uiimage "media/texture/minesweeper/globe.png" 0.028 0.028
- ]
- ]
- ] 0.032
- ]
- //ui_bar 0 1
- uivlist 0 [
- uiclamp 0 0 1 1
- uioutline 0x484848 0 0 [
- uivlist 0 [
- ui_button "" [ uitext "New Game" 0.6 ] 0 0.032 [
- _ms_make_board $_ms_th $_ms_tw $_ms_tm
- ] 0 [ uiclamp 1 1 ]
- uispace 0 0.02 [
- uivlist 0.01 [
- uihlist 0.01 [
- uiimage "media/texture/minesweeper/height.png" 0.028 0.028
- uifield _ms_th 3 [ _ms_set_options $_ms_th 0 ] 0.6 [ _style_generic_focus ]
- ]
- uihlist 0.01 [
- uiimage "media/texture/minesweeper/width.png" 0.028 0.028
- uifield _ms_tw 3 [ _ms_set_options 0 $_ms_tw ] 0.6 [ _style_generic_focus ]
- ]
- uihlist 0.01 [
- uiimage "media/texture/minesweeper/mine.png" 0.028 0.028
- uifield _ms_tm 3 [ _ms_set_options 0 0 ] 0.6 [ _style_generic_focus ]
- ]
- ]
- ]
- ui_button "" [
- ui_checkbox [^ $_ms_ua $_ms_tu] 0.016 0x4080D0 (? (+ $_ms_ua $_ms_tu) 0x40D040 0x484848)
- uitext "Use Lives" 0.6
- ] 0 0.032 [
- _ms_tu = (! $_ms_tu)
- if (! $_ms_gm) [ _ms_ua = $_ms_tu ]
- ] -1 [ uiclamp 1 1 ]
- ]
- ]
- uivlist 0 [
- uiclamp 1 1
- ui_title [ uitext "Difficulty" 0.7 ] 0 0.032
- ui_button "ms0" [ uitext "Easy" 0.6 ] 0 0.032 [ _ms_make_board 9 9 10 ]
- ui_button "ms1" [ uitext "Normal" 0.6 ] 0 0.032 [ _ms_make_board 16 16 40 ]
- ui_button "ms2" [ uitext "Hard" 0.6 ] 0 0.032 [ _ms_make_board 16 30 80 ]
- uiclamp* 1 1
- ]
- uiclamp* 1 1
- ]
- ]
- ] [
- // if game is running, restore clock on show
- if (= $_ms_gm 1) [
- _ms_t1 = (- $getmillis $_ms_t2)
- ] [ _ms_make_board $_ms_th $_ms_tw $_ms_tm ]
- ] [
- // if game is running, suspend clock on hide
- if (= $_ms_gm 1) [
- _ms_t2 = (- $getmillis $_ms_t1)
- ]
- ] [] [ uitext "Minesweeper C.S." 0.8 ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement