-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (46 loc) · 1.8 KB
/
index.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
const goTurtle = (settings, moves) => {
const { turtle } = settings;
const initialStatus = checkResult(settings, turtle);
if (initialStatus) return initialStatus;
const finalStatus = doAction(settings, turtle, moves);
if (finalStatus) return finalStatus;
return 'Still in danger';
};
const checkResult = (settings, turtle) => {
const { board, exit, mines } = settings;
if (isOutOfBounds(board, turtle)) return 'Out of bounds';
if (isSamePlace(turtle)(exit)) return 'Success';
if (mines.filter(isSamePlace(turtle)).length) return 'Mine hit';
return '';
};
const isOutOfBounds = (board, { x, y }) =>
x < 1 || y < 1 || x > board.col || y > board.row;
const isSamePlace = (thing1) => (thing2) =>
thing1.x === thing2.x && thing1.y === thing2.y;
const doAction = (settings, turtle, moves) => {
const newTurtle = moves[0] === 'm' ? move(turtle) : rotate(turtle);
const newMoves = moves.slice(1);
const status = checkResult(settings, newTurtle);
if (status || !newMoves.length) return status;
return doAction(settings, newTurtle, newMoves);
};
const move = (turtle) => {
const { x, y, direction } = turtle;
switch (direction) {
case 'north': return set(turtle, 'y', y - 1);
case 'east': return set(turtle, 'x', x + 1);
case 'south': return set(turtle, 'y', y + 1);
case 'west': return set(turtle, 'x', x - 1);
}
};
const rotate = (turtle) => {
switch (turtle.direction) {
case 'north': return set(turtle, 'direction', 'east');
case 'east': return set(turtle, 'direction', 'south');
case 'south': return set(turtle, 'direction', 'west');
case 'west': return set(turtle, 'direction', 'north');
}
};
const set = (turtle, key, value) => Object.assign({}, turtle, { [key]:value });
// Init
document.getElementById('result').innerHTML = goTurtle(settings, moves);