Go 语言实现 2048 游戏

执行代码

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
package main

import (
"fmt"
"github.com/shiyanlou/termbox-go"
"math/rand"
"time"
)

var Score int
var step int

func coverPrintStr(x, y int, str string, fg, bg termbox.Attribute) error {

xx := x
for n, c := range str {
if c == '\n' {
y++
xx = x - n - 1
}
termbox.SetCell(xx+n, y, c, fg, bg)
}
termbox.Flush()
return nil
}

type Status uint

const (
Win Status = iota
Lose
Add
Max = 2048
)

type G2048 [4][4]int

func (t *G2048) checkWinOrAdd() Status {
for _, x := range t {
for _, y := range x {
if y >= Max {
return Win
}
}
}
i := rand.Intn(len(t))
j := rand.Intn(len(t))
for x := 0; x < len(t); x++ {
for y := 0; y < len(t); y++ {
if t[i%len(t)][j%len(t)] == 0 {
t[i%len(t)][j%len(t)] = 2 << (rand.Uint32() % 2)
return Add
}
j++
}
i++
}

return Lose
}

func (t G2048) initialize(ox, oy int) error {
fg := termbox.ColorYellow
bg := termbox.ColorBlack
termbox.Clear(fg, bg)
str := " SCORE: " + fmt.Sprint(Score)
for n, c := range str {
termbox.SetCell(ox+n, oy-1, c, fg, bg)
}
str = "ESC:exit " + "Enter:replay"
for n, c := range str {
termbox.SetCell(ox+n, oy-2, c, fg, bg)
}
str = " PLAY with ARROW KEY"
for n, c := range str {
termbox.SetCell(ox+n, oy-3, c, fg, bg)
}
fg = termbox.ColorBlack
bg = termbox.ColorGreen
for i := 0; i <= len(t); i++ {
for x := 0; x < 5*len(t); x++ {
termbox.SetCell(ox+x, oy+i*2, '-', fg, bg)
}
for x := 0; x <= 2*len(t); x++ {
if x%2 == 0 {
termbox.SetCell(ox+i*5, oy+x, '+', fg, bg)
} else {
termbox.SetCell(ox+i*5, oy+x, '|', fg, bg)
}
}
}
fg = termbox.ColorYellow
bg = termbox.ColorBlack
for i := range t {
for j := range t[i] {
if t[i][j] > 0 {
str := fmt.Sprint(t[i][j])
for n, char := range str {
termbox.SetCell(ox+j*5+1+n, oy+i*2+1, char, fg, bg)
}
}
}
}
return termbox.Flush()
}

func (t *G2048) mirrorV() {
tn := new(G2048)
for i, line := range t {
for j, num := range line {
tn[len(t)-i-1][j] = num
}
}
*t = *tn
}

func (t *G2048) right90() {
tn := new(G2048)
for i, line := range t {
for j, num := range line {
tn[j][len(t)-i-1] = num
}
}
*t = *tn
}

func (t *G2048) left90() {
tn := new(G2048)
for i, line := range t {
for j, num := range line {
tn[len(line)-j-1][i] = num
}
}
*t = *tn
}

func (t *G2048) right180() {
tn := new(G2048)
for i, line := range t {
for j, num := range line {
tn[len(line)-i-1][len(line)-j-1] = num
}
}
*t = *tn
}

func (t *G2048) mergeUp() bool {
tl := len(t)
changed := false
notfull := false
for i := 0; i < tl; i++ {

np := tl
n := 0

for x := 0; x < np; x++ {
if t[x][i] != 0 {
t[n][i] = t[x][i]
if n != x {
changed = true
}
n++
}
}
if n < tl {
notfull = true
}
np = n
for x := 0; x < np-1; x++ {
if t[x][i] == t[x+1][i] {
t[x][i] *= 2
t[x+1][i] = 0
Score += t[x][i] * step
x++
changed = true
}
}
n = 0
for x := 0; x < np; x++ {
if t[x][i] != 0 {
t[n][i] = t[x][i]
n++
}
}
for x := n; x < tl; x++ {
t[x][i] = 0
}
}
return changed || !notfull
}

func (t *G2048) mergeDwon() bool {
//t.mirrorV()
t.right180()
changed := t.mergeUp()
//t.mirrorV()
t.right180()
return changed
}

func (t *G2048) mergeLeft() bool {
t.right90()
changed := t.mergeUp()
t.left90()
return changed
}

func (t *G2048) mergeRight() bool {
t.left90()
changed := t.mergeUp()
t.right90()
return changed
}

func (t *G2048) mrgeAndReturnKey() termbox.Key {
var changed bool
Lable:
changed = false
//ev := termbox.PollEvent()
event_queue := make(chan termbox.Event)
go func() {
for {
event_queue <- termbox.PollEvent()
}
}()

ev := <-event_queue

switch ev.Type {
case termbox.EventKey:
switch ev.Key {
case termbox.KeyArrowUp:
changed = t.mergeUp()
case termbox.KeyArrowDown:
changed = t.mergeDwon()
case termbox.KeyArrowLeft:
changed = t.mergeLeft()
case termbox.KeyArrowRight:
changed = t.mergeRight()
case termbox.KeyEsc, termbox.KeyEnter:
changed = true
default:
changed = false
}

if !changed {
goto Lable
}

case termbox.EventResize:
x, y := termbox.Size()
t.initialize(x/2-10, y/2-4)
goto Lable
case termbox.EventError:
panic(ev.Err)
}
step++
return ev.Key
}

func (b *G2048) clear() {
next := new(G2048)
Score = 0
step = 0
*b = *next

}

func (b *G2048) Run() {
err := termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()

rand.Seed(time.Now().UnixNano())

A:

b.clear()
for {
st := b.checkWinOrAdd()
x, y := termbox.Size()
b.initialize(x/2-10, y/2-4)
switch st {
case Win:
str := "Win!!"
strl := len(str)
coverPrintStr(x/2-strl/2, y/2, str, termbox.ColorMagenta, termbox.ColorYellow)
case Lose:
str := "Lose!!"
strl := len(str)
coverPrintStr(x/2-strl/2, y/2, str, termbox.ColorBlack, termbox.ColorRed)
case Add:
default:
fmt.Print("Err")
}
key := b.mrgeAndReturnKey()
if key == termbox.KeyEsc {
return
}
if key == termbox.KeyEnter {
goto A
}
}
}

func main() {
var game G2048
game.Run()
}

注意:创建源文件 2048.go,输入以上内容。

执行效果

运行代码 go run 2048.go 可启动游戏:

本文链接:https://my.lmcjl.com/post/18597.html

展开阅读全文

4 评论

留下您的评论.