Advertisement
RaZgRiZ

MineSweeper C.S. (nnUI) 1.3 [2/4/2014]

Apr 1st, 2014
233
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  // placed flags counter
  8. _ms_hc = 0  // hit mines counter
  9. _ms_bt = 0  // board tiles (total)
  10. _ms_t1 = 0  // timer start (ms)
  11. _ms_t2 = 0  // timer end (ms)
  12. _ms_ua = 1  // undo toggle
  13. _ms_u  = 0  // undo counter
  14. _ms_pc = 0  // completion percentage
  15. _ms_gm = 0  // game mode: 0 waiting | 1 active | 2 failure | 3 victory
  16.  
  17. _ms_mf = "" // board containing mine placement
  18. _ms_nf = "" // board containing tile numbers
  19. _ms_cf = "" // board of completion (displayed)
  20. _ms_mp = "" // supplementary list with mine positions
  21. _ms_hp = "" // supplementary list with hit mine positions
  22. _ms_fp = "" // supplementary list with flag positions
  23. _ms_tr = "" // supplementary list with tile reveal positions
  24. _ms_tq = "" // supplementary list with blank tile queue
  25.  
  26.  
  27. // ----------------------------------------- //
  28. // ---------------- Tile ID ---------------- //
  29. // ----------------------------------------- //
  30. // 0x0     = blank tile
  31. // 0x1-0x8 = number tiles
  32. // 0x9     = hit mine tile
  33. // 0xA     = mine tile (win)
  34. // 0xB     = mine tile (lose)
  35. // 0xC     = flagged tile
  36. // 0xD     = flagged mine tile (win)
  37. // 0xE     = flagged mine tile (lose)
  38. // 0xF     = hidden tile
  39.  
  40.  
  41. append = [ $arg1 = (concat (getalias $arg1) $arg2) ]
  42. precf  = [ substr $arg1 0 (+ (strstr $arg1 .) (+ $arg2 1)) ]
  43. ms     = [ toggleui minesweeper ]
  44.  
  45. _ms_clock = [
  46.     local n ; n = (mod $arg1 1000)
  47.     format "%1:%2%3" (div $arg1 1000) (if (< $n 100) [? (< $n 10) "00" "0"]) $n
  48. ]
  49.  
  50. _ms_set_options = [
  51.     local n
  52.     n = (* $_ms_th $_ms_tw)
  53.     // calculate board sizes/mines and apply limits
  54.     if $arg1 [ _ms_th = (max (min $arg1 24) 9) ]
  55.     if $arg2 [ _ms_tw = (max (min $arg2 30) 9) ]
  56.     _ms_tm = (max (min (div $n 3) $_ms_tm) (div $n 10))
  57. ]
  58.  
  59. _ms_set_markers = [
  60.     // in case of failure, update flag markers
  61.     if (= $_ms_gm 2) [
  62.         looplist i (listdel $_ms_fp $_ms_mp) [
  63.             _ms_tile_open 0xE $i 0
  64.         ]
  65.     ]
  66.     // update still hidden mine tiles
  67.     looplist i $_ms_mp [
  68.         if (= (at $_ms_cf $i) 0xF) [
  69.             _ms_tile_open (case $_ms_gm 2 0xA 3 0xB) $i 0
  70.         ]
  71.     ]
  72. ]
  73.  
  74. _ms_tile_open = [
  75.     // start the game if still in waiting phase
  76.     if (= $_ms_gm 0) [
  77.         _ms_gm = 1
  78.         _ms_t1 = (getmillis)
  79.     ]
  80.     // reveal # tile at given position and add to percentage counter if needed
  81.     _ms_cf = (listsplice $_ms_cf (tohex $arg1) $arg2 1)
  82.     if $arg3 [ _ms_pc = (+ $_ms_pc 1) ]
  83. ]
  84.  
  85. _ms_tile_find = [
  86.     // set variables and calculate repeated math
  87.     local n r  p c1 c2 c3 c4 c5 c6 c7 c8
  88.     n = 0 ; r = (mod $arg9 $_ms_w) ; p = (- $_ms_w 1)
  89.     // calculate positions of adjacent tiles
  90.     c1 = (- $arg9 $_ms_w)   // UP
  91.     c2 = (+ $arg9 $_ms_w)   // DN
  92.     c3 = (- $arg9 1)        // LT
  93.     c4 = (+ $arg9 1)        // RT
  94.     c5 = (- $arg9 $_ms_w 1) // UP-LT
  95.     c6 = (- $arg9 $p)       // UP-RT
  96.     c7 = (+ $arg9 $p)       // DN-LT
  97.     c8 = (+ $arg9 $_ms_w 1) // DN-RT
  98.     // if adjacent tiles fit conditions, execute
  99.     if (> $c1 -1)                     $arg1 // UP
  100.     if (< $c2 $_ms_bt)                $arg2 // DN
  101.     if (> $r 0)                       $arg3 // LT
  102.     if (< $r $p)                      $arg4 // RT
  103.     if (&& [> $c5 -1] [> $r 0])       $arg5 // UP-LT
  104.     if (&& [> $c6 -1] [< $r $p])      $arg6 // UP-RT
  105.     if (&& [< $c7 $_ms_bt] [> $r 0])  $arg7 // DN-LT
  106.     if (&& [< $c8 $_ms_bt] [< $r $p]) $arg8 // DN-RT
  107.     arg10
  108. ]
  109.  
  110. _ms_tile_expl = [
  111.     // add tile to work queue
  112.     append _ms_tq $arg1
  113.     // execute content as long as list is populated
  114.     while [listlen $_ms_tq] [
  115.         do [ _ms_tile_find @(
  116.             loopconcat+ i 1 8 [result [[
  117.                 n = $c@@i
  118.                 @@@arg2
  119.             ]]]
  120.         ) (at $_ms_tq 0) ]
  121.         _ms_tq = (sublist $_ms_tq 1)
  122.     ]
  123. ]
  124.  
  125. _ms_tile_view = [
  126.     local n t
  127.     n = (at $_ms_nf $arg1)
  128.     case $n 0x9 [
  129.         // mark the mine as hit
  130.         _ms_tile_open 0x9 $arg1 0
  131.         append _ms_hp $arg1
  132.         _ms_hc = (+ $_ms_hc 1)
  133.         // if lives are enabled, reduce by one, otherwise
  134.         if (&& $_ms_ua $_ms_u) [ _ms_u = (- $_ms_u 1) ] [
  135.             // game over, board is populated by hit and undiscovered mines and flag marks
  136.             _ms_gm = 2
  137.             _ms_t2 = (getmillis)
  138.             _ms_set_markers
  139.         ]
  140.     ] 0x0 [
  141.         _ms_tile_open 0x0 $arg1 1
  142.         // explore all surrounding tiles until number border
  143.         _ms_tile_expl $arg1 [
  144.             // make sure the tile is not revealed, otherwise skip tile
  145.             if (= (at $_ms_cf $n) 0xF) [
  146.                 // ensure tile is blank and not in work queue, then add it
  147.                 if (&& [= (at $_ms_nf $n) 0x0] [< (indexof $_ms_tq $n) 0]) [
  148.                     append _ms_tq $n
  149.                 ]
  150.                 // reveal the tile around the blank tile and add to completion percentage
  151.                 _ms_tile_open (at $_ms_nf $n) $n 1
  152.             ]
  153.         ]
  154.     ] () [ _ms_tile_open $n $arg1 1 ]
  155.     // in case of board completion, victory
  156.     if (= $_ms_pc (- $_ms_bt $_ms_m)) [
  157.         _ms_gm = 3 ; _ms_t2 = (getmillis)
  158.         _ms_set_markers
  159.     ]
  160. ]
  161.  
  162. _ms_make_board = [
  163.     local i n r
  164.     i = 0
  165.     // initialise board variables
  166.     _ms_th = $arg1
  167.     _ms_tw = $arg2
  168.     _ms_m  = $arg3
  169.     _ms_h  = $_ms_th
  170.     _ms_w  = $_ms_tw
  171.     _ms_tm = $_ms_m
  172.     // initialise game variables
  173.     if $_ms_ua [ _ms_u  = (+ (div $_ms_m 20) 1) ]
  174.     _ms_bt = (* $_ms_h $_ms_w) // total tiles
  175.     _ms_pc = 0 // percentage counter
  176.     _ms_t1 = 0 // timer start
  177.     _ms_t2 = 0 // timer stop
  178.     _ms_gm = 0 // gamemode: 0/1/2/3 = waiting/active/won/lost
  179.     _ms_hc = 0 // mine hit counter
  180.     _ms_fc = 0 // flag counter
  181.     // initialise boards
  182.     _ms_nf = ""
  183.     _ms_mf = (loopconcat i $_ms_bt [result "0"])
  184.     _ms_cf = (loopconcat i $_ms_bt [result 0xF])
  185.     // initialise supplementary lists
  186.     _ms_tq = ""
  187.     _ms_mp = ""
  188.     _ms_hp = ""
  189.     _ms_fp = ""
  190.     _ms_tr = ""
  191.     // generate the mines board
  192.     while [< $i $_ms_m] [
  193.         r = (rnd $_ms_bt)
  194.         // avoid writing to a previously picked spot
  195.         if (! (at $_ms_mf $r)) [
  196.             _ms_mf = (listsplice $_ms_mf "1" $r 1)
  197.             append _ms_mp $r
  198.             i = (+ $i 1)
  199.         ]
  200.     ]
  201.     // generate the numbers board
  202.     loop p $_ms_bt [
  203.         if (at $_ms_mf $p) [ append _ms_nf 0x9 ] [
  204.             do [ _ms_tile_find @(
  205.                 loopconcat+ i 1 8 [result [[
  206.                     n = (+ (at $_ms_mf $c@@i) $n)
  207.                 ]]]
  208.             ) $p [ append _ms_nf (tohex $n) ] ]
  209.         ]
  210.     ]
  211. ]
  212.  
  213. UImenu "minesweeper" [
  214.     uieschide 0
  215.     uihlist 0.01 [
  216.         uivlist 0 [
  217.             UIbox "box d d" 0 0.04 [
  218.                 uiclamp 1 1
  219.                 uispace 0.01 0 [
  220.                     uialign -1
  221.                     uihlist 0.01 [
  222.                         local n ; n = (- $_ms_m $_ms_fc $_ms_hc)
  223.                         uiimage "media/interface/minesweeper/flag.png" 0.032 0.032
  224.                         uitext (concatword (? (< $n 0) "^f3") $n) 0.65
  225.                     ]
  226.                 ]
  227.                 uispace 0.01 0 [
  228.                     uialign 1
  229.                     uihlist 0.01 [
  230.                         uitext (_ms_clock (
  231.                             case $_ms_gm 0 0 1 (- $getmillis $_ms_t1) () (- $_ms_t2 $_ms_t1)
  232.                         )) 0.65
  233.                         uiimage "media/interface/minesweeper/clock.png" 0.032 0.032
  234.                     ]
  235.                 ]
  236.             ]
  237.             uispace 0.01 0.001 [
  238.                 uigrid $_ms_w 0 0 [
  239.                     loop i $_ms_bt [
  240.                         local c ;  c = (at $_ms_cf $i)
  241.                         uiimage (format "media/interface/minesweeper/tiles/%1.png" $c) 0.04 0.04 [
  242.                             if (< $_ms_gm 2) [
  243.                                 if (uihover?) [
  244.                                     p = $i // last
  245.                                     cond [= $c 0xF] [ // HIDDEN TILE
  246.                                         if (uihold?) [
  247.                                             uioutline 0xFF0000
  248.                                         ] [ if (uieschold?) [
  249.                                             uioutline 0xFF0000
  250.                                         ] [ uioutline 0 ] ]
  251.                                         uirelease [ _ms_tile_view $i ]
  252.                                         uiescrelease [
  253.                                             _ms_tile_open 0xC $i 0
  254.                                             append _ms_fp $i
  255.                                             _ms_fc = (+ $_ms_fc 1)
  256.                                         ]
  257.                                     ] [= $c 0xC] [ // FLAGGED TILE
  258.                                         uioutline (? (uieschold?) 0xFF0000 0)
  259.                                         uiescrelease [
  260.                                             _ms_tile_open 0xF $i 0
  261.                                             _ms_fp = (listdel $_ms_fp $i)
  262.                                             _ms_fc = (- $_ms_fc 1)
  263.                                         ]
  264.                                     ] [!= $c 0x9] [ // -NOT- HIT MINE TILE
  265.                                         if (|| [uiescpress?] [uialtpress?]) [
  266.                                             _ms_tf = 0 ; o = 0
  267.                                             _ms_tile_expl $i [
  268.                                                 case (at $_ms_cf $n) 0xF [
  269.                                                     append _ms_tr $n
  270.                                                 ] 0xC [
  271.                                                     _ms_tf = (+ $_ms_tf 1)
  272.                                                 ] 0x9 [
  273.                                                     _ms_tf = (+ $_ms_tf 1)
  274.                                                 ]
  275.                                             ]
  276.                                         ]
  277.                                         if (|| [uieschold?] [uialthold?]) [ uioutline 0x00FFFF ]
  278.                                         if (|| [uipress?] [uialtpress?]) [ o = 1 ]
  279.                                         if (|| [uirelease?] [uialtrelease?]) [
  280.                                             if (= $_ms_tf (at $_ms_nf $i)) [
  281.                                                 looplist n $_ms_tr [
  282.                                                     if (= (at $_ms_cf $n) 0xF) [ _ms_tile_view $n ]
  283.                                                 ]
  284.                                             ]
  285.                                             o = 0
  286.                                             uieschold [] [ _ms_tr = "" ]
  287.                                         ]
  288.                                         uiescrelease [ _ms_tr = "" ]
  289.                                     ]
  290.                                 ] [
  291.                                     if (= (getalias p) $i) [ _ms_tr = "" ] [
  292.                                         if (> (indexof $_ms_tr $i) -1) [ uioutline (? $o 0xFF0000 0) ]
  293.                                     ]
  294.                                 ]
  295.                                 uiclamp* 1 1 1 1
  296.                             ]
  297.                         ]
  298.                     ]
  299.                 ]
  300.             ]
  301.             UIbox "box d d" 0 0.04 [
  302.                 uiclamp 1 1
  303.                 uispace 0.01 0 [
  304.                     uialign -1
  305.                     uihlist 0.01 [
  306.                         uiimage "media/interface/minesweeper/life.png" 0.032 0.032
  307.                         uitext (? $_ms_ua (? $_ms_u $_ms_u (concatword "^f3" $_ms_u)) "-") 0.65
  308.                     ]
  309.                 ]
  310.                 uispace 0.01 0 [
  311.                     uialign 1
  312.                     uihlist 0.01 [
  313.                         uitext (format "^f%1%2%%" (case $_ms_gm 2 3 3 0 () 7) (
  314.                             precf (*f (divf $_ms_pc (- $_ms_bt $_ms_m)) 100) 1
  315.                         )) 0.65
  316.                         uiimage "media/interface/minesweeper/globe.png" 0.032 0.032
  317.                     ]
  318.                 ]
  319.             ]
  320.         ]
  321.         uivlist 0 [
  322.             uiclamp 0 0 1 1
  323.             UIbox "box d n n" 0.14 0 [
  324.                 uivlist 0.02 [
  325.                     UIbuttonarea "" 0.14 0.032 [
  326.                         uirelease [ _ms_make_board $_ms_th $_ms_tw $_ms_tm ]
  327.                         uitext "New Game" 0.6
  328.                     ]
  329.                     uigrid 2 0.01 0.01 [
  330.                         uiimage "media/interface/minesweeper/height.png" 0.032 0.032
  331.                         uifill 0 0.032 [ uifield _ms_th 3 [ _ms_set_options $_ms_th 0 ] 0.55 style_generic_focus ]
  332.                         uiimage "media/interface/minesweeper/width.png" 0.032 0.032
  333.                         uifill 0 0.032 [ uifield _ms_tw 3 [ _ms_set_options 0 $_ms_tw ] 0.55 style_generic_focus ]
  334.                         uiimage "media/interface/minesweeper/mine.png" 0.032 0.032
  335.                         uifill 0 0.032 [ uifield _ms_tm 3 [ _ms_set_options 0 0 ] 0.55 style_generic_focus ]
  336.                     ]
  337.                     UIbuttonarea "" 0.14 0.032 [
  338.                         uirelease [ _ms_ua = (! $_ms_ua) ]
  339.                         uihlist 0.01 [
  340.                             UIcheckbox $_ms_ua 0.016 0 0
  341.                             uitext "Use Lives" 0.55
  342.                         ]
  343.                     ]
  344.                 ]
  345.             ]
  346.             uivlist 0.01 [
  347.                 UIbox "box d n n" 0.14 0 [
  348.                     uiclamp 1 1
  349.                     uivlist 0 [
  350.                         uiclamp 1 1
  351.                         UIbuttonarea "hold2" 0.14 0.032 [
  352.                             uirelease [ _ms_make_board 9 9 10 ]
  353.                             uitext (concatword (? $uihover? "^f0") "Easy") 0.55
  354.                         ]
  355.                         UIbuttonarea "hold2" 0.14 0.032 [
  356.                             uirelease [ _ms_make_board 16 16 40 ]
  357.                             uitext (concatword (? $uihover? "^f2") "Normal") 0.55
  358.                         ]
  359.                         UIbuttonarea "hold2" 0.14 0.032 [
  360.                             uirelease [ _ms_make_board 16 30 80 ]
  361.                             uitext (concatword (? $uihover? "^f3") "Hard") 0.55
  362.                         ]
  363.                         uiclamp* 1 1
  364.                     ]
  365.                 ]
  366.                 uitext (concat "^f4FPS:" (tabify $getfps 1)) 0.45
  367.                 uialign- 1 1
  368.             ]
  369.         ]
  370.     ]
  371. ] [
  372.     // if game is running, restore clock on show
  373.     if (= $_ms_gm 1) [
  374.         _ms_t1 = (- $getmillis $_ms_t2)
  375.     ] [ _ms_make_board $_ms_th $_ms_tw $_ms_tm ]
  376. ] [
  377.     // if game is running, suspend clock on hide
  378.     if (= $_ms_gm 1) [
  379.         _ms_t2 = (- $getmillis $_ms_t1)
  380.     ]
  381. ] [] [ uitext "Minesweeper C.S." 0.8 ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement