2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
11 #include "engineentity.h"
18 using namespace boost
;
20 EngineEntity::EngineEntity(const VariantPtr
& variant
, const shared_ptr
<Game
>& game
, int side
,
21 const shared_ptr
<Engine
>& engine
, AgentGroup
* group
)
28 , m_dispatcher(group
, this) { }
30 void EngineEntity::setup() {
32 m_engine
->setNotifier(shared_from_this());
36 // find all indexes of the current variation
37 std::stack
<Index
> m_indexes
;
38 Index index
= m_game
->index();
40 m_indexes
.push(index
);
41 kDebug() << "pushed index " << index
;
45 // set engine state playing all moves
46 // in the current variation
47 while (!m_indexes
.empty()) {
48 PositionPtr pos
= m_game
->position(index
);
52 index
= m_indexes
.top();
55 MovePtr move
= m_game
->move(index
);
56 kDebug() << "move = " << move
->toString("simple", pos
);
58 m_engine
->sendMove(move
, pos
);
61 m_last_index
= m_game
->index();
65 void EngineEntity::notifyEngineMove(const QString
& move_str
) {
66 AbstractMove::Ptr move
= position()->getMove(move_str
);
67 if (position()->testMove(move
)) {
69 AbstractPosition::Ptr ref
= position();
70 AbstractPosition::Ptr pos
= ref
->clone();
72 m_game
->add(move
, pos
);
73 m_last_index
= m_game
->index();
74 m_dispatcher
.move(m_game
->index());
77 kError() << "Engine attempted to execute an invalid move:" << move_str
;
80 void EngineEntity::checkPlaying() {
81 if (!m_playing
&& m_side
==
82 m_game
->position(m_last_index
)->turn()) {
88 void EngineEntity::notifyMove(const Index
& index
) {
89 if (index
== m_last_index
) {
90 // TODO: check for consistency and update if necessary
92 else if (index
.prev() == m_last_index
) {
94 m_engine
->sendMove(m_game
->move(index
), m_game
->position(index
.prev()));
99 // TODO: handle move notification in arbitrary indexes
100 kWarning() << "engine entity can't handle index" << index
<< "(m_last_index =" << m_last_index
<< ")";