onix-chess
Version:
Onix chess library
245 lines (224 loc) • 8.43 kB
text/typescript
import { Directions, Squares } from '../types/Types'
export namespace Direction {
const ns: Squares.Empty = undefined;
const NULL_DIR: Directions.Null = 0;
const UP: Directions.Up = 1;
const DOWN: Directions.Down = 2;
const LEFT: Directions.Left = 4;
const UP_LEFT: Directions.UpLeft = 5;
const DOWN_LEFT: Directions.DownLeft = 6;
const RIGHT: Directions.Right = 8;
const UP_RIGHT: Directions.UpRight = 9;
const DOWN_RIGHT: Directions.DownRight = 10;
/* tslint:enable:no-bitwise */
/**
* Opposite move's directions.
*/
const __dirOpposite: Directions.Direction[] = [
NULL_DIR,
DOWN, // opposite of UP (1)
UP, // opposite of DOWN (2)
NULL_DIR,
RIGHT, // opposite of LEFT (4)
DOWN_RIGHT, // opposite of UP_LEFT (5)
UP_RIGHT, // opposite of DOWN_LEFT (6)
NULL_DIR,
LEFT, // opposite of RIGHT (8)
DOWN_LEFT, // opposite of UP_RIGHT (9)
UP_LEFT // opposite of DOWN_RIGHT (10)
];
/**
* Slider moves flags.
*/
const __dirIsDiagonal: boolean[] = [
false, // 0 = for NULL_DIR
false, // 1 = for UP
false, // 2 = for DOWN
false, // 3 = invalid
false, // 4 = for LEFT
true, // 5 = for UP_LEFT
true, // 6 = for DOWN_LEFT
false, // 7 = invalid
false, // 8 = for RIGHT
true, // 9 = for UP_RIGHT
true // 10 = for DOWN_RIGHT
];
/**
* Movment distance
*/
const __dirDelta: number[] = [
0, // delta for NULL_DIR
8, // delta for UP
-8, // delta for DOWN
0, // invalid
-1, // delta for LEFT
7, // delta for UP_LEFT
-9, // delta for DOWN_LEFT
0, // invalid
1, // delta for RIGHT
9, // delta for UP_RIGHT
-7 // delta for DOWN_RIGHT
];
/**
* Allowed moves for squares.
*/
const __squareMoves: (Squares.Square | Squares.Empty)[][] = [
[], // A1
[], // A2
[], // A3
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[],
[]]; // H8
const squareMoves = (sq: Squares.Square, dir: Directions.Direction) => {
return __squareMoves[sq][dir];
};
/**
* Matrix for movment.
*/
const __sqDir: Directions.Direction[][] = [];
/**
* Initialize matrix.
*/
function initializeDirections(): void {
const __dirArray = [
UP,
DOWN,
LEFT,
RIGHT,
UP_LEFT,
UP_RIGHT,
DOWN_LEFT,
DOWN_RIGHT,
NULL_DIR
];
// Fill matrix with null direction
for (let i = 0; i <= 65; i++) {
__sqDir[i] = [];
for (let j = 0; j <= 65; j++) {
__sqDir[i][j] = NULL_DIR;
}
}
// Make allowed movment now
let l: Squares.Square | Squares.Empty;
for (let k = 0; k <= 63; k++) {
let x = 0;
while (__dirArray[x] !== NULL_DIR) {
l = squareMove((k as Squares.Square), __dirArray[x]);
while (l !== ns) {
__sqDir[k][l] = __dirArray[x];
l = squareMove(l, __dirArray[x]);
}
x++;
}
}
}
// Call initialization script.
(function () {
initializeDirections();
})();
/**
* Piece movment
*/
// None
export const Null: Directions.Null = NULL_DIR;
// Up
export const Up: Directions.Up = UP;
// Down
export const Down: Directions.Down = DOWN;
// Left
export const Left: Directions.Left = LEFT;
// Right
export const Right: Directions.Right = RIGHT;
// Up/Left
export const UpLeft: Directions.UpLeft = UP_LEFT;
// Up/Right
export const UpRight: Directions.UpRight = UP_RIGHT;
// Down/Left
export const DownLeft: Directions.DownLeft = DOWN_LEFT;
// Down/Right
export const DownRight: Directions.DownRight = DOWN_RIGHT;
/**
* Return opposite movment
*/
export function opposite(d: Directions.Direction): Directions.Direction {
return __dirOpposite[d];
}
/**
* Return true, if movment is diagonal
*/
export function isDiagonal(d: Directions.Direction) {
return __dirIsDiagonal[d];
}
/**
* Return distance for movment.
*/
export function delta(d: Directions.Direction) {
return __dirDelta[d];
}
export function squareDirection(fr: Squares.Square, to: Squares.Square): Directions.Direction {
return __sqDir[fr][to];
}
export function squareMove(sq: Squares.Square, dir: Directions.Direction): Squares.Square | Squares.Empty {
return squareMoves(sq, dir);
}
}