@@ -18,7 +18,7 @@ type Board struct {
1818
1919// Game - 遊戲物件
2020type Game struct {
21- board * Board // 棋盤物件
21+ Board * Board // 棋盤物件
2222 isGameOver bool // 是否遊戲結束
2323 isPlayerWin bool // 玩家是否獲勝
2424 remaining int // 剩餘需要翻開的格子數
@@ -40,7 +40,7 @@ func NewGame(rows, cols, mineCount int) *Game {
4040 // 計算結果
4141 board .CalculateAdjacentMines ()
4242 return & Game {
43- board : board ,
43+ Board : board ,
4444 remaining : rows * cols - mineCount ,
4545 isGameOver : false ,
4646 isPlayerWin : false ,
@@ -66,7 +66,7 @@ func NewBoard(rows, cols int) *Board {
6666
6767func (g * Game ) Init (board * Board , minePositionShuffler positionShuffler ) {
6868 if minePositionShuffler != nil {
69- g .board .minePositionShuffler = minePositionShuffler
69+ g .Board .minePositionShuffler = minePositionShuffler
7070 }
7171 // 無效的設定
7272 if board == nil || len (board .cells ) != board .rows || len (board .cells [0 ]) != board .cols {
@@ -76,16 +76,19 @@ func (g *Game) Init(board *Board, minePositionShuffler positionShuffler) {
7676 for row := range board .cells {
7777 for col := range board .cells [row ] {
7878 sourceCell := board.cells [row ][col ]
79- g .board .cells [row ][col ].AdjacenetMines = sourceCell .AdjacenetMines
80- g .board .cells [row ][col ].IsMine = sourceCell .IsMine
81- g .board .cells [row ][col ].Revealed = sourceCell .Revealed
82- g .board .cells [row ][col ].Flagged = sourceCell .Flagged
79+ g .Board .cells [row ][col ].AdjacenetMines = sourceCell .AdjacenetMines
80+ g .Board .cells [row ][col ].IsMine = sourceCell .IsMine
81+ g .Board .cells [row ][col ].Revealed = sourceCell .Revealed
82+ g .Board .cells [row ][col ].Flagged = sourceCell .Flagged
8383 }
8484 }
8585}
8686
8787// PlaceMines - 使用 minePositionShuffler 選出 mineCount 個地雷
8888func (b * Board ) PlaceMines (mineCount int ) {
89+ if mineCount < 0 {
90+ return
91+ }
8992 // 蒐集所有 coord
9093 coords := make ([]coord , 0 , b .cols * b .rows )
9194 for row := range b .cells {
@@ -134,3 +137,7 @@ func (b *Board) CalculateAdjacentMines() {
134137 }
135138 }
136139}
140+
141+ func (board * Board ) GetCell (row , col int ) * Cell {
142+ return board.cells [row ][col ]
143+ }
0 commit comments