Skip to content

Commit 69554b0

Browse files
committed
✨ (sudoku-game): add clear button
1 parent 0bfeb37 commit 69554b0

File tree

4 files changed

+89
-12
lines changed

4 files changed

+89
-12
lines changed

internal/layout/click-handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func (gameLayout *GameLayout) detectClickCell() {
1111
xPos, yPos := ebiten.CursorPosition()
1212
yPos -= PanelHeight
1313
// range check
14-
if (xPos >= 0 && xPos <= ScreenWidth) &&
14+
if (xPos >= 0 && xPos <= BoardWidth) &&
1515
(yPos >= 0 && yPos <= ScreenHeight) {
1616
row := yPos / cellSize
1717
col := xPos / cellSize

internal/layout/input-control.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func handleNonConflict(board *game.Board, cellType game.CellType,
9999
func (gameLayout *GameLayout) handleRestartButton() {
100100
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
101101
xPos, yPos := ebiten.CursorPosition()
102-
if (xPos >= 8*cellSize && xPos <= 9*cellSize) &&
102+
if (xPos >= ScreenWidth-cellSize && xPos <= ScreenWidth) &&
103103
(yPos >= cellSize && yPos <= 2*cellSize) {
104104
gameLayout.gameInstance.Board.ResetBoardToDefault()
105105
gameLayout.gameInstance.StartTime = time.Now().UTC()
@@ -112,7 +112,7 @@ func (gameLayout *GameLayout) handleRestartButton() {
112112
func (gameLayout *GameLayout) handleToggleLevelDifficultButton() {
113113
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
114114
xPos, yPos := ebiten.CursorPosition()
115-
if (xPos >= 4*cellSize && xPos <= 5*cellSize) &&
115+
if (xPos >= ScreenWidth/2-cellSize/2 && xPos <= ScreenWidth/2+cellSize/2) &&
116116
(yPos >= cellSize && yPos <= 2*cellSize) {
117117
gameLayout.difficultyLevel = (gameLayout.difficultyLevel + 1) % len(difficultyOptions)
118118
gameLayout.ResetGameWithLevel()

internal/layout/layout.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ import (
1212
)
1313

1414
const (
15-
PanelHeight = 100 // 上方面板高度
16-
ScreenWidth = 450
17-
ScreenHeight = PanelHeight + 450
18-
cellSize = 50
19-
thinkLineWidth = 3
20-
leanLineWidth = 1
15+
PanelHeight = 100 // 上方面板高度
16+
BoardWidth = 450
17+
InputPanelWidth = 200
18+
ScreenWidth = BoardWidth + InputPanelWidth
19+
ScreenHeight = PanelHeight + 450
20+
cellSize = 50
21+
thinkLineWidth = 3
22+
leanLineWidth = 1
2123
)
2224

2325
type GameLayout struct {
@@ -33,6 +35,7 @@ func (gameLayout *GameLayout) Update() error {
3335
if gameLayout.isPlayerWin {
3436
return nil
3537
}
38+
gameLayout.detectClearHandler()
3639
gameLayout.detectClickCell()
3740
gameLayout.elapsedSeconds = gameLayout.gameInstance.GetElaspedTime()
3841
gameLayout.DetectCursor()
@@ -52,6 +55,7 @@ func (gameLayout *GameLayout) Draw(screen *ebiten.Image) {
5255
gameLayout.drawRestartButton(screen)
5356
gameLayout.drawTimeLayout(screen)
5457
gameLayout.drawLevelButtonWithIcon(screen)
58+
gameLayout.drawClearButton(screen)
5559
// 畫出 cursor
5660
gameLayout.drawCursor(screen)
5761
// 根據遊戲狀態來畫出盤面
@@ -129,7 +133,7 @@ func (gameLayout *GameLayout) drawLinesOnBoard(screen *ebiten.Image) {
129133
// 畫直線
130134
ebitenUtilDrawLine(screen, x, PanelHeight+0, x, ScreenHeight, lineColor, lineWidth)
131135
// 畫橫線
132-
ebitenUtilDrawLine(screen, 0, PanelHeight+y, ScreenWidth, PanelHeight+y, lineColor, lineWidth)
136+
ebitenUtilDrawLine(screen, 0, PanelHeight+y, BoardWidth, PanelHeight+y, lineColor, lineWidth)
133137
}
134138
}
135139

@@ -316,12 +320,12 @@ func (gameLayout *GameLayout) drawBoardStatus(screen *ebiten.Image) {
316320

317321
// drawRestartButton - 繪製重新開始的 Button
318322
func (gameLayout *GameLayout) drawRestartButton(screen *ebiten.Image) {
319-
vector.DrawFilledCircle(screen, float32(8*cellSize+cellSize/2), cellSize+cellSize/2, 25,
323+
vector.DrawFilledCircle(screen, float32(ScreenWidth-cellSize/2), cellSize+cellSize/2, 25,
320324
getIconColor(Button),
321325
true,
322326
)
323327
emojiValue := "🔃"
324-
emojiXPos := 8*cellSize + len(emojiValue)
328+
emojiXPos := ScreenWidth - cellSize + len(emojiValue)
325329
emojiYPos := cellSize + cellSize/2
326330
emojiOpts := &text.DrawOptions{}
327331
emojiOpts.ColorScale.ScaleWithColor(getIconColor(Restart))

internal/layout/number-buttons.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package layout
2+
3+
import (
4+
"image"
5+
"image/color"
6+
7+
"github.com/hajimehoshi/ebiten/v2"
8+
"github.com/hajimehoshi/ebiten/v2/text/v2"
9+
"github.com/hajimehoshi/ebiten/v2/vector"
10+
"github.com/leetcode-golang-classroom/sudoku-game/internal/game"
11+
)
12+
13+
var numberButtonValues = [3][3]int{
14+
{
15+
1, 2, 3,
16+
},
17+
{
18+
4, 5, 6,
19+
},
20+
{
21+
5, 7, 8,
22+
},
23+
}
24+
25+
var buttonRectRelativePos = image.Rect(50, 0, 150, 50)
26+
27+
func (gameLayout *GameLayout) drawClearButton(screen *ebiten.Image) {
28+
vector.DrawFilledRect(screen,
29+
float32(BoardWidth+buttonRectRelativePos.Min.X),
30+
float32((ScreenHeight-5*cellSize)+buttonRectRelativePos.Min.Y),
31+
float32(buttonRectRelativePos.Dx()),
32+
float32(buttonRectRelativePos.Dy()),
33+
getIconColor(Button),
34+
true,
35+
)
36+
textValue := "Clear"
37+
textXPos := BoardWidth + 2*cellSize
38+
textYPos := ScreenHeight - 5*cellSize + cellSize/2
39+
textOpts := &text.DrawOptions{}
40+
textOpts.ColorScale.ScaleWithColor(color.Black)
41+
textOpts.PrimaryAlign = text.AlignCenter
42+
textOpts.SecondaryAlign = text.AlignCenter
43+
textOpts.GeoM.Translate(float64(textXPos), float64(textYPos))
44+
text.Draw(screen, textValue, &text.GoTextFace{
45+
Source: mplusFaceSource,
46+
Size: 30,
47+
}, textOpts)
48+
}
49+
50+
func (gameLayout *GameLayout) clearCursorValue() {
51+
board := gameLayout.gameInstance.Board
52+
cursorRow := board.CursorRow
53+
cursorCol := board.CursorCol
54+
targetCell := board.Cells[cursorRow][cursorCol]
55+
// 當為無法清除的值時
56+
if targetCell.Type == game.Preset {
57+
return
58+
}
59+
handleClearInput(board, targetCell, cursorRow, cursorCol)
60+
}
61+
62+
func (gameLayout *GameLayout) detectClearHandler() {
63+
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
64+
xPos, yPos := ebiten.CursorPosition()
65+
xPos -= BoardWidth
66+
yPos -= (ScreenHeight - 5*cellSize)
67+
// detect range
68+
if xPos >= buttonRectRelativePos.Min.X && xPos <= buttonRectRelativePos.Dx() &&
69+
yPos >= 0 && yPos <= buttonRectRelativePos.Dy() {
70+
gameLayout.clearCursorValue()
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)