Skip to content

A java implementation of the chess UCI protocol with pluggable chess engine

License

Notifications You must be signed in to change notification settings

fathzer-games/jchess-uci

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Maven Central License Quality Gate Status javadoc

jchess-uci

A partial (but still usable) java implementation of the chess UCI protocol with pluggable chess engines.

How to use it

This library requires Java 11+ and is available in Maven central.

  • First create a class that implements the com.fathzer.jchess.uci.Engine interface.
    Let's say this implementation class is MyEngine.
  • Launch the com.fathzer.jchess.uci.UCI class:
// Create your engine
final Engine = new MyEngine();
new UCI(engine).run();

That's all!

Partial implementation ... and extra functionalities

It does not directly support the following commands (but you can add them in a com.fathzer.jchess.uci.UCI subclass):

  • Dont miss the ShredderChess Annual Barbeque: This command was in the original specification ... But that was a joke.
  • register: As a promoter of open source free software, I won't encourage you to develop software that requires registration.
  • ponderhit has not yet been implemented.

It also does not recognize commands starting with unknown token (to be honest, it's not very hard to implement but seems a very bad, error prone, idea to me).

It implements the following extensions:

  • q is a shortcut for standard quit command.
  • It can accept different engines, that can be selected using the engine command. You can view these engines as plugins.
    engine [engineId] lists the available engines' ids or changes the engine if engineId is provided.
  • d [fen] displays a textual representation of the game. If the command is followed by fen, the command displays the representation of a game in the Forsyth–Edwards Notation.
  • perft depth [nbThreads] runs perft test and displays the divide result.
    depth is mandatory and is the search depth of the perft algorithm. It should be strictly positive.
    nbThreads is the number of threads used to process the queries. This number should be strictly positive. Default is 1.
    Please note this command is optional, only engines that implement com.fathzer.jchess.uci.MoveGeneratorSupplier interface support it.
  • test depth [nbThreads [cutTime]] runs a move generator test based on perft.
    It can also be used to test move generator's performance as it outputs the number of moves generated per second.
    depth is mandatory and is the search depth of the perft algorithm. It should be strictly positive.
    nbThreads is the number of threads used to process the test. This number should be strictly positive. Default is 1.
    cutTime is the number of seconds allowed to process the test. This number should be strictly positive. Default is Integer.MAX_VALUE.
    Please note:
    • This command is optional, only engines that implement com.fathzer.jchess.uci.TestableMoveGeneratorSupplier interface support it.
    • This command requires the com.fathzer.jchess.uci.UCI.readTestData() method to be overridden in order to return a non empty test data set.
      A way to easily do that is to add the com.fathzer::jchess-perft-dataset artifact to your classpath, then override readTestData:
protected Collection<PerfTTestData> readTestData() {
	try (InputStream stream = MyUCISubclass.class.getResourceAsStream("/Perft.txt")) {
		return new PerfTParser().withStartPositionPrefix("position fen").withStartPositionCustomizer(s -> s+" 0 1").read(stream, StandardCharsets.UTF_8);
	} catch (IOException e) {
		throw new UncheckedIOException(e);
	}
}

Adding custom commands

Override the com.fathzer.jchess.uci.UCI class and use its addCommand method to add your own custom commands.
Then instantiate your UCI subclass and launch its run method.

Get rid of System.out and System.in

UCI protocol uses standard input and output console to communicate which is effective ... but not really modern.
If you want another way to exchange messages, you can subclass the UCI class and override the getNextCommand and/or the out (and debug if you send debug messages) methods.

TODO

  • Verify the engine is protected against strange client behavior (like changing the position during a go request).
  • Implement support for pondering.

About

A java implementation of the chess UCI protocol with pluggable chess engine

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages