// 扫雷 —— 用 **Go** 写的手机小游戏（网格推理）
// Minesweeper — a small phone game written in **Go** (grid deduction)
//
// 玩法（与 Examples/c/app/minesweeper.c / Examples/python/app/minesweeper.py 同一套）：
//   9×9 共 10 颗雷；点格子翻开，数字是周围 8 格的雷数；翻到 0 会**连锁展开**；
//   顶部按钮切到「插旗」模式再点就是标雷；把所有非雷格子翻开即通关，踩到雷就结束。
//
// ## Go 这一路的写法约束（见 Examples/go/app/snake.go 文件头）
//   · 全部状态放一个模块级数组 `A`；数字走逐位 `digitStr`；函数名带 `ms` 前缀。
//
// 跑法 —— 手机：vml run examples/go/minesweeper.go
// 跑法 —— 桌面：vmlcli Examples/go/minesweeper.go --screen 480x640 --frames /tmp/fr

import "vmlui"

// 分区：0..80 雷；81..161 翻开；162..242 旗；243..323 队列X；324..404 队列Y
// 405=opened 406=dead 407=won 408=flagMode 409=frames 410=cell 411=ox 412=oy
// 413=w 414=h 415=lang 416=tid 417=i 418=head 419=tail 420=cx 421=cy
// 422=dx 423=dy 424=nx 425=ny 426=n 427=flags 428=tx 429=ty 430=seed 431=x 432=y 433=t
// 434=topY 435=btnH 436=btnTextSize 437=numSize 438=labelSize 439=hintSize 440=hintY
var A [441]int

const (
	msGW        = 9
	msMines     = 10
	msMaxFrames = 4000
)

func msDigitStr(d int) string {
	if d == 0 {
		return "0"
	}
	if d == 1 {
		return "1"
	}
	if d == 2 {
		return "2"
	}
	if d == 3 {
		return "3"
	}
	if d == 4 {
		return "4"
	}
	if d == 5 {
		return "5"
	}
	if d == 6 {
		return "6"
	}
	if d == 7 {
		return "7"
	}
	if d == 8 {
		return "8"
	}
	return "9"
}

func msDrawNum(x int, y int, v int, col int, size int, anchor int) {
	if v < 10 {
		ui_2d_text(x, y, msDigitStr(v), col, size, anchor)
		return
	}
	ui_2d_text(x, y, msDigitStr(v/10), col, size, anchor)
	ui_2d_text(x+size/2, y, msDigitStr(v%10), col, size, anchor)
}

func msIdx() int {
	return A[431]*msGW + A[432]
}

// 周围 8 格的雷数
func msNeighbors() int {
	A[426] = 0
	A[422] = -1
	for A[422] <= 1 {
		A[423] = -1
		for A[423] <= 1 {
			if !(A[422] == 0 && A[423] == 0) {
				A[424] = A[431] + A[422]
				A[425] = A[432] + A[423]
				if A[424] >= 0 && A[425] >= 0 && A[424] < msGW && A[425] < msGW {
					if A[A[424]*msGW+A[425]] == 1 {
						A[426] = A[426] + 1
					}
				}
			}
			A[423] = A[423] + 1
		}
		A[422] = A[422] + 1
	}
	return A[426]
}

func msRelayout() {
	A[413] = ui_scr_get_w()
	A[414] = ui_scr_get_h()
	if A[413] <= 0 {
		A[413] = 360
	}
	if A[414] <= 0 {
		A[414] = 620
	}
	small := A[413]
	if A[414] < small {
		small = A[414]
	}

	// 顶栏 / 提示行 / 棋盘全部按画布现算：240 小屏压缩顶栏，1024 大屏放宽，
	// 9×9 棋盘与底部提示都不会越出画布。
	A[436] = small / 18
	if A[436] < 10 {
		A[436] = 10
	}
	if A[436] > 15 {
		A[436] = 15
	}
	A[437] = small / 15
	if A[437] < 12 {
		A[437] = 12
	}
	if A[437] > 18 {
		A[437] = 18
	}
	A[438] = small / 24
	if A[438] < 8 {
		A[438] = 8
	}
	if A[438] > 11 {
		A[438] = 11
	}
	A[439] = small / 24
	if A[439] < 9 {
		A[439] = 9
	}
	if A[439] > 13 {
		A[439] = 13
	}
	A[434] = A[414] / 40
	if A[434] < 4 {
		A[434] = 4
	}
	A[435] = small / 16
	if A[435] < 18 {
		A[435] = 18
	}
	if A[435] > 34 {
		A[435] = 34
	}
	gap := A[414] / 60
	if gap < 4 {
		gap = 4
	}
	A[440] = A[414] - A[439] - gap
	A[412] = A[434] + A[435] + A[438] + gap

	pad := A[413] / 60
	if pad < 8 {
		pad = 8
	}
	sideW := (A[413] - 2*pad) / msGW
	sideH := (A[440] - A[412] - gap) / msGW
	A[410] = sideW
	if sideH < A[410] {
		A[410] = sideH
	}
	if A[410] < 6 {
		A[410] = 6
	}
	A[411] = (A[413] - msGW*A[410]) / 2
}

