File: History.cpp

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 (73 lines) | stat: -rw-r--r-- 2,172 bytes parent folder | download | duplicates (3)
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
//-----------------------------------------------------------------------------
/** @file libpentobi_mcts/History.cpp
    @author Markus Enzenberger
    @copyright GNU General Public License version 3 or later */
//-----------------------------------------------------------------------------

#include "History.h"

#include "libpentobi_base/BoardUtil.h"

namespace libpentobi_mcts {

using namespace std;
using libpentobi_base::get_current_position_as_setup;

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

void History::get_as_setup(Variant& variant, Setup& setup) const
{
    LIBBOARDGAME_ASSERT(is_valid());
    variant = m_variant;
    auto bd = make_unique<Board>(variant);
    for (ColorMove mv : m_moves)
        bd->play(mv);
    get_current_position_as_setup(*bd, setup);
}

void History::init(const Board& bd, Color to_play)
{
    m_is_valid = true;
    m_variant = bd.get_variant();
    m_nu_colors = bd.get_nu_colors();
    m_moves.clear();
    for (unsigned i = 0; i < bd.get_nu_moves(); ++i)
        m_moves.push_back(bd.get_move(i));
    m_to_play = to_play;
}

bool History::is_followup(
        const History& other,
        ArrayList<Move, SearchParamConst::max_moves>& sequence) const
{
    if (! m_is_valid || ! other.m_is_valid || m_variant != other.m_variant
            || m_moves.size() < other.m_moves.size())
        return false;
    unsigned i = 0;
    for ( ; i < other.m_moves.size(); ++i)
        if (m_moves[i] != other.m_moves[i])
            return false;
    sequence.clear();
    Color to_play = other.m_to_play;
    for ( ; i < m_moves.size(); ++i)
    {
        auto mv = m_moves[i];
        while (mv.color != to_play)
        {
            sequence.push_back(Move::null());
            to_play = to_play.get_next(m_nu_colors);
        }
        sequence.push_back(mv.move);
        to_play = to_play.get_next(m_nu_colors);
    }
    while (m_to_play != to_play)
    {
        sequence.push_back(Move::null());
        to_play = to_play.get_next(m_nu_colors);
    }
    return true;
}

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

} // namespace libpentobi_mcts