View difference between Paste ID: xW8cEqkE and UvprK4Pm
SHOW: | | - or go back to the newest paste.
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]
9+
padnum = [ concatword (? (< $arg1 $arg2) 0) $arg1 ]
10-
append = [$arg1 = (concat (getalias $arg1) $arg2)]
10+
append = [ $arg1 = (concat (getalias $arg1) $arg2) ]
11-
precf = [substr $arg1 0 (+ (strstr $arg1 ".") (+ $arg2 1))]
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
29+
30-
_ms_fp = "" // supplement list with flag positions
30+
31
_ms_mp = "" // supplementary list with mine positions
32
_ms_fp = "" // supplementary list with flag positions
33-
_ms_tq = "" // supplement list with tile reveal queue
33+
_ms_tq = "" // supplementary list with tile reveal queue
34
35
36
// ----------------------------------------- //
37
// ---------------- Tile ID ---------------- //
38
// ----------------------------------------- //
39
// 0   = blank tile
40
// 1-8 = number tiles
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-
	if (&& (> $c5 -1) (> (mod $arg9 $_ms_w) 0))                 $arg5 // UP-LT
50+
	local n
51-
	if (&& (> $c6 -1) (< (mod $arg9 $_ms_w) (- $_ms_w 1)))      $arg6 // UP-RT
51+
	n = (* $_ms_th $_ms_tw)
52-
	if (&& (< $c7 $_ms_bt) (> (mod $arg9 $_ms_w) 0))            $arg7 // DN-LT
52+
	// calculate board sizes/mines and apply limits
53-
	if (&& (< $c8 $_ms_bt) (< (mod $arg9 $_ms_w) (- $_ms_w 1))) $arg8 // DN-RT
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_cf = (listsplice $_ms_cf "R" $arg1 1)
63+
				_ms_tile_open "FX" $i 0
64
			] [ _ms_tile_open "FF" $i 0 ]
65
		]
66
	]
