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"
13 #include <core/state.h>
14 #include <core/variant.h>
21 using namespace boost
;
23 EngineEntity::EngineEntity(Variant
* variant
, const shared_ptr
<Game
>& game
, const IColor
* side
,
24 const shared_ptr
<Engine
>& engine
, AgentGroup
* group
)
31 , m_dispatcher(group
, this) { }
33 void EngineEntity::setup() {
35 m_engine
->setNotifier(shared_from_this());
39 // find all indexes of the current variation
40 std::stack
<Index
> m_indexes
;
41 Index index
= m_game
->index();
43 m_indexes
.push(index
);
44 std::cout
<< "pushed index " << index
<< std::endl
;
48 // set engine state playing all moves
49 // in the current variation
50 while (!m_indexes
.empty()) {
51 StatePtr state
= m_game
->position(index
);
53 index
= m_indexes
.top();
56 m_engine
->sendMove(m_game
->move(index
), state
);
59 m_last_index
= m_game
->index();
63 void EngineEntity::notifyEngineMove(const QString
& move_str
) {
64 // FIXME implement notifyEngineMove
66 if (position()->testMove(move
)) {
68 AbstractPosition::Ptr ref
= position();
69 AbstractPosition::Ptr pos
= ref
->clone();
71 m_game
->add(move
, pos
);
72 m_last_index
= m_game
->index();
73 m_dispatcher
.move(m_game
->index());
76 ERROR("Engine attempted to execute an invalid move: " << move_str
);
81 void EngineEntity::checkPlaying() {
82 if (!m_playing
&& m_side
==
83 m_game
->position(m_last_index
)->turn()) {
89 void EngineEntity::notifyMove(const Index
& index
) {
90 if (index
== m_last_index
) {
91 // TODO: check for consistency and update if necessary
93 else if (index
.prev() == m_last_index
) {
95 m_engine
->sendMove(m_game
->move(index
), m_game
->position(index
.prev()));
100 // TODO: handle move notification in arbitrary indexes
101 WARNING("engine entity can't handle index " << index
<< " (m_last_index = " << m_last_index
<< ")");