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
|
package snake
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestSnake(t *testing.T) {
scenarios := []struct {
state State
expectedState State
expectedAlive bool
}{
{
state: State{
snakePositions: []Position{{x: 5, y: 5}},
direction: Right,
lastTickDirection: Right,
foodPosition: Position{x: 9, y: 9},
},
expectedState: State{
snakePositions: []Position{{x: 6, y: 5}},
direction: Right,
lastTickDirection: Right,
foodPosition: Position{x: 9, y: 9},
},
expectedAlive: true,
},
{
state: State{
snakePositions: []Position{{x: 5, y: 5}, {x: 4, y: 5}, {x: 4, y: 4}, {x: 5, y: 4}},
direction: Up,
lastTickDirection: Up,
foodPosition: Position{x: 9, y: 9},
},
expectedState: State{},
expectedAlive: false,
},
{
state: State{
snakePositions: []Position{{x: 5, y: 5}},
direction: Right,
lastTickDirection: Right,
foodPosition: Position{x: 6, y: 5},
},
expectedState: State{
snakePositions: []Position{{x: 6, y: 5}, {x: 5, y: 5}},
direction: Right,
lastTickDirection: Right,
foodPosition: Position{x: 8, y: 8},
},
expectedAlive: true,
},
}
for _, scenario := range scenarios {
game := NewGame(10, 10, nil, func(string) {})
game.randIntFn = func(int) int { return 8 }
state, alive := game.tick(scenario.state)
assert.Equal(t, scenario.expectedAlive, alive)
assert.EqualValues(t, scenario.expectedState, state)
}
}
|