67-
				looplist i $_ms_mp [
67+
	// update mine markers, discounting flagged ones
68-
					if (!=s (at $_ms_cf $i) "R") [
68+
	looplist i $_ms_mp [
69-
						_ms_cf = (listsplice $_ms_cf "X" $i 1)
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
	// reveal # tile at given position and add to completion counter if needed
81
	_ms_cf = (listsplice $_ms_cf $arg1 $arg2 1)
82
	if $arg3 [ _ms_pc = (+ $_ms_pc 1) ]
83
]
84
85
_ms_tile_find = [
86-
						if (=s (at $_ms_cf $c@@(+ $i 1)) "H") [
86+
87-
							// make sure tile is blank and not in work queue, then add it
87+
88-
							if (&& [=s (at $_ms_nf $c@@@@(+ $i 1)) "0"] [< (indexof $_ms_tq $c@@@@(+ $i 1)) 0]) [
88+
89-
								append _ms_tq $c@@@@(+ $i 1)
89+
90
	c3 = (- $arg9 1)            // LT
91
	c4 = (+ $arg9 1)            // RT
92-
							_ms_cf = (listsplice $_ms_cf (at $_ms_nf $c@@@(+ $i 1)) $c@@@(+ $i 1) 1)
92+
93-
							_ms_pc = (+ $_ms_pc 1)
93+
94
	c7 = (+ $arg9 (- $_ms_w 1)) // DN-LT
95
	c8 = (+ $arg9 (+ $_ms_w 1)) // DN-RT
96
	if (> $c1 -1)                                               $arg1 // UP
97
	if (< $c2 $_ms_bt)                                          $arg2 // DN
98
	if (> (mod $arg9 $_ms_w) 0)                                 $arg3 // LT
99
	if (< (mod $arg9 $_ms_w) (- $_ms_w 1))                      $arg4 // RT
100-
		] () [
100+
	if (&& [> $c5 -1] [> (mod $arg9 $_ms_w) 0])                 $arg5 // UP-LT
101-
			// reveal the tile and add to completion percentage
101+
	if (&& [> $c6 -1] [< (mod $arg9 $_ms_w) (- $_ms_w 1)])      $arg6 // UP-RT
102-
			_ms_cf = (listsplice $_ms_cf $n $arg1 1)
102+
	if (&& [< $c7 $_ms_bt] [> (mod $arg9 $_ms_w) 0])            $arg7 // DN-LT
103-
			_ms_pc = (+ $_ms_pc 1)
103+
	if (&& [< $c8 $_ms_bt] [< (mod $arg9 $_ms_w) (- $_ms_w 1)]) $arg8 // DN-RT
104
	arg10
105-
			// in case of board completion, victory
105+
106
107
_ms_tile_view = [
108
	if (&& [= $_ms_gm 1] [=s (at $_ms_cf $arg1) "H"]) [
109
		local n t
110
		n = (at $_ms_nf $arg1)
111
		cases $n "X" [
112
			// mark the mine as hit
113-
	// Initialise variables
113+
			_ms_tile_open "R" $arg1 0
114
			// if lives are enabled, reduce by one, otherwise
115-
	_ms_th = $arg1 ; _ms_tw = $arg2 ; _ms_m = $arg3
115+
116-
	_ms_h = $_ms_th ; _ms_w = $_ms_tw ; _ms_tm = $_ms_m
116+
117-
	_ms_bt = (* $_ms_h $_ms_w) ; _ms_pc = 0; i = 0
117+
118-
	_ms_um = (+ (div $_ms_m 20) 1); _ms_u = $_ms_um
118+
119-
	_ms_t1 = (getmillis) ; _ms_t2 = 0 ; _ms_tq = ""
119+
				_ms_set_markers
120-
	_ms_gm = 1 ; _ms_mp = "" ; _ms_fp = "" ; _ms_nf = ""
120+
121-
	// Initialise boards
121+
122
			// do nothing here, simply skip
123
		] "F" [
124-
	// Begin randomly placing mines
124+
125
		] "0" [
126
			_ms_tile_open "0" $arg1 1
127-
		// Make sure it's not writing to a previously picked spot
127+
128
			// explore all surrounding tiles until number border
129
			while [listlen $_ms_tq] [
130
				do [ _ms_tile_find @(
131
					loopconcat i 8 [result [[
132
						n = $c@@(+ $i 1)
133
						// make sure the tile is not revealed, otherwise skip tile
134-
	// Generate complete board with numbers
134+
						if (=s (at $_ms_cf $n) "H") [
135
							// ensure tile is blank and not in work queue, then add it
136
							if (&& [=s (at $_ms_nf $n) "0"] [< (indexof $_ms_tq $n) 0]) [
137
								append _ms_tq $n
138
							]
139
							// reveal the tile around the blank tile and add to completion percentage
140
							_ms_tile_open (at $_ms_nf $n) $n 1
141
						]
142
					]]]
143
				) (at $_ms_tq 0) ]
144
				// remove used tile from work queue
145
				_ms_tq = (sublist $_ms_tq 1)
146
			]
147
		] () [ _ms_tile_open $n $arg1 1 ]
148
		// in case of board completion, victory
149
		if (= $_ms_pc (- $_ms_bt $_ms_m)) [
150
			_ms_gm = 3 ; _ms_t2 = (getmillis)
151
			_ms_set_markers
152
		]
153
	]
154
]
155
156
_ms_make_board = [
157
	local i n r
158
	i = 0
159
	// initialise board variables
160
	_ms_th = $arg1
161
	_ms_tw = $arg2
162
	_ms_m  = $arg3
163-
								_ms_tile_open @n
163+
	_ms_h  = $_ms_th
164
	_ms_w  = $_ms_tw
165
	_ms_tm = $_ms_m
166
	// initialise game variables
167
	_ms_bt = (* $_ms_h $_ms_w)
168
	_ms_um = (+ (div $_ms_m 20) 1)
169
	_ms_u  = $_ms_um
170
	_ms_pc = 0
171
	_ms_t1 = 0
172
	_ms_t2 = 0
173
	_ms_gm = 0
174
	// initialise boards
175
	_ms_nf = ""
176
	_ms_mf = (loopconcat i $_ms_bt [result "0"])
177
	_ms_cf = (loopconcat i $_ms_bt [result "H"])
178
	// initialise supplementary lists
179
	_ms_tq = ""
180-
							guifield _ms_th 2 [ _ms_th = (max (min $_ms_th 24) 9) ]
180+
	_ms_mp = ""
181
	_ms_fp = ""
182
	// generate the mines board
183
	while [< $i $_ms_m] [
184-
							guifield _ms_tw 2 [ _ms_tw = (max (min $_ms_tw 30) 9) ]
184+
185
		// avoid writing to a previously picked spot
186
		if (! (at $_ms_mf $r)) [
187
			_ms_mf = (listsplice $_ms_mf "1" $r 1)
188
			append _ms_mp $r
189-
							guifield _ms_tm 3 [ _ms_tm = (max (min $_ms_tm (div (* $_ms_th $_ms_tw) 3)) 10) ]
189+
190
		]
191
	]
192
	// generate the numbers board
193
	loop y $_ms_bt [
194
		if (at $_ms_mf $y) [ append _ms_nf "X" ] [
195
			do [ _ms_tile_find @(
196
				loopconcat i 8 [result [[
197
					n = (+ (at $_ms_mf $c@@(+ $i 1)) $n)
198
				]]]
199-
				//guibutton "^f6Show Board" [ _ms_cf = $_ms_nf ] 0
199+
200
		]
201
	]
202
]
203-
] 0
203+
204
newgui minesweeper [
205
	guistayopen [
206
		guititle (format "^f7Completed:^f%1 %2%% ^f7-- Time: ^f2%3" (
207
			result (case $_ms_gm 0 4 1 2 2 3 3 0)
208
		) (
209
			absf (precf (*f (divf $_ms_pc (- $_ms_bt $_ms_m)) 100) 1)
210
		) (
211
			_ms_clock (case $_ms_gm 0 0 1 (- (getmillis) $_ms_t1) () (- $_ms_t2 $_ms_t1))
212
		))
213
		guistrut 0.5
214
		guilist [
215
			guilist [
216
				loop y $_ms_h [
217
					guilist [
218
						loop x $_ms_w [
219
							n = (+ (* $y $_ms_w) $x)
220
							guiimage (format "packages/icons/minesweeper/%1.png" (at $_ms_cf $n)) [
221
								if $test [ // placeholder for right-click flag marks
222
									cond (=s (at $_ms_cf @@n) "H") [
223
										_ms_tile_open "F" @@@n 0
224
										append _ms_fp @@@n
225
									] (=s (at $_ms_cf @@n) "F") [
226
										_ms_tile_open "H" @@@n 0
227
										_ms_fp = (listdel $_ms_fp @@@n)
228
									]
229
								] [
230
									if (= $_ms_gm 0) [
231
										_ms_gm = 1
232
										_ms_t1 = (getmillis)
233
										_ms_t2 = 0
234
									]
235
									_ms_tile_view @@n
236
								]
237
							] 0.5 0
238
						]
239
					]
240
				]
241
			]
242
			if (!=s $_ms_cf "") [ guistrut 1.5 ]
243
			guilist [
244
				guibutton "New Minefield" [
245
					_ms_make_board $_ms_th $_ms_tw $_ms_tm
246
				] 0
247
				guitext (format "^f7Lives: ^f2%1/%2" $_ms_u $_ms_um) 0
248
				//if (! $_ms_gm) [
249
					guicheckbox test test
250
					guistrut 0.5
251
					guilist [
252
						guilist [
253
							guialign 0 [ guitext "" "minesweeper/resize_h.png" ]
254
							guifield _ms_th 2 [ _ms_set_options $_ms_th 0 ]
255
						]
256
						guilist [
257
							guialign 0 [ guitext "" "minesweeper/resize_w.png" ]
258
							guifield _ms_tw 2 [ _ms_set_options 0 $_ms_tw ]
259
						]
260
						guistrut 0.5
261
						guilist [
262
							guialign 0 [ guitext "" "minesweeper/mine_raw.png" ]
263
							guifield _ms_tm 3 [ _ms_set_options 0 0 ]
264
						]
265
					]
266
					guistrut 1
267
					guititle "^f2Difficulty"
268
					guistrut 0.5
269
					guibutton "Beginner"     [ _ms_make_board  9  9 10 ] 0
270
					guibutton "Intermediate" [ _ms_make_board 16 16 40 ] 0
271
					guibutton "Expert"       [ _ms_make_board 16 30 80 ] 0
272
				//]
273
			]
274
		]
275
	]
276
] 0 [ _ms_make_board $_ms_th $_ms_tw $_ms_tm ]