/
ui.go
225 lines (204 loc) · 4.99 KB
/
ui.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
package terminalui
import (
"log"
"strconv"
"github.com/brandenc40/2048/game"
"github.com/mattn/go-runewidth"
"github.com/nsf/termbox-go"
)
const (
// game dimensions
width = 60
height = width / 3
x, y = 2, 1
xGap, yGap = 3, 2
// game cells
borderXStart = x
borderXEnd = borderXStart + width
borderYStart = y
borderYEnd = borderYStart + height
cellsXStart = borderXStart + 2
cellsYStart = borderYStart + 1
cellXGap = xGap
cellYGap = yGap
cellWidth = (borderXEnd - borderXStart - cellXGap*3) / 4
cellHeight = (borderYEnd - borderYStart - cellYGap*3) / 4
// score
scoreX = borderXStart + 2
scoreY = borderYEnd + 2
)
var textMsg = [...]string{
"HOW TO PLAY: Use your arrow keys to move the",
"tiles. Tiles with the same number merge into",
"one when they touch. Add them up to reach 2048!",
"",
"Reset the game with 'R' or 'r'",
"",
"Quit with ESC or CTRL+C",
}
var logo = [...]string{
" .-----. .----. .---. .-----. ",
" / ,-. \\ / .. \\ / . | / .-. \\ ",
" '-' | |. / \\ . / /| | | \\_.' / ",
" .' / | | ' | / / | |_ / .-. '. ",
" .' /__ ' \\ / '/ '-' || | | | ",
" | | \\ `' / `----| |-' \\ '-' / ",
" -------' `---'' `--' `----'' ",
" ",
}
type ui struct {
isOver bool
gc game.Controller
colorPalate colorPalate
}
// Run -
func Run(gc game.Controller, options ...Option) {
u := &ui{
gc: gc,
colorPalate: normalPalate(),
}
closeFunc := u.initialize(options...)
defer closeFunc()
u.drawGameBoard()
u.runGameLoop()
}
func (u *ui) initialize(options ...Option) (closeFunc func()) {
if err := termbox.Init(); err != nil {
log.Fatal(err)
}
for _, option := range options {
option.apply(u)
}
closeFunc = termbox.Close
return
}
func (u *ui) drawGameBoard() {
u.drawGameBackground()
u.drawGameCells()
u.drawScore()
u.drawGuide()
if err := termbox.Flush(); err != nil {
log.Fatal(err)
}
}
func (u *ui) shiftGameController(direction game.Direction) {
if u.isOver {
return
}
u.gc.Shift(direction)
u.drawGameCells()
u.drawScore()
if u.gc.Won() {
u.drawOverlayMessage("YOU WIN!")
u.isOver = true
}
if u.gc.Lost() {
u.drawOverlayMessage("NO MORE MOVES, TRY AGAIN")
u.isOver = true
}
if err := termbox.Flush(); err != nil {
log.Fatal(err)
}
}
func (u *ui) resetGameBoard() {
u.gc.Reset()
u.isOver = false
u.drawGameBoard()
}
func (u *ui) runGameLoop() {
for {
switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventKey:
switch ev.Key {
case termbox.KeyArrowUp:
u.shiftGameController(game.DirectionUp)
case termbox.KeyArrowDown:
u.shiftGameController(game.DirectionDown)
case termbox.KeyArrowRight:
u.shiftGameController(game.DirectionRight)
case termbox.KeyArrowLeft:
u.shiftGameController(game.DirectionLeft)
case termbox.KeyCtrlC, termbox.KeyEsc:
return
default:
switch ev.Ch {
case 'r', 'R':
u.resetGameBoard()
}
}
case termbox.EventResize:
u.drawGameBoard()
case termbox.EventError:
log.Fatal(ev.Err)
}
}
}
func (u *ui) drawGameBackground() {
for x := borderXStart; x <= borderXEnd; x++ {
for y := borderYStart; y <= borderYEnd; y++ {
termbox.SetCell(x, y, ' ', u.colorPalate.border, u.colorPalate.border)
}
}
}
func (u *ui) drawGameCells() {
for rowIdx, row := range u.gc.GetCells() {
for colIdx, col := range row {
u.drawGameCell(colIdx, rowIdx, col)
}
}
}
func (u *ui) drawGameCell(colIdx, rowIdx int, value uint16) {
var (
bg = u.colorPalate.empty
xStart = cellsXStart + (colIdx * cellWidth) + (colIdx * cellXGap)
xEnd = xStart + cellWidth
yStart = cellsYStart + (rowIdx * cellHeight) + (rowIdx * cellYGap)
yEnd = yStart + cellHeight
xMid int
yMid int
)
if value > 0 {
bg = u.colorPalate.values[value]
xMid = (xStart + xEnd) / 2
yMid = (yStart + yEnd) / 2
if value > 100 {
xMid -= 1
}
}
for x := xStart; x <= xEnd; x++ {
for y := yStart; y <= yEnd; y++ {
termbox.SetCell(x, y, ' ', termbox.ColorWhite, bg)
}
}
if value > 0 {
val := strconv.FormatUint(uint64(value), 10)
tbPrint(xMid, yMid, u.colorPalate.valueText, bg, val)
}
}
func (u *ui) drawScore() {
msg := "Current Score: " + strconv.Itoa((int)(u.gc.GetScore()))
tbPrint(scoreX, scoreY, u.colorPalate.score, termbox.ColorDefault, msg)
}
func (u *ui) drawGuide() {
y := borderYStart
x := borderXEnd + 2
for _, line := range logo {
y++
tbPrint(x, y, u.colorPalate.guide, termbox.ColorDefault, line)
}
for _, line := range textMsg {
y++
tbPrint(x, y, u.colorPalate.guide, termbox.ColorDefault, line)
}
}
func (u *ui) drawOverlayMessage(message string) {
x := borderXStart + (width-len(message))/2
y := borderYStart + height/2
tbPrint(x, y, u.colorPalate.overlayText, u.colorPalate.overlayBg, message)
}
func tbPrint(x, y int, fg, bg termbox.Attribute, msg string) {
for _, c := range msg {
termbox.SetCell(x, y, c, fg, bg)
x += runewidth.RuneWidth(c)
}
}