forked from BattlesnakeOfficial/rules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
standard.go
392 lines (355 loc) · 9.58 KB
/
standard.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package rules
import (
"math/rand"
"sort"
)
var standardRulesetStages = []string{
StageGameOverStandard,
StageMovementStandard,
StageStarvationStandard,
StageHazardDamageStandard,
StageFeedSnakesStandard,
StageEliminationStandard,
}
func MoveSnakesStandard(b *BoardState, settings Settings, moves []SnakeMove) (bool, error) {
if IsInitialization(b, settings, moves) {
return false, nil
}
// no-op when moves are empty
if len(moves) == 0 {
return false, nil
}
// Sanity check that all non-eliminated snakes have moves and bodies.
for i := 0; i < len(b.Snakes); i++ {
snake := &b.Snakes[i]
if snake.EliminatedCause != NotEliminated {
continue
}
if len(snake.Body) == 0 {
return false, ErrorZeroLengthSnake
}
moveFound := false
for _, move := range moves {
if snake.ID == move.ID {
moveFound = true
break
}
}
if !moveFound {
return false, ErrorNoMoveFound
}
}
for i := 0; i < len(b.Snakes); i++ {
snake := &b.Snakes[i]
if snake.EliminatedCause != NotEliminated {
continue
}
for _, move := range moves {
if move.ID == snake.ID {
appliedMove := move.Move
switch move.Move {
case MoveUp, MoveDown, MoveRight, MoveLeft:
break
default:
appliedMove = getDefaultMove(snake.Body)
}
newHead := Point{}
switch appliedMove {
// Guaranteed to be one of these options given the clause above
case MoveUp:
newHead.X = snake.Body[0].X
newHead.Y = snake.Body[0].Y + 1
case MoveDown:
newHead.X = snake.Body[0].X
newHead.Y = snake.Body[0].Y - 1
case MoveLeft:
newHead.X = snake.Body[0].X - 1
newHead.Y = snake.Body[0].Y
case MoveRight:
newHead.X = snake.Body[0].X + 1
newHead.Y = snake.Body[0].Y
}
// Append new head, pop old tail
snake.Body = append([]Point{newHead}, snake.Body[:len(snake.Body)-1]...)
}
}
}
return false, nil
}
func getDefaultMove(snakeBody []Point) string {
if len(snakeBody) >= 2 {
// Use neck to determine last move made
head, neck := snakeBody[0], snakeBody[1]
// Situations where neck is next to head
if head.X == neck.X+1 {
return MoveRight
} else if head.X == neck.X-1 {
return MoveLeft
} else if head.Y == neck.Y+1 {
return MoveUp
} else if head.Y == neck.Y-1 {
return MoveDown
}
// Consider the wrapped cases using zero axis to anchor
if head.X == 0 && neck.X > 0 {
return MoveRight
} else if neck.X == 0 && head.X > 0 {
return MoveLeft
} else if head.Y == 0 && neck.Y > 0 {
return MoveUp
} else if neck.Y == 0 && head.Y > 0 {
return MoveDown
}
}
return MoveUp
}
func ReduceSnakeHealthStandard(b *BoardState, settings Settings, moves []SnakeMove) (bool, error) {
if IsInitialization(b, settings, moves) {
return false, nil
}
for i := 0; i < len(b.Snakes); i++ {
if b.Snakes[i].EliminatedCause == NotEliminated {
b.Snakes[i].Health = b.Snakes[i].Health - 1
}
}
return false, nil
}
func DamageHazardsStandard(b *BoardState, settings Settings, moves []SnakeMove) (bool, error) {
if IsInitialization(b, settings, moves) {
return false, nil
}
hazardDamage := settings.Int(ParamHazardDamagePerTurn, 0)
for i := 0; i < len(b.Snakes); i++ {
snake := &b.Snakes[i]
if snake.EliminatedCause != NotEliminated {
continue
}
head := snake.Body[0]
for _, p := range b.Hazards {
if head == p {
// If there's a food in this square, don't reduce health
foundFood := false
for _, food := range b.Food {
if p == food {
foundFood = true
}
}
if foundFood {
continue
}
// Snake is in a hazard, reduce health
snake.Health = snake.Health - hazardDamage
if snake.Health < 0 {
snake.Health = 0
}
if snake.Health > SnakeMaxHealth {
snake.Health = SnakeMaxHealth
}
if snakeIsOutOfHealth(snake) {
EliminateSnake(snake, EliminatedByHazard, "", b.Turn+1)
}
}
}
}
return false, nil
}
func EliminateSnakesStandard(b *BoardState, settings Settings, moves []SnakeMove) (bool, error) {
if IsInitialization(b, settings, moves) {
return false, nil
}
// First order snake indices by length.
// In multi-collision scenarios we want to always attribute elimination to the longest snake.
snakeIndicesByLength := make([]int, len(b.Snakes))
for i := 0; i < len(b.Snakes); i++ {
snakeIndicesByLength[i] = i
}
sort.Slice(snakeIndicesByLength, func(i int, j int) bool {
lenI := len(b.Snakes[snakeIndicesByLength[i]].Body)
lenJ := len(b.Snakes[snakeIndicesByLength[j]].Body)
return lenI > lenJ
})
// First, iterate over all non-eliminated snakes and eliminate the ones
// that are out of health or have moved out of bounds.
for i := 0; i < len(b.Snakes); i++ {
snake := &b.Snakes[i]
if snake.EliminatedCause != NotEliminated {
continue
}
if len(snake.Body) <= 0 {
return false, ErrorZeroLengthSnake
}
if snakeIsOutOfHealth(snake) {
EliminateSnake(snake, EliminatedByOutOfHealth, "", b.Turn+1)
continue
}
if snakeIsOutOfBounds(snake, b.Width, b.Height) {
EliminateSnake(snake, EliminatedByOutOfBounds, "", b.Turn+1)
continue
}
}
// Next, look for any collisions. Note we apply collision eliminations
// after this check so that snakes can collide with each other and be properly eliminated.
type CollisionElimination struct {
ID string
Cause string
By string
}
collisionEliminations := []CollisionElimination{}
for i := 0; i < len(b.Snakes); i++ {
snake := &b.Snakes[i]
if snake.EliminatedCause != NotEliminated {
continue
}
if len(snake.Body) <= 0 {
return false, ErrorZeroLengthSnake
}
// Check for self-collisions first
if snakeHasBodyCollided(snake, snake) {
collisionEliminations = append(collisionEliminations, CollisionElimination{
ID: snake.ID,
Cause: EliminatedBySelfCollision,
By: snake.ID,
})
continue
}
// Check for body collisions with other snakes second
hasBodyCollided := false
for _, otherIndex := range snakeIndicesByLength {
other := &b.Snakes[otherIndex]
if other.EliminatedCause != NotEliminated {
continue
}
if snake.ID != other.ID && snakeHasBodyCollided(snake, other) {
collisionEliminations = append(collisionEliminations, CollisionElimination{
ID: snake.ID,
Cause: EliminatedByCollision,
By: other.ID,
})
hasBodyCollided = true
break
}
}
if hasBodyCollided {
continue
}
// Check for head-to-heads last
hasHeadCollided := false
for _, otherIndex := range snakeIndicesByLength {
other := &b.Snakes[otherIndex]
if other.EliminatedCause != NotEliminated {
continue
}
if snake.ID != other.ID && snakeHasLostHeadToHead(snake, other) {
collisionEliminations = append(collisionEliminations, CollisionElimination{
ID: snake.ID,
Cause: EliminatedByHeadToHeadCollision,
By: other.ID,
})
hasHeadCollided = true
break
}
}
if hasHeadCollided {
continue
}
}
// Apply collision eliminations
for _, elimination := range collisionEliminations {
for i := 0; i < len(b.Snakes); i++ {
snake := &b.Snakes[i]
if snake.ID == elimination.ID {
EliminateSnake(snake, elimination.Cause, elimination.By, b.Turn+1)
break
}
}
}
return false, nil
}
func snakeIsOutOfHealth(s *Snake) bool {
return s.Health <= 0
}
func snakeIsOutOfBounds(s *Snake, boardWidth int, boardHeight int) bool {
for _, point := range s.Body {
if (point.X < 0) || (point.X >= boardWidth) {
return true
}
if (point.Y < 0) || (point.Y >= boardHeight) {
return true
}
}
return false
}
func snakeHasBodyCollided(s *Snake, other *Snake) bool {
head := s.Body[0]
for i, body := range other.Body {
if i == 0 {
continue
} else if head.X == body.X && head.Y == body.Y {
return true
}
}
return false
}
func snakeHasLostHeadToHead(s *Snake, other *Snake) bool {
if s.Body[0].X == other.Body[0].X && s.Body[0].Y == other.Body[0].Y {
return len(s.Body) <= len(other.Body)
}
return false
}
func FeedSnakesStandard(b *BoardState, settings Settings, moves []SnakeMove) (bool, error) {
newFood := []Point{}
for _, food := range b.Food {
foodHasBeenEaten := false
for i := 0; i < len(b.Snakes); i++ {
snake := &b.Snakes[i]
// Ignore eliminated and zero-length snakes, they can't eat.
if snake.EliminatedCause != NotEliminated || len(snake.Body) == 0 {
continue
}
if snake.Body[0].X == food.X && snake.Body[0].Y == food.Y {
feedSnake(snake)
foodHasBeenEaten = true
}
}
// Persist food to next BoardState if not eaten
if !foodHasBeenEaten {
newFood = append(newFood, food)
}
}
b.Food = newFood
return false, nil
}
func feedSnake(snake *Snake) {
growSnake(snake)
snake.Health = SnakeMaxHealth
}
func growSnake(snake *Snake) {
if len(snake.Body) > 0 {
snake.Body = append(snake.Body, snake.Body[len(snake.Body)-1])
}
}
// Deprecated: handled by maps.Standard
func SpawnFoodStandard(b *BoardState, settings Settings, moves []SnakeMove) (bool, error) {
if IsInitialization(b, settings, moves) {
return false, nil
}
minimumFood := settings.Int(ParamMinimumFood, 0)
foodSpawnChance := settings.Int(ParamFoodSpawnChance, 0)
numCurrentFood := int(len(b.Food))
if numCurrentFood < minimumFood {
return false, PlaceFoodRandomly(GlobalRand, b, minimumFood-numCurrentFood)
}
if foodSpawnChance > 0 && int(rand.Intn(100)) < foodSpawnChance {
return false, PlaceFoodRandomly(GlobalRand, b, 1)
}
return false, nil
}
func GameOverStandard(b *BoardState, settings Settings, moves []SnakeMove) (bool, error) {
numSnakesRemaining := 0
for i := 0; i < len(b.Snakes); i++ {
if b.Snakes[i].EliminatedCause == NotEliminated {
numSnakesRemaining++
}
}
return numSnakesRemaining <= 1, nil
}