-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
298 lines (230 loc) · 5.63 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package main
import (
"fmt"
"strconv"
"strings"
// "fmt"
"github.com/DeyV/go_game/board"
"github.com/nsf/termbox-go"
)
func main() {
b := board.NewBoard()
err := termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
// b.Put(3, 3, board.BLACK)
// b.Put(3, 4, board.BLACK)
// b.Put(5, 4, board.WHITE)
var game = &OpenGame{NextMove: board.BLACK}
showBoardFrame(b, game)
termbox.Flush()
termbox.SetInputMode(termbox.InputEsc)
runGame(b, game)
}
/******************************************/
type OpenGame struct {
NextMove board.StoneColor
InputValue string
}
func (g *OpenGame) Move(b *board.PlayBoard) (bool, error) {
defer func() { g.InputValue = "" }()
return false, fmt.Errorf("wrong move")
}
func (g *OpenGame) PressKey(r rune) {
if len(g.InputValue) >= 2 {
return
}
g.InputValue += string(r)
}
func (g *OpenGame) PressBackspace() {
if g.InputValue != "" {
g.InputValue = g.InputValue[:len(g.InputValue)-1]
}
}
func (g *OpenGame) CheckKey(r rune, s int) bool {
size := rune(s)
if r >= 'A' && r < 'A'+size {
return true
}
if r >= 'a' && r < 'a'+size {
return true
}
if r >= '1' && (r < '1'+size || r <= '9') {
return true
}
return false
}
func (g *OpenGame) convertValue(val string) (x, y int) {
var xx rune
var yy string
for _, r := range val {
if r >= 'a' {
xx = r - ('a' - 'A')
continue
}
if r >= 'A' {
xx = r
continue
}
// on big board it can will be bigger number then 9
if r >= '1' {
yy += string(r)
}
}
x = int(xx - 'A' + 1)
y, _ = strconv.Atoi(yy)
return
}
/*******************************************/
var FirstRow = 3
var FirstCol = 5
func showBoardFrame(b *board.PlayBoard, game *OpenGame) {
size := b.Size()
// top label
for i := 1; i <= size; i++ {
// nr := strconv.Itoa(i)
showField(FirstCol+i*4, FirstRow, rune(i+64), termbox.ColorWhite, termbox.ColorBlue)
}
// left label
for i := 1; i <= size; i++ {
nr := strconv.Itoa(i)
showField(FirstCol, FirstRow+2+(i-1)*2, rune(nr[0]), termbox.ColorWhite, termbox.ColorBlue)
}
showBoard(b)
showLabels()
showGameInput(game)
showAlert("Write field address", 0)
}
func showBoard(b *board.PlayBoard) {
size := b.Size()
fg, bg := termbox.ColorWhite, termbox.ColorBlack
for y := 0; y < size; y++ {
for x := 0; x < size; x++ {
field := b.Get(x, y)
xx := FirstCol + (x+1)*4
yy := FirstRow + 2 + y*2
if field.IsEmpty() {
showField(xx, yy, ' ', fg, bg)
} else {
r := rune(field.String()[0])
showField(xx, yy, r, fg|termbox.AttrBold, bg)
}
fg, bg = bg, fg
}
}
}
func showField(x, y int, ch rune, fg, bg termbox.Attribute) {
termbox.SetCell(x, y, ' ', fg, bg)
termbox.SetCell(x+1, y, ch, fg, bg)
termbox.SetCell(x+2, y, ' ', fg, bg)
}
func showAlert(val string, col int) {
x, y := termbox.Size()
valLengh := len(val)
spaces := strings.Repeat(" ", x-valLengh-col-1)
printText(col, y-1, termbox.ColorWhite|termbox.AttrBold, termbox.ColorWhite, " "+val+spaces)
}
func runGame(b *board.PlayBoard, game *OpenGame) {
loop:
for {
switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventKey:
switch ev.Key {
case termbox.KeyEsc:
break loop
case termbox.KeyEnter:
if len(game.InputValue) >= 2 {
showAlert("Pressed enter", 30)
_, err := game.Move(b)
if err != nil {
showAlert(err.Error(), 50)
}
showGameInput(game)
} else {
showAlert("To fast, write correct address first", 30)
}
case termbox.KeyBackspace:
showAlert("Pressed backspace", 30)
game.PressBackspace()
showGameInput(game)
default:
if !game.CheckKey(ev.Ch, b.Size()) {
showAlert("Wrong key", 30)
} else {
game.PressKey(ev.Ch)
showGameInput(game)
showBoard(b)
}
}
// dispatch_press(&ev)
// pretty_print_press(&ev)
termbox.Flush()
case termbox.EventResize:
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
showBoardFrame(b, game)
termbox.Flush()
case termbox.EventError:
panic(ev.Err)
}
}
}
func showLabels() {
printText(10, 1, termbox.ColorBlue|termbox.AttrBold, termbox.ColorDefault, "WELCOME IN \"GO\" GAME")
printText(40, 1, termbox.ColorGreen, termbox.ColorDefault, "(Esc to close game)")
}
func showGameInput(game *OpenGame) {
showInput(game.InputValue)
printText(24, 23, termbox.ColorBlue, termbox.ColorDefault, "move:")
var userColor string
if game.NextMove == board.BLACK {
userColor = "BLACK"
} else {
userColor = "WHITE"
}
printText(30, 23, termbox.ColorBlue|termbox.AttrBold, termbox.ColorDefault, userColor)
}
func showInput(val string) {
valLengh := len(val)
inputLenght := 4
spaces := strings.Repeat(" ", inputLenght-valLengh-1)
printText(inputLenght, 23, termbox.ColorBlue|termbox.AttrBold, termbox.ColorYellow, " "+val+spaces)
termbox.SetCursor(inputLenght+valLengh+1, 23)
if valLengh >= 2 {
printText(12, 23, termbox.ColorYellow, termbox.ColorDefault, "(enter)")
} else {
printText(12, 23, termbox.ColorYellow, termbox.ColorDefault, " ")
}
}
func printText(x, y int, fg, bg termbox.Attribute, msg string) {
for _, c := range msg {
termbox.SetCell(x, y, c, fg, bg)
x++
}
}
/*
func dispatch_press(ev *termbox.Event) {
// if ev.Mod & termbox.ModAlt != 0 {
// draw_key(K_LALT, termbox.ColorWhite, termbox.ColorRed);
// draw_key(K_RALT, termbox.ColorWhite, termbox.ColorRed);
// }
var k *combo
if ev.Key >= termbox.KeyArrowRight {
k = &func_combos[0xFFFF-ev.Key]
} else if ev.Ch < 128 {
if ev.Ch == 0 && ev.Key < 128 {
k = &combos[ev.Key]
} else {
k = &combos[ev.Ch]
}
}
if k == nil {
return
}
keys := k.keys
for _, k := range keys {
draw_key(k, termbox.ColorWhite, termbox.ColorRed)
}
}
*/