Advertisement
RaZgRiZ

MineSweeper WIP (awaiting flags)

Jul 8th, 2013
249
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. precf = [substr $arg1 0 (+ (strstr $arg1 ".") (+ $arg2 1))]
  12.  
  13. _ms_h  = 9  // board height
  14. _ms_w  = 9  // board width
  15. _ms_m  = 10 // board mines
  16. _ms_th = 9  // temp H
  17. _ms_tw = 9  // temp W
  18. _ms_tm = 10 // temp M
  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_um = 0  // undo max
  25. _ms_pc = 0  // completion percentage
  26. _ms_gm = 0  // game mode: 0 waiting; 1 active; 2 failure ; 3 victory
  27.  
  28. _ms_mf = "" // board containing mine placement
  29. _ms_mp = "" // supplement list with mine positions
  30. _ms_fp = "" // supplement list with flag positions
  31. _ms_nf = "" // board containing tile numbers
  32. _ms_cf = "" // board of completion (displayed)
  33. _ms_tq = "" // supplement list with tile reveal queue
  34.  
  35. _ms_tile_find = [
  36.     local n c1 c2 c3 c4 c5 c6 c7 c8
  37.     n = 0
  38.     c1 = (- $arg9 $_ms_w)       // UP
  39.     c2 = (+ $arg9 $_ms_w)       // DN
  40.     c3 = (- $arg9 1)            // LT
  41.     c4 = (+ $arg9 1)            // RT
  42.     c5 = (- $arg9 (+ $_ms_w 1)) // UP-LT
  43.     c6 = (- $arg9 (- $_ms_w 1)) // UP-RT
  44.     c7 = (+ $arg9 (- $_ms_w 1)) // DN-LT
  45.     c8 = (+ $arg9 (+ $_ms_w 1)) // DN-RT
  46.     if (> $c1 -1)                                               $arg1 // UP
  47.     if (< $c2 $_ms_bt)                                          $arg2 // DN
  48.     if (> (mod $arg9 $_ms_w) 0)                                 $arg3 // LT
  49.     if (< (mod $arg9 $_ms_w) (- $_ms_w 1))                      $arg4 // RT
  50.     if (&& (> $c5 -1) (> (mod $arg9 $_ms_w) 0))                 $arg5 // UP-LT
  51.     if (&& (> $c6 -1) (< (mod $arg9 $_ms_w) (- $_ms_w 1)))      $arg6 // UP-RT
  52.     if (&& (< $c7 $_ms_bt) (> (mod $arg9 $_ms_w) 0))            $arg7 // DN-LT
  53.     if (&& (< $c8 $_ms_bt) (< (mod $arg9 $_ms_w) (- $_ms_w 1))) $arg8 // DN-RT
  54.     arg10
  55. ]
  56.  
  57. _ms_tile_open = [
  58.     if (&& [= $_ms_gm 1] [=s (at $_ms_cf $arg1) "H"]) [
  59.         local n t
  60.         n = (at $_ms_nf $arg1)
  61.         cases $n "X" [
  62.             // mark the mine as hit
  63.             _ms_cf = (listsplice $_ms_cf "R" $arg1 1)
  64.             // if lives are enabled, reduce by one, otherwise
  65.             if (&& $_ms_ua $_ms_u) [ _ms_u = (- $_ms_u 1) ] [
  66.                 // game over, board is populated by hit and undiscovered mines and flag marks
  67.                 looplist i $_ms_mp [
  68.                     if (!=s (at $_ms_cf $i) "R") [
  69.                         _ms_cf = (listsplice $_ms_cf "X" $i 1)
  70.                     ]
  71.                 ]
  72.                 _ms_gm = 2
  73.                 _ms_t2 = (getmillis)
  74.             ]
  75.         ] "R" [
  76.             // do nothing here, simply skip
  77.         ] "F" [
  78.             // do nothing here, simply skip
  79.         ] "0" [
  80.             // explore all surrounding tiles until number border
  81.             append _ms_tq $arg1
  82.             while [listlen $_ms_tq] [
  83.                 do [ _ms_tile_find @(
  84.                     loopconcat i 8 [result [[
  85.                         // make sure the tile is not revealed, otherwise skip tile
  86.                         if (=s (at $_ms_cf $c@@(+ $i 1)) "H") [
  87.                             // make sure tile is blank and not in work queue, then add it
  88.                             if (&& [=s (at $_ms_nf $c@@@@(+ $i 1)) "0"] [< (indexof $_ms_tq $c@@@@(+ $i 1)) 0]) [
  89.                                 append _ms_tq $c@@@@(+ $i 1)
  90.                             ]
  91.                             // reveal the tile around the blank tile and add to completion percentage
  92.                             _ms_cf = (listsplice $_ms_cf (at $_ms_nf $c@@@(+ $i 1)) $c@@@(+ $i 1) 1)
  93.                             _ms_pc = (+ $_ms_pc 1)
  94.                         ]
  95.                     ]]]
  96.                 ) (at $_ms_tq 0) ]
  97.                 // remove used tile from work queue
  98.                 _ms_tq = (sublist $_ms_tq 1)
  99.             ]
  100.         ] () [
  101.             // reveal the tile and add to completion percentage
  102.             _ms_cf = (listsplice $_ms_cf $n $arg1 1)
  103.             _ms_pc = (+ $_ms_pc 1)
  104.         ]
  105.             // in case of board completion, victory
  106.         if (= $_ms_pc (- $_ms_bt $_ms_m)) [
  107.             _ms_gm = 3 ; _ms_t2 = (getmillis)
  108.         ]
  109.     ]
  110. ]
  111.  
  112. _ms_make_board = [
  113.     // Initialise variables
  114.     local i n r
  115.     _ms_th = $arg1 ; _ms_tw = $arg2 ; _ms_m = $arg3
  116.     _ms_h = $_ms_th ; _ms_w = $_ms_tw ; _ms_tm = $_ms_m
  117.     _ms_bt = (* $_ms_h $_ms_w) ; _ms_pc = 0; i = 0
  118.     _ms_um = (+ (div $_ms_m 20) 1); _ms_u = $_ms_um
  119.     _ms_t1 = (getmillis) ; _ms_t2 = 0 ; _ms_tq = ""
  120.     _ms_gm = 1 ; _ms_mp = "" ; _ms_fp = "" ; _ms_nf = ""
  121.     // Initialise boards
  122.     _ms_mf = (loopconcat i $_ms_bt [result "0"])
  123.     _ms_cf = (loopconcat i $_ms_bt [result "H"])
  124.     // Begin randomly placing mines
  125.     while [< $i $_ms_m] [
  126.         r = (rnd $_ms_bt)
  127.         // Make sure it's not writing to a previously picked spot
  128.         if (! (at $_ms_mf $r)) [
  129.             _ms_mf = (listsplice $_ms_mf "1" $r 1)
  130.             append _ms_mp $r
  131.             i = (+ $i 1)
  132.         ]
  133.     ]
  134.     // Generate complete board with numbers
  135.     loop y $_ms_bt [
  136.         if (at $_ms_mf $y) [ append _ms_nf "X" ] [
  137.             do [ _ms_tile_find @(
  138.                 loopconcat i 8 [result [[
  139.                     n = (+ (at $_ms_mf $c@@(+ $i 1)) $n)
  140.                 ]]]
  141.             ) $y [ append _ms_nf $n ] ]
  142.         ]
  143.     ]
  144. ]
  145.  
  146. newgui minesweeper [
  147.     guistayopen [
  148.         guititle (format "^f7Completed:^f%1 %2%% ^f7-- Time: ^f2%3" (
  149.             result (case $_ms_gm 0 4 1 2 2 3 3 0)
  150.         ) (
  151.             absf (precf (*f (divf $_ms_pc (- $_ms_bt $_ms_m)) 100) 1)
  152.         ) (
  153.             _ms_clock (case $_ms_gm 0 0 1 (- (getmillis) $_ms_t1) () (- $_ms_t2 $_ms_t1))
  154.         ))
  155.         guistrut 0.5
  156.         guilist [
  157.             guilist [
  158.                 loop y $_ms_h [
  159.                     guilist [
  160.                         loop x $_ms_w [
  161.                             n = (+ (* $y $_ms_w) $x)
  162.                             guiimage (format "packages/icons/minesweeper/%1.png" (at $_ms_cf $n)) [
  163.                                 _ms_tile_open @n
  164.                             ] 0.5 0
  165.                         ]
  166.                     ]
  167.                 ]
  168.             ]
  169.             if (!=s $_ms_cf "") [ guistrut 1.5 ]
  170.             guilist [
  171.                 guibutton "New Minefield" [
  172.                     _ms_make_board $_ms_th $_ms_tw $_ms_tm
  173.                 ] 0
  174.                 guitext (format "^f7Lives: ^f2%1/%2" $_ms_u $_ms_um) 0
  175.                 //if (! $_ms_gm) [
  176.                     guistrut 0.5
  177.                     guilist [
  178.                         guilist [
  179.                             guialign 0 [ guitext "" "minesweeper/resize_h.png" ]
  180.                             guifield _ms_th 2 [ _ms_th = (max (min $_ms_th 24) 9) ]
  181.                         ]
  182.                         guilist [
  183.                             guialign 0 [ guitext "" "minesweeper/resize_w.png" ]
  184.                             guifield _ms_tw 2 [ _ms_tw = (max (min $_ms_tw 30) 9) ]
  185.                         ]
  186.                         guistrut 0.5
  187.                         guilist [
  188.                             guialign 0 [ guitext "" "minesweeper/mine_raw.png" ]
  189.                             guifield _ms_tm 3 [ _ms_tm = (max (min $_ms_tm (div (* $_ms_th $_ms_tw) 3)) 10) ]
  190.                         ]
  191.                     ]
  192.                     guistrut 1
  193.                     guititle "^f2Difficulty"
  194.                     guistrut 0.5
  195.                     guibutton "Beginner"     [ _ms_make_board  9  9 10 ] 0
  196.                     guibutton "Intermediate" [ _ms_make_board 16 16 40 ] 0
  197.                     guibutton "Expert"       [ _ms_make_board 16 30 80 ] 0
  198.                 //]
  199.                 //guibutton "^f6Show Board" [ _ms_cf = $_ms_nf ] 0
  200.             ]
  201.         ]
  202.     ]
  203. ] 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement