RaZgRiZ

MineSweeper w/NNUI 1.0

Aug 13th, 2013
281
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_tu = 1  // temp U
  8. _ms_fc = 0  // placed flags counter
  9. _ms_hc = 0  // hit mines counter
  10. _ms_bt = 0  // board tiles (total)
  11. _ms_t1 = 0  // timer start (ms)
  12. _ms_t2 = 0  // timer end (ms)
  13. _ms_ua = 1  // undo toggle
  14. _ms_u  = 0  // undo counter
  15. _ms_pc = 0  // completion percentage
  16. _ms_gm = 0  // game mode: 0 waiting; 1 active; 2 failure ; 3 victory
  17.  
  18. _ms_mf = "" // board containing mine placement
  19. _ms_nf = "" // board containing tile numbers
  20. _ms_cf = "" // board of completion (displayed)
  21. _ms_mp = "" // supplementary list with mine positions
  22. _ms_hp = "" // supplementary list with hit mine positions
  23. _ms_fp = "" // supplementary list with flag positions
  24. _ms_tq = "" // supplementary list with tile reveal queue
  25.  
  26.  
  27. // ----------------------------------------- //
  28. // ---------------- Tile ID ---------------- //
  29. // ----------------------------------------- //
  30. // 0   = blank tile
  31. // 1-8 = number tiles
  32. // H   = hidden tile
  33. // X   = mine tile (failure)
  34. // HX  = mine tile (victory)
  35. // R   = hit mine tile
  36. // F   = flagged tile
  37. // FX  = flagged mine tile (success)
  38. // FF  = flagged mine tile (failure)
  39.  
  40.  
  41. append = [ $arg1 = (concat (getalias $arg1) $arg2) ]
  42.  
  43. _ms_clock = [
  44.     local n
  45.     n = (mod $arg1 1000)
  46.     format "%1:%2%3" (div $arg1 1000) (? (< $n 100) (? (< $n 10) "00" "0")) $n
  47. ]
  48.  
  49. _ms_set_options = [
  50.     local n
  51.     n = (* $_ms_th $_ms_tw)
  52.     // calculate board sizes/mines and apply limits
  53.     if $arg1 [ _ms_th = (max (min $arg1 24) 9) ]
  54.     if $arg2 [ _ms_tw = (max (min $arg2 30) 9) ]
  55.     _ms_tm = (max (min (div $n 3) $_ms_tm) (div $n 10))
  56. ]
  57.  
  58. _ms_set_markers = [
  59.     // in case of failure, update flag markers
  60.     if (= $_ms_gm 2) [
  61.         looplist i $_ms_fp [
  62.             if (=s (at $_ms_nf $i) "X") [
  63.                 _ms_tile_open "FX" $i 0
  64.             ] [ _ms_tile_open "FF" $i 0 ]
  65.         ]
  66.     ]
  67.     // update mine markers, discounting flagged ones
  68.     looplist i $_ms_mp [
  69.         case $_ms_gm 2 [
  70.             if (< (indexof "R FX FF" (at $_ms_cf $i)) 0) [
  71.                 _ms_tile_open "X" $i 0
  72.             ]
  73.         ] 3 [
  74.             if (< (indexof "R F" (at $_ms_cf $i)) 0) [
  75.                 _ms_tile_open "HX" $i 0
  76.             ]
  77.         ]
  78.     ]
  79. ]
  80.  
  81. _ms_tile_open = [
  82.     // start the game if still in waiting phase
  83.     if (= $_ms_gm 0) [
  84.         _ms_gm = 1
  85.         _ms_t1 = (getmillis)
  86.     ]
  87.     // reveal # tile at given position and add to completion counter if needed
  88.     _ms_cf = (listsplice $_ms_cf $arg1 $arg2 1)
  89.     if $arg3 [ _ms_pc = (+ $_ms_pc 1) ]
  90. ]
  91.  
  92. _ms_tile_find = [
  93.     local n c1 c2 c3 c4 c5 c6 c7 c8
  94.     n = 0
  95.     c1 = (- $arg9 $_ms_w)       // UP
  96.     c2 = (+ $arg9 $_ms_w)       // DN
  97.     c3 = (- $arg9 1)            // LT
  98.     c4 = (+ $arg9 1)            // RT
  99.     c5 = (- $arg9 (+ $_ms_w 1)) // UP-LT
  100.     c6 = (- $arg9 (- $_ms_w 1)) // UP-RT
  101.     c7 = (+ $arg9 (- $_ms_w 1)) // DN-LT
  102.     c8 = (+ $arg9 (+ $_ms_w 1)) // DN-RT
  103.     if (> $c1 -1)                                               $arg1 // UP
  104.     if (< $c2 $_ms_bt)                                          $arg2 // DN
  105.     if (> (mod $arg9 $_ms_w) 0)                                 $arg3 // LT
  106.     if (< (mod $arg9 $_ms_w) (- $_ms_w 1))                      $arg4 // RT
  107.     if (&& [> $c5 -1] [> (mod $arg9 $_ms_w) 0])                 $arg5 // UP-LT
  108.     if (&& [> $c6 -1] [< (mod $arg9 $_ms_w) (- $_ms_w 1)])      $arg6 // UP-RT
  109.     if (&& [< $c7 $_ms_bt] [> (mod $arg9 $_ms_w) 0])            $arg7 // DN-LT
  110.     if (&& [< $c8 $_ms_bt] [< (mod $arg9 $_ms_w) (- $_ms_w 1)]) $arg8 // DN-RT
  111.     arg10
  112. ]
  113.  
  114. _ms_tile_view = [
  115.     local n t
  116.     n = (at $_ms_nf $arg1)
  117.     cases $n "X" [
  118.         // mark the mine as hit
  119.         _ms_tile_open "R" $arg1 0
  120.         append _ms_hp $arg1
  121.         _ms_hc = (+ $_ms_hc 1)
  122.         // if lives are enabled, reduce by one, otherwise
  123.         if (&& $_ms_ua $_ms_u) [ _ms_u = (- $_ms_u 1) ] [
  124.             // game over, board is populated by hit and undiscovered mines and flag marks
  125.             _ms_gm = 2
  126.             _ms_t2 = (getmillis)
  127.             _ms_set_markers
  128.         ]
  129.     ] "0" [
  130.         _ms_tile_open "0" $arg1 1
  131.         append _ms_tq $arg1
  132.         // explore all surrounding tiles until number border
  133.         while [listlen $_ms_tq] [
  134.             do [ _ms_tile_find @(
  135.                 loopconcat i 8 [result [[
  136.                     n = $c@@(+ $i 1)
  137.                     // make sure the tile is not revealed, otherwise skip tile
  138.                     if (=s (at $_ms_cf $n) "H") [
  139.                         // ensure tile is blank and not in work queue, then add it
  140.                         if (&& [=s (at $_ms_nf $n) "0"] [< (indexof $_ms_tq $n) 0]) [
  141.                             append _ms_tq $n
  142.                         ]
  143.                         // reveal the tile around the blank tile and add to completion percentage
  144.                         _ms_tile_open (at $_ms_nf $n) $n 1
  145.                     ]
  146.                 ]]]
  147.             ) (at $_ms_tq 0) ]
  148.             // remove used tile from work queue
  149.             _ms_tq = (sublist $_ms_tq 1)
  150.         ]
  151.     ] () [ _ms_tile_open $n $arg1 1 ]
  152.     // in case of board completion, victory
  153.     if (= $_ms_pc (- $_ms_bt $_ms_m)) [
  154.         _ms_gm = 3 ; _ms_t2 = (getmillis)
  155.         _ms_set_markers
  156.     ]
  157. ]
  158.  
  159. _ms_make_board = [
  160.     local i n r
  161.     i = 0
  162.     // initialise board variables
  163.     _ms_th = $arg1
  164.     _ms_tw = $arg2
  165.     _ms_m  = $arg3
  166.     _ms_h  = $_ms_th
  167.     _ms_w  = $_ms_tw
  168.     _ms_tm = $_ms_m
  169.     // initialise game variables
  170.     _ms_ua = $_ms_tu
  171.     if $_ms_ua [ _ms_u  = (+ (div $_ms_m 20) 1) ]
  172.     _ms_bt = (* $_ms_h $_ms_w)
  173.     _ms_pc = 0
  174.     _ms_t1 = 0
  175.     _ms_t2 = 0
  176.     _ms_gm = 0
  177.     _ms_hc = 0
  178.     _ms_fc = 0
  179.     // initialise boards
  180.     _ms_nf = ""
  181.     _ms_mf = (loopconcat i $_ms_bt [result "0"])
  182.     _ms_cf = (loopconcat i $_ms_bt [result "H"])
  183.     // initialise supplementary lists
  184.     _ms_tq = ""
  185.     _ms_mp = ""
  186.     _ms_hp = ""
  187.     _ms_fp = ""
  188.     // generate the mines board
  189.     while [< $i $_ms_m] [
  190.         r = (rnd $_ms_bt)
  191.         // avoid writing to a previously picked spot
  192.         if (! (at $_ms_mf $r)) [
  193.             _ms_mf = (listsplice $_ms_mf "1" $r 1)
  194.             append _ms_mp $r
  195.             i = (+ $i 1)
  196.         ]
  197.     ]
  198.     // generate the numbers board
  199.     loop y $_ms_bt [
  200.         if (at $_ms_mf $y) [ append _ms_nf "X" ] [
  201.             do [ _ms_tile_find @(
  202.                 loopconcat i 8 [result [[
  203.                     n = (+ (at $_ms_mf $c@@(+ $i 1)) $n)
  204.                 ]]]
  205.             ) $y [ append _ms_nf $n ] ]
  206.         ]
  207.     ]
  208. ]
  209.  
  210. _ms_ui_diff = [
  211.     uimodcolor 0xEEEEEE 0.22 0.03 [
  212.         uialign 0 1
  213.         uihold [ uihover [ uimodvgradient $arg3 $arg2 ] ] [
  214.             uihover [ uimodvgradient $arg2 $arg3 ]
  215.         ]
  216.         uirelease [ arg4 ]
  217.         uioutline 0x181818
  218.         uiclamp* 1 1 1 1
  219.         uispace 0.01 0.002 [
  220.             uitext $arg1 0.6
  221.         ]
  222.     ]
  223. ]
  224.  
  225. minesweeper = [ toggleui minesweeper ]
  226.  
  227. newui minesweeper [
  228.     uicolor 0xB0EEEEEE 0 0 [
  229.         uioutline 0x181818 0 0 [ uiclamp 1 1 1 1 ]
  230.         uispace 0.005 0.005 [
  231.             uihlist 0.008 [
  232.                 uivlist 0.004 [
  233.                     uihlist -0.1 [
  234.                         uiclamp 1 1
  235.                         uimodvgradient 0xBBBBBB 0xEEEEEE (*f $_ms_w 0.0225) 0.045 [
  236.                             uiclamp 1 1
  237.                             uioutline 0x181818 0 0 [ uiclamp 1 1 1 1 ]
  238.                             uispace 0.01 0 [
  239.                                 uialign -1 0
  240.                                 uihlist 0.01 [
  241.                                     uiimage "media/texture/minesweeper/globe.png" 0.04 0.04
  242.                                     uitext (format "^f%1%2%%" (case $_ms_gm 2 3 3 0 () 7) (
  243.                                         absf (precf (*f (divf $_ms_pc (- $_ms_bt $_ms_m)) 100) 1))
  244.                                     ) 0.8
  245.                                 ]
  246.                             ]
  247.                         ]
  248.                         uimodvgradient 0xBBBBBB 0xEEEEEE (*f $_ms_w 0.0225) 0.045 [
  249.                             uiclamp 1 1
  250.                             uioutline 0x181818 0 0 [ uiclamp 1 1 1 1 ]
  251.                             uispace 0.01 0 [
  252.                                 uialign 1 0
  253.                                 uihlist 0.01 [
  254.                                     uitext (_ms_clock (case $_ms_gm 0 0 1 (- $getmillis $_ms_t1) () (- $_ms_t2 $_ms_t1))) 0.8
  255.                                     uiimage "media/texture/minesweeper/clock.png" 0.04 0.04
  256.                                 ]
  257.                             ]
  258.                         ]
  259.                     ]
  260.                     uivlist 0 [
  261.                         loop y $_ms_h [
  262.                             uihlist 0 [
  263.                                 loop x $_ms_w [
  264.                                     n = (+ (* $y $_ms_w) $x)
  265.                                     uiimage (format "media/texture/minesweeper/tiles/%1.png" (at $_ms_cf $n)) 0.045 0.045 [
  266.                                         if (&& [< $_ms_gm 2] [|| [=s (at $_ms_cf $n) "H"] [=s (at $_ms_cf $n) "F"]]) [
  267.                                             uihold [ uihover [ uioutline 0xFF0000 ] ] [
  268.                                                 uialthold [
  269.                                                     uihover [ uioutline 0xFF0000 ]
  270.                                                 ] [
  271.                                                     uihover [ uioutline 0xFFFFFF ]
  272.                                                 ]
  273.                                             ]
  274.                                             if (=s (at $_ms_cf $n) "H") [
  275.                                                 uirelease [ _ms_tile_view $n ]
  276.                                             ]
  277.                                             uialtrelease [
  278.                                                 cases (at $_ms_cf $n) "H" [
  279.                                                     _ms_tile_open "F" $n 0
  280.                                                     append _ms_fp $n
  281.                                                     _ms_fc = (+ $_ms_fc 1)
  282.                                                 ] "F" [
  283.                                                     _ms_tile_open "H" $n 0
  284.                                                     _ms_fp = (listdel $_ms_fp $n)
  285.                                                     _ms_fc = (- $_ms_fc 1)
  286.                                                 ]
  287.                                             ]
  288.                                             uiclamp* 1 1 1 1
  289.                                         ]
  290.                                     ]
  291.                                 ]
  292.                             ]
  293.                         ]
  294.                     ]
  295.                 ]
  296.                 uioutline 0x181818 0 0 [ uiclamp 1 1 1 1 ]
  297.                 uivlist 0 [
  298.                     uiclamp 0 0 1 1
  299.                     uivlist 0.03 [
  300.                         uimodcolor (? (> $_ms_gm 1) 0xAAFFAA 0xEEEEEE) 0.22 0.045 [
  301.                             uialign 0 -1
  302.                             uihold [ uihover [ uimodvgradient 0xCCCCCC 0xFFFFFF ] ] [
  303.                                 uihover [ uimodvgradient 0xFFFFFF 0xCCCCCC ]
  304.                             ]
  305.                             uirelease [ _ms_make_board $_ms_th $_ms_tw $_ms_tm ]
  306.                             uioutline 0x181818
  307.                             uiclamp* 1 1 1 1
  308.                             uispace 0.01 0.002 [
  309.                                 uihlist 0.01 [
  310.                                     uialign 0 0
  311.                                     uiimage "media/texture/minesweeper/new.png" 0.04 0.04
  312.                                     uitext "New Game" 0.8
  313.                                 ]
  314.                             ]
  315.                         ]
  316.                         uihlist 0 [
  317.                             uialign -1 -1
  318.                             uispace 0.01 0.002 [
  319.                                 uihlist 0.01 [
  320.                                     uialign 0 0
  321.                                     uiimage "media/texture/minesweeper/life.png" 0.04 0.04
  322.                                     uitext (concatword (? $_ms_u "^f7" "^f3") (? $_ms_ua $_ms_u 0)) 0.8
  323.                                 ]
  324.                             ]
  325.                             uispace 0.01 0.002 [
  326.                                 uihlist 0.01 [
  327.                                     uialign 0 0
  328.                                     uiimage "media/texture/minesweeper/flag.png" 0.04 0.04
  329.                                     uitext (- (- $_ms_m $_ms_fc) $_ms_hc) 0.8
  330.                                 ]
  331.                             ]
  332.                         ]
  333.                     ]
  334.                     uivlist 0.02 [
  335.                         uihlist 0 [
  336.                             uialign -1 -1
  337.                             uivlist 0 [
  338.                                 uispace 0.01 0.002 [
  339.                                     uihlist 0.01 [
  340.                                         uialign 0 0
  341.                                         uiimage "media/texture/minesweeper/height.png" 0.03 0.03
  342.                                         uimodcolor 0xEEEEEE 0 0 [
  343.                                             uihover [ uimodvgradient 0xFFFFFF 0xCCCCCC ]
  344.                                             uiclamp- 1 1 1 1
  345.                                             uifield _ms_th 2 [ _ms_set_options $_ms_th 0 ] 0.6 [
  346.                                                 uioutline 0x181818
  347.                                                 uiclamp- 1 1 1 1
  348.                                             ]
  349.                                         ]
  350.                                     ]
  351.                                 ]
  352.                                 uispace 0.01 0.002 [
  353.                                     uihlist 0.01 [
  354.                                         uialign 0 0
  355.                                         uiimage "media/texture/minesweeper/width.png" 0.03 0.03
  356.                                         uimodcolor 0xEEEEEE 0 0 [
  357.                                             uihover [ uimodvgradient 0xFFFFFF 0xCCCCCC ]
  358.                                             uiclamp- 1 1 1 1
  359.                                             uifield _ms_tw 2 [ _ms_set_options 0 $_ms_tw ] 0.6 [
  360.                                                 uioutline 0x181818
  361.                                                 uiclamp- 1 1 1 1
  362.                                             ]
  363.                                         ]
  364.                                     ]
  365.                                 ]
  366.                             ]
  367.                             uispace 0 0.002 [
  368.                                 uivlist 0.004 [
  369.                                     uialign 0 0
  370.                                     uiimage "media/texture/minesweeper/mine.png" 0.03 0.03
  371.                                     uimodcolor 0xEEEEEE 0 0 [
  372.                                         uihover [ uimodvgradient 0xFFFFFF 0xCCCCCC ]
  373.                                         uiclamp- 1 1 1 1
  374.                                         uifield _ms_tm 3 [ _ms_set_options 0 0 ] 0.6 [
  375.                                             uioutline 0x181818
  376.                                             uiclamp- 1 1 1 1
  377.                                         ]
  378.                                     ]
  379.                                 ]
  380.                             ]
  381.                         ]
  382.                         uitext "Difficulty" 0.8
  383.                         uivlist -0.0001 [
  384.                             uialign 0 1
  385.                             _ms_ui_diff "Easy"   0x4DFF00 0x279900 [ _ms_make_board  9  9 10 ]
  386.                             _ms_ui_diff "Normal" 0xFFE800 0x998400 [ _ms_make_board 16 16 40 ]
  387.                             _ms_ui_diff "Hard"   0xFF2020 0x991010 [ _ms_make_board 16 30 80 ]
  388.                             uispace 0 0.005
  389.                             uimodcolor 0xEEEEEE 0.22 0.03 [
  390.                                 uialign 0 -1
  391.                                 uihold [ uihover [ uimodvgradient 0xCCCCCC 0xFFFFFF ] ] [
  392.                                     uihover [ uimodvgradient 0xFFFFFF 0xCCCCCC ]
  393.                                 ]
  394.                                 uirelease [
  395.                                     _ms_tu = (! $_ms_tu)
  396.                                     if (! $_ms_gm) [ _ms_ua = $_ms_tu ]
  397.                                 ]
  398.                                 uioutline 0x181818
  399.                                 uiclamp* 1 1 1 1
  400.                                 uispace 0.01 0.002 [
  401.                                     uihlist 0.01 [
  402.                                         uialign 0 0
  403.                                         uiimage (concatword "media/texture/minesweeper/check_" (
  404.                                             case (+ $_ms_tu $_ms_ua) 0 [ result "off" ] 1 [ result "sel" ] 2 [ result "on" ]
  405.                                         ) ".png") 0.025 0.025
  406.                                         uitext "Use Lives" 0.6
  407.                                     ]
  408.                                 ]
  409.                             ]
  410.                         ]
  411.                     ]
  412.                 ]
  413.             ]
  414.         ]
  415.     ]
  416. ] [ _ms_make_board $_ms_th $_ms_tw $_ms_tm ]
Advertisement
Add Comment
Please, Sign In to add comment