@@ -10,19 +10,21 @@ type Cell struct {
1010
1111// Board - 棋盤
1212type 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 - 遊戲物件
2123type 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 - 初始化盤面
5253func 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+ }
0 commit comments