aboutsummaryrefslogtreecommitdiff
path: root/src/HalfMove.cpp
blob: 81d9a671694b30e29a6e637d8e6a7fdd275c5fcd (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
#include "HalfMove.hpp"

namespace pgnp {

HalfMove::HalfMove() : count(-1), isBlack(false), MainLine(NULL), NAG(0) {}

HalfMove::~HalfMove() {
  delete MainLine;
  for (auto *move : variations) {
    delete move;
  }
}

std::string HalfMove::NestedDump(const HalfMove *m, int indent) const{
  std::stringstream ss;

  for (int i = 0; i < indent; i++) {
    ss << "   ";
  }
  ss << "  "
     << " Move=" << m->move << " Count=" << m->count << " Comment=\""
     << m->comment << "\""
     << " NAG=" << m->NAG << " IsBlack=" << m->isBlack
     << " Variations=" << m->variations.size() << std::endl;

  for (auto *var : m->variations) {
    ss << NestedDump(var, indent + 1);
  }

  if (m->MainLine != NULL) {
    ss << NestedDump(m->MainLine, indent);
  }
  return (ss.str());
}

std::string HalfMove::Dump() const { return (NestedDump(this, 0)); }

int HalfMove::GetLength() const {
  int length = 0;
  const HalfMove *m = this;
  while (m != NULL) {
    length++;
    m = m->MainLine;
  }
  return length;
}

void HalfMove::Copy(HalfMove *copy) {
  copy->count = count;
  copy->isBlack = isBlack;
  copy->move = move;
  copy->comment = comment;
  copy->NAG = NAG;

  // Copy MainLine
  if (MainLine != NULL) {
    copy->MainLine = new HalfMove();
    MainLine->Copy(copy->MainLine);
  }

  // Copy variation
  for (HalfMove *var : variations) {
    HalfMove *new_var = new HalfMove();
    copy->variations.push_back(new_var);
    var->Copy(new_var);
  }
}

HalfMove *HalfMove::GetHalfMoveAt(int distance) {
  HalfMove *tmp = this;
  while (distance > 0) {
    if (tmp == NULL) {
      throw HalfMoveOutOfRange();
    }
    distance--;
    tmp = tmp->MainLine;
  }
  return (tmp);
}

} // namespace pgnp