-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.js
64 lines (53 loc) · 1.66 KB
/
solution.js
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
function canMouseEat(direction, game) {
let mouseEats = false
const gameMaxX = game[0].length
const gameMaxY = game.length
game.forEach((row, index) => {
if (row.indexOf('m') > -1) {
const mouse = [index, row.indexOf('m')]
let eat = null
switch (direction) {
case 'up':
eat = [mouse[0] - 1, mouse[1]]
break
case 'down':
eat = [mouse[0] + 1, mouse[1]]
break
case 'left':
eat = [mouse[0], mouse[1] - 1]
break
case 'right':
eat = [mouse[0], mouse[1] + 1]
break
}
const validEatX = eat[1] >= 0 && eat[1] < gameMaxX
const validEatY = eat[0] >= 0 && eat[0] < gameMaxY
mouseEats = validEatX && validEatY && game[eat[0]][eat[1]] === '*'
}
})
return mouseEats
}
// const room = [
// [' ', ' ', ' '],
// [' ', ' ', 'm'],
// [' ', ' ', '*'],
// ]
// console.log(canMouseEat('up', room)) // false
// console.log(canMouseEat('down', room)) // true
// console.log(canMouseEat('right', room)) // false
// console.log(canMouseEat('left', room)) // false
// const room2 = [
// ['*', ' ', ' ', ' '],
// [' ', 'm', '*', ' '],
// [' ', ' ', ' ', ' '],
// [' ', ' ', ' ', '*'],
// ]
// console.log(canMouseEat('up', room2)) // false
// console.log(canMouseEat('down', room2)) // false
// console.log(canMouseEat('right', room2)) // true
// console.log(canMouseEat('left', room2)) // false
const room3 = [['*', 'm']]
console.log(canMouseEat('up', room3)) // false
// console.log(canMouseEat('down', room3)) // false
// console.log(canMouseEat('right', room3)) // false
// console.log(canMouseEat('left', room3)) // false