forked from trekhleb/javascript-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 8
/
QueenPosition.js
39 lines (36 loc) · 1.07 KB
/
QueenPosition.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
/**
* Class that represents queen position on the chessboard.
*/
export default class QueenPosition {
/**
* @param {number} rowIndex
* @param {number} columnIndex
*/
constructor(rowIndex, columnIndex) {
this.rowIndex = rowIndex;
this.columnIndex = columnIndex;
}
/**
* @return {number}
*/
get leftDiagonal() {
// Each position on the same left (\) diagonal has the same difference of
// rowIndex and columnIndex. This fact may be used to quickly check if two
// positions (queens) are on the same left diagonal.
// @see https://youtu.be/xouin83ebxE?t=1m59s
return this.rowIndex - this.columnIndex;
}
/**
* @return {number}
*/
get rightDiagonal() {
// Each position on the same right diagonal (/) has the same
// sum of rowIndex and columnIndex. This fact may be used to quickly
// check if two positions (queens) are on the same right diagonal.
// @see https://youtu.be/xouin83ebxE?t=1m59s
return this.rowIndex + this.columnIndex;
}
toString() {
return `${this.rowIndex},${this.columnIndex}`;
}
}