Skip to content

Commit 2167e38

Browse files
committed
✨ (sudoku-game): add number input button
1 parent 69554b0 commit 2167e38

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

internal/layout/layout.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func (gameLayout *GameLayout) Update() error {
3636
return nil
3737
}
3838
gameLayout.detectClearHandler()
39+
gameLayout.detectNumberButtonHandler()
3940
gameLayout.detectClickCell()
4041
gameLayout.elapsedSeconds = gameLayout.gameInstance.GetElaspedTime()
4142
gameLayout.DetectCursor()
@@ -56,6 +57,7 @@ func (gameLayout *GameLayout) Draw(screen *ebiten.Image) {
5657
gameLayout.drawTimeLayout(screen)
5758
gameLayout.drawLevelButtonWithIcon(screen)
5859
gameLayout.drawClearButton(screen)
60+
gameLayout.drawNumberButtons(screen)
5961
// 畫出 cursor
6062
gameLayout.drawCursor(screen)
6163
// 根據遊戲狀態來畫出盤面

internal/layout/number-buttons.go

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package layout
22

33
import (
4+
"fmt"
45
"image"
56
"image/color"
67

78
"github.com/hajimehoshi/ebiten/v2"
9+
"github.com/hajimehoshi/ebiten/v2/inpututil"
810
"github.com/hajimehoshi/ebiten/v2/text/v2"
911
"github.com/hajimehoshi/ebiten/v2/vector"
1012
"github.com/leetcode-golang-classroom/sudoku-game/internal/game"
@@ -18,7 +20,7 @@ var numberButtonValues = [3][3]int{
1820
4, 5, 6,
1921
},
2022
{
21-
5, 7, 8,
23+
7, 8, 9,
2224
},
2325
}
2426

@@ -71,3 +73,77 @@ func (gameLayout *GameLayout) detectClearHandler() {
7173
}
7274
}
7375
}
76+
77+
func (gameLayout *GameLayout) drawNumberButtons(screen *ebiten.Image) {
78+
// draw each button
79+
for row := 0; row < 3; row++ {
80+
for col := 0; col < 3; col++ {
81+
gameLayout.drawNumberButton(screen, row, col)
82+
}
83+
}
84+
}
85+
86+
func (gameLayout *GameLayout) drawNumberButton(screen *ebiten.Image, row, col int) {
87+
buttonXPos := BoardWidth + col*cellSize + cellSize
88+
buttonYPos := (ScreenHeight - 3*cellSize) + row*cellSize
89+
vector.DrawFilledCircle(screen,
90+
float32(buttonXPos),
91+
float32(buttonYPos+cellSize/2),
92+
25,
93+
getIconColor(Button),
94+
true,
95+
)
96+
textValue := fmt.Sprintf("%d", numberButtonValues[row][col])
97+
textXPos := buttonXPos
98+
textYPos := buttonYPos + cellSize/2
99+
textOpts := &text.DrawOptions{}
100+
textOpts.ColorScale.ScaleWithColor(getTileColor(game.Input))
101+
textOpts.PrimaryAlign = text.AlignCenter
102+
textOpts.SecondaryAlign = text.AlignCenter
103+
textOpts.GeoM.Translate(float64(textXPos), float64(textYPos))
104+
text.Draw(screen, textValue, &text.GoTextFace{
105+
Source: mplusFaceSource,
106+
Size: 30,
107+
}, textOpts)
108+
}
109+
110+
func (gameLayout *GameLayout) detectNumberButtonHandler() {
111+
if inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
112+
xPos, yPos := ebiten.CursorPosition()
113+
xPos -= (BoardWidth + cellSize/2)
114+
yPos -= (ScreenHeight - 3*cellSize)
115+
// detect range
116+
if xPos >= 0 && xPos <= 3*cellSize &&
117+
yPos >= 0 && yPos <= 3*cellSize {
118+
row := yPos / cellSize
119+
col := xPos / cellSize
120+
gameLayout.handleNumberButtonClick(row, col)
121+
}
122+
return
123+
}
124+
}
125+
126+
func (gameLayout *GameLayout) handleNumberButtonClick(row, col int) {
127+
board := gameLayout.gameInstance.Board
128+
cursorRow := board.CursorRow
129+
cursorCol := board.CursorCol
130+
targetCell := board.Cells[cursorRow][cursorCol]
131+
cellType := targetCell.Type
132+
// 當為無法清除的值時
133+
if cellType == game.Preset {
134+
return
135+
}
136+
// 當輸入格為空格時
137+
if cellType == game.Empty {
138+
board.IncreaseFilledCount()
139+
}
140+
value := numberButtonValues[row][col]
141+
safed := board.IsSafe(cursorRow, cursorCol, value)
142+
if !safed {
143+
handleConflict(board, cellType, cursorRow, cursorCol)
144+
} else {
145+
handleNonConflict(board, cellType, cursorRow, cursorCol)
146+
}
147+
// 更新輸入
148+
board.Cells[cursorRow][cursorCol].Value = value
149+
}

0 commit comments

Comments
 (0)