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

β™žCatur

deno.land/x/catur Contributor Covenant

Usage:

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

Example: Pecel AI vs Lele AI

// import ....

const main = async () => {
  const caturGame = CaturGame.newStandardGame();
  const pecelAI = new AI.PecelAI(caturGame);
  const leleAI = new AI.LeleAI(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();
    // pecelAI as white, and leleAI as black
    const move = mv++ % 2 == 0 ? pecelAI.getMove() : leleAI.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));

Licensed under the MIT License. See LICENSE.txt for more information.