@nodots-llc/backgammon-ai
Version:
AI and integration for nodots-backgammon using gnubg as a backend engine.
74 lines (73 loc) • 2.8 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExamplePluginAnalyzer = exports.FurthestFromOffMoveAnalyzer = exports.RandomMoveAnalyzer = void 0;
/**
* Randomly selects a move from the list.
*/
class RandomMoveAnalyzer {
selectMove(moves) {
return __awaiter(this, void 0, void 0, function* () {
if (!moves.length)
return null;
const idx = Math.floor(Math.random() * moves.length);
return moves[idx];
});
}
}
exports.RandomMoveAnalyzer = RandomMoveAnalyzer;
/**
* Type guard to check if a move has an origin property.
*/
function hasOrigin(move) {
return move.origin && move.origin.position;
}
/**
* Selects the move that leaves the most checkers furthest from being borne off.
* If origin is available, uses its position; otherwise, falls back to dieValue.
*/
class FurthestFromOffMoveAnalyzer {
selectMove(moves) {
return __awaiter(this, void 0, void 0, function* () {
if (!moves.length)
return null;
let maxScore = -Infinity;
let bestMove = null;
for (const move of moves) {
let score = 0;
if (hasOrigin(move)) {
// Use the clockwise position as a proxy for distance from off
score = move.origin.position.clockwise;
}
else {
// Fallback: use dieValue as a proxy
score = move.dieValue;
}
if (score > maxScore) {
maxScore = score;
bestMove = move;
}
}
return bestMove;
});
}
}
exports.FurthestFromOffMoveAnalyzer = FurthestFromOffMoveAnalyzer;
// Template for plugin authors
class ExamplePluginAnalyzer {
selectMove(moves, context) {
return __awaiter(this, void 0, void 0, function* () {
// Your custom logic here
return moves[0] || null;
});
}
}
exports.ExamplePluginAnalyzer = ExamplePluginAnalyzer;