-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.cpp
More file actions
76 lines (61 loc) · 1.34 KB
/
Node.cpp
File metadata and controls
76 lines (61 loc) · 1.34 KB
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
#include <algorithm>
#include <iostream>
#include "Node.h"
Node::Node(unsigned short team, double probability) {
this->team = team;
this->probability = probability;
}
Node::~Node() {
for (auto &child : children) {
delete child;
}
}
unsigned short Node::getTeam() const {
return this->team;
}
Node* Node::getChild(unsigned short key) {
for (auto &child : children) {
if (child->team == key) {
return child;
}
}
return nullptr;
}
void Node::addChild(unsigned short key, double probability) {
for (auto &child : children) {
if (child->team == key) {
return;
}
}
children.push_back(new Node(key, probability));
}
double Node::getProbability() const {
return probability;
}
void Node::updateProbability(double delta) {
probability += delta;
}
bool compareNodePointers(Node* a, Node* b) {
return (a->getProbability() > b->getProbability());
}
void Node::log(
unsigned short level,
double initialprobability
) {
std::sort(
children.begin(),
children.end(),
compareNodePointers);
std::cout.precision(4);
for (auto &child : children) {
for (int i = 0; i < level; i++) {
std::cout << "\t";
}
std::cout << "Rank " << level + 1 << ": "
<< child->team
<< " @ "
<< (child->probability / initialprobability * 100)
<< "%" << std::endl;
child->log(level + 1, child->probability);
}
}