Skip to content

Commit 31889a1

Browse files
committed
Fix bug with balls being wrongfully removed
This was causing the game to continue while it should show a gameover screen.
1 parent 56450b9 commit 31889a1

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

components/GameBoard.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ const GameOverScreen = ({ gameover }) => (
3131
fontFamily: 'Open sans, sans-serif',
3232
whiteSpace: 'nowrap',
3333
color: '#d50000',
34-
fontWeight: 'bold'
34+
fontWeight: 'bold',
35+
zIndex: '2',
36+
background: '#fff',
37+
padding: '5px 20px',
38+
border: '4px solid rgb(213, 0, 0)'
3539
}}
3640
>
3741
GAME OVER

lib/game.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,13 @@ const game = (getBallLevel, sleepFunction, gameBoard) => {
176176
return [...dots]
177177
}
178178

179-
function cleanupDots (dots, cb) {
179+
function cleanupDots (dots) {
180180
const map = {}
181181
const keep = {}
182182
for (let i = 0; i < 7; i++) {
183183
map[i] = {}
184184
keep[i] = {}
185-
for (let j = -1; j < 7; j++) {
185+
for (let j = -2; j < 7; j++) {
186186
map[i][j] = null
187187
keep[i][j] = true
188188
}
@@ -198,9 +198,9 @@ const game = (getBallLevel, sleepFunction, gameBoard) => {
198198
map[d.x >= 6 ? 6 : d.x + 1][d.y],
199199
map[d.x <= 0 ? 0 : d.x - 1][d.y],
200200
map[d.x][d.y >= 6 ? 6 : d.y + 1],
201-
map[d.x][d.y <= 0 ? 0 : d.y - 1]
201+
map[d.x][d.y <= -1 ? -1 : d.y - 1]
202202
]
203-
.filter(n => n && n.type === d.type)
203+
.filter(n => n && n.type === d.type && n.key !== d .key)
204204
.reduce((l, d) => l.concat(checkTile(d)), [d])
205205
}
206206

@@ -223,7 +223,7 @@ const game = (getBallLevel, sleepFunction, gameBoard) => {
223223
ball(newColoredDot.x, newColoredDot.y, newColoredDot.type + 1)
224224
)
225225
}
226-
l.forEach((d, i) => {
226+
l.forEach((d) => {
227227
keep[d.x][d.y] = false
228228
updateScore(state.score + (newColoredDot.type + 1) * 5)
229229
})

tests/game.test.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@ const visualizeBalls = balls => {
2424
}
2525

2626
const compareBoard = (t, board, game, msg) => {
27+
if (board === 'gameover') {
28+
t.deepEqual(game.gameover(), true, `${msg} is gameover`)
29+
return
30+
}
2731
const output = visualizeBalls(game.balls()).split('\n')
28-
t.deepEqual(board.trim().split('\n'), output, msg)
32+
t.deepEqual(output, board.trim().split('\n'), msg)
2933
}
3034

31-
const testCommands = t => (commands, board) => {
32-
const g = game(l => l, cb => cb())
35+
const testCommands = t => (commands, board, initBoard) => {
36+
const g = game(l => l, cb => cb(), initBoard)
3337
commands.split('').forEach(command => {
3438
switch (command) {
3539
case 'l':
@@ -445,6 +449,21 @@ test('it executes commands correctly', t => {
445449
`
446450
)
447451

452+
executeCommands(
453+
'ud',
454+
`gameover`,
455+
`
456+
.....0.
457+
.....3.
458+
.....3.
459+
.....2.
460+
.....2.
461+
....33.
462+
..3.102
463+
`.trim()
464+
)
465+
466+
448467
t.end()
449468
})
450469

0 commit comments

Comments
 (0)