forked from mangin/HW0
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.js
More file actions
85 lines (84 loc) · 2.38 KB
/
Copy path1.js
File metadata and controls
85 lines (84 loc) · 2.38 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*global prompt,console*/
function Batman() {
'use strict';
var inputs = prompt("W H", "4 8").split(' '),
W = parseInt(inputs[0], 10),
H = parseInt(inputs[1], 10),
N = parseInt(prompt("Maximum number of jumps", "10"), 10),
DefaultDirArr = ["DR", "D", "D"], // Only for test
I = 0, // Iterator for test values
X0,
Y0,
X,
Y,
XMax,
YMax,
XMin,
YMin,
BOMB_DIR;
inputs = prompt("X0, Y0", "2 3").split(' ');
X0 = parseInt(inputs[0], 10);
Y0 = parseInt(inputs[1], 10);
X = X0;
Y = Y0;
XMax = W;
YMax = H;
XMin = 0;
YMin = 0;
console.warn(N); //only for JSLint(Unused variable)
while (I < 3) { //For test directions. Must be X,Y is not equal bomb coordinates.
//BOMB_DIR = prompt("direction U, UR, R, DR, D, DL, L or UL", " "); //for input
BOMB_DIR = DefaultDirArr[I]; //Using test values
I = I + 1;
switch (BOMB_DIR) {
case "U":
YMax = Y;
Y = Math.floor((Y + YMin) / 2);
break;
case "D":
YMin = Y + 1;
Y = Math.floor((Y + YMax) / 2);
break;
case "L":
XMax = X;
X = Math.floor((X + XMin) / 2);
break;
case "R":
XMin = X + 1;
X = Math.floor((X + XMax) / 2);
break;
case "UR":
YMax = Y;
XMin = X + 1;
Y = Math.floor((Y + YMin) / 2);
X = Math.floor((X + XMax) / 2);
break;
case "UL":
YMax = Y;
XMax = X;
Y = Math.floor((Y + YMin) / 2);
X = Math.floor((X + XMin) / 2);
break;
case "DL":
YMin = Y + 1;
XMax = X;
Y = Math.floor((Y + YMax) / 2);
X = Math.floor((X + XMin) / 2);
break;
case "DR":
YMin = Y + 1;
XMin = X + 1;
Y = Math.floor((Y + YMax) / 2);
X = Math.floor((X + XMax) / 2);
break;
default:
console.error('Unknown value');
break;
}
if (X < 0 || Y < 0 || X >= W || Y >= H) {
console.error('Out of building');
} else {
console.log(X, Y); // the location of the next window Batman should jump to.
}
}
}