-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay9.js
46 lines (44 loc) · 1.35 KB
/
Day9.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
function moveTrain(board, mov) {
let actions = {"·" : "none", "*" : "eat", "o" : "crash"}
//Localizamos la cabeza
let i = 0
let j = 0
while(i < board.length){
while(j < board[i].length){
if(board[i][j] =="@"){
switch (mov){
case "D":
if((i + 1) == board.length)
return "crash"
else
return actions[board[i + 1][j]]
case "U":
if((i - 1) < 0)
return "crash"
else
return actions[board[i - 1][j]]
case "R":
if((j + 1) == board[i].length)
return "crash"
else
return actions[board[i][j + 1]]
case "L":
if((j - 1) < 0)
return "crash"
else
return actions[board[i][j - 1]]
default:
return "error"
}
}
j += 1
}
j = 0
i+=1
}
}
const board = ['·····', '*····', '@····', 'o····', 'o····']
console.log(moveTrain(board, 'U'))
console.log(moveTrain(board, 'D'))
console.log(moveTrain(board, 'L'))
console.log(moveTrain(board, 'R'))