@@ -16,29 +16,79 @@ func (gameLayout *GameLayout) DetectInput() {
1616 // 數字輸入
1717 for key := ebiten .KeyDigit1 ; key <= ebiten .KeyDigit9 ; key ++ {
1818 if inpututil .IsKeyJustPressed (key ) {
19- if targetCell .Type != game .Preset {
20- value := int (key - ebiten .KeyDigit0 )
21- // 檢查輸入的值是否為放入是否能會造成 Conflict
22- if ! board .IsSafe (targetRow , targetCol , value ) {
23- // 標示為 Conflict Input
24- board.Cells [targetRow ][targetCol ].Type = game .InputConflict
25- } else {
26- board.Cells [targetRow ][targetCol ].Type = game .Input
27- }
28- // 更新輸入
29- board.Cells [targetRow ][targetCol ].Value = value
30- }
19+ handleKeyInput (board , targetCell , key , targetRow , targetCol )
3120 return
3221 }
3322 }
3423
3524 // 清除輸入
3625 if inpututil .IsKeyJustPressed (ebiten .Key0 ) || inpututil .IsKeyJustPressed (ebiten .KeyDelete ) {
37- if targetCell .Type != game .Preset {
38- // 清空目前 Cell 的值
39- board.Cells [targetRow ][targetCol ].Value = 0
40- board.Cells [targetRow ][targetCol ].Type = game .Empty
41- }
26+ handleClearInput (board , targetCell , targetRow , targetCol )
27+ return
28+ }
29+ }
30+
31+ // handleClearInput - 處理清除
32+ func handleClearInput (board * game.Board , targetCell * game.Cell ,
33+ targetRow , targetCol int ) {
34+ cellType := targetCell .Type
35+ // 當遇到題目時
36+ if cellType == game .Preset {
4237 return
4338 }
39+ // 原本輸入是 conflict
40+ if cellType == game .InputConflict {
41+ board .DescreaseConflictCount ()
42+ }
43+ // 原本輸入非空
44+ if cellType != game .Empty {
45+ board .DecreaseFilledCount ()
46+ }
47+ // 清空目前 Cell 的值
48+ board.Cells [targetRow ][targetCol ].Value = 0
49+ board.Cells [targetRow ][targetCol ].Type = game .Empty
50+ }
51+
52+ // handleKeyInput - 處理輸入時
53+ func handleKeyInput (board * game.Board , targetCell * game.Cell , key ebiten.Key ,
54+ targetRow , targetCol int ) {
55+ cellType := targetCell .Type
56+ // 當格子為題目時
57+ if cellType == game .Preset {
58+ return
59+ }
60+ value := int (key - ebiten .KeyDigit0 )
61+ // 當輸入格為空格時
62+ if cellType == game .Empty {
63+ board .IncreaseFilledCount ()
64+ }
65+ safed := board .IsSafe (targetRow , targetCol , value )
66+ if ! safed {
67+ handleConflict (board , cellType , targetRow , targetCol )
68+ } else {
69+ handleNonConflict (board , cellType , targetRow , targetCol )
70+ }
71+ // 更新輸入
72+ board.Cells [targetRow ][targetCol ].Value = value
73+ }
74+
75+ // handleConflict - 處理 Conflict Cell
76+ func handleConflict (board * game.Board , cellType game.CellType ,
77+ targetRow , targetCol int ) {
78+ if cellType != game .InputConflict {
79+ board .IncreaseConflictCount ()
80+ }
81+ // 標示為 Conflict Input
82+ board.Cells [targetRow ][targetCol ].Type = game .InputConflict
83+ }
84+
85+ // handleNonConflict - 處理 Non-Conflict Cell
86+ func handleNonConflict (board * game.Board , cellType game.CellType ,
87+ targetRow , targetCol int ) {
88+ // 當輸入為 Conflict 時
89+ if cellType == game .InputConflict {
90+ board .DescreaseConflictCount ()
91+ }
92+ // 標示為 Input
93+ board.Cells [targetRow ][targetCol ].Type = game .Input
4494}
0 commit comments