func msNewGame() {
	i := 0
	for i < 81 {
		A[i] = 0
		A[81+i] = 0
		A[162+i] = 0
		i = i + 1
	}
	i = 0
	for i < msMines {
		A[430] = (A[430] * 1103 + 12345) % 32768
		A[431] = (A[430] / 256) % msGW
		A[430] = (A[430] * 1103 + 12345) % 32768
		A[432] = (A[430] / 256) % msGW
		if A[msIdx()] == 0 {
			A[msIdx()] = 1
			i = i + 1
		}
	}
	A[405] = 0
	A[406] = 0
	A[407] = 0
	A[408] = 0
	A[409] = 0
}

// 连锁展开（显式队列）
func msReveal() {
	if A[81+msIdx()] != 0 {
		return
	}
	if A[162+msIdx()] != 0 {
		return
	}
	A[243] = A[431]
	A[324] = A[432]
	A[418] = 0
	A[419] = 1
	for A[418] < A[419] {
		A[431] = A[243+A[418]]
		A[432] = A[324+A[418]]
		A[418] = A[418] + 1
		if A[81+msIdx()] != 0 {
			continue
		}
		if A[162+msIdx()] != 0 {
			continue
		}
		A[81+msIdx()] = 1
		A[405] = A[405] + 1
		if A[msIdx()] == 1 {
			A[406] = 1
			return
		}
		if msNeighbors() != 0 {
			continue
		}
		A[422] = -1
		for A[422] <= 1 {
			A[423] = -1
			for A[423] <= 1 {
				if !(A[422] == 0 && A[423] == 0) {
					A[424] = A[431] + A[422]
					A[425] = A[432] + A[423]
					if A[424] >= 0 && A[425] >= 0 && A[424] < msGW && A[425] < msGW {
						A[433] = A[424]*msGW + A[425]
						if A[81+A[433]] == 0 && A[162+A[433]] == 0 {
							if A[419] < 81 {
								A[243+A[419]] = A[424]
								A[324+A[419]] = A[425]
								A[419] = A[419] + 1
							}
						}
					}
				}
				A[423] = A[423] + 1
			}
			A[422] = A[422] + 1
		}
	}
	if A[405] >= msGW*msGW-msMines {
		A[407] = 1
	}
}

