Advertisement
RaZgRiZ

MineSweeper WIP (working on mines)

Jun 5th, 2013
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. clock = [
  2.     strreplace (concat (
  3.         zeropad (div $arg1 3600000)
  4.     ) (
  5.         zeropad (mod (div $arg1 60000) 60)
  6.     ) (
  7.         zeropad (mod (div $arg1 1000) 60)
  8.     )) " " ":"
  9. ]
  10.  
  11. zeropad = [concatword (? (< $arg1 10) 0) $arg1]
  12. append = [$arg1 = (concat (getalias $arg1) $arg2)]
  13. precf = [substr $arg1 0 (+ (strstr $arg1 .) (+ $arg2 1))]
  14.  
  15. _ms_h  = 9  // board height
  16. _ms_w  = 9  // board width
  17. _ms_th = 9  // temp H
  18. _ms_tw = 9  // temp W
  19. _ms_mt = 0  // board tiles (total)
  20. _ms_m  = 10 // board mines
  21. _ms_t  = 0  // timer start (ms)
  22. _ms_tt = 0  // timer toggle
  23. _ms_u  = 0  // undo counter
  24. _ms_um = 0  // undo max
  25. _ms_up = -1 // undo tile position
  26. _ms_pc = 0  // completion percentage
  27. _ms_gm = 0  // game mode: 0 waiting; 1 active; 2 failure ; 3 victory
  28.  
  29. _ms_mf = "" // board containing mine placement
  30. _ms_nf = "" // board containing tile numbers
  31. _ms_cf = "" // board of completion (displayed)
  32. _ms_tf = "" // board used upon victory/fail
  33.  
  34. _ms_tile_find = [
  35.     local n c1 c2 c3 c4 c5 c6 c7 c8
  36.     n = 0
  37.     c1 = (- $arg9 $_ms_w)       // UP
  38.     c2 = (+ $arg9 $_ms_w)       // DN
  39.     c3 = (- $arg9 1)            // LT
  40.     c4 = (+ $arg9 1)            // RT
  41.     c5 = (- $arg9 (+ $_ms_w 1)) // UP-LT
  42.     c6 = (- $arg9 (- $_ms_w 1)) // UP-RT
  43.     c7 = (+ $arg9 (- $_ms_w 1)) // DN-LT
  44.     c8 = (+ $arg9 (+ $_ms_w 1)) // DN-RT
  45.     if (> $c1 -1)                                               $arg1 // UP
  46.     if (< $c2 $_ms_mt)                                          $arg2 // DN
  47.     if (> (mod $arg9 $_ms_w) 0)                                 $arg3 // LT
  48.     if (< (mod $arg9 $_ms_w) (- $_ms_w 1))                      $arg4 // RT
  49.     if (&& (> $c5 -1) (> (mod $arg9 $_ms_w) 0))                 $arg5 // UP-LT
  50.     if (&& (> $c6 -1) (< (mod $arg9 $_ms_w) (- $_ms_w 1)))      $arg6 // UP-RT
  51.     if (&& (< $c7 $_ms_mt) (> (mod $arg9 $_ms_w) 0))            $arg7 // DN-LT
  52.     if (&& (< $c8 $_ms_mt) (< (mod $arg9 $_ms_w) (- $_ms_w 1))) $arg8 // DN-RT
  53.     arg10
  54. ]
  55.  
  56. _ms_tile_open = [
  57.     if (&& [= $_ms_gm 1] [=s (at $_ms_cf $arg1) "C"]) [
  58.         local n
  59.         n = (at $_ms_nf $arg1)
  60.         cases $n "X" [
  61.             // Reserved for mines!
  62.             _ms_cf = (listsplice $_ms_cf $n $arg1 1)
  63.             _ms_up = $arg1 ; _ms_gm = 2
  64.            
  65.         ] "F" [
  66.             // Reserved for flags!
  67.            
  68.         ] () [
  69.             _ms_cf = (listsplice $_ms_cf $n $arg1 1)
  70.             do [ _ms_tile_find @(
  71.                 loopconcat i 8 [result [[
  72.                     if (= @@@n 0) [
  73.                         _ms_tile_open $c@@@(+ $i 1)
  74.                     ]
  75.                 ]]]
  76.             ) $arg1 ]
  77.             _ms_pc = (+ $_ms_pc 1)
  78.             if (= $_ms_pc (- $_ms_mt $_ms_m)) [
  79.                 // In case of board completion, victory
  80.                 _ms_gm = 3
  81.             ]
  82.         ]
  83.     ]
  84. ]
  85.  
  86. _ms_make_board = [
  87.     // Initialise variables
  88.     local i n r
  89.     _ms_th = $arg1 ; _ms_tw = $arg2 ; _ms_m = $arg3
  90.     _ms_h = $_ms_th ; _ms_w = $_ms_tw ; _ms_nf = ""
  91.     _ms_mt = (* $_ms_h $_ms_w) ; _ms_pc = 0; i = 0
  92.     _ms_um = (+ (div $_ms_m 20) 1); _ms_u = $_ms_um
  93.     _ms_up = -1 ; _ms_t = (getmillis) ; _ms_tt = 1
  94.     _ms_gm = 1 ;
  95.     // Initialise boards
  96.     _ms_mf = (loopconcat i $_ms_mt [result "0"])
  97.     _ms_cf = (loopconcat i $_ms_mt [result "C"])
  98.     // Begin randomly placing mines
  99.     while [< $i $_ms_m] [
  100.         r = (rnd $_ms_mt)
  101.         if (! (at $_ms_mf $r)) [ // Make sure it's not writing to a previously picked spot
  102.             _ms_mf = (listsplice $_ms_mf "1" $r 1)
  103.             i = (+ $i 1)
  104.         ]
  105.     ]
  106.     // Generate complete board with numbers
  107.     loop y $_ms_mt [
  108.         if (at $_ms_mf $y) [ append _ms_nf "X" ] [
  109.             do [ _ms_tile_find @(
  110.                 loopconcat i 8 [result [[
  111.                     n = (+ (at $_ms_mf $c@@(+ $i 1)) $n)
  112.                 ]]]
  113.             ) $y [ append _ms_nf $n ] ]
  114.         ]
  115.     ]
  116. ]
  117.  
  118. newgui minesweeper [
  119.     guistayopen [
  120.         guititle (format "^f7Completed:^f%1 %2%% ^f7-- Time: ^f2%3" (
  121.             result (case $_ms_gm 0 4 1 2 2 3 3 0)
  122.         ) (
  123.             absf (precf (*f (divf $_ms_pc (- $_ms_mt $_ms_m)) 100) 1)
  124.         ) (
  125.             clock (? $_ms_tt (- (getmillis) $_ms_t) 0)
  126.         ))
  127.         guistrut 0.5
  128.         guilist [
  129.             guilist [
  130.                 loop y $_ms_h [
  131.                     guilist [
  132.                         loop x $_ms_w [
  133.                             n = (+ (* $y $_ms_w) $x)
  134.                             guiimage (format "packages/icons/minesweeper/%1.png" (at $_ms_cf $n)) [
  135.                                 _ms_tile_open @n
  136.                             ] 0.5 0
  137.                         ]
  138.                     ]
  139.                 ]
  140.             ]
  141.             if (!=s $_ms_cf "") [ guistrut 1.5 ]
  142.             guilist [
  143.                 guibutton "New Minefield" [
  144.                     _ms_make_board $_ms_th $_ms_tw $_ms_m
  145.                 ] 0
  146.                 guilist [
  147.                     guibutton "Undo Move" [
  148.                         if (&& [> $_ms_up -1] $_ms_u) [
  149.                             _ms_cf = (listsplice $_ms_cf "C" $_ms_up 1)
  150.                             _ms_u = (- $_ms_u 1) ; _ms_up = -1 ; _ms_gm = 1
  151.                         ]
  152.                     ] 0
  153.                     guistrut 0.5
  154.                     guitext (format "^f2(%1/%2)" $_ms_u $_ms_um) 0
  155.                 ]
  156.                 guistrut 0.5
  157.                 guilist [
  158.                     guilist [
  159.                         guialign 0 [ guitext "" "minesweeper/resize_h.png" ]
  160.                         guifield _ms_th 2 [ _ms_th = (max (min $_ms_th 24) 9) ]
  161.                     ]
  162.                     guilist [
  163.                         guialign 0 [ guitext "" "minesweeper/resize_w.png" ]
  164.                         guifield _ms_tw 2 [ _ms_tw = (max (min $_ms_tw 30) 9) ]
  165.                     ]
  166.                     guistrut 0.5
  167.                     guilist [
  168.                         guialign 0 [ guitext "" "minesweeper/mine_raw.png" ]
  169.                         guifield _ms_m 3 [ _ms_m = (max (min $_ms_m (div (* $_ms_th $_ms_tw) 3)) 10) ]
  170.                     ]
  171.                 ]
  172.                 guistrut 1
  173.                 guibutton "Beginner"     [ _ms_make_board  9  9 10 ] 0
  174.                 guibutton "Intermediate" [ _ms_make_board 16 16 40 ] 0
  175.                 guibutton "Expert"       [ _ms_make_board 16 30 80 ] 0
  176.                 guistrut 1
  177.                 guibutton "^f6Show Board" [ _ms_cf = $_ms_nf ] 0
  178.             ]
  179.         ]
  180.     ]
  181. ] 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement