Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clock = [
- strreplace (concat (
- zeropad (div $arg1 3600000)
- ) (
- zeropad (mod (div $arg1 60000) 60)
- ) (
- zeropad (mod (div $arg1 1000) 60)
- )) " " ":"
- ]
- zeropad = [concatword (? (< $arg1 10) 0) $arg1]
- append = [$arg1 = (concat (getalias $arg1) $arg2)]
- precf = [substr $arg1 0 (+ (strstr $arg1 .) (+ $arg2 1))]
- _ms_h = 9 // board height
- _ms_w = 9 // board width
- _ms_th = 9 // temp H
- _ms_tw = 9 // temp W
- _ms_mt = 0 // board tiles (total)
- _ms_m = 10 // board mines
- _ms_t = 0 // timer start (ms)
- _ms_tt = 0 // timer toggle
- _ms_u = 0 // undo counter
- _ms_um = 0 // undo max
- _ms_up = -1 // undo tile position
- _ms_pc = 0 // completion percentage
- _ms_gm = 0 // game mode: 0 waiting; 1 active; 2 failure ; 3 victory
- _ms_mf = "" // board containing mine placement
- _ms_nf = "" // board containing tile numbers
- _ms_cf = "" // board of completion (displayed)
- _ms_tf = "" // board used upon victory/fail
- _ms_tile_find = [
- local n c1 c2 c3 c4 c5 c6 c7 c8
- n = 0
- c1 = (- $arg9 $_ms_w) // UP
- c2 = (+ $arg9 $_ms_w) // DN
- c3 = (- $arg9 1) // LT
- c4 = (+ $arg9 1) // RT
- c5 = (- $arg9 (+ $_ms_w 1)) // UP-LT
- c6 = (- $arg9 (- $_ms_w 1)) // UP-RT
- c7 = (+ $arg9 (- $_ms_w 1)) // DN-LT
- c8 = (+ $arg9 (+ $_ms_w 1)) // DN-RT
- if (> $c1 -1) $arg1 // UP
- if (< $c2 $_ms_mt) $arg2 // DN
- if (> (mod $arg9 $_ms_w) 0) $arg3 // LT
- if (< (mod $arg9 $_ms_w) (- $_ms_w 1)) $arg4 // RT
- if (&& (> $c5 -1) (> (mod $arg9 $_ms_w) 0)) $arg5 // UP-LT
- if (&& (> $c6 -1) (< (mod $arg9 $_ms_w) (- $_ms_w 1))) $arg6 // UP-RT
- if (&& (< $c7 $_ms_mt) (> (mod $arg9 $_ms_w) 0)) $arg7 // DN-LT
- if (&& (< $c8 $_ms_mt) (< (mod $arg9 $_ms_w) (- $_ms_w 1))) $arg8 // DN-RT
- arg10
- ]
- _ms_tile_open = [
- if (&& [= $_ms_gm 1] [=s (at $_ms_cf $arg1) "C"]) [
- local n
- n = (at $_ms_nf $arg1)
- cases $n "X" [
- // Reserved for mines!
- _ms_cf = (listsplice $_ms_cf $n $arg1 1)
- _ms_up = $arg1 ; _ms_gm = 2
- ] "F" [
- // Reserved for flags!
- ] () [
- _ms_cf = (listsplice $_ms_cf $n $arg1 1)
- do [ _ms_tile_find @(
- loopconcat i 8 [result [[
- if (= @@@n 0) [
- _ms_tile_open $c@@@(+ $i 1)
- ]
- ]]]
- ) $arg1 ]
- _ms_pc = (+ $_ms_pc 1)
- if (= $_ms_pc (- $_ms_mt $_ms_m)) [
- // In case of board completion, victory
- _ms_gm = 3
- ]
- ]
- ]
- ]
- _ms_make_board = [
- // Initialise variables
- local i n r
- _ms_th = $arg1 ; _ms_tw = $arg2 ; _ms_m = $arg3
- _ms_h = $_ms_th ; _ms_w = $_ms_tw ; _ms_nf = ""
- _ms_mt = (* $_ms_h $_ms_w) ; _ms_pc = 0; i = 0
- _ms_um = (+ (div $_ms_m 20) 1); _ms_u = $_ms_um
- _ms_up = -1 ; _ms_t = (getmillis) ; _ms_tt = 1
- _ms_gm = 1 ;
- // Initialise boards
- _ms_mf = (loopconcat i $_ms_mt [result "0"])
- _ms_cf = (loopconcat i $_ms_mt [result "C"])
- // Begin randomly placing mines
- while [< $i $_ms_m] [
- r = (rnd $_ms_mt)
- if (! (at $_ms_mf $r)) [ // Make sure it's not writing to a previously picked spot
- _ms_mf = (listsplice $_ms_mf "1" $r 1)
- i = (+ $i 1)
- ]
- ]
- // Generate complete board with numbers
- loop y $_ms_mt [
- if (at $_ms_mf $y) [ append _ms_nf "X" ] [
- do [ _ms_tile_find @(
- loopconcat i 8 [result [[
- n = (+ (at $_ms_mf $c@@(+ $i 1)) $n)
- ]]]
- ) $y [ append _ms_nf $n ] ]
- ]
- ]
- ]
- newgui minesweeper [
- guistayopen [
- guititle (format "^f7Completed:^f%1 %2%% ^f7-- Time: ^f2%3" (
- result (case $_ms_gm 0 4 1 2 2 3 3 0)
- ) (
- absf (precf (*f (divf $_ms_pc (- $_ms_mt $_ms_m)) 100) 1)
- ) (
- clock (? $_ms_tt (- (getmillis) $_ms_t) 0)
- ))
- guistrut 0.5
- guilist [
- guilist [
- loop y $_ms_h [
- guilist [
- loop x $_ms_w [
- n = (+ (* $y $_ms_w) $x)
- guiimage (format "packages/icons/minesweeper/%1.png" (at $_ms_cf $n)) [
- _ms_tile_open @n
- ] 0.5 0
- ]
- ]
- ]
- ]
- if (!=s $_ms_cf "") [ guistrut 1.5 ]
- guilist [
- guibutton "New Minefield" [
- _ms_make_board $_ms_th $_ms_tw $_ms_m
- ] 0
- guilist [
- guibutton "Undo Move" [
- if (&& [> $_ms_up -1] $_ms_u) [
- _ms_cf = (listsplice $_ms_cf "C" $_ms_up 1)
- _ms_u = (- $_ms_u 1) ; _ms_up = -1 ; _ms_gm = 1
- ]
- ] 0
- guistrut 0.5
- guitext (format "^f2(%1/%2)" $_ms_u $_ms_um) 0
- ]
- guistrut 0.5
- guilist [
- guilist [
- guialign 0 [ guitext "" "minesweeper/resize_h.png" ]
- guifield _ms_th 2 [ _ms_th = (max (min $_ms_th 24) 9) ]
- ]
- guilist [
- guialign 0 [ guitext "" "minesweeper/resize_w.png" ]
- guifield _ms_tw 2 [ _ms_tw = (max (min $_ms_tw 30) 9) ]
- ]
- guistrut 0.5
- guilist [
- guialign 0 [ guitext "" "minesweeper/mine_raw.png" ]
- guifield _ms_m 3 [ _ms_m = (max (min $_ms_m (div (* $_ms_th $_ms_tw) 3)) 10) ]
- ]
- ]
- guistrut 1
- guibutton "Beginner" [ _ms_make_board 9 9 10 ] 0
- guibutton "Intermediate" [ _ms_make_board 16 16 40 ] 0
- guibutton "Expert" [ _ms_make_board 16 30 80 ] 0
- guistrut 1
- guibutton "^f6Show Board" [ _ms_cf = $_ms_nf ] 0
- ]
- ]
- ]
- ] 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement