-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueenCheck.js
89 lines (72 loc) · 2.77 KB
/
QueenCheck.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
Have the function QueenCheck(strArr) read strArr which will be an array consisting of the locations of a Queen and King on a standard 8x8 chess board with no other pieces on the board. The structure of strArr will be the following: ["(x1,y1)","(x2,y2)"] with (x1,y1) representing the position of the queen and (x2,y2) representing the location of the king with x and y ranging from 1 to 8. Your program should determine if the king is in check based on the current positions of the pieces, and if so, return the number of spaces it can move to in order to get out of check. If the king is not in check, return -1. For example: if strArr is ["(4,4)","(6,6)"] then your program should output 6. Remember, because only the queen and king are on the board, if the queen is checking the king by being directly adjacent to it, technically the king can capture the queen.
Advanced challenges are worth 15 points and you are not timed for them. Use the Parameter Testing feature in the box below to test your code with different arguments.
*/
var strArr = ["(1,1)","(1,4)"];
//8x8 board, 1-8
var queen = strArr[0].match(/[0-9]{1}/g);
var king = strArr[1].match(/[0-9]{1}/g);
for (var i=0; i<2; i++) {
queen[i] = Number(queen[i]);
king[i] = Number(king[i]);
}
function ValidPosition(array) {
if (array[0] < 1 || array[0] > 8) {
return false;
}
if (array[1] < 1 || array[1] > 8) {
return false;
}
return true;
}
function KingsMoves(array) {
var moves = [];
if (ValidPosition([array[0],array[1]+1])) {
moves.push([array[0],array[1]+1]);
}
if (ValidPosition([array[0]+1,array[1]+1])) {
moves.push([array[0]+1,array[1]+1]);
}
if (ValidPosition([array[0]+1,array[1]])) {
moves.push([array[0]+1,array[1]]);
}
if (ValidPosition([array[0]+1,array[1]-1])) {
moves.push([array[0]+1,array[1]-1]);
}
if (ValidPosition([array[0],array[1]-1])) {
moves.push([array[0],array[1]-1]);
}
if (ValidPosition([array[0]-1,array[1]-1])) {
moves.push([array[0]-1,array[1]-1]);
}
if (ValidPosition([array[0]-1,array[1]])) {
moves.push([array[0]-1,array[1]]);
}
if (ValidPosition([array[0]-1,array[1]+1])) {
moves.push([array[0]-1,array[1]+1]);
}
return moves;
}
if (king[0] != queen[0] && king[1] != queen[1] && Math.abs(king[0]-queen[0]) != Math.abs(king[1]-queen[1])) {
return '-1'
}
var kingPositions = KingsMoves(king);
var notAllowed = 0;
for (var i=0; i<kingPositions.length; i++) {
if (kingPositions[i][0] == queen[0] && kingPositions[i][1] == queen[1]) {
return '-1';
}
if (kingPositions[i][0] == queen[0]) {
notAllowed += 1;
continue;
}
if (kingPositions[i][1] == queen[1]) {
notAllowed += 1;
continue;
}
if (Math.abs(kingPositions[i][0]-queen[0]) == Math.abs(kingPositions[i][1]-queen[1])) {
notAllowed += 1;
continue;
}
}
return kingPositions.length-notAllowed;