Improve BaseTab pgn loading

This commit is contained in:
Loic Guegan 2022-02-24 15:22:56 +01:00
parent 40c6df0e7c
commit f99a7b699a
7 changed files with 87 additions and 48 deletions

View file

@ -4,7 +4,8 @@
class GameBase {
public:
virtual bool HasNextGame() = 0;
virtual Game *GetGame(std::uint32_t id) = 0;
virtual Game *GetNextGame() = 0;
virtual bool NextGame() = 0;
virtual std::string GetTag(std::string tag) = 0;
virtual void Reset() = 0;
};

View file

@ -1,35 +1,54 @@
#include "PGNGameBase.hpp"
PGNGameBase::PGNGameBase(std::string pgn_file)
: pgn(new pgnp::PGN()), hasNextGame(false) {
PGNGameBase::PGNGameBase(std::string pgn_file) : pgn(new pgnp::PGN()) {
file = pgn_file;
pgn->FromFile(pgn_file);
ParseNextGame();
}
bool PGNGameBase::HasNextGame() { return (hasNextGame); }
void PGNGameBase::ParseNextGame() {
bool PGNGameBase::NextGame() {
bool game_found = false;
try {
pgn->ParseNextGame();
hasNextGame = true;
game_found = true;
} catch (...) {
hasNextGame = false;
game_found = false;
}
return (game_found);
}
Game *PGNGameBase::GetNextGame() {
pgnp::HalfMove *pgnp_moves = new pgnp::HalfMove();
pgn->GetMoves(pgnp_moves);
std::string fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
if (pgn->HasTag("FEN")) {
fen = pgn->GetTagValue("FEN");
std::string PGNGameBase::GetTag(std::string tag) {
if (pgn->HasTag(tag)) {
return (pgn->GetTagValue(tag));
}
HalfMove *m = new HalfMove(pgnp_moves, fen);
Game *g = new Game(m, fen);
for (std::string &s : pgn->GetTagList()) {
g->SetTag(s, pgn->GetTagValue(s));
}
ParseNextGame();
return (g);
return ("");
}
void PGNGameBase::Reset() {
delete pgn;
pgn = new pgnp::PGN();
pgn->FromFile(file);
}
Game *PGNGameBase::GetGame(std::uint32_t id) {
Reset();
std::uint32_t curid = 0;
while(NextGame()) {
if (id == curid) {
pgnp::HalfMove *pgnp_moves = new pgnp::HalfMove();
pgn->GetMoves(pgnp_moves);
std::string fen =
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
if (pgn->HasTag("FEN")) {
fen = pgn->GetTagValue("FEN");
}
HalfMove *m = new HalfMove(pgnp_moves, fen);
Game *g = new Game(m, fen);
for (std::string &s : pgn->GetTagList()) {
g->SetTag(s, pgn->GetTagValue(s));
}
return (g);
}
curid++;
}
return (NULL);
}

View file

@ -2,14 +2,14 @@
#include "pgnp.hpp"
class PGNGameBase : public GameBase {
pgnp::PGN *pgn;
bool hasNextGame;
pgnp::PGN *pgn;
bool hasNextGame;
std::string file;
void ParseNextGame();
public:
PGNGameBase(std::string pgn_file);
bool HasNextGame();
Game *GetGame(std::uint32_t id) { return (new Game()); };
Game *GetNextGame();
Game *GetGame(std::uint32_t id);
bool NextGame();
std::string GetTag(std::string tag);
void Reset();
};