diff options
Diffstat (limited to 'src/game_tab/HalfMove.cpp')
| -rw-r--r-- | src/game_tab/HalfMove.cpp | 202 |
1 files changed, 46 insertions, 156 deletions
diff --git a/src/game_tab/HalfMove.cpp b/src/game_tab/HalfMove.cpp index e152ce0..e1c2926 100644 --- a/src/game_tab/HalfMove.cpp +++ b/src/game_tab/HalfMove.cpp @@ -2,22 +2,13 @@ HalfMove::HalfMove(std::string move_absolute,std::string move_san) : capture(' ') { SetAbsoluteMove(move_absolute); - this->move = move_san; + SetSAN(move_san); fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; } HalfMove::HalfMove(std::string move_absolute, std::string move_san, std::string fen) : fen(fen), capture(' ') { SetAbsoluteMove(move_absolute); - this->move = move_san; -} - -HalfMove::~HalfMove() { - if (mainline != nullptr) { - delete mainline; - } - for (HalfMove *m : variations) { - delete m; - } + SetSAN(move_san); } void HalfMove::SetOpening(const std::string &name, const std::string &eco){ @@ -25,8 +16,8 @@ void HalfMove::SetOpening(const std::string &name, const std::string &eco){ while(m!=nullptr){ m->opening=name; m->eco=eco; - if(m->parent != nullptr && m->parent->mainline==m) - m=m->parent; + if(m->GetParent() != nullptr && static_cast<HalfMove*>(m->GetParent()->GetMainline())==m) + m=static_cast<HalfMove*>(m->GetParent()); else break; } @@ -43,10 +34,10 @@ std::vector<HalfMove *> HalfMove::GetLine(){ 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 + if(m->GetParent()!=nullptr && static_cast<HalfMove*>(m->GetParent()->GetMainline())!=m) + m=static_cast<HalfMove*>(m->GetParent()->GetParent()); // Because we are in a variation else - m=m->parent; + m=static_cast<HalfMove*>(m->GetParent()); } // Reverse the order to get it in the played order: std::reverse(line.begin(), line.end()); @@ -64,7 +55,7 @@ std::string HalfMove::GetLineAsSAN(){ pgn+=std::to_string(count)+"."; count+=1; } - pgn+=line[i]->move +" "; + pgn+=line[i]->GetSAN() +" "; } return pgn; } @@ -72,29 +63,21 @@ std::string HalfMove::GetLineAsSAN(){ HalfMove::HalfMove(HalfMove *m){ src=m->src; dst=m->dst; - move=m->move; + SetSAN(m->GetSAN()); fen=m->fen; capture=m->capture; - IsBlack = m->IsBlack; - Number = m->Number; - nag = m->nag; - comment=m->comment; - if(m->mainline != nullptr){ - SetMainline(new HalfMove(m->mainline)); + SetIsBlack(m->IsBlack()); + SetNumber(m->GetNumber()); + SetNAG(m->GetNAG()); + SetComment(m->GetComment()); + if(m->GetMainline() != nullptr){ + SetMainline(static_cast<CMI::HalfMove*>(new HalfMove(static_cast<HalfMove*>(m->GetMainline())))); } - for (int i=0; i < m->variations.size(); i++) { - AddVariation(new HalfMove(m->variations[i])); + for (CMI::HalfMove *v: m->GetVariations()) { + AddVariation(new HalfMove(static_cast<HalfMove*>(v))); } } -void HalfMove::AddVariation(HalfMove *m) { - m->IsBlack = this->IsBlack; - m->Number = this->Number; - HalfMove::variations.push_back(m); - cgeditor::CGEHalfMove::variations.push_back(m); - m->SetParent(this); -} - std::map<char, std::uint8_t> HalfMove::GetLineCaptures() { std::map<char, std::uint8_t> captures; HalfMove *m = this; @@ -105,7 +88,7 @@ std::map<char, std::uint8_t> HalfMove::GetLineCaptures() { } else { captures[c] = 1; } - m = m->parent; + m = static_cast<HalfMove*>(m->GetParent()); } while (m != nullptr); return (captures); } @@ -113,96 +96,30 @@ std::map<char, std::uint8_t> HalfMove::GetLineCaptures() { void HalfMove::SetCapture(char c) { capture = c; } void HalfMove::AddMove(HalfMove *m) { - if (this->mainline == nullptr) { + if (GetMainline() == nullptr) { SetMainline(m); } else { - if (mainline != nullptr) { - mainline->AddVariation(m); - } - } -} - -void HalfMove::SetMainline(HalfMove *m) { - if (!this->IsBlack) { - m->IsBlack = true; - m->Number = this->Number; - } else { - m->IsBlack = false; - m->Number = this->Number + 1; - } - HalfMove::mainline = m; - cgeditor::CGEHalfMove::MainLine = m; - if (m != nullptr) { - m->SetParent(this); + if(GetMainline()!=nullptr) + GetMainline()->AddVariation(m); } } -void HalfMove::SetParent(HalfMove *m) { - HalfMove::parent = m; - CGEHalfMove::Parent = m; -} - -void HalfMove::RemoveChild(HalfMove *m) { - std::uint32_t i = 0; - bool found = false; - for (i; i < HalfMove::variations.size(); i++) { - if (HalfMove::variations[i] == m) { - found = true; - break; - } - } - if (found) { - HalfMove::variations.erase(HalfMove::variations.begin() + i); - } - if (HalfMove::mainline == m) { - HalfMove::mainline = nullptr; - } - cgeditor::CGEHalfMove::RemoveChild((CGEHalfMove *)m); -} - -HalfMove *HalfMove::GetParent() { return (parent); } - -HalfMove *HalfMove::GetRoot() { - HalfMove *m = this; - HalfMove *p = HalfMove::parent; - while (p != nullptr) { - if (p->mainline != m) { - return (m); - } - m = p; - p = m->HalfMove::parent; - } - return (m); -} - -void HalfMove::SetAsMainline() { - HalfMove *root = GetRoot(); - HalfMove *lastRoot; - do { - lastRoot = root; - root->HalfMove::Promote(); - root = GetRoot(); - } while (root != lastRoot); -} - -HalfMove *HalfMove::GetMainline() { return (mainline); } - void HalfMove::SetAbsoluteMove(const std::string &move_absolute){ this->src=move_absolute.substr(0,2); this->dst=move_absolute.substr(2,2); } -HalfMove::HalfMove(pgnp::HalfMove *m) : capture(' ') { - this->move = m->move; - this->nag = m->NAG; - this->IsBlack = m->isBlack; - this->comment=m->comment; - this->Number = m->count; - if (m->MainLine != nullptr) { - this->SetMainline(new HalfMove(m->MainLine)); +HalfMove::HalfMove(CMI::HalfMove *m) : capture(' ') { + SetSAN(m->GetSAN()); + SetNAG(m->GetNAG()); + SetIsBlack(m->IsBlack()); + SetComment(m->GetComment()); + SetNumber(m->GetNumber()); + if (m->GetMainline() != nullptr) { + SetMainline(new HalfMove(m->GetMainline())); } - for (pgnp::HalfMove *v : m->variations) { - this->AddVariation(new HalfMove(v)); + for (CMI::HalfMove *v : m->GetVariations()) { + AddVariation(new HalfMove(v)); } } @@ -213,42 +130,19 @@ void HalfMove::GetAbsoluteMove(std::string &src,std::string &dst){ void HalfMove::SetFen(std::string fen) { this->fen = fen; } -void HalfMove::Promote() { - HalfMove *root = GetRoot(); - if (root->parent != nullptr) { - HalfMove *p = root->parent; - if (p->HalfMove::mainline != root) { - if (root->parent->HalfMove::parent != nullptr) { - HalfMove *pp = root->parent->HalfMove::parent; - if (pp->HalfMove::mainline == p) { - pp->HalfMove::SetMainline(root); - } else { - pp->AddVariation(root); - pp->HalfMove::RemoveChild(p); - } - } - if (p->HalfMove::mainline == root) { - p->HalfMove::SetMainline(nullptr); - } else { - p->HalfMove::RemoveChild(root); - } - root->AddVariation(p); - } - } -} bool HalfMove::HasParent(HalfMove*m){ - return m==parent; + return m==static_cast<HalfMove*>(GetParent()); } bool HalfMove::HasChild(HalfMove*m){ if(m==nullptr) return false; - if(mainline==m){ + if(static_cast<HalfMove*>(GetMainline())==m){ return true; } - for(auto var: variations){ - if(var == m) + for(auto var: GetVariations()){ + if(static_cast<HalfMove*>(var) == m) return true; } return false; @@ -256,38 +150,34 @@ bool HalfMove::HasChild(HalfMove*m){ bool HalfMove::IsVariation() { HalfMove *m = this; - HalfMove *p = HalfMove::parent; + HalfMove *p = static_cast<HalfMove*>(GetParent()); while (p != nullptr) { - if (p->mainline != m) { + if (static_cast<HalfMove*>(p->GetMainline()) != m) { return (true); } m = p; - p = m->HalfMove::parent; + p = static_cast<HalfMove*>(m->GetParent()); } return (false); } std::string HalfMove::GetFen() { return (fen); } -std::vector<HalfMove *> HalfMove::GetVariations() { return (variations); } - -bool HalfMove::IsABlackMove() { return (IsBlack); } - void HalfMove::BuildAndVerify(HalfMove *m, std::string fen) { arbiter.Setup(fen); - std::string move_absolute=arbiter.ParseSAN(m->move); + std::string move_absolute=arbiter.ParseSAN(m->GetSAN()); m->SetAbsoluteMove(move_absolute); - bool work = arbiter.Play(move_absolute,arbiter.ParseSANPromotion(m->move)); + bool work = arbiter.Play(move_absolute,arbiter.ParseSANPromotion(m->GetSAN())); if (!work) { - wxLogDebug("Bug! %s", m->move); + wxLogDebug("Bug! %s", m->GetSAN()); } char capture = arbiter.GetCapture(); if (capture != ' ') { m->capture = capture; } m->fen = arbiter.GetFEN(); - if (m->mainline != nullptr) { - BuildAndVerify(m->mainline, arbiter.GetFEN()); + if (m->GetMainline() != nullptr) { + BuildAndVerify(static_cast<HalfMove*>(m->GetMainline()), arbiter.GetFEN()); } else { // Otherwise we are on a leaf! So, guess the opening: std::string name,eco; @@ -295,10 +185,10 @@ void HalfMove::BuildAndVerify(HalfMove *m, std::string fen) { if(eco.size()>0) m->SetOpening(name,eco); } - for (HalfMove *v : m->variations) { - BuildAndVerify(v, fen); + for (CMI::HalfMove *v : m->GetVariations()) { + BuildAndVerify(static_cast<HalfMove*>(v), fen); } } void HalfMove::BuildAndVerify(std::string initial_fen) { BuildAndVerify(this, initial_fen); -}
\ No newline at end of file +} |
