/
game.go
364 lines (326 loc) · 8.34 KB
/
game.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package game
import (
"bytes"
"context"
"fmt"
"io"
"strconv"
"time"
"github.com/nsf/termbox-go"
)
// PlayerChar defines how should each player entries rendered
var PlayerChar = []string{"\\//\\", "/\\\\/"}
var piece = '█'
const (
winsLabelX = -2
winsCountX = winsLabelX + 1
p1WinsLabelY = 0
p2WinsLabelY = 1
)
// Game defines the state of the game and arena details
type Game struct {
State [][]int
CurrentPlayer int
Width int
Height int
offsetX int
offsetY int
Winner int
players []string
wonState [][]int
ctx context.Context
Cancel context.CancelFunc
PlayerOneWins int
PlayerTwoWins int
}
func getEmptyState(height, width int) [][]int {
state := [][]int{}
for i := 0; i < height; i++ {
rowState := make([]int, width)
state = append(state, rowState)
}
return state
}
// NewGame return a new instance of game
func NewGame(width, height, playerOneWins, playerTwoWins int) *Game {
ctx, cancel := context.WithCancel(context.Background())
game := &Game{
Width: width,
Height: height,
State: getEmptyState(height, width),
CurrentPlayer: 1,
ctx: ctx,
Cancel: cancel,
PlayerOneWins: playerOneWins,
PlayerTwoWins: playerTwoWins,
}
game.getOffset()
return game
}
// Draw is the main routine which paints the current state
func (g *Game) Draw() {
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
for x := 0; x < g.Width; x++ {
g.paintCell(x, -2, rune(48+x), termbox.ColorYellow, termbox.ColorDefault)
}
// Place player one wins to the left, a few rows down
r, fore, bg := getplayerDisplayProps(1)
g.paintCell(winsLabelX, p1WinsLabelY, r, fore, bg)
g.paintCell(winsCountX, p1WinsLabelY, rune(48+g.PlayerOneWins), termbox.ColorGreen, termbox.ColorDefault)
// Place player two wins right below the player one wins
r, fore, bg = getplayerDisplayProps(2)
g.paintCell(winsLabelX, p2WinsLabelY, r, fore, bg)
g.paintCell(winsCountX, p2WinsLabelY, rune(48+g.PlayerTwoWins), termbox.ColorGreen, termbox.ColorDefault)
// Place a cell showing what is the current "player" to run
text := "Current Player: "
for k, v := range text {
g.paintCell(winsLabelX-len(text)+k, p2WinsLabelY+2, v, termbox.ColorGreen, termbox.ColorDefault)
}
r, fore, bg = getplayerDisplayProps(g.CurrentPlayer)
g.paintCell(winsLabelX, p2WinsLabelY+2, r, fore, bg)
for y := 0; y < g.Height; y++ {
for x := 0; x < g.Width; x++ {
g.setContent(x, y)
}
}
termbox.Flush()
}
func (g *Game) getOffset() {
sw, sh := termbox.Size()
g.offsetX = (sw - g.Width*3) / 2
g.offsetY = (sh - g.Height*2) / 2
}
func (g *Game) setContent(x, y int) {
ch, fore, bg := getplayerDisplayPropsLarge(g.State[y][x])
// Paint a double square
g.setCell(x*3, y*2, ch, fore, bg)
g.setCell(x*3+1, y*2, ch, fore, bg)
}
func (g *Game) paintCellWithSpacing(x, y int, ch rune, fore termbox.Attribute, bg termbox.Attribute) {
g.paintCell(x, y, ch, fore, bg)
// g.paintCell(x+1, y, rune('h'), fore, bg)
}
// Paint Cell performs the multiplication of location and defers to setCell for offset calculation,
// given absolute coordinates and what to draw.
func (g *Game) paintCell(x, y int, ch rune, fore termbox.Attribute, bg termbox.Attribute) {
g.setCell(x*3, y*2, ch, fore, bg)
}
// setCell performs offset calculation given coordinates and paints using termbox
func (g *Game) setCell(x, y int, ch rune, fore termbox.Attribute, bg termbox.Attribute) {
termbox.SetCell((g.offsetX + x), (g.offsetY + y), ch, fore, bg)
}
func getplayerDisplayProps(player int) (rune, termbox.Attribute, termbox.Attribute) {
if player == 1 {
return piece, termbox.ColorRed, termbox.ColorDefault
}
if player == 2 {
return piece, termbox.ColorBlue, termbox.ColorDefault
}
return piece, termbox.ColorDefault, termbox.ColorBlack
}
// TODO: move getting display related functions to another place,
// and make this one actually return large props
func getplayerDisplayPropsLarge(player int) (rune, termbox.Attribute, termbox.Attribute) {
if player == 1 {
return piece, termbox.ColorRed, termbox.ColorDefault
}
if player == 2 {
return piece, termbox.ColorBlue, termbox.ColorDefault
}
return ' ', termbox.ColorDefault, termbox.ColorBlack
}
func (g *Game) addEntry(col, player int) {
if g.Winner != 0 {
return
}
if col < 0 || col > g.Width-1 {
return
}
if g.State[0][col] != 0 {
return
}
i := 0
for ; i < g.Height && g.State[i][col] == 0; i++ {
g.State[i][col] = player
if i > 0 {
g.State[i-1][col] = 0
}
g.Draw()
time.Sleep(30 * time.Millisecond)
}
g.togglePlayer()
if g.isWon(col, i-1, player) {
g.declareWinner()
}
}
func (g *Game) isWon(col, row, player int) bool {
// check vertically
wonState := make([][]int, 0)
count := 0
for i := row; i < g.Height && g.State[i][col] == player; i++ {
count++
wonState = append(wonState, []int{i, col})
}
if count >= 4 {
g.Winner = player
g.wonState = wonState
return true
}
// check horizontally left
wonState = make([][]int, 0)
count = 0
i := col
for ; i >= 0 && g.State[row][i] == player; i-- {
}
i++
// check horizontally right
for ; i < g.Width && g.State[row][i] == player; i++ {
wonState = append(wonState, []int{row, i})
count++
}
if count >= 4 {
g.Winner = player
g.wonState = wonState
return true
}
// check positive diagonal
wonState = make([][]int, 0)
i = 0
count = 0
for ; col-i >= 0 && row-i >= 0 && g.State[row-i][col-i] == player; i++ {
}
i--
for ; col-i < g.Width && row-i < g.Height && g.State[row-i][col-i] == player; i-- {
wonState = append(wonState, []int{row - i, col - i})
count++
}
if count >= 4 {
g.wonState = wonState
g.Winner = player
return true
}
// check negative diagonal
wonState = make([][]int, 0)
i = 0
count = 0
for ; col+i <= g.Width && row-i >= 0 && g.State[row-i][col+i] == player; i++ {
}
i--
for ; col+i >= 0 && row-i < g.Height && g.State[row-i][col+i] == player; i-- {
wonState = append(wonState, []int{row - i, col + i})
count++
}
if count >= 4 {
g.wonState = wonState
g.Winner = player
return true
}
return false
}
func (g *Game) declareWinner() {
go func(g *Game) {
show := true
inf_loop:
for {
select {
case <-g.ctx.Done():
// Once cancel is called lets break out of this loop
break inf_loop
default:
}
// Until the cancel is called, this text will flash every 500 seconds
g.renderText("% won the game", show)
show = !show
termbox.Flush()
time.Sleep(time.Millisecond * 500)
}
}(g)
}
func (g *Game) renderText(text string, show bool) {
x, y := g.offsetX+8, g.offsetY+9
for i := 0; i < len(text); i++ {
if !show {
termbox.SetCell(x, y+i, rune(' '), termbox.ColorDefault, termbox.ColorDefault)
termbox.SetCell(x+i, y, rune(' '), termbox.ColorDefault, termbox.ColorDefault)
continue
}
if text[i] == byte('%') {
ch, fore, bg := getplayerDisplayPropsLarge(g.Winner)
termbox.SetCell(x, y+i, ch, fore, bg)
} else {
termbox.SetCell(x+i, y, rune(text[i]), termbox.ColorWhite, termbox.ColorDefault)
}
}
termbox.Flush()
}
func (g *Game) togglePlayer() {
if g.CurrentPlayer == 1 {
g.CurrentPlayer = 2
return
}
g.CurrentPlayer = 1
}
// Input adds an input to game
func (g *Game) Input(col int) {
g.addEntry(col, g.CurrentPlayer)
}
func (g *Game) generateSplashContent() {
g.State[1][5] = 1
g.State[2][4] = 1
g.State[2][5] = 2
g.State[3][4] = 1
g.State[3][5] = 2
g.State[4][3] = 2
g.State[4][5] = 1
g.State[5][3] = 2
g.State[5][5] = 1
g.State[6][2] = 2
g.State[6][3] = 1
g.State[6][4] = 2
g.State[6][5] = 1
g.State[6][6] = 2
g.State[6][7] = 1
g.State[7][5] = 2
g.State[8][5] = 2
g.Draw()
g.Winner = 3
}
// SplashScreen will render splash screen animation
func (g *Game) SplashScreen() {
go func(g *Game) {
show := true
inf_loop:
for {
select {
case <-g.ctx.Done():
g = NewGame(g.Width, g.Height, 0, 0)
g.Draw()
break inf_loop
default:
}
if show {
g.generateSplashContent()
} else {
g.State = getEmptyState(g.Height, g.Width)
}
g.Draw()
show = !show
time.Sleep(time.Millisecond * 500)
}
}(g)
}
func (g *Game) StateString() string {
var buf bytes.Buffer
for _, cols := range g.State {
buf.WriteRune('`')
for _, c := range cols {
buf.WriteString(strconv.Itoa(c))
}
buf.WriteString("`,\n")
}
return buf.String()
}
func (g *Game) WriteState(w io.Writer) {
fmt.Fprintf(w, "+%v\n", g)
w.Write([]byte(g.StateString()))
}