Hacker News new | past | comments | ask | show | jobs | submit login

Playing chess with strings to build datasets for text generation.

I want to share this quick win.

The other day I was asking myself some theoretical chess questions, and wanted to answer them programmatically and needed to build some custom chess datasets for that.

I needed the chess basic routines, like getting the next legal moves, displaying the board, and some rudimentary position scores. I contemplated writing from scratch. I contemplated using some library. But instead I settled for a higher level choice : interfacing with Stockfish game engine over a text interface.

There is something called UCI, which stands for Universal Chess Interface, ( https://official-stockfish.github.io/docs/stockfish-wiki/UCI... ), to use it you start a new stockfish process and write and read from the standard inputs.

So instead of writing bug prone routines to check the validity of board positions, it turn the basic routines into a simple wrapper of parsing task to read and write UCI protocol to use a battle tested engine.

A chess position state is simply defined as a vector<string> representing the sequence of moves. Moves are string in long algebraic notation.

This architectural decision allows for very quick (LLM-powered development) prototyping.

namespace bp = boost::process; bp::ipstream is; bp::opstream os;

bp::child c("../Stockfish/src/stockfish", bp::std_in < os, bp::std_out > is);

void displayBoard( const vector<string> & moveSeq, bp::ipstream& is, bp::opstream& os );

void getLegalMoves( const vector<string> & moveSeq, vector<string>& legalMoves, bp::ipstream& is, bp::opstream& os );

void getTopKMoveAndScoreAtDepthFromPosition(const vector<string> & moveSeq,int K, int D, vector<pair<string,int> >& topkmoves, bp::ipstream& is, bp::opstream& os , bool debug = false);

void displayBoard( const vector<string> & moveSeq, bp::ipstream& is, bp::opstream& os ) {

os << "position startpos moves";

for( int i = 0 ; i < moveSeq.size() ; i++)

{

  os << " " << moveSeq[i];
}

os << endl;

os << "d" << endl;

os << "isready" << endl;

string line;

while (getline(is, line)) { if (!line.compare(0, 7, "readyok")) break; cout << line << endl; }

}

You get the gist...




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: