From 0069e90bd3792dd486eaee4d4b4d37c7a10d1a5f Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 25 Dec 2024 11:16:06 +0000 Subject: [PATCH] chore: remove international --- .gitignore | 1 + src/international/mask.ts | 61 ------- src/international/move-generation.ts | 248 --------------------------- src/international/utils.ts | 28 --- 4 files changed, 1 insertion(+), 337 deletions(-) delete mode 100644 src/international/mask.ts delete mode 100644 src/international/move-generation.ts delete mode 100644 src/international/utils.ts diff --git a/.gitignore b/.gitignore index 3b0b403..30d049d 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ node_modules dist dist-ssr *.local +src/international/* # Editor directories and files .vscode/* diff --git a/src/international/mask.ts b/src/international/mask.ts deleted file mode 100644 index d9bff00..0000000 --- a/src/international/mask.ts +++ /dev/null @@ -1,61 +0,0 @@ -import { - InternationalDraughtsBitSquare as S, - bitwiseOr as or, -} from './utils'; - -/* We use the following bitboard layout for International Draughts - * - * [57][58][59][60][61][62] - * [63] 00, 01, 02, 03, 04, - * 05, 06, 07, 08, 09, [10] - * [10] 11, 12, 13, 14, 15, - * 16, 17, 18, 19, 20, [21] - * [21] 22, 23, 24, 25, 26, - * 27, 28, 29, 30, 31, [32] - * [32] 33, 34, 35, 36, 37, - * 38, 39, 40, 41, 42, [43] - * [43] 44, 45, 46, 47, 48, - * 49, 50, 51, 52, 53, [54] - * [55][56][57][58][59][60] - * - * Access the uint32 value of a square with S[n] - * - * Squares encased in brackets, such as [57], are invalid. - * A move forward to the left is a rotate right 6 bits. - * A move forward to the right is a rotate right 5 bit. - */ - -const RANK_9 = or(S[0], S[1], S[2], S[3], S[4]); -const RANK_8 = or(S[5], S[6], S[7], S[8], S[9]); -const RANK_7 = or(S[12], S[13], S[14], S[15], S[16]); -const RANK_6 = or(S[17], S[18], S[19], S[20], S[21]); -const RANK_5 = or(S[22], S[23], S[24], S[25], S[26]); -const RANK_4 = or(S[27], S[28], S[29], S[30], S[31]); -const RANK_3 = or(S[36], S[37], S[38], S[39], S[40]); -const RANK_2 = or(S[41], S[42], S[43], S[44], S[45]); -const RANK_1 = or(S[48], S[49], S[50], S[51], S[52]); -const RANK_0 = or(S[53], S[54], S[55], S[56], S[57]); - -const BOARD = or( - RANK_9, - RANK_8, - RANK_7, - RANK_6, - RANK_5, - RANK_4, - RANK_3, - RANK_2, - RANK_1, - RANK_0 -); - -const DARK_START = or(RANK_0, RANK_1, RANK_2, RANK_3); -const LIGHT_START = or(RANK_6, RANK_7, RANK_8, RANK_9); - -const Mask = { - BOARD, - LIGHT_START, - DARK_START, -}; - -export default Mask; diff --git a/src/international/move-generation.ts b/src/international/move-generation.ts deleted file mode 100644 index dae4f92..0000000 --- a/src/international/move-generation.ts +++ /dev/null @@ -1,248 +0,0 @@ -import { DraughtsEngineMove, DraughtsPlayer } from '../core/engine'; -import Mask from './mask'; -import Long from 'long'; - -export type InternationalDraughtsBoardIntermediates = { - forward: Long; - backward: Long; - opponent: Long; - empty: Long; -}; - -export class InternationalDraughtsMoveGenerator { - private intermediates: InternationalDraughtsBoardIntermediates; - - constructor(intermediates: InternationalDraughtsBoardIntermediates) { - this.intermediates = intermediates; - } - - getJumpers(): Long { - let capture = this.intermediates.empty - .rotateRight(6) - .and(this.intermediates.opponent.and(Mask.FORWARD_LEFT)); - let jumpers = capture - .rotateRight(6) - .and(this.intermediates.forward.and(Mask.FORWARD_LEFT)); - capture = this.intermediates.empty - .rotateRight(5) - .and(this.intermediates.opponent.and(Mask.FORWARD_RIGHT)); - jumpers = jumpers.or( - capture - .rotateRight(5) - .and(this.intermediates.forward.and(Mask.FORWARD_RIGHT)) - ); - - capture = this.intermediates.empty - .rotateLeft(5) - .and(this.intermediates.opponent.and(Mask.BACKWARD_LEFT)); - jumpers = jumpers.or( - capture - .rotateLeft(5) - .and(this.intermediates.backward.and(Mask.BACKWARD_LEFT)) - ); - - capture = this.intermediates.empty - .rotateLeft(6) - .and(this.intermediates.opponent.and(Mask.BACKWARD_RIGHT)); - jumpers = jumpers.or( - capture - .rotateLeft(6) - .and(this.intermediates.backward.and(Mask.BACKWARD_RIGHT)) - ); - - return jumpers; - } - - getMovers(): Long { - let movers = Long.fromNumber(0); - - if (this.intermediates.forward.notEquals(Long.fromNumber(0))) { - movers = movers.or( - this.intermediates.empty - .rotateRight(6) - .and(this.intermediates.forward.and(Mask.FORWARD_LEFT)) - ); - movers = movers.or( - this.intermediates.empty - .rotateRight(5) - .and(this.intermediates.forward.and(Mask.FORWARD_RIGHT)) - ); - } - if (this.intermediates.backward.notEquals(Long.fromNumber(0))) { - movers = movers.or( - this.intermediates.empty - .rotateLeft(5) - .and(this.intermediates.backward.and(Mask.BACKWARD_LEFT)) - ); - movers = movers.or( - this.intermediates.empty - .rotateLeft(6) - .and(this.intermediates.backward.and(Mask.BACKWARD_RIGHT)) - ); - } - - return movers; - } - - getMovesFromOrigin(origin: Long): DraughtsEngineMove[] { - const moves: DraughtsEngineMove[] = []; - - if (origin.and(this.intermediates.forward).notEquals(Long.fromNumber(0))) { - const d1 = origin - .and(Mask.FORWARD_LEFT) - .rotateLeft(6) - .and(this.intermediates.empty) - .toUnsigned(); - if (d1.notEquals(Long.fromNumber(0))) { - moves.push({ origin, destination: d1, captures: Long.fromNumber(0) }); - } - - const d2 = origin - .and(Mask.FORWARD_RIGHT) - .rotateLeft(5) - .and(this.intermediates.empty) - .toUnsigned(); - if (d2.notEquals(Long.fromNumber(0))) { - moves.push({ origin, destination: d2, captures: Long.fromNumber(0) }); - } - } - - if (origin.and(this.intermediates.backward).notEquals(Long.fromNumber(0))) { - const d3 = origin - .and(Mask.BACKWARD_LEFT) - .rotateRight(5) - .and(this.intermediates.empty) - .toUnsigned(); - if (d3.notEquals(Long.fromNumber(0))) { - moves.push({ origin, destination: d3, captures: Long.fromNumber(0) }); - } - - const d4 = origin - .and(Mask.BACKWARD_RIGHT) - .rotateRight(6) - .and(this.intermediates.empty) - .toUnsigned(); - if (d4.notEquals(Long.fromNumber(0))) { - moves.push({ origin, destination: d4, captures: Long.fromNumber(0) }); - } - } - - return moves; - } - - getJumpsFromOrigin(origin: Long) { - const searchStack = this.getSingleJumpFromOrigin(origin); - const moves: DraughtsEngineMove[] = []; - - while (searchStack.length > 0) { - const searchJump = searchStack.pop(); - if (searchJump === undefined) break; - - const nextBoard = this.applyUnfinishedCapture({ - ...searchJump, - origin, - }); - - const nextJumps = nextBoard.getSingleJumpFromOrigin( - searchJump.destination - ); - - for (const nextJump of nextJumps) { - searchStack.push({ - origin, - destination: nextJump.destination, - captures: searchJump.captures.or(nextJump.captures), - }); - } - - if (nextJumps.length === 0) moves.push(searchJump); - } - - return moves; - } - - getSingleJumpFromOrigin(origin: Long): DraughtsEngineMove[] { - const moves: DraughtsEngineMove[] = []; - - if (origin.and(this.intermediates.forward).notEquals(Long.fromNumber(0))) { - const c1 = origin - .and(Mask.FORWARD_LEFT) - .rotateLeft(6) - .and(this.intermediates.opponent) - .toUnsigned(); - const d1 = c1 - .and(Mask.FORWARD_LEFT) - .rotateLeft(6) - .and(this.intermediates.empty) - .toUnsigned(); - if (d1.notEquals(Long.fromNumber(0))) { - moves.push({ origin, destination: d1, captures: c1 }); - } - - const c2 = origin - .and(Mask.FORWARD_RIGHT) - .rotateLeft(5) - .and(this.intermediates.opponent) - .toUnsigned(); - const d2 = c2 - .and(Mask.FORWARD_RIGHT) - .rotateLeft(5) - .and(this.intermediates.empty) - .toUnsigned(); - if (d2.notEquals(Long.fromNumber(0))) { - moves.push({ origin, destination: d2, captures: c2 }); - } - } - - if (origin.and(this.intermediates.backward).notEquals(Long.fromNumber(0))) { - const c3 = origin - .and(Mask.BACKWARD_LEFT) - .rotateRight(5) - .and(this.intermediates.opponent) - .toUnsigned(); - const d3 = c3 - .and(Mask.BACKWARD_LEFT) - .rotateRight(5) - .and(this.intermediates.empty) - .toUnsigned(); - if (d3.notEquals(Long.fromNumber(0))) { - moves.push({ origin, destination: d3, captures: c3 }); - } - - const c4 = origin - .and(Mask.BACKWARD_RIGHT) - .rotateRight(6) - .and(this.intermediates.opponent) - .toUnsigned(); - const d4 = c4 - .and(Mask.BACKWARD_RIGHT) - .rotateRight(6) - .and(this.intermediates.empty) - .toUnsigned(); - if (d4.notEquals(Long.fromNumber(0))) { - moves.push({ origin, destination: d4, captures: c4 }); - } - } - - return moves; - } - - private applyUnfinishedCapture( - move: DraughtsEngineMove - ): InternationalDraughtsMoveGenerator { - return new InternationalDraughtsMoveGenerator({ - forward: this.intermediates.forward - .and(move.origin) - .notEquals(Long.fromNumber(0)) - ? this.intermediates.forward.or(move.destination) - : this.intermediates.forward, - backward: this.intermediates.backward - .and(move.origin) - .notEquals(Long.fromNumber(0)) - ? this.intermediates.backward.or(move.destination) - : this.intermediates.backward, - opponent: this.intermediates.opponent.and(move.captures.not()), - empty: this.intermediates.empty, - }); - } -} diff --git a/src/international/utils.ts b/src/international/utils.ts deleted file mode 100644 index a62ddd1..0000000 --- a/src/international/utils.ts +++ /dev/null @@ -1,28 +0,0 @@ -import Long from 'long'; - -const BITS = 64; - -const InternationalDraughtsBitSquare: Record = []; -InternationalDraughtsBitSquare[0] = Long.fromNumber(1); -for (let index = 1; index < BITS; index++) { - InternationalDraughtsBitSquare[index] = - InternationalDraughtsBitSquare[index - 1].shiftLeft(1); -} -export { InternationalDraughtsBitSquare }; - -export function bitwiseOr(...args: Long[]): Long { - let acc = Long.fromNumber(0); - for (const arg of args) { - acc = acc.or(arg); - } - return acc; -} - -export function rotRight(value: Long, bits: number): Long { - return value.rotateRight(bits); -} - -export function rotLeft(value: Long, bits: number): Long { - return value.rotateLeft(bits); -} -