-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.go
More file actions
182 lines (168 loc) · 3.13 KB
/
Copy pathsnake.go
File metadata and controls
182 lines (168 loc) · 3.13 KB
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
package snake
import (
"math/rand"
"time"
)
type snake struct {
position []Position
}
func randomPos(width, height int) Position {
return Position{x: rand.Intn(width-2) + 1, y: rand.Intn(height-2) + 1}
}
func randomDir() (int, int) {
dir := rand.Int() % 4
if dir == 0 {
return -1, 0
} else if dir == 1 {
return 1, 0
} else if dir == 2 {
return 0, -1
} else {
return 0, 1
}
}
func newSnake(board Board, id ID) *snake {
rand.Seed(time.Now().UnixNano())
s := snake{position: make([]Position, 2)}
diry, dirx := randomDir()
for {
pos := randomPos(len(board[0]), len(board))
x := pos.x
y := pos.y
if board[y][x] == empty && board[y+diry][x+dirx] == empty {
board[y][x] = int8(id)
board[y+diry][x+dirx] = int8(id)
s.position[0].x, s.position[0].y = x, y
s.position[1].x, s.position[1].y = x+dirx, y+diry
return &s
}
}
}
type direction string
const (
north direction = "north"
south direction = "south"
west direction = "west"
east direction = "east"
)
func (s *snake) reduceSize() bool {
if len(s.position) <= 2 {
return true
}
s.position = s.position[1:]
return false
}
func (s *snake) tail() Position {
return s.position[0]
}
func (s *snake) head() Position {
if s == nil || len(s.position) == 0 {
return Position{0, 0}
}
return s.position[len(s.position)-1]
}
func (s *snake) body() Position {
if s == nil || len(s.position) == 0 {
return Position{0, 0}
}
if len(s.position) < 2 {
return Position{0, 0}
}
return s.position[len(s.position)-2]
}
func (s *snake) getDir() direction {
head := s.head()
body := s.body()
if head.x == body.x {
if head.y > body.y {
return south
}
return north
} else if head.x > body.x {
return east
}
return west
}
func (s *snake) newHeadPos(m Move) Position {
dir := s.getDir()
head := s.head()
c := m.getChoice()
var newPos Position
switch dir {
case north:
if choice(c) == left {
newPos = Position{
x: head.x - 1,
y: head.y,
}
} else if choice(c) == right {
newPos = Position{
x: head.x + 1,
y: head.y,
}
} else if choice(c) == straight {
newPos = Position{
x: head.x,
y: head.y - 1,
}
}
case south:
if choice(c) == left {
newPos = Position{
x: head.x + 1,
y: head.y,
}
} else if choice(c) == right {
newPos = Position{
x: head.x - 1,
y: head.y,
}
} else if choice(c) == straight {
newPos = Position{
x: head.x,
y: head.y + 1,
}
}
case west:
if choice(c) == left {
newPos = Position{
x: head.x,
y: head.y + 1,
}
} else if choice(c) == right {
newPos = Position{
x: head.x,
y: head.y - 1,
}
} else if choice(c) == straight {
newPos = Position{
x: head.x - 1,
y: head.y,
}
}
case east:
if choice(c) == left {
newPos = Position{
x: head.x,
y: head.y - 1,
}
} else if choice(c) == right {
newPos = Position{
x: head.x,
y: head.y + 1,
}
} else if choice(c) == straight {
newPos = Position{
x: head.x + 1,
y: head.y,
}
}
}
return newPos
}
func (s *snake) moveTo(p Position, food bool) {
s.position = append(s.position, p)
if !food {
s.position = s.position[1:]
}
}