Advertisement
RaZgRiZ

MineSweeper C.S. (nnUI) 1.4 [28/07/2014]

Jul 27th, 2014
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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  // [FLAG] 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_t2 = 0  // timer end (ms)
  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_nf = "" // board containing tile numbers
  21. _ms_cf = "" // board of completion (displayed)
  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
  25. _ms_tr = "" // supplementary list with tile reveal positions
  26.  
  27.  
  28. // ----------------------------------------- //
  29. // ---------------- Tile ID ---------------- //
  30. // ----------------------------------------- //
  31. // 0x0     = [BLANK]
  32. // 0x1-0x8 = [NUM#]
  33. // 0x9     = [MINE-HIT]
  34. // 0xA     = [MINE-WIN]
  35. // 0xB     = [MINE-LOSE]
  36. // 0xC     = [FLAG]
  37. // 0xD     = [FLAG-WIN]
  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.  
  46. _ms_set_options = [
  47.     local n
  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.     if (< $r $p)                      $arg4 // RT
  88.     if (&& [> $c5 -1] [> $r 0])       $arg5 // UP-LT
  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.         if (&& $_ms_ua $_ms_uc) [ _ms_uc = (- $_ms_uc 1) ] [
  116.             // game over, undiscovered mines are shown and flag markers updated
  117.             _ms_gm = 2
  118.             _ms_t2 = (getmillis)
  119.             _ms_set_markers
  120.         ]
  121.     ] 0x0 [
  122.         _ms_tile_open 0x0 $arg1 1
  123.         // explore all surrounding tiles until number border
  124.         _ms_tile_expl $arg1 [
  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.                 // reveal the tested tile and add to completion counter
  132.                 _ms_tile_open (at $_ms_nf $n) $n 1
  133.             ]
  134.         ]
  135.     ] () [ _ms_tile_open $n $arg1 1 ]
  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.     // start the game if still in waiting phase
  145.     if (= $_ms_gm 0) [
  146.         _ms_init_game $arg2
  147.         _ms_gm = 1
  148.         _ms_t1 = (getmillis)
  149.     ]
  150.     // reveal # tile at given position and add to percentage counter if needed
  151.     _ms_cf = (listsplice $_ms_cf (tohex $arg1) $arg2 1)
  152.     if $arg3 [ _ms_pc = (+ $_ms_pc 1) ]
  153. ]
  154.  
  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_tm = $_ms_m
  163.     // initialise game variables
  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.     _ms_nf = ""
  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_fp = ""
  180.     _ms_tr = ""
  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.     ]) $arg1 [ i = 0 ] ]
  189.     // initialise the minefield board
  190.     while [< $i $_ms_m] [
  191.         r = (rnd $_ms_bt)
  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.             append _ms_mp $r
  196.             i = (+ $i 1)
  197.         ]
  198.     ]
  199.     // initialise the numerical board
  200.     loop p $_ms_bt [
  201.         if (at $_ms_mf $p) [ append _ms_nf 0x9 ] [
  202.             do [ _ms_tile_find @(loopconcat+ i 1 8 [
  203.                 result [[ n = (+ (at $_ms_mf $c@@i) $n) ]]
  204.             ]) $p [ append _ms_nf (tohex $n) ] ]
  205.         ]
  206.     ]
  207. ]
  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.                     uialign 1
  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.                         uiimage "media/interface/minesweeper/clock.png" 0.032 0.032
  231.                     ]
  232.                 ]
  233.             ]
  234.             uispace 0.01 0.001 [
  235.                 uigroup [
  236.                     uigrid $_ms_w 0 0 [
  237.                         loop i $_ms_bt [
  238.                             local c ; c = (at $_ms_cf $i)
  239.                             uiimage (format "%1media/interface/minesweeper/tiles/%2.png" (
  240.                                 ? (|| [
  241.                                     && uihover+? [! $uihold] [|| [= $c 0xF] [= $c 0xC]]
  242.                                 ] [
  243.                                     && $_ms_qh [> (listfind= $_ms_tr $i) -1]
  244.                                 ]) "<mad:1.1/0.8/0.8>"
  245.                             ) (? (&& [= $c 0xF] uihold+?) 0x0 $c)) 0.04 0.04 [
  246.                                 if (&& [< $_ms_gm 2] uihover?) [
  247.                                     _ms_pos = $i
  248.                                     cases $c 0xF [
  249.                                         uirelease [ _ms_tile_read $i ]
  250.                                         uiescpress [
  251.                                             _ms_tile_open 0xC $i 0
  252.                                             append _ms_fp $i
  253.                                             _ms_fc = (+ $_ms_fc 1)
  254.                                         ]
  255.                                     ] 0xC [
  256.                                         uiescpress [
  257.                                             _ms_tile_open 0xF $i 0
  258.                                             _ms_fp = (listdel $_ms_fp $i)
  259.                                             _ms_fc = (- $_ms_fc 1)
  260.                                         ]
  261.                                     ]
  262.                                 ]
  263.                             ]
  264.                         ]
  265.                     ]
  266.                     if (&& [< $_ms_gm 2] uihover? [< 0x0 (at $_ms_cf $_ms_pos) 0x9]) [
  267.                         uitarget 0 0 [
  268.                             uiclamp 1 1 1 1
  269.                             if (|| uialthold? [&& uieschold? uihold?]) [
  270.                                 local focuslist
  271.                                 _ms_sm = 0
  272.                                 _ms_qh = 1
  273.                                 _ms_tile_expl $_ms_pos [
  274.                                     case (at $_ms_cf $n) 0xF [
  275.                                         append focuslist $n
  276.                                     ] 0xC [
  277.                                         _ms_sm = (+ $_ms_sm 1)
  278.                                     ] 0x9 [
  279.                                         _ms_sm = (+ $_ms_sm 1)
  280.                                     ]
  281.                                 ]
  282.                                 _ms_tr = $focuslist
  283.                             ]
  284.                             if (|| uialtrelease? [&& $_ms_qh [! $uialthold?] [|| [! $uihold?] [! $uieschold?]]]) [
  285.                                 local p ; p = (at $_ms_nf $_ms_pos)
  286.                                 if (&& [= $_ms_sm $p] [< 0x0 $p 0x9]) [
  287.                                     looplist n $_ms_tr [
  288.                                         if (= (at $_ms_cf $n) 0xF) [ _ms_tile_read $n ]
  289.                                     ]
  290.                                 ]
  291.                                 _ms_qh = 0
  292.                             ]
  293.                         ]
  294.                     ] [ _ms_qh = 0 ]
  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.                     uialign 1
  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.             ]
  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.                         uiimage "media/interface/minesweeper/height.png" 0.032 0.032
  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_init_main 16 16 40 ]
  353.                             uitext (concatword (uihover? "^f2" "") "Normal") 0.55
  354.                         ]
  355.                         UIbuttonarea "hold2" 0.14 0.032 [
  356.                             uirelease [ _ms_init_main 16 30 80 ]
  357.                             uitext (concatword (uihover? "^f3" "") "Hard") 0.55
  358.                         ]
  359.                         uiclamp* 1 1
  360.                     ]
  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_t2 = (- $getmillis $_ms_t1)
  376.     ]
  377. ] [] [ uitext "Minesweeper C.S." 0.8 ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement