-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.go
134 lines (108 loc) · 3.06 KB
/
game.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
package main
import (
"context"
"fmt"
"image"
"time"
"github.com/golang-cz/snake/proto"
)
func (s *Server) Run(ctx context.Context) error {
go s.generateFood()
for i := 0; i < 3; i++ {
s.createSnake("AI")
}
ticker := time.NewTicker(75 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ticker.C:
if err := s.gameTick(); err != nil {
return fmt.Errorf("advancing the game: %w", err)
}
}
}
}
func (s *Server) gameTick() error {
s.mu.Lock()
defer s.mu.Unlock()
grid := s.currentGrid()
defer fmt.Println(grid)
s.generateSnakeTurns(grid)
// Move snakes.
for _, snake := range s.state.Snakes {
next := &proto.Square{}
if len(snake.NextDirections) > 0 {
snake.Direction = snake.NextDirections[0]
snake.NextDirections = snake.NextDirections[1:]
}
switch *snake.Direction {
case proto.Direction_up:
next.X = snake.Body[0].X
next.Y = min(snake.Body[0].Y-1, s.state.Height-1)
case proto.Direction_down:
next.X = snake.Body[0].X
next.Y = min(snake.Body[0].Y+1) % s.state.Height
case proto.Direction_left:
next.X = min(snake.Body[0].X-1, s.state.Width-1)
next.Y = snake.Body[0].Y
case proto.Direction_right:
next.X = min(snake.Body[0].X+1) % s.state.Width
next.Y = snake.Body[0].Y
}
nextPoint := image.Point{X: int(next.X), Y: int(next.Y)}
switch grid.get(squareToPoint(next)) {
case '*':
// Move snake's head.
grid.put(nextPoint, snakeRune(snake.Id))
snake.Body = append([]*proto.Square{next}, snake.Body...)
// Remove food.
for _, item := range s.state.Items {
if item.Body.X == next.X && item.Body.Y == next.Y {
delete(s.state.Items, item.Id)
}
}
case ' ', '.', snakeRune(snake.Id):
// Move snake's head & remove tail.
tailPoint := squareToPoint(snake.Body[len(snake.Body)-1])
grid.put(tailPoint, ' ')
grid.put(nextPoint, snakeRune(snake.Id))
snake.Body = append([]*proto.Square{next}, snake.Body[:len(snake.Body)-1]...)
default:
// Crashed into another snake.
delete(s.state.Snakes, snake.Id)
// Create food from snake's body.
for i, square := range snake.Body {
if i%2 == 0 {
s.state.Items[s.lastItemId] = &proto.Item{
Id: s.lastItemId,
Color: "red",
Body: square,
}
s.lastItemId++
}
}
// Reborn AI.
if snake.Name == "AI" {
go func() {
<-time.After(10 * time.Second)
s.createSnake("AI")
}()
}
}
}
return s.sendState(s.state)
}
// TODO: We send the whole state on each update. Optimize to send events (diffs) only.
func (s *Server) sendState(state *proto.State) error {
for _, sub := range s.subs {
sub := sub
go func() {
sub <- state
}()
}
return nil
}
var runes = []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}
func snakeRune(snakeId uint64) rune {
return runes[int(snakeId)%len(runes)]
}