Skip to content

Commit c6a57cc

Browse files
committed
✨ (mine-sweeper): add level setup
1 parent 30e0105 commit c6a57cc

File tree

5 files changed

+193
-55
lines changed

5 files changed

+193
-55
lines changed

cmd/main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"fmt"
45
"log"
56

67
"github.com/hajimehoshi/ebiten/v2"
@@ -9,9 +10,9 @@ import (
910
)
1011

1112
func main() {
12-
ebiten.SetWindowSize(layout.ScreenWidth, layout.ScreenHeight)
13-
ebiten.SetWindowTitle("Mine Sweeper Grid - Flood Filled")
14-
gameInstance := game.NewGame(layout.Rows, layout.Cols, layout.MineCounts)
13+
ebiten.SetWindowSize(layout.DefaultScreenWidth, layout.DefaultScreenHeight)
14+
ebiten.SetWindowTitle(fmt.Sprintf("%s Mine Sweeper Grid", layout.LevelMessage[layout.Easy]))
15+
gameInstance := game.NewGame(layout.DefaultRows, layout.DefaultCols, layout.DefaultMineCounts)
1516
gameLayout := layout.NewGameLayout(gameInstance)
1617
if err := ebiten.RunGame(gameLayout); err != nil {
1718
log.Fatal(err)

internal/game/game.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ type Cell struct {
1212

1313
// Board - 棋盤
1414
type Board struct {
15-
rows int // 總共格數
16-
cols int // 總共列數
15+
Rows int // 總共格數
16+
Cols int // 總共列數
1717
cells [][]*Cell // 整格棋盤狀態
1818
minePositionShuffler positionShuffler // 亂序器用來安排地雷格子
1919
remainingFlags int // 剩餘標記數
@@ -27,6 +27,7 @@ type Game struct {
2727
IsGameOver bool // 是否遊戲結束
2828
IsPlayerWin bool // 玩家是否獲勝
2929
startTime time.Time // 遊戲開始時間
30+
MineCounts int // minecounts
3031
}
3132

3233
// coord - 紀錄該格字座標
@@ -49,14 +50,15 @@ func NewGame(rows, cols, mineCount int) *Game {
4950
IsGameOver: false,
5051
IsPlayerWin: false,
5152
startTime: time.Now().UTC(),
53+
MineCounts: mineCount,
5254
}
5355
}
5456

5557
// NewBoard - 初始化盤面
5658
func NewBoard(rows, cols, mineCount int) *Board {
5759
board := &Board{
58-
rows: rows,
59-
cols: cols,
60+
Rows: rows,
61+
Cols: cols,
6062
minePositionShuffler: defaultPositionShuffler,
6163
remainingFlags: mineCount,
6264
remainingUnRevealedCells: rows*cols - mineCount,
@@ -76,7 +78,7 @@ func (g *Game) Init(board *Board, minePositionShuffler positionShuffler) {
7678
g.Board.minePositionShuffler = minePositionShuffler
7779
}
7880
// 無效的設定
79-
if board == nil || len(board.cells) != board.rows || len(board.cells[0]) != board.cols {
81+
if board == nil || len(board.cells) != board.Rows || len(board.cells[0]) != board.Cols {
8082
return
8183
}
8284
// 設定資料
@@ -97,7 +99,7 @@ func (b *Board) PlaceMines(mineCount int) {
9799
return
98100
}
99101
// 蒐集所有 coord
100-
coords := make([]coord, 0, b.cols*b.rows)
102+
coords := make([]coord, 0, b.Cols*b.Rows)
101103
for row := range b.cells {
102104
for col := range b.cells[row] {
103105
coords = append(coords, coord{Row: row, Col: col})
@@ -135,8 +137,8 @@ func (b *Board) CalculateAdjacentMines() {
135137
accumCount := 0
136138
for _, direction := range neighborDirections {
137139
neighborRow, neighborCol := row+direction.Row, col+direction.Col
138-
if neighborRow >= 0 && neighborRow < b.rows &&
139-
neighborCol >= 0 && neighborCol < b.cols &&
140+
if neighborRow >= 0 && neighborRow < b.Rows &&
141+
neighborCol >= 0 && neighborCol < b.Cols &&
140142
b.cells[neighborRow][neighborCol].IsMine {
141143
accumCount++
142144
}
@@ -153,8 +155,8 @@ func (board *Board) GetCell(row, col int) *Cell {
153155
// ToggleFlag - 標記地雷
154156
func (board *Board) ToggleFlag(row, col int) {
155157
// 超出邊界
156-
if row < 0 || row >= board.rows ||
157-
col < 0 || col >= board.cols {
158+
if row < 0 || row >= board.Rows ||
159+
col < 0 || col >= board.Cols {
158160
return
159161
}
160162

@@ -191,8 +193,8 @@ func (board *Board) Reveal(row, col int) {
191193
curRow, curCol := cellCoord.Row, cellCoord.Col
192194

193195
// 超出邊界
194-
if curRow < 0 || curRow >= board.rows ||
195-
curCol < 0 || curCol >= board.cols {
196+
if curRow < 0 || curRow >= board.Rows ||
197+
curCol < 0 || curCol >= board.Cols {
196198
continue
197199
}
198200

internal/game/game_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ func TestGameInit(t *testing.T) {
2525
cols: 5,
2626
minesNumber: 5,
2727
board: &Board{
28-
rows: 5,
29-
cols: 5,
28+
Rows: 5,
29+
Cols: 5,
3030
cells: [][]*Cell{
3131
{
3232
{
@@ -192,8 +192,8 @@ func TestGameInit(t *testing.T) {
192192
},
193193
},
194194
want: &Board{
195-
rows: 5,
196-
cols: 5,
195+
Rows: 5,
196+
Cols: 5,
197197
cells: [][]*Cell{
198198
{
199199
{
@@ -387,8 +387,8 @@ func TestCalculateAdjacentMines(t *testing.T) {
387387
cols: 5,
388388
minesNumber: 4,
389389
board: &Board{
390-
rows: 5,
391-
cols: 5,
390+
Rows: 5,
391+
Cols: 5,
392392
minePositionShuffler: func(coords []coord) {},
393393
cells: [][]*Cell{
394394
{
@@ -480,8 +480,8 @@ func TestCalculateAdjacentMines(t *testing.T) {
480480
},
481481
},
482482
want: &Board{
483-
rows: 5,
484-
cols: 5,
483+
Rows: 5,
484+
Cols: 5,
485485
minePositionShuffler: func(coords []coord) {},
486486
cells: [][]*Cell{
487487
{

0 commit comments

Comments
 (0)