Prioritize self-collisions over other collisions. Fixes #16.

This commit is contained in:
Brad Van Vugt 2020-07-21 17:11:12 -07:00
parent c74436e709
commit 5aec70de2b
3 changed files with 103 additions and 7 deletions

View file

@ -349,15 +349,21 @@ func (r *StandardRuleset) maybeEliminateSnakes(b *BoardState) error {
continue
}
// Always check body collisions before head-to-heads
// Check for self-collisions first
if r.snakeHasBodyCollided(snake, snake) {
snake.EliminatedCause = EliminatedBySelfCollision
snake.EliminatedBy = snake.ID
continue
}
// Check for body collisions with other snakes second
for _, otherIndex := range snakeIndicesByLength {
other := &b.Snakes[otherIndex]
if snake.ID == other.ID {
continue
}
if r.snakeHasBodyCollided(snake, other) {
if snake.ID == other.ID {
snake.EliminatedCause = EliminatedBySelfCollision
} else {
snake.EliminatedCause = EliminatedByCollision
}
snake.EliminatedCause = EliminatedByCollision
snake.EliminatedBy = other.ID
break
}
@ -366,7 +372,7 @@ func (r *StandardRuleset) maybeEliminateSnakes(b *BoardState) error {
continue
}
// Always check head-to-heads after body collisions
// Check for head-to-heads last
for _, otherIndex := range snakeIndicesByLength {
other := &b.Snakes[otherIndex]
if snake.ID != other.ID && r.snakeHasLostHeadToHead(snake, other) {