-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
128 lines (115 loc) · 4.08 KB
/
app.js
File metadata and controls
128 lines (115 loc) · 4.08 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
var queen = {
position: {
x: 0,
y: 0
}
};
var board = [
["a8", "b8", "c8", "d8", "e8", "f8", "g8", "h8"],
["a7", "b7", "c7", "d7", "e7", "f7", "g7", "h7"],
["a6", "b6", "c6", "d6", "e6", "f6", "g6", "h6"],
["a5", "b5", "c5", "d5", "e5", "f5", "g5", "h5"],
["a4", "b4", "c4", "d4", "e4", "f4", "g4", "h4"],
["a3", "b3", "c3", "d3", "e3", "f3", "g3", "h3"],
["a2", "b2", "c2", "d2", "e2", "f2", "g2", "h2"],
["a1", "b1", "c1", "d1", "e1", "f1", "g1", "h1"]
];
function move(numSteps, direction) {
var temp;
var temp1;
switch (direction) {
case 'E':
temp = queen.position.y + numSteps;
if (temp > 8 || temp < 0) {
console.log("Out of boundary");
document.write("Out of boundary");
} else {
queen.position.y = temp;
}
break;
case 'W':
temp = queen.position.y - numSteps;
if (temp > 8 || temp < 0) {
console.log("Out of boundary");
document.write("Out of boundary");
} else {
queen.position.y = temp;
}
break;
case 'S':
temp = queen.position.x + numSteps;
if (temp > 8 || temp < 0) {
console.log("Out of boundary");
document.write("Out of boundary");
} else {
queen.position.x = temp;
}
break;
case 'N':
temp = queen.position.x - numSteps;
if (temp > 8 || temp < 0) {
console.log("Out of boundary");
document.write("Out of boundary");
} else {
queen.position.x = temp;
}
break;
case 'SE':
temp = queen.position.x + numSteps;
temp1 = queen.position.y + numSteps;
if ((temp > 8 || temp < 0) && (temp1 > 8 || temp1 < 0)) {
console.log("Out of boundary");
document.write("Out of boundary");
} else {
queen.position.x = temp;
queen.position.y = temp1;
}
break;
case 'NW':
temp = queen.position.x - numSteps;
temp1 = queen.position.y - numSteps;
if ((temp > 8 || temp < 0) && (temp1 > 8 || temp1 < 0)) {
console.log("Out of boundary");
document.write("Out of boundary");
} else {
queen.position.x = temp;
queen.position.y = temp1;
}
break;
case 'SW':
temp = queen.position.x + numSteps;
temp1 = queen.position.y - numSteps;
if ((temp > 8 || temp < 0) && (temp1 > 8 || temp1 < 0)) {
console.log("Out of boundary");
document.write("Out of boundary");
} else {
queen.position.x = temp;
queen.position.y = temp1;
}
break;
case 'NE':
temp = queen.position.x - numSteps;
temp1 = queen.position.y + numSteps;
if ((temp > 8 || temp < 0) && (temp1 > 8 || temp1 < 0)) {
console.log("Out of boundary");
document.write("Out of boundary");
} else {
queen.position.x = temp;
queen.position.y = temp1;
}
break;
default:
document.write("Invalid output");
}
}
function play() {
var value = document.getElementById("position").value;
var input = value.split(" ");
input.forEach(num => {
let number = Number(num.slice(num.length - 1));
let dir = num.slice(0, num.length - 1);
move(number, dir);
})
console.log("Queen's current position is -> " + board[queen.position.x][queen.position.y]);
document.write("Queen's current position is : " + board[queen.position.x][queen.position.y]);
}