pgnp/src/HalfMove.cpp

81 lines
1.7 KiB
C++
Raw Normal View History

2022-01-25 10:53:10 +01:00
#include "HalfMove.hpp"
namespace pgnp {
HalfMove::HalfMove() : count(-1), isBlack(false), MainLine(NULL) {}
HalfMove::~HalfMove() {
delete MainLine;
2022-01-25 10:53:10 +01:00
for (auto *move : variations) {
delete move;
}
}
2023-01-15 14:23:54 +01:00
std::string HalfMove::NestedDump(const HalfMove *m, int indent) const{
2022-01-25 10:53:10 +01:00
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;
2022-01-25 10:53:10 +01:00
for (auto *var : m->variations) {
ss << NestedDump(var, indent + 1);
}
if (m->MainLine != NULL) {
ss << NestedDump(m->MainLine, indent);
}
return (ss.str());
}
2023-01-15 14:23:54 +01:00
std::string HalfMove::Dump() const { return (NestedDump(this, 0)); }
2022-01-25 10:53:10 +01:00
2023-01-15 14:23:54 +01:00
int HalfMove::GetLength() const {
2022-01-25 10:53:10 +01:00
int length = 0;
2023-01-15 14:23:54 +01:00
const HalfMove *m = this;
2022-01-25 10:53:10 +01:00
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;
2022-01-25 10:53:10 +01:00
// 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);
}
}
2022-01-25 14:53:34 +01:00
HalfMove *HalfMove::GetHalfMoveAt(int distance) {
HalfMove *tmp = this;
while (distance > 0) {
if (tmp == NULL) {
2022-01-25 14:53:34 +01:00
throw HalfMoveOutOfRange();
}
distance--;
tmp = tmp->MainLine;
2022-01-25 14:53:34 +01:00
}
return (tmp);
2022-01-25 14:53:34 +01:00
}
2022-01-25 10:53:10 +01:00
} // namespace pgnp