I would like to show you a simple application providing a little feature on top of the good
old xboard FICS interface
(I'm still relying on it as I haven't found a completely satisfying alternative on linux).
The idea is to have, when a new game is started, a single-line report on my opponent, telling me
how many games we had
played each other and what is the overall score.
The program has two main functions, running on separate threads: the first
one duty is to parse the text coming from
the Internet Chess Server to get the name of my opponent.
This is a very easy task as all you have to do is to wait for a "Creating game" line and get the proper token out of it.
The second function, the pgnlib related one, looks for all the games between the two of us, into my
fics.pgn file and
produces the output.
This is how the thing looks like:
It turned out very easy to write the report generator function with pgnlib. This is the main code:
... // loading the games std::ifstream pgnfile("/home/vinzoni/scacchi/scid/fics.pgn"); pgn::GameCollection games; pgnfile >> games; // get rid of games with other players NotMyOpponent notMyOpponent(opponent); games.erase(std::remove_if(games.begin(), games.end(), NotMyOpponent), games.end()); // do the counting int topogigio_wins = std::count_if(games.begin(), games.end(), TopogigioWins()); int topogigio_loses = std::count_if(games.begin(), games.end(), TopogigioLoses()); int topogigio_draws = std::count_if(games.begin(), games.end(), TopogigioDraws()); // print the result printout_report(opponent, topogigio_wins, topogigio_draws, topogigio_loses); ... |
Here I defined four function objects (printed in bold-face) working together with common STL-algorithms.
They are all rather straightforward to implement, I'll show you just the code of one of them, the others are similar.
class TopogigioWins // topogigio is my nickname on FICS { public: bool operator () (const pgn::Game &g) { if ((g.white() == "topogigio") && (g.result().isWhiteWin())) return true; else if ((g.black() == "topogigio") && (g.result().isBlackWin())) return true; else return false; } }; |
Now it's just a matter of composing the message and pass it along to the system facility send-notify
to have it displayed
in the bubble above the board.
void printout_report(const std::string &opponent, int topogigio_wins, int topogigio_draws, int topogigio_loses) { int total = topogigio_wins + topogigio_draws + topogigio_loses; std::stringstream ss_title; ss_title << "topogigio - " << opponent << ": " << total << " games. ( +" << topogigio_wins << " =" << topogigio_draws << " -" << topogigio_loses << " )" << std::endl; std::stringstream ss_cmd; ss_cmd << "notify-send \"" << ss_title.str() << "\""; system(ss_cmd.str().c_str()); } |
And that's it! It took me no more than a few hours to assemble and test the program.
That's all for now, have a good time with pgnlib!
[7. The Position class] | [home page] |