-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.go
218 lines (183 loc) · 3.08 KB
/
engine.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
package main
import (
"math/rand"
"time"
)
const x = 40
const y = 40
const (
UP Direction = "UP"
DOWN Direction = "DOWN"
LEFT Direction = "LEFT"
RIGHT Direction = "RIGHT"
)
type Direction string
type Point struct {
X int
Y int
}
type Snake struct {
Direction Direction
Body []Point
}
type Game struct {
Snake *Snake
Checkpoint *CheckPoint
FieldSize Point
GameOver bool
Running bool
StartTime time.Time
}
type CheckPoint struct {
Point Point
Taken bool
}
func (s *Snake) NewHead() Point {
var newHead Point
switch s.Direction {
case UP:
newHead = Point{
X: s.Body[0].X,
Y: s.Body[0].Y - 1,
}
case DOWN:
newHead = Point{
X: s.Body[0].X,
Y: s.Body[0].Y + 1,
}
case LEFT:
newHead = Point{
X: s.Body[0].X - 1,
Y: s.Body[0].Y,
}
case RIGHT:
newHead = Point{
X: s.Body[0].X + 1,
Y: s.Body[0].Y,
}
}
return newHead
}
func (s *Snake) Move(n Point, keepTail bool) {
if keepTail {
s.Body = append([]Point{n}, s.Body...)
} else {
s.Body = append([]Point{n}, s.Body[:len(s.Body)-1]...)
}
}
func (s *Snake) ChangeDirection(n Direction) bool {
changed := false
if s.Direction.Opposite() != n && s.Direction != n {
s.Direction = n
changed = true
}
return changed
}
func Equal(p1 Point, p2 Point) bool {
return p1.X == p2.X && p1.Y == p2.Y
}
func (f *Game) CheckPointCollisions(p Point) bool {
return Equal(p, f.Checkpoint.Point)
}
func (f *Game) BorderCollisions(p Point) bool {
return p.X < 0 || p.Y < 0 || p.X >= f.FieldSize.X || p.Y >= f.FieldSize.Y
}
func (f Game) SnakeEatItself(p Point) bool {
res := false
for _, v := range f.Snake.Body {
res = res || Equal(v, p)
}
return res
}
func (d Direction) Opposite() Direction {
var o Direction
switch d {
case UP:
o = DOWN
case DOWN:
o = UP
case LEFT:
o = RIGHT
case RIGHT:
o = LEFT
}
return o
}
func (f *Game) GameStep() {
n := f.Snake.NewHead()
g := f.BorderCollisions(n) || f.SnakeEatItself(n)
if g {
f.GameOver = true
}
if !f.GameOver && f.Running {
y := f.CheckPointCollisions(n)
f.Snake.Move(n, y)
if y {
f.NewCheckPoint()
}
}
}
func NewGame() *Game {
var g *Game
g = &Game{
FieldSize: Point{20, 20},
GameOver: false,
Running: false,
StartTime: time.Now(),
}
g.NewSnake()
g.NewCheckPoint()
return g
}
func (f *Game) NewSnake() {
x := f.FieldSize.X / 2
y := f.FieldSize.Y / 2
f.Snake = &Snake{
Direction: UP,
Body: []Point{
{
X: x,
Y: y,
},
{
X: x + 1,
Y: y,
},
{
X: x + 2,
Y: y,
},
{
X: x + 3,
Y: y,
}},
}
}
func (g *Game) NewCheckPoint() {
FreePoints := []Point{}
for i := 0; i < g.FieldSize.X; i++ {
for j := 0; j < g.FieldSize.X; j++ {
pfree := true
for _, v := range g.Snake.Body {
if v.X == i && v.Y == j {
pfree = false
}
}
if pfree {
FreePoints = append(FreePoints, Point{
X: i,
Y: j,
})
}
}
}
l := int32(len(FreePoints))
pnum := rand.Int31n(l)
g.Checkpoint = &CheckPoint{
Point: FreePoints[pnum],
Taken: false,
}
}
func (g Game) SnakeLength() int {
return len(g.Snake.Body)
}