-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame-logic.test.ts
214 lines (198 loc) · 5.51 KB
/
game-logic.test.ts
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
import test from 'ava'
import { GameLogicState, Direction } from 'models'
const { handleGameTick: _handleGameTick } = require(process.env.NEXT_PUBLIC_USE_SOLUTION === 'true' ? 'solution' : 'game-logic')
const clone = obj => JSON.parse(JSON.stringify(obj))
const handleGameTick = (state: GameLogicState) => {
const copiedState = clone(state)
return _handleGameTick(copiedState)
}
const initialState: GameLogicState = {
food: {
x: 20,
y: 20
},
boardSize: {
columns: 40,
rows: 40
},
snake: [{
x: 7,
y: 20
}, {
x: 6,
y: 20
}, {
x: 5,
y: 20
}],
currentDirection: Direction.Up,
dead: false,
score: 0
}
test('Move the snake > Up when direction is "Up"', t => {
const state = handleGameTick(initialState)
const correctSnakePosition = [...initialState.snake]
correctSnakePosition.unshift({
x: initialState.snake[0].x,
y: initialState.snake[0].y - 1
})
correctSnakePosition.pop()
t.deepEqual(state.snake, correctSnakePosition)
})
test('Move the snake > Right when direction is "Right"', t => {
const state = handleGameTick({
...initialState,
currentDirection: Direction.Right
})
const correctSnakePosition = [...initialState.snake]
correctSnakePosition.unshift({
x: initialState.snake[0].x + 1,
y: initialState.snake[0].y
})
correctSnakePosition.pop()
t.deepEqual(state.snake, correctSnakePosition)
})
test('Move the snake > Down when direction is "Down"', t => {
const state = handleGameTick({
...initialState,
currentDirection: Direction.Down
})
const correctSnakePosition = [...initialState.snake]
correctSnakePosition.unshift({
x: correctSnakePosition[0].x,
y: correctSnakePosition[0].y + 1
})
correctSnakePosition.pop()
t.deepEqual(state.snake, correctSnakePosition)
})
test('Move the snake > Left when direction is "Left"', t => {
const state = handleGameTick({
...initialState,
snake: initialState.snake.sort(() => -1),
currentDirection: Direction.Left
})
const correctSnakePosition = [...initialState.snake]
correctSnakePosition.unshift({
x: correctSnakePosition[0].x - 1,
y: correctSnakePosition[0].y
})
correctSnakePosition.pop()
t.deepEqual(state.snake, correctSnakePosition)
})
test('Die when the snake is hitting a wall > Upper wall', t => {
const state = handleGameTick({
...initialState,
snake: [{
x: 0,
y: 0
}]
})
t.true(state.dead)
})
test('Die when the snake is hitting a wall > Right wall', t => {
const state = handleGameTick({
...initialState,
currentDirection: Direction.Right,
snake: [{
x: initialState.boardSize.columns,
y: 0
}]
})
t.true(state.dead)
})
test('Die when the snake is hitting a wall > Lower wall', t => {
const state = handleGameTick({
...initialState,
currentDirection: Direction.Down,
snake: [{
x: 0,
y: initialState.boardSize.columns
}]
})
t.true(state.dead)
})
test('Die when the snake is hitting a wall > Left wall', t => {
const state = handleGameTick({
...initialState,
currentDirection: Direction.Left,
snake: [{
x: 0,
y: 0
}]
})
t.true(state.dead)
})
test('Die when the snake is hitting itself', t => {
const state = handleGameTick({
...initialState,
snake: [{
x: 2,
y: 2
}, {
x: 1,
y: 2
}, {
x: 1,
y: 1
}, {
x: 2,
y: 1
}, {
x: 3,
y: 1
}]
})
t.true(state.dead)
})
test('Grow the snake when it\'s eating food', t => {
const state = handleGameTick({
...initialState,
food: {
x: initialState.snake[0].x,
y: initialState.snake[0].y - 1
}
})
const correctSnakePosition = [...initialState.snake]
correctSnakePosition.unshift({
x: correctSnakePosition[0].x,
y: correctSnakePosition[0].y - 1
})
t.deepEqual(state.snake, correctSnakePosition)
})
test('Raise score when the snake is eating food', t => {
const state = handleGameTick({
...initialState,
food: {
x: initialState.snake[0].x,
y: initialState.snake[0].y - 1
}
})
t.is(state.score, initialState.score + 1)
})
test('Generate new food when it has been eaten > Food is not underneath the snake', t => {
for (let i = 0; i <= 10000; i++) {
const state = handleGameTick({
...initialState,
food: {
x: initialState.snake[0].x,
y: initialState.snake[0].y - 1
}
})
state.snake.forEach(snakeCoordinate => t.notDeepEqual(state.food, snakeCoordinate))
}
})
test('Generate new food when it has been eaten > Food is within the game board', t => {
for (let i = 0; i <= 10000; i++) {
const state = handleGameTick({
...initialState,
food: {
x: initialState.snake[0].x,
y: initialState.snake[0].y - 1
}
})
t.false(state.food.x < 0)
t.false(state.food.x > initialState.boardSize.columns)
t.false(state.food.y < 0)
t.false(state.food.y > initialState.boardSize.rows)
}
})