Skip to main content
Deno 2 is finally here πŸŽ‰οΈ
Learn more

Catur

Unrealistically Simple Chess Engine.

Usage:

  • DENO
import { ... } from 'https://deno.land/x/catur/mod.ts'
  • NODE
npm i catur
import { ... } from 'catur'

Example: Easy AI vs Monkey AI

// import ....

const main = async () => {
  const caturGame = CaturGame.newStandardGame();
  const easyAI = new AI.EasyAI(caturGame);
  const monkeyAI = new AI.MonkeyAI(caturGame);

  // if we didn't set the piece type on promote, the piece will turn into a queen
  caturGame.onBlackPromote = () =>
    caturGame.mover.promoteLastMoveTo(
      Utils.randomFromArray([Type.bishop, Type.queen, Type.knight, Type.rook])
    );

  console.log(caturGame.board.toString());
  /**
        β™œ β™ž ♝ β™› β™š ♝ β™ž β™œ 
        β™Ÿ β™Ÿ β™Ÿ β™Ÿ β™Ÿ β™Ÿ β™Ÿ β™Ÿ 




        β™™ β™™ β™™ β™™ β™™ β™™ β™™ β™™ 
        β™– β™˜ β™— β™• β™” β™— β™˜ β™– 
     */

  let mv = 0;

  while (!caturGame.gameOver) {
    console.clear();
    // easyAI as white, and monkeyAI as black
    const move = mv++ % 2 == 0 ? easyAI.getMove() : monkeyAI.getMove();
    if (move) {
      caturGame.mover.moveStrict(move.from, move.to);
      // NEEDED
      caturGame.mover.next();
    }
    console.log(move);
    console.log(caturGame.board.toString());
    await Utils.sleep(500);
  }

  console.log(caturGame.gameOverReason + "!");
};

main().catch((err) => console.error(err));