Skip to content

Commit 1d880cb

Browse files
committed
✨ (sudoku-game): add time box
1 parent 7b5aa36 commit 1d880cb

File tree

5 files changed

+62
-6
lines changed

5 files changed

+62
-6
lines changed

internal/game/sudoku.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package game
22

3+
import "time"
4+
35
const (
46
BoardSize = 9
57
BoxSize = 3
@@ -28,16 +30,22 @@ func NewBoard() *Board {
2830

2931
// Game - 遊戲結構
3032
type Game struct {
31-
Board *Board
33+
Board *Board
34+
StartTime time.Time // 遊戲開始時間
3235
}
3336

3437
// NewGame - 建構遊戲結構
3538
func NewGame() *Game {
3639
return &Game{
37-
Board: NewBoard(),
40+
Board: NewBoard(),
41+
StartTime: time.Now().UTC(),
3842
}
3943
}
4044

45+
func (game *Game) GetElaspedTime() int {
46+
return int(time.Since(game.StartTime).Seconds())
47+
}
48+
4149
func (board *Board) IncreaseCursorRow() {
4250
if board.CursorRow < BoardSize-1 {
4351
board.CursorRow++

internal/layout/font.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ const (
6565
Bug
6666
Restart
6767
Button
68+
IsClock
6869
)
6970

7071
func getIconColor(iconType IconType) color.Color {
@@ -73,7 +74,7 @@ func getIconColor(iconType IconType) color.Color {
7374
return color.RGBA{0xCD, 0xC9, 0xC9, 0xFF}
7475
case Playing, Bug, Restart: // Green
7576
return color.RGBA{0x22, 0x8B, 0x22, 0xFF}
76-
case Win: // Gold
77+
case Win, IsClock: // Gold
7778
return color.RGBA{0xFF, 0xD7, 0x00, 0xFF}
7879
default: // 其他都是預設 白色
7980
return color.RGBA{0xFF, 0xFF, 0xFF, 0xFF}

internal/layout/input-control.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package layout
22

33
import (
4+
"time"
5+
46
"github.com/hajimehoshi/ebiten/v2"
57
"github.com/hajimehoshi/ebiten/v2/inpututil"
68
"github.com/leetcode-golang-classroom/sudoku-game/internal/game"
@@ -100,6 +102,7 @@ func (gameLayout *GameLayout) handleRestartButton() {
100102
if (xPos >= 8*cellSize && xPos <= 9*cellSize) &&
101103
(yPos >= cellSize && yPos <= 2*cellSize) {
102104
gameLayout.gameInstance.Board.ResetBoardToDefault()
105+
gameLayout.gameInstance.StartTime = time.Now().UTC()
103106
}
104107
}
105108
gameLayout.isPlayerWin = gameLayout.checkIfPlayerWin()

internal/layout/layout.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package layout
33
import (
44
"fmt"
55
"image/color"
6+
"time"
67

78
"github.com/hajimehoshi/ebiten/v2"
89
"github.com/hajimehoshi/ebiten/v2/text/v2"
@@ -20,16 +21,18 @@ const (
2021
)
2122

2223
type GameLayout struct {
23-
gameInstance *game.Game
24-
difficulty game.Difficulty
25-
isPlayerWin bool
24+
gameInstance *game.Game
25+
difficulty game.Difficulty
26+
isPlayerWin bool
27+
elapsedSeconds int
2628
}
2729

2830
func (gameLayout *GameLayout) Update() error {
2931
gameLayout.handleRestartButton()
3032
if gameLayout.isPlayerWin {
3133
return nil
3234
}
35+
gameLayout.elapsedSeconds = gameLayout.gameInstance.GetElaspedTime()
3336
gameLayout.DetectCursor()
3437
gameLayout.DetectInput()
3538
// 檢查狀態
@@ -45,6 +48,7 @@ func (gameLayout *GameLayout) Draw(screen *ebiten.Image) {
4548
gameLayout.drawBugCount(screen)
4649
gameLayout.drawBoardStatus(screen)
4750
gameLayout.drawRestartButton(screen)
51+
gameLayout.drawTimeLayout(screen)
4852
// 畫出 cursor
4953
gameLayout.drawCursor(screen)
5054
// 根據遊戲狀態來畫出盤面
@@ -144,6 +148,7 @@ func NewGameLayout() *GameLayout {
144148
gameInstance.Board.GenerateSolution()
145149
defaultDifficulty := game.Easy
146150
gameInstance.Board.MakePuzzleFromSolution(int(defaultDifficulty))
151+
gameInstance.StartTime = time.Now().UTC()
147152
return &GameLayout{
148153
gameInstance: gameInstance,
149154
difficulty: defaultDifficulty,

internal/layout/timebox.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package layout
2+
3+
import (
4+
"fmt"
5+
"image/color"
6+
7+
"github.com/hajimehoshi/ebiten/v2"
8+
"github.com/hajimehoshi/ebiten/v2/text/v2"
9+
)
10+
11+
func (gameLayout *GameLayout) drawTimeLayout(screen *ebiten.Image) {
12+
elapsedSeconds := gameLayout.elapsedSeconds
13+
secondPart := elapsedSeconds % 60
14+
minutePart := (elapsedSeconds / 60) % 60
15+
textValue := fmt.Sprintf("%02d:%02d", minutePart, secondPart)
16+
textXPos := ScreenWidth - cellSize/4 + len(textValue)
17+
textYPos := cellSize / 2
18+
textOpts := &text.DrawOptions{}
19+
textOpts.ColorScale.ScaleWithColor(color.Black)
20+
textOpts.PrimaryAlign = text.AlignEnd
21+
textOpts.SecondaryAlign = text.AlignCenter
22+
textOpts.GeoM.Translate(float64(textXPos), float64(textYPos))
23+
text.Draw(screen, textValue, &text.GoTextFace{
24+
Source: mplusFaceSource,
25+
Size: 30,
26+
}, textOpts)
27+
emojiValue := "⏰"
28+
emojiXPos := ScreenWidth - 3*cellSize + len(emojiValue)
29+
emojiYPos := cellSize / 2
30+
emojiOpts := &text.DrawOptions{}
31+
emojiOpts.ColorScale.ScaleWithColor(getIconColor(IsClock))
32+
emojiOpts.PrimaryAlign = text.AlignStart
33+
emojiOpts.SecondaryAlign = text.AlignCenter
34+
emojiOpts.GeoM.Translate(float64(emojiXPos), float64(emojiYPos))
35+
text.Draw(screen, emojiValue, &text.GoTextFace{
36+
Source: emojiFaceSource,
37+
Size: 30,
38+
}, emojiOpts)
39+
}

0 commit comments

Comments
 (0)