Now we improve on the previous example and have the Game class join the party.
#include <PGNGameCollection.h> #include <fstream> int main(int argc, char *argv[]) { try { ifstream pgnfile("./sample.pgn"); pgn::GameCollection games; // collecting games from file pgnfile >> games; // producing a simple report int whiteWins = 0; int draws = 0; int blackWins = 0; for (pgn::GameCollection::iterator itr = games.begin(); itr != games.end(); itr++) { if (itr->result().isWhiteWin()) whiteWins++; else if (itr->result().isBlackWin()) blackWins++; else if (itr->result().isDrawn()) draws++; } cout << "games in file sample.pgn : " << games.size() << endl; cout << "white wins : " << whiteWins << endl; cout << "draws : " << draws << endl; cout << "black wins : " << blackWins << endl; } catch (pgn::Exception &e) { cerr << "exception: " << e.what() << endl; return -1; } return 0; } |
Our task here is to produce a very simple report and, to accomplish that, we need to know the result of each game in the sample.pgn
We start filling our GameCollection from sample.pgn file as we did in the previous example,
then we define three counters for white
wins, black wins and draw games.
At this point we meet a for loop introducing a new feature: the GameCollection iterator.
This is much like an iterator for a generic container from the standard
template library (STL), whose purpose is to cycle through
the elements of the collection, the Game objects.
At every cycle a new Game object is pointed by the 'itr' variable.
The class Game provides a result() method returning a GameResult
object: we query this object in order to know which counter we
have to increment.
The last thing to do is to display the report: we should get an output like
this:
games in file sample.pgn : 37 white wins : 21 draws : 5 black wins : 11
[3. Hello World!] | [home page] | [5. Give me the moves] |