Skip to content

Commit

Permalink
chore: store -> stats
Browse files Browse the repository at this point in the history
  • Loading branch information
loks0n committed Dec 10, 2024
1 parent d2eb875 commit acae1d7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 46 deletions.
36 changes: 19 additions & 17 deletions src/core/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,41 @@ export enum DraughtsPlayer {
DARK = 'dark',
}

export type DraughtsEngineData<TBitboard extends Bitboard, E> = {
export type DraughtsEngineData<TBitboard extends Bitboard> = {
player: DraughtsPlayer;
board: DraughtsEngineBoard<TBitboard>;
store: E;
stats: {
sinceCapture: number;
sinceNonKingAdvance: number;
}
};

export type DraughtsEngineStrategy<TBitboard extends Bitboard, TStore> = {
export type DraughtsEngineStrategy<TBitboard extends Bitboard> = {
moves: (
engine: DraughtsEngine<TBitboard, TStore>
engine: DraughtsEngine<TBitboard>
) => DraughtsEngineMove<TBitboard>[];
status: (engine: DraughtsEngine<TBitboard, TStore>) => DraughtsStatus;
status: (engine: DraughtsEngine<TBitboard>) => DraughtsStatus;
isValidMove: (
engine: DraughtsEngine<TBitboard, TStore>,
engine: DraughtsEngine<TBitboard>,
move: DraughtsEngineMove<TBitboard>
) => boolean;
move: (
engine: DraughtsEngine<TBitboard, TStore>,
engine: DraughtsEngine<TBitboard>,
move: DraughtsEngineMove<TBitboard>
) => DraughtsEngineData<TBitboard, TStore>;
serializeStore: (store: TStore) => TStore;
) => DraughtsEngineData<TBitboard>;
};

export class DraughtsEngine<TBitboard extends Bitboard, TStore> {
data: DraughtsEngineData<TBitboard, TStore>;
export class DraughtsEngine<TBitboard extends Bitboard> {
data: DraughtsEngineData<TBitboard>;

private strategy: DraughtsEngineStrategy<TBitboard, TStore>;
private strategy: DraughtsEngineStrategy<TBitboard>;

private _moves: DraughtsEngineMove<TBitboard>[] | undefined;
private _status: DraughtsStatus | undefined;

constructor(
data: DraughtsEngineData<TBitboard, TStore>,
strategy: DraughtsEngineStrategy<TBitboard, TStore>
data: DraughtsEngineData<TBitboard>,
strategy: DraughtsEngineStrategy<TBitboard>
) {
this.data = data;
this.strategy = strategy;
Expand All @@ -82,19 +84,19 @@ export class DraughtsEngine<TBitboard extends Bitboard, TStore> {
* Clones the current engine instance
* @returns A new cloned engine instance
*/
clone(): DraughtsEngine<TBitboard, TStore> {
clone(): DraughtsEngine<TBitboard> {
return new DraughtsEngine(this.serialize(), this.strategy);
}

/**
* Serializes the engine data
* @returns The serialized engine data
*/
serialize(): DraughtsEngineData<TBitboard, TStore> {
serialize(): DraughtsEngineData<TBitboard> {
return {
board: { ...this.data.board },
store: this.strategy.serializeStore(this.data.store),
player: this.data.player,
stats: { ...this.data.stats },
};
}

Expand Down
6 changes: 3 additions & 3 deletions src/core/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export type DraughtsAdapter1D<TBitboard extends Bitboard> = {
toBoard1D: (engineBoard: DraughtsEngineBoard<TBitboard>) => DraughtsBoard1D;
};

export class DraughtsGame1D<TBitboard extends Bitboard, TStore> {
engine: DraughtsEngine<TBitboard, TStore>;
export class DraughtsGame1D<TBitboard extends Bitboard> {
engine: DraughtsEngine<TBitboard>;
history: DraughtsGameHistory1D;

private _board: DraughtsBoard1D | undefined;
Expand All @@ -56,7 +56,7 @@ export class DraughtsGame1D<TBitboard extends Bitboard, TStore> {
private readonly adapter: DraughtsAdapter1D<TBitboard>;

constructor(
engine: DraughtsEngine<TBitboard, TStore>,
engine: DraughtsEngine<TBitboard>,
history: DraughtsGameHistory1D,
adapter: DraughtsAdapter1D<TBitboard>
) {
Expand Down
38 changes: 12 additions & 26 deletions src/english/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,12 @@ import Mask from './mask';
import { EnglishDraughtsMoveGeneratorFactory } from './move-generation';
import { decomposeBits } from './utils';

export type EnglishDraughtsEngineStore = {
readonly sinceCapture: number;
readonly sinceNonKingAdvance: number;
};

export type EnglishDraughtsEngineData = DraughtsEngineData<
number,
EnglishDraughtsEngineStore
number
>;

export type EnglishDraughtsEngine = DraughtsEngine<
number,
EnglishDraughtsEngineStore
number
>;

export const EnglishDraughtsEngineDefaultData: EnglishDraughtsEngineData = {
Expand All @@ -31,31 +24,24 @@ export const EnglishDraughtsEngineDefaultData: EnglishDraughtsEngineData = {
dark: Mask.DARK_START,
king: 0,
},
store: {
stats: {
sinceCapture: 0,
sinceNonKingAdvance: 0,
},
};

export const EnglishDraughtsEngineStrategy: DraughtsEngineStrategy<
number,
EnglishDraughtsEngineStore
number
> = {
serializeStore(store: EnglishDraughtsEngineStore) {
return {
...store,
};
},

status(engine: EnglishDraughtsEngine) {
if (engine.moves.length === 0) {
return engine.data.player === DraughtsPlayer.LIGHT
? DraughtsStatus.DARK_WON
: DraughtsStatus.LIGHT_WON;
}
if (
engine.data.store.sinceCapture >= 40 &&
engine.data.store.sinceNonKingAdvance >= 40
engine.data.stats.sinceCapture >= 40 &&
engine.data.stats.sinceNonKingAdvance >= 40
) {
return DraughtsStatus.DRAW;
}
Expand Down Expand Up @@ -95,7 +81,7 @@ export const EnglishDraughtsEngineStrategy: DraughtsEngineStrategy<
...engine.data.board,
};

const store = { ...engine.data.store };
const stats = { ...engine.data.stats };

board.light &= ~(move.origin | move.captures);
board.dark &= ~(move.origin | move.captures);
Expand All @@ -111,15 +97,15 @@ export const EnglishDraughtsEngineStrategy: DraughtsEngineStrategy<

if (engine.data.board.king & move.origin) {
board.king |= move.destination;
store.sinceNonKingAdvance += 1;
stats.sinceNonKingAdvance += 1;
} else {
store.sinceNonKingAdvance = 0;
stats.sinceNonKingAdvance = 0;
}

if (move.captures) {
store.sinceCapture = 0;
stats.sinceCapture = 0;
} else {
store.sinceCapture += 1;
stats.sinceCapture += 1;
}

return {
Expand All @@ -128,7 +114,7 @@ export const EnglishDraughtsEngineStrategy: DraughtsEngineStrategy<
? DraughtsPlayer.DARK
: DraughtsPlayer.LIGHT,
board,
store,
stats,
};
},
};
Expand Down

0 comments on commit acae1d7

Please sign in to comment.