RaZgRiZ

MineSweeper BETA (awaiting optimisation)

Jul 19th, 2013
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. _ms_clock = [
  2.     strreplace (concat (
  3.         div $arg1 1000
  4.     ) (
  5.         padnum (padnum (mod $arg1 1000) 10) 100
  6.     )) " " ":"
  7. ]
  8.  
  9. padnum = [ concatword (? (< $arg1 $arg2) 0) $arg1 ]
  10. append = [ $arg1 = (concat (getalias $arg1) $arg2) ]
  11.  
  12. _ms_h  = 9  // board height
  13. _ms_w  = 9  // board width
  14. _ms_m  = 10 // board mines
  15. _ms_th = 9  // temp H
  16. _ms_tw = 9  // temp W
  17. _ms_tm = 10 // temp M
  18. _ms_tu = 1  // temp U
  19. _ms_bt = 0  // board tiles (total)
  20. _ms_t1 = 0  // timer start (ms)
  21. _ms_t2 = 0  // timer end (ms)
  22. _ms_ua = 1  // undo toggle
  23. _ms_u  = 0  // undo counter
  24. _ms_pc = 0  // completion percentage
  25. _ms_gm = 0  // game mode: 0 waiting; 1 active; 2 failure ; 3 victory
  26.  
  27. _ms_mf = "" // board containing mine placement
  28. _ms_nf = "" // board containing tile numbers
  29. _ms_cf = "" // board of completion (displayed)
  30. _ms_mp = "" // supplementary list with mine positions
  31. _ms_fp = "" // supplementary list with flag positions
  32. _ms_tq = "" // supplementary list with tile reveal queue
  33.  
  34.  
  35. // ----------------------------------------- //
  36. // ---------------- Tile ID ---------------- //
  37. // ----------------------------------------- //
  38. // 0   = blank tile
  39. // 1-8 = number tiles
  40. // H   = hidden tile
  41. // X   = mine tile (failure)
  42. // HX  = mine tile (victory)
  43. // R   = hit mine tile
  44. // F   = flagged tile
  45. // FX  = flagged mine tile (success)
  46. // FF  = flagged mine tile (failure)
  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.         if (< (indexof "R FX FF" (at $_ms_cf $i)) 0) [
  70.             case $_ms_gm 2 [
  71.                 _ms_tile_open "X" $i 0
  72.             ] 3 [
  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.         _ms_t2 = 0
  85.     ]
  86.     // reveal # tile at given position and add to completion counter if needed
  87.     _ms_cf = (listsplice $_ms_cf $arg1 $arg2 1)
  88.     if $arg3 [ _ms_pc = (+ $_ms_pc 1) ]
  89. ]
  90.  
  91. _ms_tile_find = [
  92.     local n c1 c2 c3 c4 c5 c6 c7 c8
  93.     n = 0
  94.     c1 = (- $arg9 $_ms_w)       // UP
  95.     c2 = (+ $arg9 $_ms_w)       // DN
  96.     c3 = (- $arg9 1)            // LT
  97.     c4 = (+ $arg9 1)            // RT
  98.     c5 = (- $arg9 (+ $_ms_w 1)) // UP-LT
  99.     c6 = (- $arg9 (- $_ms_w 1)) // UP-RT
  100.     c7 = (+ $arg9 (- $_ms_w 1)) // DN-LT
  101.     c8 = (+ $arg9 (+ $_ms_w 1)) // DN-RT
  102.     if (> $c1 -1)                                               $arg1 // UP
  103.     if (< $c2 $_ms_bt)                                          $arg2 // DN
  104.     if (> (mod $arg9 $_ms_w) 0)                                 $arg3 // LT
  105.     if (< (mod $arg9 $_ms_w) (- $_ms_w 1))                      $arg4 // RT
  106.     if (&& [> $c5 -1] [> (mod $arg9 $_ms_w) 0])                 $arg5 // UP-LT
  107.     if (&& [> $c6 -1] [< (mod $arg9 $_ms_w) (- $_ms_w 1)])      $arg6 // UP-RT
  108.     if (&& [< $c7 $_ms_bt] [> (mod $arg9 $_ms_w) 0])            $arg7 // DN-LT
  109.     if (&& [< $c8 $_ms_bt] [< (mod $arg9 $_ms_w) (- $_ms_w 1)]) $arg8 // DN-RT
  110.     arg10
  111. ]
  112.  
  113. _ms_tile_view = [
  114.     if (&& [< $_ms_gm 2] [=s (at $_ms_cf $arg1) "H"]) [
  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.             // if lives are enabled, reduce by one, otherwise
  121.             if (&& $_ms_ua $_ms_u) [ _ms_u = (- $_ms_u 1) ] [
  122.                 // game over, board is populated by hit and undiscovered mines and flag marks
  123.                 _ms_gm = 2
  124.                 _ms_t2 = (getmillis)
  125.                 _ms_set_markers
  126.             ]
  127.         ] "R" [
  128.             // do nothing here, simply skip
  129.         ] "F" [
  130.             // do nothing here, simply skip
  131.         ] "0" [
  132.             _ms_tile_open "0" $arg1 1
  133.             append _ms_tq $arg1
  134.             // explore all surrounding tiles until number border
  135.             while [listlen $_ms_tq] [
  136.                 do [ _ms_tile_find @(
  137.                     loopconcat i 8 [result [[
  138.                         n = $c@@(+ $i 1)
  139.                         // make sure the tile is not revealed, otherwise skip tile
  140.                         if (=s (at $_ms_cf $n) "H") [
  141.                             // ensure tile is blank and not in work queue, then add it
  142.                             if (&& [=s (at $_ms_nf $n) "0"] [< (indexof $_ms_tq $n) 0]) [
  143.                                 append _ms_tq $n
  144.                             ]
  145.                             // reveal the tile around the blank tile and add to completion percentage
  146.                             _ms_tile_open (at $_ms_nf $n) $n 1
  147.                         ]
  148.                     ]]]
  149.                 ) (at $_ms_tq 0) ]
  150.                 // remove used tile from work queue
  151.                 _ms_tq = (sublist $_ms_tq 1)
  152.             ]
  153.         ] () [ _ms_tile_open $n $arg1 1 ]
  154.         // in case of board completion, victory
  155.         if (= $_ms_pc (- $_ms_bt $_ms_m)) [
  156.             _ms_gm = 3 ; _ms_t2 = (getmillis)
  157.             _ms_set_markers
  158.         ]
  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.     _ms_ua = $_ms_tu
  174.     if $_ms_ua [ _ms_u  = (+ (div $_ms_m 20) 1) ]
  175.     _ms_bt = (* $_ms_h $_ms_w)
  176.     _ms_pc = 0
  177.     _ms_t1 = 0
  178.     _ms_t2 = 0
  179.     _ms_gm = 0
  180.     // initialise boards
  181.     _ms_nf = ""
  182.     _ms_mf = (loopconcat i $_ms_bt [result "0"])
  183.     _ms_cf = (loopconcat i $_ms_bt [result "H"])
  184.     // initialise supplementary lists
  185.     _ms_tq = ""
  186.     _ms_mp = ""
  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. newgui minesweeper [
  211.     guistayopen [
  212.         guilist [
  213.             guilist [
  214.                 guilist [
  215.                     guistrut 1
  216.                     guititle (format "^f(textures/minesweeper/globe) ^f%1%2%%" (
  217.                         result (case $_ms_gm 0 7 1 7 2 3 3 0)
  218.                     ) (absf (precf (*f (divf $_ms_pc (- $_ms_bt $_ms_m)) 100) 1)))
  219.                     guispring
  220.                     guititle (format "^f7%1 ^f(textures/minesweeper/clock)" (
  221.                         _ms_clock (case $_ms_gm 0 0 1 (- (getmillis) $_ms_t1) () (- $_ms_t2 $_ms_t1))
  222.                     ))
  223.                     guistrut 1
  224.                 ]
  225.                 guistrut 0.5
  226.                 loop y $_ms_h [
  227.                     guilist [
  228.                         loop x $_ms_w [
  229.                             n = (+ (* $y $_ms_w) $x)
  230.                             guifont "huge" [
  231.                                 guibutton (format "^f(textures/minesweeper/tiles/%1)" (at $_ms_cf $n)) [
  232.                                     _ms_tile_view @n
  233.                                 ] [
  234.                                     cond (=s (at $_ms_cf @n) "H") [
  235.                                         _ms_tile_open "F" @@n 0
  236.                                         append _ms_fp @@n
  237.                                     ] (=s (at $_ms_cf @n) "F") [
  238.                                         _ms_tile_open "H" @@n 0
  239.                                         _ms_fp = (listdel $_ms_fp @@n)
  240.                                     ]
  241.                                 ]
  242.                             ]
  243.                         ]
  244.                     ]
  245.                 ]
  246.             ]
  247.             if (!=s $_ms_cf "") [ guistrut 2 ]
  248.             guilist [
  249.                 guifont "super" [
  250.                     guibutton "^f(textures/minesweeper/new) ^f7New Game" [
  251.                         _ms_make_board $_ms_th $_ms_tw $_ms_tm
  252.                     ] []
  253.                     guistrut 1.5
  254.                     guilist [
  255.                         guitext (format "^f(textures/minesweeper/life) ^f%1%2" (? $_ms_u 5 3) $_ms_u)
  256.                         guistrut 1.8
  257.                         guitext (format "^f(textures/minesweeper/flag) ^f5%1" (- $_ms_m (listlen $_ms_fp)))
  258.                     ]
  259.                     guispring
  260.                     guilist [
  261.                         guilist [
  262.                             guilist [
  263.                                 guitext "^f(textures/minesweeper/height)"
  264.                                 guifield _ms_th 2 [ _ms_set_options $_ms_th 0 ]
  265.                             ]
  266.                             guilist [
  267.                                 guitext "^f(textures/minesweeper/width)"
  268.                                 guifield _ms_tw 2 [ _ms_set_options 0 $_ms_tw ]
  269.                             ]
  270.                         ]
  271.                         guistrut 2.5
  272.                         guilist [
  273.                             guicenter [ guitext "^f(textures/minesweeper/mine)" ]
  274.                             guifield _ms_tm 3 [ _ms_set_options 0 0 ]
  275.                         ]
  276.                     ]
  277.                 ]
  278.                 guistrut 1
  279.                 guititle "^f4Difficulty"
  280.                 guistrut 0.25
  281.                 guibutton "^f7Beginner"     [ _ms_make_board  9  9 10 ] []
  282.                 guibutton "^f7Intermediate" [ _ms_make_board 16 16 40 ] []
  283.                 guibutton "^f7Expert"       [ _ms_make_board 16 30 80 ] []
  284.                 guistrut 1
  285.                 guibutton (format "^f(textures/minesweeper/check_%1) Use Lives" (
  286.                     ? (= (+ $_ms_tu $_ms_ua) 0) "off" (? (= (+ $_ms_tu $_ms_ua) 2) "on" "sel")
  287.                 )) [ _ms_tu = (! $_ms_tu) ]
  288.                 guistrut 0.5
  289.             ]
  290.         ]
  291.     ]
  292. ] [
  293.     if (= $guipasses 0) [
  294.         _ms_make_board $_ms_th $_ms_tw $_ms_tm
  295.     ]
  296. ]
Advertisement
Add Comment
Please, Sign In to add comment