Skip to content

Commit 137b921

Browse files
committed
✨ (mine-sweeper): add CheckIsPlayerWin and Show Game Over
1 parent f7306ce commit 137b921

File tree

5 files changed

+201
-62
lines changed

5 files changed

+201
-62
lines changed

internal/fonts/embed.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,7 @@ var (
2424

2525
//go:embed pressstart2p.ttf
2626
PressStart2P_ttf []byte
27+
28+
//go:embed noto-emoji-regular.ttf
29+
NotoEmojiRegular_ttf []byte
2730
)
858 KB
Binary file not shown.

internal/game/game.go

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,21 @@ type Cell struct {
1010

1111
// Board - 棋盤
1212
type Board struct {
13-
rows int // 總共格數
14-
cols int // 總共列數
15-
cells [][]*Cell // 整格棋盤狀態
16-
minePositionShuffler positionShuffler // 亂序器用來安排地雷格子
17-
RemainingFlags int // 剩餘標記數
13+
rows int // 總共格數
14+
cols int // 總共列數
15+
cells [][]*Cell // 整格棋盤狀態
16+
minePositionShuffler positionShuffler // 亂序器用來安排地雷格子
17+
remainingFlags int // 剩餘標記數
18+
mineCoords []coord // 紀錄被設定成 mines 的座標
19+
remainingUnRevealedCells int // 剩餘需要翻開的格子數
1820
}
1921

2022
// Game - 遊戲物件
2123
type Game struct {
2224
Board *Board // 棋盤物件
23-
isGameOver bool // 是否遊戲結束
24-
isPlayerWin bool // 玩家是否獲勝
25-
remaining int // 剩餘需要翻開的格子數
25+
IsGameOver bool // 是否遊戲結束
26+
IsPlayerWin bool // 玩家是否獲勝
27+
2628
}
2729

2830
// coord - 紀錄該格字座標
@@ -42,19 +44,19 @@ func NewGame(rows, cols, mineCount int) *Game {
4244
board.CalculateAdjacentMines()
4345
return &Game{
4446
Board: board,
45-
remaining: rows*cols - mineCount,
46-
isGameOver: false,
47-
isPlayerWin: false,
47+
IsGameOver: false,
48+
IsPlayerWin: false,
4849
}
4950
}
5051

5152
// NewBoard - 初始化盤面
5253
func NewBoard(rows, cols, mineCount int) *Board {
5354
board := &Board{
54-
rows: rows,
55-
cols: cols,
56-
minePositionShuffler: defaultPositionShuffler,
57-
RemainingFlags: mineCount,
55+
rows: rows,
56+
cols: cols,
57+
minePositionShuffler: defaultPositionShuffler,
58+
remainingFlags: mineCount,
59+
remainingUnRevealedCells: rows*cols - mineCount,
5860
}
5961
board.cells = make([][]*Cell, rows)
6062
for row := range board.cells {
@@ -108,6 +110,7 @@ func (b *Board) PlaceMines(mineCount int) {
108110
// 設定前 mineCount 為地雷
109111
for i := 0; i < mineCount; i++ {
110112
b.cells[coords[i].Row][coords[i].Col].IsMine = true
113+
b.mineCoords = append(b.mineCoords, coords[i])
111114
}
112115
}
113116

@@ -164,10 +167,10 @@ func (board *Board) ToggleFlag(row, col int) {
164167
} else {
165168
count++
166169
}
167-
if board.RemainingFlags-count < 0 {
170+
if board.remainingFlags-count < 0 {
168171
return
169172
}
170-
board.RemainingFlags -= count
173+
board.remainingFlags -= count
171174
board.cells[row][col].Flagged = !cell.Flagged
172175
}
173176

@@ -198,11 +201,15 @@ func (board *Board) Reveal(row, col int) {
198201

199202
// 標注該格已經被揭開
200203
board.cells[curRow][curCol].Revealed = true
204+
board.remainingUnRevealedCells--
201205
if cell.Flagged {
202-
board.RemainingFlags++
206+
board.remainingFlags++
203207
board.cells[curRow][curCol].Flagged = false
204208
}
205-
209+
if cell.IsMine {
210+
board.revealMines()
211+
return
212+
}
206213
// 如果是空白格 (AdjacenetMines = 0, 且不是地雷)
207214
if !cell.IsMine && cell.AdjacenetMines == 0 {
208215
// 鄰近所有方向
@@ -221,3 +228,26 @@ func (board *Board) Reveal(row, col int) {
221228
}
222229
}
223230
}
231+
232+
// revealMines - 顯示所有 Mines
233+
func (board *Board) revealMines() {
234+
for _, mineCoord := range board.mineCoords {
235+
if !board.cells[mineCoord.Row][mineCoord.Col].Flagged &&
236+
!board.cells[mineCoord.Row][mineCoord.Col].Revealed {
237+
board.cells[mineCoord.Row][mineCoord.Col].Revealed = true
238+
}
239+
if board.cells[mineCoord.Row][mineCoord.Col].Flagged {
240+
board.cells[mineCoord.Row][mineCoord.Col].Revealed = true
241+
}
242+
}
243+
}
244+
245+
// CheckIsPlayerWin - 檢查是否所有該非地雷的格子都有被掀開
246+
func (board *Board) CheckIsPlayerWin() bool {
247+
return board.remainingUnRevealedCells == 0
248+
}
249+
250+
// GetRemainingFlags - 取出有標記旗號的個數
251+
func (board *Board) GetRemainingFlags() int {
252+
return board.remainingFlags
253+
}

internal/layout/font.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111

1212
var (
1313
mplusFaceSource *text.GoTextFaceSource
14+
// emojiFace font.Face
15+
emojiFaceSource *text.GoTextFaceSource
1416
)
1517

1618
func init() {
@@ -19,12 +21,27 @@ func init() {
1921
log.Fatal(err)
2022
}
2123
mplusFaceSource = s
24+
25+
s, err = text.NewGoTextFaceSource(bytes.NewReader(fonts.NotoEmojiRegular_ttf))
26+
if err != nil {
27+
log.Fatal(err)
28+
}
29+
emojiFaceSource = s
2230
}
2331

32+
const (
33+
IsMine = iota + 100
34+
IsFlag
35+
)
36+
2437
func getTileColor(value int) color.Color {
2538
switch value {
2639
case 0:
2740
return color.RGBA{0x77, 0x6e, 0x65, 0xff}
41+
case IsFlag:
42+
return color.RGBA{0xf9, 0xf6, 0xf2, 0xff}
43+
case IsMine:
44+
return color.Black
2845
default:
2946
return color.RGBA{0xf9, 0xf6, 0xf2, 0xff}
3047
}

0 commit comments

Comments
 (0)