aboutsummaryrefslogtreecommitdiff
path: root/src/CMI.cpp
blob: a57bcebf743381f68ff96eabbf17f8f664f1ab90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "CMI.hpp"

namespace CMI {

HalfMove::HalfMove()
    : number(1), isBlack(false), NAG(0), parent(nullptr), mainline(nullptr) {}

HalfMove::HalfMove(const std::string &SAN)
    : number(1), isBlack(false), parent(nullptr), mainline(nullptr) {}

HalfMove::HalfMove(const std::string &SAN, const std::string &comment,
                   std::uint16_t number, std::uint8_t NAG, bool isBlack)
    : SAN(SAN), comment(comment), number(number), NAG(NAG), isBlack(isBlack),
      parent(nullptr), mainline(nullptr) {}

HalfMove::~HalfMove() {
  if (mainline != nullptr)
    delete mainline;
  for (HalfMove *v : variations)
    delete v;
}

void HalfMove::SetParent(HalfMove *m) { parent = m; }

std::vector<HalfMove *> HalfMove::GetVariations() const { return variations; }

HalfMove *HalfMove::GetMainline() const { return mainline; };

HalfMove *HalfMove::GetParent() const { return parent; };

std::string HalfMove::GetSAN() const { return SAN; };

void HalfMove::SetSAN(const std::string &newSAN) { SAN = newSAN; };

std::uint16_t HalfMove::GetNumber() const { return number; };

void HalfMove::SetNumber(std::uint16_t n) { number = n; };

std::uint8_t HalfMove::GetNAG() const { return NAG; };

void HalfMove::SetNAG(std::uint8_t n) { NAG = n; };

std::string HalfMove::GetComment() const { return comment; };

void HalfMove::SetComment(const std::string &c) { comment = c; };

bool HalfMove::IsBlack() const { return isBlack; };

void HalfMove::SetIsBlack(bool b) { isBlack = b; };

void HalfMove::ClearVariations() { variations.clear(); }

void HalfMove::SetMainline(HalfMove *m) {
  mainline = m;
  if (m != nullptr) {
    if (!this->isBlack) {
      m->SetIsBlack(true);
      m->SetNumber(this->number);
    } else {
      m->SetIsBlack(false);
      m->SetNumber(this->number + 1);
    }
    m->SetParent(this);
  }
}

void HalfMove::Promote() {
  HalfMove *broot = GetBranchRoot();
  if (broot != nullptr) {
    HalfMove *parent = broot->GetParent();
    if (parent != nullptr) {
      if (parent->GetMainline() != broot) {
        HalfMove *pparent = parent->GetParent();
        // First update parent of parent:
        if (pparent != nullptr) {
          if (pparent->GetMainline() == parent)
            pparent->SetMainline(broot);
          else {
            pparent->AddVariation(broot);
            pparent->RemoveChild(parent);
          }
        }
        // Now update parent:
        parent->RemoveChild(broot);
        broot->AddVariation(parent);
      }
    }
  }
}

void HalfMove::SetAsMainline() {
  HalfMove *broot = GetBranchRoot();
  HalfMove *lastRoot;
  // Just promote until we cannot anymore
  do {
    lastRoot = broot;
    broot->Promote();
    broot = GetBranchRoot();
  } while (broot != lastRoot);
}

HalfMove *HalfMove::GetBranchRoot() {
  HalfMove *m = this;
  HalfMove *p = parent;
  while (p != nullptr) {
    if (p->GetMainline() != m) {
      return (m);
    }
    m = p;
    p = m->GetParent();
  }
  return m;
}

void HalfMove::AddVariation(HalfMove *m) {
  m->SetIsBlack(IsBlack());
  m->SetNumber(GetNumber());
  m->SetParent(this);
  variations.push_back(m);
}

bool HalfMove::RemoveVariation(HalfMove *m) {
  std::vector<HalfMove *> vars;
  bool removed = false;
  for (HalfMove *v : GetVariations()) {
    if (m != v)
      vars.push_back(v);
    else
      removed = true;
  }
  if (removed)
    variations = vars;
  return removed;
}

bool HalfMove::RemoveChild(HalfMove *m) {
  if (GetMainline() == m) {
    mainline = nullptr;
    return true;
  }
  return RemoveVariation(m);
}

bool HalfMove::Contains(HalfMove *m) const {
  if (m == nullptr)
    return false;
  else if (this == m)
    return true;
  else if (mainline != nullptr && mainline->Contains(m)) {
    return true;
  } else {
    for (HalfMove *v : variations) {
      if (v->Contains(m))
        return true;
    }
  }
  return false;
}

bool HalfMove::IsConsistent() const {
  // Check mainline
  if (mainline != nullptr) {
    if (isBlack == mainline->isBlack)
      return false;
    if (isBlack && number + 1 != mainline->number)
      return false;
    if (!isBlack && number != mainline->number)
      return false;
    if (mainline->parent != this)
      return false;
    if (!mainline->IsConsistent())
      return false;
  }
  // Check variations
  for (HalfMove *v : variations) {
    if (number != v->number || isBlack != v->isBlack || v->parent != this)
      return false;
    if (!v->IsConsistent())
      return false;
  }
  return true;
}
} // namespace CMI