Improve openings database code

This commit is contained in:
Loic Guegan 2023-01-16 14:55:48 +01:00
parent b7667d1d40
commit 3b11b9d4f3
19 changed files with 3709 additions and 28331 deletions

View file

@ -131,6 +131,39 @@ bool Game::Play(std::string move,char promotion) {
return (false);
}
void Game::GetOpening(std::string &name,std::string &eco){
HalfMove *m=current;
if(m == nullptr)
m=moves;
if(m!=nullptr){
// First check if opening already set
std::string cname,ceco;
m->GetOpening(cname,ceco);
if(ceco.size()>0){
name=cname;
eco=ceco;
}else {
// If not, get the current move line (or first move)
// and try to guess opening
auto line=m->GetLine(); // Vector of HalfMove
std::string pgn;
int count=1;
for(int i=0;i<line.size();i++){
if(i%2==0){
pgn+=std::to_string(count)+".";
count+=1;
}
pgn+=line[i]->move +" ";
}
// If there is a line, try to guess the opening
if(pgn.size()>0){
wxGetApp().GetBook().GuessOpening(pgn,name,eco);
m->SetOpening(name,eco);
}
}
}
}
void Game::Previous() {
if (current != nullptr) {
current = current->GetParent();

View file

@ -35,6 +35,7 @@ public:
HalfMove *GetMoves();
std::string GetFen();
std::string GetResult();
void GetOpening(std::string &name,std::string &eco);
/// @brief Play the given absolute move
bool Play(std::string move,char promotion='q');
bool IsBlackToPlay();

View file

@ -20,6 +20,38 @@ HalfMove::~HalfMove() {
}
}
void HalfMove::SetOpening(const std::string &name, const std::string &eco){
HalfMove *m=this;
while(m!=nullptr){
m->opening=name;
m->eco=eco;
if(m->parent != nullptr && m->parent->mainline==m)
m=m->parent;
else
break;
}
}
void HalfMove::GetOpening(std::string &name, std::string &eco){
name=this->opening;
eco=this->eco;
}
std::vector<HalfMove *> HalfMove::GetLine(){
std::vector<HalfMove *> line;
HalfMove *m=this;
while(m!=nullptr){
line.push_back(m);
// Check if in a variation:
if(m->parent!=nullptr && m->parent->mainline!=m)
m=m->parent->parent; // Because we are in a variation
else
m=m->parent;
}
std::reverse(line.begin(), line.end());
return line;
}
HalfMove::HalfMove(HalfMove *m){
src=m->src;
dst=m->dst;

View file

@ -20,6 +20,8 @@ class HalfMove : public cgeditor::CGEHalfMove {
void BuildAndVerify(HalfMove *m, std::string fen);
/// @brief Store the source and destination square of the current move (mainly used for pieces animation)
std::string src,dst;
/// @brief Opening reach by that move while taking into account all the parents
std::string opening, eco;
public:
HalfMove(HalfMove *m);
@ -49,9 +51,13 @@ public:
HalfMove *GetParent();
HalfMove *GetMainline();
std::vector<HalfMove *> GetVariations();
/// @brief Retrieve the list of moves from the current one to the first one
std::vector<HalfMove *> GetLine();
std::map<char, std::uint8_t> GetLineCaptures();
/// @brief The opening name of current line
void SetOpening(const std::string &name, const std::string &eco);
/// @brief Getters for name and eco
void GetOpening(std::string &name, std::string &eco);
/// @brief Set parent of the current move
void SetParent(HalfMove *m);
std::string GetFen();

View file

@ -81,7 +81,6 @@ void GameTabLeftPanel::OnPlay(wxCommandEvent &event) {
ProcessEvent(event);
}
}
Notify(true); // Redraw event is move failed! Otherwise piece not resets to it initial position after dragging
}

View file

@ -11,7 +11,7 @@ GameTabRightPanel::GameTabRightPanel(wxFrame *parent, std::shared_ptr<Game> game
tags_list->InsertColumn(1, L"Value", wxLIST_FORMAT_LEFT, 500);
tagTextCtrl->SetHint("Tag");
valueTextCtrl->SetHint("Value");
opening_label->SetHint("Current opening");
RefreshTagsList();
// Bind events
@ -42,6 +42,7 @@ GameTabRightPanel::GameTabRightPanel(wxFrame *parent, std::shared_ptr<Game> game
Bind(wxEVT_KEY_UP, [p=this](wxKeyEvent &e){e.ResumePropagation(1);e.Skip();});
ApplyPreferences();
analyze_game_button->Disable();
}
void GameTabRightPanel::OnLiveAnalysis(wxCommandEvent &event) {
@ -156,6 +157,11 @@ void GameTabRightPanel::Notify() {
if (live_engine != nullptr) {
live_engine->SetFEN(game->GetFen());
}
// Update opening name
std::string opening,eco;
game->GetOpening(opening,eco);
if(eco.size()>0)
opening_label->SetValue(eco+": "+opening);
}
void GameTabRightPanel::ApplyPreferences() {