Skip to content

Commit

Permalink
Fix terminal solver for MCTS_SINGLE_PLAYER (#168)
Browse files Browse the repository at this point in the history
Add template <NodeType nodeType> bool only_child_nodes_of_one_kind()
const
  • Loading branch information
QueensGambit committed Feb 16, 2022
1 parent 7e85a42 commit bc1c882
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
22 changes: 6 additions & 16 deletions engine/src/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,23 +144,18 @@ bool Node::at_least_one_drawn_child() const

bool Node::only_won_child_nodes() const
{
for (auto it = d->childNodes.begin(); it != d->childNodes.end(); ++it) {
const Node* childNode = it->get();
if (childNode->d->nodeType != WIN) {
return false;
}
}
return true;
return only_child_nodes_of_one_kind<WIN>();
}

bool Node::solved_loss(const Node* childNode) const
{
#ifndef MCTS_SINGLE_PLAYER
if (d->numberUnsolvedChildNodes == 0 && childNode->d->nodeType == WIN) {
return only_won_child_nodes();
#else
if (d->numberUnsolvedChildNodes == 0 && childNode->d->nodeType == LOSS) {
return only_child_nodes_of_one_kind<LOSS>();
#endif
return only_won_child_nodes();
}
return false;
}
Expand Down Expand Up @@ -210,23 +205,18 @@ bool Node::solved_tb_loss(const Node* childNode) const
{
#ifndef MCTS_SINGLE_PLAYER
if (d->numberUnsolvedChildNodes == 0 && childNode->d->nodeType == TB_WIN) {
return only_won_tb_child_nodes();
#else
if (d->numberUnsolvedChildNodes == 0 && childNode->d->nodeType == TB_LOSS) {
return only_child_nodes_of_one_kind<TB_LOSS>();
#endif
return only_won_tb_child_nodes();
}
return false;
}

bool Node::only_won_tb_child_nodes() const
{
for (auto it = d->childNodes.begin(); it != d->childNodes.end(); ++it) {
const Node* childNode = it->get();
if (childNode->d->nodeType != TB_WIN) {
return false;
}
}
return true;
return only_child_nodes_of_one_kind<TB_WIN>();
}

void Node::mark_as_tb_loss()
Expand Down
16 changes: 16 additions & 0 deletions engine/src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,22 @@ class Node
*/
bool only_won_child_nodes() const;

/**
* @brief only_child_nodes_of_one_kind Check if all expanded child nodes are of the same kind.
* @return true if only child nodes of type <nodeType> exist else false
*/
template <NodeType nodeType>
bool only_child_nodes_of_one_kind() const
{
for (auto it = d->childNodes.begin(); it != d->childNodes.end(); ++it) {
const Node* childNode = it->get();
if (childNode->d->nodeType != nodeType) {
return false;
}
}
return true;
}

/**
* @brief solved_loss Checks if the current node is a solved loss based on the given child node
* @param childNode Child nodes which backpropagates the value
Expand Down

0 comments on commit bc1c882

Please sign in to comment.