Replaced all std::cout with kDebug.
[tagua/yd.git] / src / entities / engineentity.cpp
blob83db0009ff340e5bc71ed95b71d1c4ad1420b485
1 /*
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.
9 */
11 #include "engineentity.h"
12 #include <KDebug>
13 #include "game.h"
14 #include "engine.h"
16 #include <stack>
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)
22 : Entity(game)
23 , m_variant(variant)
24 , m_side(side)
25 , m_last_index(0)
26 , m_playing(false)
27 , m_engine(engine)
28 , m_dispatcher(group, this) { }
30 void EngineEntity::setup() {
31 // setup engine
32 m_engine->setNotifier(shared_from_this());
33 m_engine->start();
34 m_engine->reset();
36 // find all indexes of the current variation
37 std::stack<Index> m_indexes;
38 Index index = m_game->index();
39 while (index != 0) {
40 m_indexes.push(index);
41 kDebug() << "pushed index " << index;
42 index = index.prev();
45 // set engine state playing all moves
46 // in the current variation
47 while (!m_indexes.empty()) {
48 PositionPtr pos = m_game->position(index);
49 kDebug() << "REF:";
50 pos->dump();
52 index = m_indexes.top();
53 m_indexes.pop();
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();
62 checkPlaying();
65 void EngineEntity::notifyEngineMove(const QString& move_str) {
66 AbstractMove::Ptr move = position()->getMove(move_str);
67 if (position()->testMove(move)) {
68 Q_ASSERT(move);
69 AbstractPosition::Ptr ref = position();
70 AbstractPosition::Ptr pos = ref->clone();
71 pos->move(move);
72 m_game->add(move, pos);
73 m_last_index = m_game->index();
74 m_dispatcher.move(m_game->index());
76 else
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()) {
83 m_engine->play();
84 m_playing = true;
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) {
93 // play move
94 m_engine->sendMove(m_game->move(index), m_game->position(index.prev()));
95 m_last_index = index;
96 checkPlaying();
98 else {
99 // TODO: handle move notification in arbitrary indexes
100 kWarning() << "engine entity can't handle index" << index << "(m_last_index =" << m_last_index << ")";