File: PentobiTree.h

package info (click to toggle)
pentobi 29.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,892 kB
  • sloc: cpp: 25,719; javascript: 875; xml: 40; makefile: 13; sh: 6
file content (142 lines) | stat: -rw-r--r-- 4,159 bytes parent folder | download | duplicates (2)
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
//-----------------------------------------------------------------------------
/** @file libpentobi_base/PentobiTree.h
    @author Markus Enzenberger
    @copyright GNU General Public License version 3 or later */
//-----------------------------------------------------------------------------

#ifndef LIBPENTOBI_BASE_PENTOBI_TREE_H
#define LIBPENTOBI_BASE_PENTOBI_TREE_H

#include "ColorMove.h"
#include "BoardConst.h"
#include "NodeUtil.h"
#include "Variant.h"
#include "Setup.h"
#include "PentobiSgfUtil.h"
#include "libboardgame_base/SgfTree.h"

namespace libpentobi_base {

using libboardgame_base::SgfNode;
using libboardgame_base::SgfTree;

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

/** Blokus SGF tree.
    See also doc/blksgf/Pentobi-SGF.html in the Pentobi distribution for
    a description of the properties used. */
class PentobiTree
    : public SgfTree
{
public:
    /** Parse the GM property of a root node.
        @throws MissingProperty
        @throws InvalidProperty */
    static Variant get_variant(const SgfNode& root);


    explicit PentobiTree(Variant variant);

    explicit PentobiTree(unique_ptr<SgfNode>& root);

    void init(unique_ptr<SgfNode>& root) override;

    void init_variant(Variant variant);

    void set_move(const SgfNode& node, ColorMove mv);

    void set_move(const SgfNode& node, Color c, Move mv);

    bool has_move(const SgfNode& node) const;

    /** Return move or ColorMove::null() if node has no move property.
        @throws SgfError if the node has a move property with an invalid
        value. */
    ColorMove get_move(const SgfNode& node) const;

    const SgfNode* find_child_with_move(const SgfNode& node,
                                        ColorMove mv) const;

    const SgfNode* get_node_before_move_number(unsigned move_number) const;

    Variant get_variant() const;

    string get_player_name(Color c) const;

    void set_player_name(Color c, const string& name);

    void keep_only_subtree(const SgfNode& node);

    /** Add a piece as setup.
        @pre ! mv.is_null()
        If the node already contains a move, a new child will be created.
        @pre The piece points must be empty on the board
        @return The node or the new child if one was created. */
    const SgfNode& add_setup(const SgfNode& node, Color c, Move mv);

    /** Remove a piece using setup properties.
        @pre ! mv.is_null()
        If the node already contains a move, a new child will be created.
        @pre The move must exist on the board
        @return The node or the new child if one was created. */
    const SgfNode& remove_setup(const SgfNode& node, Color c, Move mv);

    /** Set the color to play in a setup position (PL property). */
    void set_player(const SgfNode& node, Color c);

    /** Remove the PL property.
        @see set_player() */
    bool remove_player(const SgfNode& node);

private:
    Variant m_variant;

    const BoardConst* m_bc;

    const char* get_color(Color c) const;

    Setup::PlacementList get_setup_property(const SgfNode& node,
                                            const char* id) const;

    const char* get_setup_prop_id(Color c) const;

    void set_setup(const SgfNode& node, const Setup& setup);

    void init_board_const(Variant variant);

    void set_game_property();

    void set_setup_property(const SgfNode& node, const char* id,
                            const Setup::PlacementList& placements);
};

inline const char* PentobiTree::get_color(Color c) const
{
    return get_color_id(m_variant, c);
}

inline const char* PentobiTree::get_setup_prop_id(Color c) const
{
    return get_setup_id(m_variant, c);
}

inline Variant PentobiTree::get_variant() const
{
    return m_variant;
}

inline bool PentobiTree::has_move(const SgfNode& node) const
{
    return libpentobi_base::has_move(node, m_variant);
}

inline void PentobiTree::set_move(const SgfNode& node, ColorMove mv)
{
    set_move(node, mv.color, mv.move);
}

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

} // namespace libpentobi_base

#endif // LIBPENTOBI_BASE_PENTOBI_TREE_H