Skip to content

Commit

Permalink
feat: rename S to EnglishDraughtsBitSquare
Browse files Browse the repository at this point in the history
  • Loading branch information
loks0n committed Mar 30, 2023
1 parent c99afc2 commit d5d713f
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 36 deletions.
181 changes: 153 additions & 28 deletions src/english/engine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,24 @@ import {
EnglishDraughtsEngineFactory as EngineFactory,
EnglishDraughtsEngine,
} from './engine';
import { S } from './utils';
import { EnglishDraughtsBitSquare as S } from './utils';

/* We use the following bitboard layout for English Draughts
*
* 11 05 31 25
* 10 04 30 24
* 03 29 23 17
* 02 28 22 16
* 27 21 15 09
* 26 20 14 08
* 19 13 07 01
* 18 12 06 00
*
* Access the uint32 value of a square with S[n]
*
* A move forward to the left is a rotate left 7 bits.
* A move forward to the right is a rotate left 1 bit.
*/

describe('possible openings', () => {
let engine: EnglishDraughtsEngine;
Expand All @@ -17,18 +34,50 @@ describe('possible openings', () => {
test('correct dark moves', () => {
assert.equal(engine.data.player, DraughtsPlayer.DARK);
assert.sameDeepMembers(engine.moves, [
{ origin: S[3], destination: S[2], captures: 0 },
{ origin: S[3], destination: S[28], captures: 0 },
{ origin: S[29], destination: S[28], captures: 0 },
{ origin: S[29], destination: S[22], captures: 0 },
{ origin: S[23], destination: S[22], captures: 0 },
{ origin: S[23], destination: S[16], captures: 0 },
{ origin: S[17], destination: S[16], captures: 0 },
{
origin: S[3],
destination: S[2],
captures: 0,
},
{
origin: S[3],
destination: S[28],
captures: 0,
},
{
origin: S[29],
destination: S[28],
captures: 0,
},
{
origin: S[29],
destination: S[22],
captures: 0,
},
{
origin: S[23],
destination: S[22],
captures: 0,
},
{
origin: S[23],
destination: S[16],
captures: 0,
},
{
origin: S[17],
destination: S[16],
captures: 0,
},
]);
});

test('correct board after move', () => {
engine.move({ origin: S[3], destination: S[2], captures: 0 });
engine.move({
origin: S[3],
destination: S[2],
captures: 0,
});
assert.equal(engine.data.player, DraughtsPlayer.LIGHT);
assert.equal(
engine.data.board.dark,
Expand All @@ -49,16 +98,48 @@ describe('possible openings', () => {
});

test('correct light moves', () => {
engine.move({ origin: S[3], destination: S[2], captures: 0 });
engine.move({
origin: S[3],
destination: S[2],
captures: 0,
});
assert.equal(engine.data.player, DraughtsPlayer.LIGHT);
assert.sameDeepMembers(engine.moves, [
{ origin: S[26], destination: S[27], captures: 0 },
{ origin: S[20], destination: S[27], captures: 0 },
{ origin: S[20], destination: S[21], captures: 0 },
{ origin: S[14], destination: S[21], captures: 0 },
{ origin: S[14], destination: S[15], captures: 0 },
{ origin: S[8], destination: S[15], captures: 0 },
{ origin: S[8], destination: S[9], captures: 0 },
{
origin: S[26],
destination: S[27],
captures: 0,
},
{
origin: S[20],
destination: S[27],
captures: 0,
},
{
origin: S[20],
destination: S[21],
captures: 0,
},
{
origin: S[14],
destination: S[21],
captures: 0,
},
{
origin: S[14],
destination: S[15],
captures: 0,
},
{
origin: S[8],
destination: S[15],
captures: 0,
},
{
origin: S[8],
destination: S[9],
captures: 0,
},
]);
});
});
Expand All @@ -68,19 +149,35 @@ describe('simple move', () => {

beforeEach(() => {
engine = EngineFactory.setup({
board: { light: S[21] | S[0], dark: S[24], king: 0 },
board: {
light: S[21] | S[0],
dark: S[24],
king: 0,
},
});
});

test('correct moves', () => {
assert.sameDeepMembers(engine.moves, [
{ origin: S[24], destination: S[23], captures: 0 },
{ origin: S[24], destination: S[17], captures: 0 },
{
origin: S[24],
destination: S[23],
captures: 0,
},
{
origin: S[24],
destination: S[17],
captures: 0,
},
]);
});

test('correct board after move', () => {
engine.move({ origin: S[24], destination: S[23], captures: 0 });
engine.move({
origin: S[24],
destination: S[23],
captures: 0,
});
assert.equal(engine.data.board.light, S[21] | S[0]);
assert.equal(engine.data.board.dark, S[23]);
assert.equal(engine.data.board.king, 0);
Expand All @@ -92,15 +189,27 @@ describe('tricky move', () => {

beforeEach(() => {
engine = EngineFactory.setup({
board: { light: S[30], dark: S[21], king: 0 },
board: {
light: S[30],
dark: S[21],
king: 0,
},
player: DraughtsPlayer.LIGHT,
});
});

test('correct moves', () => {
assert.sameDeepMembers(engine.moves, [
{ origin: S[30], destination: S[31], captures: 0 },
{ origin: S[30], destination: S[5], captures: 0 },
{
origin: S[30],
destination: S[31],
captures: 0,
},
{
origin: S[30],
destination: S[5],
captures: 0,
},
]);
});
});
Expand All @@ -110,13 +219,21 @@ describe('simple jump', () => {

beforeEach(() => {
engine = EngineFactory.setup({
board: { light: S[21] | S[0], dark: S[22], king: 0 },
board: {
light: S[21] | S[0],
dark: S[22],
king: 0,
},
});
});

test('correct moves', () => {
assert.sameDeepMembers(engine.moves, [
{ origin: S[22], destination: S[20], captures: S[21] },
{
origin: S[22],
destination: S[20],
captures: S[21],
},
]);
});
});
Expand All @@ -126,14 +243,22 @@ describe('jump and become king', () => {

beforeEach(() => {
engine = EngineFactory.setup({
board: { light: S[17], dark: S[24] | S[14], king: 0 },
board: {
light: S[17],
dark: S[24] | S[14],
king: 0,
},
player: DraughtsPlayer.LIGHT,
});
});

test('correct moves', () => {
assert.sameDeepMembers(engine.moves, [
{ origin: S[17], destination: S[31], captures: S[24] },
{
origin: S[17],
destination: S[31],
captures: S[24],
},
]);
});
});
2 changes: 1 addition & 1 deletion src/english/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
EnglishDraughtsEngineStore,
} from './engine';

import { S, decomposeBits } from './utils';
import { EnglishDraughtsBitSquare as S, decomposeBits } from './utils';

const ENGLISH_DRAUGHTS_LAYOUT = [
S[11],
Expand Down
2 changes: 1 addition & 1 deletion src/english/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { EnglishDraughts, type EnglishDraughtsGame } from './game';
export { S } from './utils';
export { EnglishDraughtsBitSquare } from './utils';
export {
EnglishDraughtsComputerFactory,
type EnglishDraughtsComputer,
Expand Down
4 changes: 2 additions & 2 deletions src/english/mask.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { S } from './utils';
import { EnglishDraughtsBitSquare as S } from './utils';

/* We use the following bitboard layout for English Draughts
*
Expand All @@ -11,7 +11,7 @@ import { S } from './utils';
* 19 13 07 01
* 18 12 06 00
*
* Access the value of a square with S[n]
* Access the uint32 value of a square with S[n]
*
* A move forward to the left is a rotate left 7 bits.
* A move forward to the right is a rotate left 1 bit.
Expand Down
8 changes: 4 additions & 4 deletions src/english/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const BITS = 32;

const S: Record<number, number> = [];
S[0] = 1;
const EnglishDraughtsBitSquare: Record<number, number> = [];
EnglishDraughtsBitSquare[0] = 1;
for (let index = 1; index < BITS; index++) {
S[index] = S[index - 1] * 2;
EnglishDraughtsBitSquare[index] = EnglishDraughtsBitSquare[index - 1] * 2;
}
export { S };
export { EnglishDraughtsBitSquare };

const BIT_MASK = 0xff_ff_ff_ff;

Expand Down

0 comments on commit d5d713f

Please sign in to comment.