Roller chess

Roller is a fun little javascript chess engine program that only knows the rules (except 3-fold repetition).  It has absolutely no semantic knowledge at all, not even the relative piece values.  It plays by performing thousands of totally random games (rollouts) from the current position.  There is no search strategy as such at all.  In effect it’s a “pure” MCTS algorithm.  Wins, losses and draws are scored 1, -1, 0 and it simply chooses the move with the highest net score.  Obviously at the start of a game this is mostly just random play but it can avoid mate and give mate when they are clearly in sight.  You can play Roller here:-

http://op12no2.me/toys/roller/play.htm

The “kr/s” figure is showing thousands of complete rollouts (random games) per second.

Interestingly when pitted against a true random mover version of itself, Roller wins all of the games, other than sometimes accidentally falling into 3-fold repetition because it does not know that rule.

Roller can be used in Arena etc like Lozza.

Roller source is here:-

http://op12no2.me/toys/roller/roller.js

If rollouts last longer than 450 ply, a draw is assumed, as some crazy games can last longer than 1000 ply.  This does kinda of compromise the no-knowledge (or “zero”) element, but I can live with it.

It does not terminate at known draws like KK for example, because that is  definitely feeding in knowledge; however tempting it is to do so in the name of kr/s.

You can change the number of seconds Roller takes to move from the entry field above the board, which by default is 10s.

While thinking Roller displays a status above the board of the form:-

time so far (seconds) |
thousands of rollouts (games) so far |
thousands of rollouts per second so far |
best move so far

After moving Roller displays the net score and number of rollouts for each  legal move, for example:-

...
e6 9 / 1150
d5 31 / 1192 ****
d6 -8 / 1197
...

**** depicts the best score and the move chosen.

 

Connecting node.js to stdin/stdout

The context here is a UCI chess interface such that a Javascript chess engine running in node.js can communicate with standard chess UIs like Arena and Winboard via stdin/stdout.  But the code may be useful in other contexts too.

My own UCI interface (node.js and web worker compatible) can be found here in the lozUCI class and also some initialisation code (like that below) at the end of the file.

http://op12no2.me/toys/lozza/lozza.js

If you mine it for your own projects please attribute and link back here.

stdin

var nodefs = require('fs');

process.stdin.setEncoding('utf8');

process.stdin.on('readable', function() {
  var chunk = process.stdin.read();
  if (chunk !== null) {
    onmessage({data: chunk});
  }
});

process.stdin.on('end', function() {
  process.exit();
});

The UCI commands are then available in data.data in onmessage().  Note that more than one command can be present separated by \n and depending on OS also \r, so assume \n and filter out \r.  In my own experience null commands can be present \n\n, so watch out for those too.

stdout

var str = ''
... 
nodefs.writeSync(1, str + '\n');

The use of writeSync() is so that that the PV feedback displays in chess UIs like Arena and Winboard in real time.  If you write using async functions it appears all at once when the best move is sent back.

It’s useful to name the capture function onmessage() because that what web workers assume, thus it can be used in both web and node.js contexts.

Integrating Lozza into your project

You are free to integrate an unmodified Lozza into your own non-commercial projects. In return please link to http://op12no2.me/toys/lozza. If you wish to modify Lozza please email me for a chat first: colin@sailwave.com, thanks.

The Lozza engine itself is self-contained in a single Javascript file (link at bottom of this post) and typically runs in a web worker.  A controlling program  can communicate with Lozza using the string based UCI protocol over the JSON web worker interface.

This is a very simple example:-

http://op12no2.me/toys/lozza/ex.htm

It fires up Lozza in a web worker and asks it to find the best move for a 10 ply search, sending all output to a HTML element called dump using jQuery:-

It looks like this:-

var lozza = new Worker('lozza.js');

lozza.onmessage = function (e) {
  $('#dump').append(e.data);
};

lozza.postMessage('uci');         // get build etc
lozza.postMessage('ucinewgame');  // reset TT
lozza.postMessage('position startpos');
lozza.postMessage('go depth 10'); // 10 ply search

Lozza can auto-detect if it’s running in a node.js context allowing seamless use online in a browser context and offline in node.

This allows Lozza run both online and offline on pretty much any platform, including Windows, Linux and Mac.

For example this image is a dump of the same 10 ply search using node from the Windows command line:-

The “info string debug…” lines show overall time and node count etc and are not strictly part of the UCI protocol.  You can either filter them out or change this.debugging to false in the UCI class.

Lozza can be used outside of a web worker by including lozza.js in your HTML file and then using the loz* classes directly.  This is useful for profiling etc.

The Lozza source itself is here:-

http://op12no2.me/toys/lozza/lozza.js

You are also free to mine any of the example user interfaces, but please do let me know what you use and credit appropriately; thanks.

Using Lozza offline

Command line, Winboard, Arena etc.

Lozza needs a Javascript engine to live in.  Usually this is supplied by a web browser, but other applications also embed Javascript engines, the most common and flexible being an application called node.js, allowing you to execute Javascript outside of your browser. node.js is available for most operating systems, including Windows, Linux and Apple.

Download and install node.js (if you get a choice of engines choose google Chrome V8).  node.js adds itself to your path and this is assumed in the example commands below.

Download Lozza from:-

http://op12no2.me/toys/lozza/lozza.js

Save into a folder of your choice.

To use from the command line do something like:-

cd lozza folder
node lozza.js

If that fails maybe node is not in your path, so either use an absolute directory to address node or tweak your path.

To use in a chess UI like Arena or WInboard, use node.exe (etc) as the chess engine and lozza.js as a command line parameter.

To enter a Javascript engine into HG Muller’s monthly chess tourney. First ask him for a user name and password and install Winboard. Then connect with something like this from the winboard directory itself:-

winboard -zp -zippyGameEnd "say Thanks for the game." -ics -icshost http://winboard.nl -icshelper timeseal -fcp "c:\nodejs\node.exe c:\lozza\lozza.js" -fd . -autoKibitz -fUCI -keepAlive 4 -firstXBook

 

Lozza analysis UI

Allows you to set up positions and analyse them.

http://op12no2.me/toys/lozza/fen.htm

Optional URL parameters

fen=fen

Load the specified FEN as the position to analyse.

act=ana

Auto start an analysis.  Be a little bit careful about using this as some people do not like their computer to use a lot of CPU time on an analysis without them starting it explicitly.

act=eva

Auto start an evaluation.

Example:-

http://op12no2.me/toys/lozza/fen.htm?act=eva&fen=5k2/p7/3R4/2p3p1/1bP3P1/5N1P/3p1PK1/3r4

Lozza play UI

Play black or white against Lozza.

http://op12no2.me/toys/lozza/play.htm

No proper time controls. Lozza takes N seconds to move as specified by you in the text field above the board. The default is 1 second. You can change the value at any time during the game.

Optional URL parameters

h=y

Hide Lozza’s workings other than announcements of mate and the info above the board.

c=b

You play black. Default is you play white.

t=secondspermove

Lozza uses specified number of seconds to make her moves. Default is 1. For example to play at 10 seconds per move and you play black use:-

http://op12no2.me/toys/lozzadev/play.htm?t=10&c=b

fen=fen

Use the specified FEN string to start the game from. The default is the standard start position.

m=ucigocommand

Lozza uses the UCI go command to make her move rather than the fixed time. For example to ask Lozza to search to a depth of 2 ply only use:-

http://op12no2.me/toys/lozzadev/play.htm?m=go depth 2

Another example, to ask Lozza to make her move after searching 100000 nodes, use:-

http://op12no2.me/toys/lozzadev/play.htm?m=go nodes 100000

This can also be used to play with a fixed move time of less than 1 second, for example to make Lozza move after 200ms use:-

http://op12no2.me/toys/lozzadev/play.htm?m=go movetime 200

Lozza mate in 1 trainer

This is a simple trainer that get’s you to solve mate in 1 problems under time pressure; giving penalties for wrong tries.

http://op12no2.me/toys/lozza/mate.htm

You must solve 10 mate-in-1 puzzles against the clock.  Time penalties are added for illegal and wrong moves.  You must drag the piece to the mating square to solve the puzzle.  The puzzles are loaded at random from a large collection sourced from here:-

http://www.talkchess.com/forum/viewtopic.php?t=55104

All the puzzles are loaded to your computer the first time you play. It can take a little while depending on the speed of your internet connection.

Please let me know if you’d like to see any other trainers.

Lozza UI console

This Lozza UI allows you to type UCI commands in a console-like manner.

http://op12no2.me/toys/lozza/console.htm

COMMAND EXTENSIONS

In addition to the standard UCI protocol , the following commands can be used:-

stop

Stop the Lozza engine.   Useful to stop a long perft or analysis.  What actually happens is that the web worker containing Lozza is killed.  You’ll need to restart one.

start

Start Lozza.  i.e. create a new web worker containing the Lozza.  After a start the following are automatically sent:-

uci
ucinewgame
position startpos
board

A start command is issued when the console UI starts.

clear

Clear the previous output.

board

Display a textual FEN and graphical representation of the current position.

perft depth d [moves m] [inner 1|0]

Perform a perft analysis to a depth of d ply based on the current position.  m is the expected number of moves and is optional, defaulting to 0.  inner 1 will show inner node counts with the default being inner 0

eval

Show how the static evaluation of the current position is constructed.

id idstring

Attach an identification label to the current position.  Only relevant if UCI debugging is on.

ping

Check that the engine is alive.

SHORTCUTS

u == ucinewgame

q == quit

p == position

p s == position startpos

g == go

g d = go depth

b == board

NOTES

An explicit ucinewgame is needed to reset the transposition tables; and is required before the first position.  It is optional during a game since it has no effect other than to clear the TT.

Lozza plays it’s best move  when a  go command completes, changing the current position.

The current position is unchanged after a perft

The start, stop and clear commands are not be available when using Lozza offline; for example with node.js.  Similarly only the textual board representation is shown.