Skip to content

Commit 7b5aa36

Browse files
committed
⚡️ (sudoku-game): add restart button
1 parent dacad25 commit 7b5aa36

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

internal/game/sudoku.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,17 @@ func (board *Board) IncreaseFilledCount() {
8181
func (board *Board) DecreaseFilledCount() {
8282
board.FilledCount--
8383
}
84+
85+
// ResetBoardToDefault - 重開始功能
86+
func (board *Board) ResetBoardToDefault() {
87+
for row := 0; row < BoardSize; row++ {
88+
for col := 0; col < BoardSize; col++ {
89+
if board.Cells[row][col].Type != Preset {
90+
board.Cells[row][col].Type = Empty
91+
board.Cells[row][col].Value = 0
92+
}
93+
}
94+
}
95+
board.FilledCount = 0
96+
board.ConflictCount = 0
97+
}

internal/layout/font.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,15 @@ const (
6363
Playing
6464
Win
6565
Bug
66+
Restart
67+
Button
6668
)
6769

6870
func getIconColor(iconType IconType) color.Color {
6971
switch iconType {
70-
case RemainingCount: // 顯示亮灰色
72+
case RemainingCount, Button: // 顯示亮灰色
7173
return color.RGBA{0xCD, 0xC9, 0xC9, 0xFF}
72-
case Playing, Bug: // Green
74+
case Playing, Bug, Restart: // Green
7375
return color.RGBA{0x22, 0x8B, 0x22, 0xFF}
7476
case Win: // Gold
7577
return color.RGBA{0xFF, 0xD7, 0x00, 0xFF}

internal/layout/input-control.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,15 @@ func handleNonConflict(board *game.Board, cellType game.CellType,
9292
// 標示為 Input
9393
board.Cells[targetRow][targetCol].Type = game.Input
9494
}
95+
96+
// handleRestartButton
97+
func (gameLayout *GameLayout) handleRestartButton() {
98+
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
99+
xPos, yPos := ebiten.CursorPosition()
100+
if (xPos >= 8*cellSize && xPos <= 9*cellSize) &&
101+
(yPos >= cellSize && yPos <= 2*cellSize) {
102+
gameLayout.gameInstance.Board.ResetBoardToDefault()
103+
}
104+
}
105+
gameLayout.isPlayerWin = gameLayout.checkIfPlayerWin()
106+
}

internal/layout/layout.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type GameLayout struct {
2626
}
2727

2828
func (gameLayout *GameLayout) Update() error {
29+
gameLayout.handleRestartButton()
2930
if gameLayout.isPlayerWin {
3031
return nil
3132
}
@@ -43,6 +44,7 @@ func (gameLayout *GameLayout) Draw(screen *ebiten.Image) {
4344
gameLayout.drawRemainingUnsolvedCount(screen)
4445
gameLayout.drawBugCount(screen)
4546
gameLayout.drawBoardStatus(screen)
47+
gameLayout.drawRestartButton(screen)
4648
// 畫出 cursor
4749
gameLayout.drawCursor(screen)
4850
// 根據遊戲狀態來畫出盤面
@@ -267,6 +269,7 @@ func (gameLayout *GameLayout) checkIfPlayerWin() bool {
267269
return remainingCount == 0 && conflictCount == 0
268270
}
269271

272+
// drawBoardStatus - 根據是否勝利來畫出不同的提示詞
270273
func (gameLayout *GameLayout) drawBoardStatus(screen *ebiten.Image) {
271274
emojiValue := "⏳"
272275
message := "Keep going"
@@ -300,3 +303,23 @@ func (gameLayout *GameLayout) drawBoardStatus(screen *ebiten.Image) {
300303
Size: 25,
301304
}, textOpts)
302305
}
306+
307+
// drawRestartButton - 繪製重新開始的 Button
308+
func (gameLayout *GameLayout) drawRestartButton(screen *ebiten.Image) {
309+
vector.DrawFilledCircle(screen, float32(8*cellSize+cellSize/2), cellSize+cellSize/2, 25,
310+
getIconColor(Button),
311+
true,
312+
)
313+
emojiValue := "🔃"
314+
emojiXPos := 8*cellSize + len(emojiValue)
315+
emojiYPos := cellSize + cellSize/2
316+
emojiOpts := &text.DrawOptions{}
317+
emojiOpts.ColorScale.ScaleWithColor(getIconColor(Restart))
318+
emojiOpts.PrimaryAlign = text.AlignStart
319+
emojiOpts.SecondaryAlign = text.AlignCenter
320+
emojiOpts.GeoM.Translate(float64(emojiXPos), float64(emojiYPos))
321+
text.Draw(screen, emojiValue, &text.GoTextFace{
322+
Source: emojiFaceSource,
323+
Size: 30,
324+
}, emojiOpts)
325+
}

0 commit comments

Comments
 (0)