From 408e61013437be201aa18c1728022c18b760a164 Mon Sep 17 00:00:00 2001 From: Yann Dirson Date: Sun, 20 Apr 2008 21:52:42 +0200 Subject: [PATCH] Finalize support for delegating behaviours. --- src/core/behaviour.h | 3 +++ src/core/delegators/behaviour.h | 16 ++++++++++++---- src/variants/chess/behaviour.h | 2 +- src/variants/crazyhouse/behaviour.cpp | 9 ++++++++- src/variants/crazyhouse/behaviour.h | 7 +++++-- src/variants/crazyhouse/crazyhouse.cpp | 10 ++++++++-- 6 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/core/behaviour.h b/src/core/behaviour.h index bfecefb..7438e74 100644 --- a/src/core/behaviour.h +++ b/src/core/behaviour.h @@ -22,6 +22,7 @@ class Move; * @brief Encapsulates general rules on piece movement. */ class TAGUA_EXPORT IBehaviour: public Component { +Q_OBJECT public: virtual ~IBehaviour(); @@ -61,6 +62,8 @@ public: * @return Direction associated with @a player. */ virtual Point direction(const IColor* player) const = 0; + + virtual void setDelegator(IBehaviour* behaviour) = 0; }; #endif // CORE__BEHAVIOUR_H diff --git a/src/core/delegators/behaviour.h b/src/core/delegators/behaviour.h index 2447a94..f8778fc 100644 --- a/src/core/delegators/behaviour.h +++ b/src/core/delegators/behaviour.h @@ -12,13 +12,19 @@ #include "../behaviour.h" +#include + namespace Delegators { class Behaviour : public IBehaviour { protected: - const IBehaviour* m_dgate_behaviour; + IBehaviour* m_dgate_behaviour; public: - Behaviour(const IBehaviour* behaviour) : m_dgate_behaviour(behaviour) { } + Behaviour(IBehaviour* delegate): m_dgate_behaviour(delegate) { + Q_ASSERT(delegate); + m_dgate_behaviour->setDelegator(this); + //kDebug() << this << "m_dgate_behaviour=" << m_dgate_behaviour; + } virtual ~Behaviour() { delete m_dgate_behaviour; } virtual void captureOn(IState* state, const Point& square) const { @@ -39,10 +45,12 @@ public: virtual Point direction(const IColor* player) const { return m_dgate_behaviour->direction(player); } + virtual void setDelegator(IBehaviour* behaviour) { + m_dgate_behaviour->setDelegator(behaviour); + //kDebug() << this << "m_dgate_behaviour=" << m_dgate_behaviour; + } }; } #endif // DELEGATORS__BEHAVIOUR_H - - diff --git a/src/variants/chess/behaviour.h b/src/variants/chess/behaviour.h index ff00a49..b821508 100644 --- a/src/variants/chess/behaviour.h +++ b/src/variants/chess/behaviour.h @@ -19,13 +19,13 @@ Q_OBJECT public: Behaviour(); Behaviour(const Behaviour& other); - virtual void captureOn(IState* state, const Point& square) const; virtual void move(IState* state, const Move& m) const; virtual void advanceTurn(IState* state) const; virtual Point captureSquare(const IState* state, const Move& m) const; virtual const IColor* opponent(const IColor* player) const; virtual Point direction(const IColor* player) const; + virtual void setDelegator(IBehaviour* behaviour) { } public Q_SLOTS: virtual IBehaviour* clone() const; }; diff --git a/src/variants/crazyhouse/behaviour.cpp b/src/variants/crazyhouse/behaviour.cpp index d2be9a2..8434e2e 100644 --- a/src/variants/crazyhouse/behaviour.cpp +++ b/src/variants/crazyhouse/behaviour.cpp @@ -18,9 +18,16 @@ namespace Crazyhouse { -Behaviour::Behaviour(const IBehaviour* behaviour) +Behaviour::Behaviour(IBehaviour* behaviour) : Delegators::Behaviour(behaviour) { } +IBehaviour* Behaviour::clone() const { + IBehaviour* dgate_clone; + Q_ASSERT(QMetaObject::invokeMethod(m_dgate_behaviour, "clone", + Q_RETURN_ARG(IBehaviour*, dgate_clone))); + return new Behaviour(dgate_clone); +} + void Behaviour::captureOn(IState* state, const Point& square) const { Piece captured = state->board()->get(square); if (captured != Piece()) { diff --git a/src/variants/crazyhouse/behaviour.h b/src/variants/crazyhouse/behaviour.h index 0c68701..b644fe7 100644 --- a/src/variants/crazyhouse/behaviour.h +++ b/src/variants/crazyhouse/behaviour.h @@ -17,9 +17,12 @@ namespace Crazyhouse { class Behaviour : public Delegators::Behaviour { Q_OBJECT public: - Behaviour(const IBehaviour* behaviour); - + Behaviour(IBehaviour* behaviour); + virtual void captureOn(IState* state, const Point& square) const; + +public Q_SLOTS: + virtual IBehaviour* clone() const; }; } // namespace Crazyhouse diff --git a/src/variants/crazyhouse/crazyhouse.cpp b/src/variants/crazyhouse/crazyhouse.cpp index 7283c0c..8674449 100644 --- a/src/variants/crazyhouse/crazyhouse.cpp +++ b/src/variants/crazyhouse/crazyhouse.cpp @@ -47,8 +47,14 @@ taguacrazyhouse_initrepo(IVariantLoader* loader) { Component* chess_state_comp = chess->getComponent("state"); IState* chess_state = requestInterface(chess_state_comp); - // create crazyhouse behaviour - const IBehaviour* behaviour = new Behaviour(chess_state->behaviour()); + // create crazyhouse behaviour based on chess + Component* chess_behaviour_comp = chess->getComponent("behaviour"); + Q_ASSERT(chess_behaviour_comp); + IBehaviour* behaviour_clone = NULL; + Q_ASSERT(QMetaObject::invokeMethod(chess_behaviour_comp, "clone", + Q_RETURN_ARG(IBehaviour*, behaviour_clone))); + Q_ASSERT(behaviour_clone); + const Behaviour* behaviour = new Behaviour(behaviour_clone); // create crazyhouse state factory Component* state_component = 0; -- 2.11.4.GIT