File: NodeUtil.cpp

package info (click to toggle)
pentobi 26.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 4,068 kB
  • sloc: cpp: 25,986; javascript: 897; xml: 57; makefile: 13; sh: 11
file content (198 lines) | stat: -rw-r--r-- 5,404 bytes parent folder | download | duplicates (4)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//-----------------------------------------------------------------------------
/** @file libpentobi_base/NodeUtil.cpp
    @author Markus Enzenberger
    @copyright GNU General Public License version 3 or later */
//-----------------------------------------------------------------------------

#include "NodeUtil.h"

namespace libpentobi_base {

using libboardgame_base::InvalidProperty;

//-----------------------------------------------------------------------------

bool has_move(const SgfNode& node, Variant variant)
{
    // See also comment in get_move()
    switch (get_nu_colors(variant))
    {
    case 2:
        for (auto& prop : node.get_properties())
        {
            auto& id = prop.id;
            if (id == "B" || id == "W" || id == "1" || id == "2"
                    || id == "BLUE" || id == "GREEN")
                return true;
        }
        break;
    case 3:
        for (auto& prop : node.get_properties())
        {
            auto& id = prop.id;
            if (id == "1" || id == "2" || id == "3" || id == "BLUE"
                    || id == "YELLOW" || id == "RED")
                return true;
        }
        break;
    case 4:
        for (auto& prop : node.get_properties())
        {
            auto& id = prop.id;
            if (id == "1" || id == "2" || id == "3" || id == "4"
                    || id == "BLUE" || id == "YELLOW" || id == "RED"
                     || id == "GREEN")
                return true;
        }
        break;
    default:
        LIBBOARDGAME_ASSERT(false);
    }
    return false;
}

bool get_move(const SgfNode& node, Variant variant, Color& c,
              MovePoints& points)
{
    auto nu_colors = get_nu_colors(variant);
    string id;
    // Pentobi 0.1 used BLUE/YELLOW/RED/GREEN instead of 1/2/3/4 as suggested
    // by SGF FF[5]. Pentobi 12.0 erroneosly used 1/2 for two-player Callisto
    // instead of B/W. We still want to be able to read files written by older
    // versions. They will be converted to the current format by
    // PentobiTreeWriter.
    if (nu_colors == 2)
    {
        if (node.has_property("B"))
        {
            id = "B";
            c = Color(0);
        }
        else if (node.has_property("W"))
        {
            id = "W";
            c = Color(1);
        }
        else if (node.has_property("1"))
        {
            id = "1";
            c = Color(0);
        }
        else if (node.has_property("2"))
        {
            id = "2";
            c = Color(1);
        }
        else if (node.has_property("BLUE"))
        {
            id = "BLUE";
            c = Color(0);
        }
        else if (node.has_property("GREEN"))
        {
            id = "GREEN";
            c = Color(1);
        }
    }
    else
    {
        if (node.has_property("1"))
        {
            id = "1";
            c = Color(0);
        }
        else if (node.has_property("2"))
        {
            id = "2";
            c = Color(1);
        }
        else if (node.has_property("3"))
        {
            id = "3";
            c = Color(2);
        }
        else if (node.has_property("4"))
        {
            id = "4";
            c = Color(3);
        }
        else if (node.has_property("BLUE"))
        {
            id = "BLUE";
            c = Color(0);
        }
        else if (node.has_property("YELLOW"))
        {
            id = "YELLOW";
            c = Color(1);
        }
        else if (node.has_property("RED"))
        {
            id = "RED";
            c = Color(2);
        }
        else if (node.has_property("GREEN"))
        {
            id = "GREEN";
            c = Color(3);
        }
    }
    if (id.empty() || c.to_int() >= nu_colors)
        return false;
    // Note: we still support having the points of a move in a list of point
    // values instead of a single value as used by Pentobi <= 0.2, but it
    // is deprecated
    points.clear();
    auto& geo = get_geometry(variant);
    for (auto& s : node.get_multi_property(id))
    {
        auto begin = s.begin();
        auto end = begin;
        while (true)
        {
            while (end != s.end() && *end != ',')
                ++end;
            Point p;
            if (! geo.from_string(begin, end, p)
                    || points.size() == MovePoints::max_size)
                throw InvalidProperty(id, string(begin, end));
            points.push_back(p);
            if (end == s.end())
                break;
            ++end;
            begin = end;
        }
    }
    return true;
}

bool get_player(const SgfNode& node, Color::IntType nu_colors, Color& c)
{
    if (! node.has_property("PL"))
        return false;
    string value = node.get_property("PL");
    if (value == "B" || value == "1")
        c = Color(0);
    else if (value == "W" || value == "2")
        c = Color(1);
    else if (value == "3" && nu_colors > 2)
        c = Color(2);
    else if (value == "4" && nu_colors > 3)
        c = Color(3);
    else
        return false;
    return true;
}

bool has_setup(const SgfNode& node)
{
    for (auto& i : node.get_properties())
        if (i.id == "AB" || i.id == "AW" || i.id == "A1" || i.id == "A2"
                || i.id == "A3" || i.id == "A4" || i.id == "AE")
            return true;
    return false;
}

//-----------------------------------------------------------------------------

} // namespace libpentobi_base