Skip to content
dbohdan edited this page Feb 4, 2017 · 1 revision

This uses chess.js.

#! /usr/bin/env tclsh
package require duktape 0.3.0
package require duktape::oo
package require fileutil
package require http

set dt [::duktape::oo::Duktape new]

# Download chess.js if necessary.
if {![file exists chess.js]} {
    set req [::http::geturl http://cdnjs.cloudflare.com/ajax/libs/chess.js/0.10.2/chess.js]
    set c [::http::data $req]
    ::http::cleanup $req
    ::fileutil::writeFile chess.js $c
}

# Set up the game.
$dt eval [::fileutil::cat chess.js]
$dt eval {
    function Game() {
        'use strict';
        const game = {};
        game.chess = new Chess();

        game.next_move = function () {
            if (game.chess.game_over()) {
                return null;
            } else {
                const moves = game.chess.moves();
                const move = moves[Math.floor(Math.random()*moves.length)];
                game.chess.move(move);
                return move;
            };
        };

        return game;
    };

    const game1 = Game();
}

# Play.
while 1 {
    set move [$dt call-method-str game1.next_move undefined]
    if {$move eq {null}} break
    puts $move
}
Clone this wiki locally