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_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 ---------------- //
- // ----------------------------------------- //
- // 0x0 = blank tile
- // 0x1-0x8 = number tiles
- // 0x9 = hit mine tile
- // 0xA = mine tile (win)
- // 0xB = mine tile (lose)
- // 0xC = flagged tile
- // 0xD = flagged mine tile (win)
- // 0xE = flagged mine tile (lose)
- // 0xF = 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 (listdel $_ms_fp $_ms_mp) [
- _ms_tile_open 0xE $i 0
- ]
- ]
- // update still hidden mine tiles
- looplist i $_ms_mp [
- if (= (at $_ms_cf $i) 0xF) [
- _ms_tile_open (case $_ms_gm 2 0xA 3 0xB) $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 percentage counter if needed
- _ms_cf = (listsplice $_ms_cf (tohex $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 0x9 [
- // mark the mine as hit
- _ms_tile_open 0x9 $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
- ]
- ] 0x0 [
- _ms_tile_open 0x0 $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) 0xF) [
- // ensure tile is blank and not in work queue, then add it
- if (&& [= (at $_ms_nf $n) 0x0] [< (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
- if $_ms_ua [ _ms_u = (+ (div $_ms_m 20) 1) ]
- _ms_bt = (* $_ms_h $_ms_w) // total tiles
- _ms_pc = 0 // percentage counter
- _ms_t1 = 0 // timer start
- _ms_t2 = 0 // timer stop
- _ms_gm = 0 // gamemode: 0/1/2/3 = waiting/active/won/lost
- _ms_hc = 0 // mine hit counter
- _ms_fc = 0 // flag counter
- // initialise boards
- _ms_nf = ""
- _ms_mf = (loopconcat i $_ms_bt [result "0"])
- _ms_cf = (loopconcat i $_ms_bt [result 0xF])
- // 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 0x9 ] [
- do [ _ms_tile_find @(
- loopconcat+ i 1 8 [result [[
- n = (+ (at $_ms_mf $c@@i) $n)
- ]]]
- ) $p [ append _ms_nf (tohex $n) ] ]
- ]
- ]
- ]
- UImenu "minesweeper" [
- uieschide 0
- uihlist 0.01 [
- uivlist 0 [
- UIbox "box d d" 0 0.04 [
- uiclamp 1 1
- uispace 0.01 0 [
- uialign -1
- uihlist 0.01 [
- local n ; n = (- $_ms_m $_ms_fc $_ms_hc)
- uiimage "media/interface/minesweeper/flag.png" 0.032 0.032
- uitext (concatword (? (< $n 0) "^f3") $n) 0.65
- ]
- ]
- 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.65
- uiimage "media/interface/minesweeper/clock.png" 0.032 0.032
- ]
- ]
- ]
- uispace 0.01 0.001 [
- uigrid $_ms_w 0 0 [
- loop i $_ms_bt [
- local c ; c = (at $_ms_cf $i)
- uiimage (format "media/interface/minesweeper/tiles/%1.png" $c) 0.04 0.04 [
- if (< $_ms_gm 2) [
- if (uihover?) [
- p = $i // last
- cond [= $c 0xF] [ // HIDDEN TILE
- if (uihold?) [
- uioutline 0xFF0000
- ] [ if (uieschold?) [
- uioutline 0xFF0000
- ] [ uioutline 0 ] ]
- uirelease [ _ms_tile_view $i ]
- uiescrelease [
- _ms_tile_open 0xC $i 0
- append _ms_fp $i
- _ms_fc = (+ $_ms_fc 1)
- ]
- ] [= $c 0xC] [ // FLAGGED TILE
- uioutline (? (uieschold?) 0xFF0000 0)
- uiescrelease [
- _ms_tile_open 0xF $i 0
- _ms_fp = (listdel $_ms_fp $i)
- _ms_fc = (- $_ms_fc 1)
- ]
- ] [!= $c 0x9] [ // -NOT- HIT MINE TILE
- if (|| [uiescpress?] [uialtpress?]) [
- _ms_tf = 0 ; o = 0
- _ms_tile_expl $i [
- case (at $_ms_cf $n) 0xF [
- append _ms_tr $n
- ] 0xC [
- _ms_tf = (+ $_ms_tf 1)
- ] 0x9 [
- _ms_tf = (+ $_ms_tf 1)
- ]
- ]
- ]
- if (|| [uieschold?] [uialthold?]) [ uioutline 0x00FFFF ]
- if (|| [uipress?] [uialtpress?]) [ o = 1 ]
- if (|| [uirelease?] [uialtrelease?]) [
- if (= $_ms_tf (at $_ms_nf $i)) [
- looplist n $_ms_tr [
- if (= (at $_ms_cf $n) 0xF) [ _ms_tile_view $n ]
- ]
- ]
- o = 0
- uieschold [] [ _ms_tr = "" ]
- ]
- uiescrelease [ _ms_tr = "" ]
- ]
- ] [
- if (= (getalias p) $i) [ _ms_tr = "" ] [
- if (> (indexof $_ms_tr $i) -1) [ uioutline (? $o 0xFF0000 0) ]
- ]
- ]
- uiclamp* 1 1 1 1
- ]
- ]
- ]
- ]
- ]
- UIbox "box d d" 0 0.04 [
- uiclamp 1 1
- uispace 0.01 0 [
- uialign -1
- uihlist 0.01 [
- uiimage "media/interface/minesweeper/life.png" 0.032 0.032
- uitext (? $_ms_ua (? $_ms_u $_ms_u (concatword "^f3" $_ms_u)) "-") 0.65
- ]
- ]
- 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.65
- uiimage "media/interface/minesweeper/globe.png" 0.032 0.032
- ]
- ]
- ]
- ]
- uivlist 0 [
- uiclamp 0 0 1 1
- UIbox "box d n n" 0.14 0 [
- uivlist 0.02 [
- UIbuttonarea "" 0.14 0.032 [
- uirelease [ _ms_make_board $_ms_th $_ms_tw $_ms_tm ]
- uitext "New Game" 0.6
- ]
- uigrid 2 0.01 0.01 [
- uiimage "media/interface/minesweeper/height.png" 0.032 0.032
- uifill 0 0.032 [ uifield _ms_th 3 [ _ms_set_options $_ms_th 0 ] 0.55 style_generic_focus ]
- uiimage "media/interface/minesweeper/width.png" 0.032 0.032
- uifill 0 0.032 [ uifield _ms_tw 3 [ _ms_set_options 0 $_ms_tw ] 0.55 style_generic_focus ]
- uiimage "media/interface/minesweeper/mine.png" 0.032 0.032
- uifill 0 0.032 [ uifield _ms_tm 3 [ _ms_set_options 0 0 ] 0.55 style_generic_focus ]
- ]
- UIbuttonarea "" 0.14 0.032 [
- uirelease [ _ms_ua = (! $_ms_ua) ]
- uihlist 0.01 [
- UIcheckbox $_ms_ua 0.016 0 0
- uitext "Use Lives" 0.55
- ]
- ]
- ]
- ]
- uivlist 0.01 [
- UIbox "box d n n" 0.14 0 [
- uiclamp 1 1
- uivlist 0 [
- uiclamp 1 1
- UIbuttonarea "hold2" 0.14 0.032 [
- uirelease [ _ms_make_board 9 9 10 ]
- uitext (concatword (? $uihover? "^f0") "Easy") 0.55
- ]
- UIbuttonarea "hold2" 0.14 0.032 [
- uirelease [ _ms_make_board 16 16 40 ]
- uitext (concatword (? $uihover? "^f2") "Normal") 0.55
- ]
- UIbuttonarea "hold2" 0.14 0.032 [
- uirelease [ _ms_make_board 16 30 80 ]
- uitext (concatword (? $uihover? "^f3") "Hard") 0.55
- ]
- uiclamp* 1 1
- ]
- ]
- uitext (concat "^f4FPS:" (tabify $getfps 1)) 0.45
- uialign- 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