RaZgRiZ

MineSweeper WIP (board functional)

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