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
|
//-----------------------------------------------------------------------------
/** @file libpentobi_base/Game.cpp
@author Markus Enzenberger
@copyright GNU General Public License version 3 or later */
//-----------------------------------------------------------------------------
#include "Game.h"
#include "libboardgame_base/SgfError.h"
#include "libboardgame_base/SgfUtil.h"
namespace libpentobi_base {
using libboardgame_base::SgfError;
//-----------------------------------------------------------------------------
Game::Game(Variant variant)
: m_bd(new Board(variant)),
m_tree(variant)
{
init(variant);
}
void Game::add_setup(Color c, Move mv)
{
auto& node = m_tree.add_setup(*m_current, c, mv);
goto_node(node);
}
void Game::delete_all_variations()
{
goto_node(back_to_main_variation(*m_current));
m_tree.delete_all_variations();
}
string Game::get_charset() const
{
return get_root().get_property("CA", "");
}
Color Game::get_to_play_default(const Game& game)
{
auto& tree = game.get_tree();
auto& bd = game.get_board();
auto node = &game.get_current();
Color next(0);
do
{
auto mv = tree.get_move(*node);
if (! mv.is_null())
{
next = bd.get_next(mv.color);
break;
}
Color c;
if (libpentobi_base::get_player(*node, bd.get_nu_colors(), c))
return c;
node = node->get_parent_or_null();
}
while (node != nullptr);
return bd.get_effective_to_play(next);
}
void Game::goto_node(const SgfNode& node)
{
auto old = m_current;
try
{
update(node);
}
catch (const SgfError&)
{
// Try to restore the old state.
if (&node != old)
{
try
{
update(*old);
}
catch (const SgfError&)
{
}
}
throw;
}
}
void Game::init(Variant variant)
{
m_bd->init(variant);
m_tree.init_variant(variant);
m_current = &m_tree.get_root();
}
void Game::init(unique_ptr<SgfNode>& root)
{
m_tree.init(root);
m_bd->init(m_tree.get_variant());
m_current = &m_tree.get_root();
goto_node(m_tree.get_root());
}
void Game::keep_only_position()
{
m_tree.keep_only_subtree(*m_current);
m_tree.remove_children(m_tree.get_root());
m_current = &m_tree.get_root();
goto_node(m_tree.get_root());
}
void Game::keep_only_subtree()
{
m_tree.keep_only_subtree(*m_current);
m_current = &m_tree.get_root();
goto_node(m_tree.get_root());
}
void Game::play(ColorMove mv, bool always_create_new_node)
{
m_bd->play(mv);
const SgfNode* child = nullptr;
if (! always_create_new_node)
child = m_tree.find_child_with_move(*m_current, mv);
if (child != nullptr)
m_current = child;
else
{
m_current = &m_tree.create_new_child(*m_current);
m_tree.set_move(*m_current, mv);
}
set_to_play(get_to_play_default(*this));
}
void Game::remove_player()
{
if (m_tree.remove_player(*m_current))
update(*m_current);
}
void Game::remove_setup(Color c, Move mv)
{
auto& node = m_tree.remove_setup(*m_current, c, mv);
goto_node(node);
}
void Game::set_player(Color c)
{
m_tree.set_player(*m_current, c);
update(*m_current);
}
void Game::set_to_play(Color c)
{
m_bd->set_to_play(c);
}
void Game::truncate()
{
goto_node(m_tree.truncate(*m_current));
}
void Game::undo()
{
LIBBOARDGAME_ASSERT(m_tree.has_move(*m_current));
LIBBOARDGAME_ASSERT(m_current->has_parent());
truncate();
}
void Game::update(const SgfNode& node)
{
m_updater.update(*m_bd, m_tree, node);
m_current = &node;
set_to_play(get_to_play_default(*this));
}
//-----------------------------------------------------------------------------
} // namespace libpentobi_base
|