/
snake.go
57 lines (46 loc) · 1.17 KB
/
snake.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
package main
import (
log "github.com/sirupsen/logrus"
"time"
"math/rand"
)
type Snake struct {
Name string
GameSettings GameSettings
ID string
}
func (snake Snake) GetNextMove(m Map) string {
directions := []Direction{Up, Down, Left, Right}
var possibleDirections []Direction
for _, direction := range directions {
if m.CanSnakeMoveInDirection(snake.ID, direction) {
possibleDirections = append(possibleDirections, direction)
}
}
nbrOfDirections := len(possibleDirections)
if nbrOfDirections <= 0 {
return Down.Name
}
return possibleDirections[rand.Intn(nbrOfDirections)].Name
}
func (snake *Snake) OnPlayerRegistered(s GameSettings, snakeID string) {
snake.GameSettings = s
snake.ID = snakeID
log.Debug("Player registered.")
}
func (snake Snake) OnSnakeDead(reason string) {
log.WithField("reason", reason).Debug("The snake died")
}
func (snake Snake) OnGameStarting() {
rand.Seed(time.Now().Unix())
log.Debug("All snakes are ready to rock. Game is starting.")
}
func (snake Snake) OnInvalidPlayername(reasonCode int) {
log.Debug("Player name is invalid.")
}
func GetSnake() Snake {
return Snake{
Name: "golang",
ID: "",
}
}