-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathTreeUtil.cpp
88 lines (80 loc) · 2.4 KB
/
TreeUtil.cpp
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
//-----------------------------------------------------------------------------
/** @file libpentobi_base/TreeUtil.cpp
@author Markus Enzenberger
@copyright GNU General Public License version 3 or later */
//-----------------------------------------------------------------------------
#include "TreeUtil.h"
namespace libpentobi_base {
//-----------------------------------------------------------------------------
const SgfNode* get_move_node(const PentobiTree& tree, const SgfNode& node,
unsigned n)
{
auto move_number = get_move_number(tree, node);
if (n == move_number)
return &node;
if (n < move_number)
{
auto current = &node;
do
{
if (tree.has_move(*current))
{
if (move_number == n)
return current;
--move_number;
}
if (libpentobi_base::has_setup(*current))
break;
current = current->get_parent_or_null();
}
while (current != nullptr);
}
else
{
auto current = node.get_first_child_or_null();
while (current != nullptr)
{
if (libpentobi_base::has_setup(*current))
break;
if (tree.has_move(*current))
{
++move_number;
if (move_number == n)
return current;
}
current = current->get_first_child_or_null();
}
}
return nullptr;
}
unsigned get_move_number(const PentobiTree& tree, const SgfNode& node)
{
unsigned move_number = 0;
auto current = &node;
do
{
if (tree.has_move(*current))
++move_number;
if (libpentobi_base::has_setup(*current))
break;
current = current->get_parent_or_null();
}
while (current != nullptr);
return move_number;
}
unsigned get_moves_left(const PentobiTree& tree, const SgfNode& node)
{
unsigned moves_left = 0;
auto current = node.get_first_child_or_null();
while (current != nullptr)
{
if (libpentobi_base::has_setup(*current))
break;
if (tree.has_move(*current))
++moves_left;
current = current->get_first_child_or_null();
}
return moves_left;
}
//-----------------------------------------------------------------------------
} // namespace libpentobi_base