func msDraw() {
	lang := A[415]
	tFlagMode := "Flag mode"
	tRevealMode := "Reveal mode"
	tLeft := "mines left"
	tDead := "Boom - ESC to restart"
	tWon := "Cleared! ESC for a new board"
	tHint := "Tap a cell / tap the top button to flag"
	if lang == 0 {
		tFlagMode = "插旗模式"
		tRevealMode = "翻开模式"
		tLeft = "剩余雷数"
		tDead = "踩雷了，ESC 重来"
		tWon = "扫清了！ESC 再来"
		tHint = "点格子翻开 / 点顶部按钮插旗"
	}

	ui_2d_clear(-15724520)

	ui_2d_rect(8, A[434], A[413]/3, A[435], -14013898, 1, 0, 8)
	if A[408] != 0 {
		ui_2d_text(8+A[413]/6, A[434]+A[435]/2+A[436]/3, tFlagMode, -15724520, A[436], 1)
	} else {
		ui_2d_text(8+A[413]/6, A[434]+A[435]/2+A[436]/3, tRevealMode, -1513232, A[436], 1)
	}
	ui_2d_rect(8, A[434], A[413]/3, A[435], -2069424, 0, 1, 8)

	A[427] = 0
	A[417] = 0
	for A[417] < 81 {
		if A[162+A[417]] != 0 {
			A[427] = A[427] + 1
		}
		A[417] = A[417] + 1
	}
	msDrawNum(A[413]-20, A[434]+A[435]/2+A[437]/3, msMines-A[427], -2069424, A[437], 0)
	ui_2d_text(A[413]-10, A[434]+A[435]+A[438], tLeft, -6643536, A[438], 2)

	A[431] = 0
	for A[431] < msGW {
		A[432] = 0
		for A[432] < msGW {
			if A[81+msIdx()] != 0 {
				if A[msIdx()] == 1 {
					ui_2d_rect(A[411]+A[431]*A[410]+1, A[412]+A[432]*A[410]+1, A[410]-2, A[410]-2, -2069424, 1, 0, 2)
				} else {
					ui_2d_rect(A[411]+A[431]*A[410]+1, A[412]+A[432]*A[410]+1, A[410]-2, A[410]-2, -15066588, 1, 0, 2)
					A[426] = msNeighbors()
					if A[426] > 0 {
						msDrawNum(A[411]+A[431]*A[410]+A[410]/3, A[412]+A[432]*A[410]+A[410]/3, A[426], -1513232, A[410]/2, 0)
					}
				}
			} else {
				ui_2d_rect(A[411]+A[431]*A[410]+1, A[412]+A[432]*A[410]+1, A[410]-2, A[410]-2, -14013898, 1, 0, 3)
				if A[162+msIdx()] != 0 {
					ui_2d_circle(A[411]+A[431]*A[410]+A[410]/2, A[412]+A[432]*A[410]+A[410]/2, A[410]/4, -2069424, 1, 0)
				}
			}
			A[432] = A[432] + 1
		}
		A[431] = A[431] + 1
	}

	if A[406] != 0 {
		ui_2d_text(A[413]/2, A[440], tDead, -2069424, A[439]+2, 1)
	} else if A[407] != 0 {
		ui_2d_text(A[413]/2, A[440], tWon, -1513232, A[439]+2, 1)
	} else {
		ui_2d_text(A[413]/2, A[440], tHint, -6643536, A[439], 1)
	}
	ui_2d_present()
}

func main() {
	A[415] = ui_scr_get_language()
	lang := A[415]
	tTitle := "Minesweeper"
	if lang == 0 {
		tTitle = "扫雷"
	}
	ui_win_open(tTitle, 0, 0)
	ui_scr_set_keep_on(1)

	msRelayout()
	A[430] = 20261117
	msNewGame()
	msDraw()

	A[416] = ui_timer_set(80, 0)

	for ui_win_is_closed() == 0 {
		t := ui_msg_fetch_wait(0)
		if t == 10 {
			break
		}

		if t == 6 || t == 4 {
			A[428] = ui_msg_get_a()
			A[429] = ui_msg_get_b()
			if A[428] >= 8 && A[428] <= 8+A[413]/3 && A[429] >= A[434] && A[429] <= A[434]+A[435] {
				if A[408] != 0 {
					A[408] = 0
				} else {
					A[408] = 1
				}
			} else {
				A[431] = (A[428] - A[411]) / A[410]
				A[432] = (A[429] - A[412]) / A[410]
				if A[431] >= 0 && A[432] >= 0 && A[431] < msGW && A[432] < msGW {
					if A[406] == 0 && A[407] == 0 {
						if A[408] != 0 {
							if A[81+msIdx()] == 0 {
								if A[162+msIdx()] != 0 {
									A[162+msIdx()] = 0
								} else {
									A[162+msIdx()] = 1
								}
							}
						} else {
							msReveal()
						}
					}
				}
			}
		}

		if t == 1 {
			A[417] = ui_msg_get_a()
			if A[417] == 27 || A[417] == 82 {
				msNewGame()
			}
		}

		if t == 11 {
			msRelayout()
		}

		A[409] = A[409] + 1
		if A[409] >= msMaxFrames {
			break
		}
		msDraw()
	}

	ui_timer_kill(A[416])
	ui_scr_set_keep_on(0)
	ui_win_close()
}
