forked from omiras/solved-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
catch-the-mouse-2-D.js
49 lines (33 loc) · 1.32 KB
/
catch-the-mouse-2-D.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
function catMouse(map, moves) {
// romper el string literal usando como el separador '\n'
let rows = map.split("\n");
console.log("Array", rows)
// findIndex: https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Array/findIndex
let rowCat = rows.findIndex((element) => {
// string includes: https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/String/includes
return element.includes("C");
});
let rowMouse = rows.findIndex((element) => {
return element.includes("m");
});
console.log('rowCat:', rowCat)
console.log('rowMouse:', rowMouse)
if (rowCat==-1 ||rowMouse==-1) {
return "boring without two animals"
}
// Distancia entre el gato y el ratón a nivel de filas
let rowDistance = (Math.abs(rowCat-rowMouse))
// Distancia entre gato y el raton entre columnas
let columnDistance = Math.abs(rows[rowCat].indexOf('C') - rows[rowMouse].indexOf('m'))
console.log('rowDistance:', rowDistance)
console.log('columDistance:', columnDistance)
if (rowDistance + columnDistance > moves) {
return "Escaped!"
}
return "Caught!"
}
map = `.........
.........
..C.m....`;
let res = catMouse(map, 5);
console.log(res);