Skip to content

Commit

Permalink
Merge pull request #6 from nguyenphuminh/search-extensions
Browse files Browse the repository at this point in the history
Add multiple search extensions
  • Loading branch information
nguyenphuminh authored Feb 15, 2024
2 parents ff79c6a + 7a6635f commit 94363ae
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,14 @@ module.exports = {
* Transposition table (enabled without Zobrist hashing).
* Null-move pruning.
* Late move reductions.
* Checkmate and stalemate detection.
* Search extensions:
* Check extensions.
* One reply extensions.
* Quiescence search.
* Evalution:
* PeSTO evaluation.
* Pawn structure.
* Checkmate and draw detection.
* UCI.


Expand Down
12 changes: 8 additions & 4 deletions catto.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
module.exports = {
// General
searchDepth: 5,
uci: true,
fen: "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
version: "v0.4.2",

// For dev
version: "v0.5.0",
debug: false,
// stable: true,

// LMR config
lmrFullDepth: 4,
lmrMaxReduction: 3
lmrMaxReduction: 3,

// Search extensions config
maxExtensions: 3
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "catto",
"version": "0.4.2",
"version": "0.5.0",
"description": "The Catto chess engine",
"main": "index.js",
"scripts": {
Expand Down
31 changes: 25 additions & 6 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface EngineOptions {
searchDepth: number;
lmrFullDepth: number;
lmrMaxReduction: number;
maxExtensions: number;
uci: boolean;
version: string;
}
Expand All @@ -41,6 +42,7 @@ export class Engine {
public searchedMoves: number = 0;
public lmrMaxReduction: number;
public lmrFullDepth: number;
public maxExtensions: number;
public ply: number = 0;
public prevMove?: Move;
public bestMove?: Move;
Expand All @@ -64,6 +66,7 @@ export class Engine {
this.stable = options.stable;
this.lmrMaxReduction = options.lmrMaxReduction;
this.lmrFullDepth = options.lmrFullDepth;
this.maxExtensions = options.maxExtensions;
}

// Tranposition table
Expand Down Expand Up @@ -205,7 +208,23 @@ export class Engine {
return alpha;
}

negamax(depth: number, alpha: number, beta: number): number {
// Calculate extensions
calculateExtensions() {
let extensions = 0;

// One reply extension and check extension
if (this.chess.moves().length === 1 || this.chess.inCheck()) {
extensions = 1;
}

return extensions;
}

// The main negamax search algorithm
negamax(depth: number, alpha: number, beta: number, extended: number): number {
// Calculate extensions
const extensions = extended < this.maxExtensions ? this.calculateExtensions() : 0;

this.nodes++;

let hashFlag = HashFlag.alpha;
Expand Down Expand Up @@ -237,7 +256,7 @@ export class Engine {
this.chess.load(tokens.join(" "));

// Search with reduced depth
const score = -this.negamax(depth - 1 - 2, -beta, -beta + 1);
const score = -this.negamax(depth - 1 - 2, -beta, -beta + 1, extended);

// Reconstruct chess obj prior to null move
this.chess.load(this.fen);
Expand Down Expand Up @@ -282,7 +301,7 @@ export class Engine {

// Late move reduction
if (this.searchedMoves === 0) {
score = -this.negamax(depth-1, -beta, -alpha);
score = -this.negamax(depth - 1 + extensions, -beta, -alpha, extended + extensions);
} else {
if (
this.searchedMoves >= this.lmrFullDepth &&
Expand All @@ -291,9 +310,9 @@ export class Engine {
!move.captured &&
!move.promotion
) {
score = -this.negamax(depth-2, -beta, -alpha);
score = -this.negamax(depth - 2, -beta, -alpha, extended);
} else {
score = -this.negamax(depth-1, -beta, -alpha);
score = -this.negamax(depth - 1 + extensions, -beta, -alpha, extended + extensions);
}
}

Expand Down Expand Up @@ -349,7 +368,7 @@ export class Engine {
findMove() {
const start = Date.now();

this.negamax(this.searchDepth, -50000, 50000);
this.negamax(this.searchDepth, -50000, 50000, 0);

if (this.debug) {
console.log("Nodes searched:", this.nodes);
Expand Down

0 comments on commit 94363ae

Please sign in to comment.