11package layout
22
33import (
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