Advertisement
RaZgRiZ

MineSweeper w/NNUI 1.2

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