Advertisement
RaZgRiZ

MineSweeper w/NNUI WIP

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