aboutsummaryrefslogtreecommitdiff
path: root/src/game_tab
diff options
context:
space:
mode:
Diffstat (limited to 'src/game_tab')
-rw-r--r--src/game_tab/Game.cpp33
-rw-r--r--src/game_tab/Game.hpp1
-rw-r--r--src/game_tab/HalfMove.cpp32
-rw-r--r--src/game_tab/HalfMove.hpp10
-rw-r--r--src/game_tab/left_panel/GameTabLeftPanel.cpp1
-rw-r--r--src/game_tab/right_panel/GameTabRightPanel.cpp8
6 files changed, 81 insertions, 4 deletions
diff --git a/src/game_tab/Game.cpp b/src/game_tab/Game.cpp
index 315a4de..9028223 100644
--- a/src/game_tab/Game.cpp
+++ b/src/game_tab/Game.cpp
@@ -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();
diff --git a/src/game_tab/Game.hpp b/src/game_tab/Game.hpp
index 6f9bbb7..d6e1582 100644
--- a/src/game_tab/Game.hpp
+++ b/src/game_tab/Game.hpp
@@ -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();
diff --git a/src/game_tab/HalfMove.cpp b/src/game_tab/HalfMove.cpp
index 32924e1..0bac980 100644
--- a/src/game_tab/HalfMove.cpp
+++ b/src/game_tab/HalfMove.cpp
@@ -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;
diff --git a/src/game_tab/HalfMove.hpp b/src/game_tab/HalfMove.hpp
index 0666f38..bc56557 100644
--- a/src/game_tab/HalfMove.hpp
+++ b/src/game_tab/HalfMove.hpp
@@ -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();
diff --git a/src/game_tab/left_panel/GameTabLeftPanel.cpp b/src/game_tab/left_panel/GameTabLeftPanel.cpp
index 1d6ed61..6a8deec 100644
--- a/src/game_tab/left_panel/GameTabLeftPanel.cpp
+++ b/src/game_tab/left_panel/GameTabLeftPanel.cpp
@@ -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
}
diff --git a/src/game_tab/right_panel/GameTabRightPanel.cpp b/src/game_tab/right_panel/GameTabRightPanel.cpp
index 29f91b7..21b4ed7 100644
--- a/src/game_tab/right_panel/GameTabRightPanel.cpp
+++ b/src/game_tab/right_panel/GameTabRightPanel.cpp
@@ -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() {