-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
26 lines (20 loc) · 674 Bytes
/
index.js
File metadata and controls
26 lines (20 loc) · 674 Bytes
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
function moveTrain(board, mov) {
const directions = {
U: (row, col) => board[row - 1]?.[col],
D: (row, col) => board[row + 1]?.[col],
R: (row, col) => board[row][col + 1],
L: (row, col) => board[row][col - 1],
};
const flatBoard = board.join('');
const trainHeadIndex = flatBoard.indexOf('@');
const totalColumns = board[0].length;
const currentRow = Math.floor(trainHeadIndex / totalColumns);
const currentColumn = trainHeadIndex % totalColumns;
const nextCell = directions[mov](currentRow, currentColumn);
const results = {
'*': 'eat',
'·': 'none',
};
return results[nextCell] || 'crash';
}
module.exports = moveTrain;