From 65eaf0b0149a2793cb30b00ae769f45b4a923dae Mon Sep 17 00:00:00 2001 From: Yann Dirson Date: Mon, 7 Apr 2008 23:04:33 +0200 Subject: [PATCH] refresh dc9ccb8c0bf4bc198802d4d39ad2bc523eb5e06f --- src/core/behaviour.h | 1 + src/core/delegators/behaviour.h | 5 ++--- src/variants/crazyhouse/behaviour.cpp | 5 ++++- src/variants/crazyhouse/crazyhouse.cpp | 10 +++++++-- src/variants/minishogi/CMakeLists.txt | 2 +- src/variants/minishogi/behaviour.cpp | 8 +++++++ src/variants/minishogi/behaviour.h | 37 ++++++++++++++++++++++++++----- src/variants/minishogi/minishogi.cpp | 20 ++++++++++------- src/variants/shogi/CMakeLists.txt | 12 +--------- src/variants/shogi/behaviour.cpp | 29 +++++++++++++++++++----- src/variants/shogi/behaviour.h | 12 +++++----- src/variants/shogi/delegators/behaviour.h | 19 ++++++++++++++++ src/variants/shogi/shogi.cpp | 15 +++++++------ src/variants/shogi/types/bishop.cpp | 18 +++++++++++---- 14 files changed, 140 insertions(+), 53 deletions(-) create mode 100644 src/variants/minishogi/behaviour.cpp diff --git a/src/core/behaviour.h b/src/core/behaviour.h index c16b194..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(); diff --git a/src/core/delegators/behaviour.h b/src/core/delegators/behaviour.h index 841fb02..1c98081 100644 --- a/src/core/delegators/behaviour.h +++ b/src/core/delegators/behaviour.h @@ -20,7 +20,8 @@ class Behaviour : virtual public IBehaviour { protected: IBehaviour* m_dgate_behaviour; public: - Behaviour(IBehaviour* delegate) : m_dgate_behaviour(delegate) { + Behaviour(IBehaviour* delegate): m_dgate_behaviour(delegate) { + Q_ASSERT(delegate); m_dgate_behaviour->setDelegator(this); //kDebug() << this << "m_dgate_behaviour=" << m_dgate_behaviour; } @@ -53,5 +54,3 @@ public: } #endif // DELEGATORS__BEHAVIOUR_H - - diff --git a/src/variants/crazyhouse/behaviour.cpp b/src/variants/crazyhouse/behaviour.cpp index 35a832e..8434e2e 100644 --- a/src/variants/crazyhouse/behaviour.cpp +++ b/src/variants/crazyhouse/behaviour.cpp @@ -22,7 +22,10 @@ Behaviour::Behaviour(IBehaviour* behaviour) : Delegators::Behaviour(behaviour) { } IBehaviour* Behaviour::clone() const { - return new Behaviour(m_dgate_behaviour->clone()); + 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 { diff --git a/src/variants/crazyhouse/crazyhouse.cpp b/src/variants/crazyhouse/crazyhouse.cpp index 33a1c9c..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()->clone()); + // 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; diff --git a/src/variants/minishogi/CMakeLists.txt b/src/variants/minishogi/CMakeLists.txt index e6d7a36..30c7b4b 100644 --- a/src/variants/minishogi/CMakeLists.txt +++ b/src/variants/minishogi/CMakeLists.txt @@ -1,5 +1,6 @@ set(taguaminishogi_SRCS minishogi.cpp + behaviour.cpp state.cpp ) @@ -9,7 +10,6 @@ kde4_add_plugin(taguaminishogi ${taguaminishogi_SRCS}) target_link_libraries(taguaminishogi taguacore - taguashogilib ${KDE4_KDECORE_LIBS} ) diff --git a/src/variants/minishogi/behaviour.cpp b/src/variants/minishogi/behaviour.cpp new file mode 100644 index 0000000..1a3d88f --- /dev/null +++ b/src/variants/minishogi/behaviour.cpp @@ -0,0 +1,8 @@ +#include "behaviour.h" + +namespace MiniShogi { + +Behaviour::Behaviour(IBehaviour* behaviour) + : Delegators::Behaviour(behaviour) { } + +} // namespace MiniShogi diff --git a/src/variants/minishogi/behaviour.h b/src/variants/minishogi/behaviour.h index 790513b..cdcaf8b 100644 --- a/src/variants/minishogi/behaviour.h +++ b/src/variants/minishogi/behaviour.h @@ -10,18 +10,43 @@ #ifndef MINISHOGI__BEHAVIOUR_H #define MINISHOGI__BEHAVIOUR_H -#include "../shogi/delegators/behaviour.h" +#include +#include class Move; namespace MiniShogi { -class Behaviour : public Delegators::ShogiBehaviour { +class TAGUA_EXPORT Behaviour : public Delegators::Behaviour { +Q_OBJECT + Behaviour* m_delegator; public: - Behaviour(IBehaviour* behaviour) - : Delegators::Behaviour(behaviour) - , Delegators::ShogiBehaviour(behaviour) { } - virtual unsigned promotionZoneWidth() const { return 1; } + Behaviour(IBehaviour* behaviour); + +public Q_SLOTS: // delegated + void setPromotionManager(PromotionManager* pm) { + Q_ASSERT(QMetaObject::invokeMethod(m_dgate_behaviour, "setPromotionManager", + Q_ARG(PromotionManager*, pm))); + } + + const PromotionManager* promotionManager() const { + const PromotionManager* result; + Q_ASSERT(QMetaObject::invokeMethod(m_dgate_behaviour, "promotionManager", + Q_RETURN_ARG(const PromotionManager*, result))); + return result; + } + + bool mayPromote(const IState* state, const Move& move) const { + bool result; + Q_ASSERT(QMetaObject::invokeMethod(m_dgate_behaviour, "mayPromote", + Q_RETURN_ARG(bool, result), + Q_ARG(const IState*, state), + Q_ARG(const Move, move))); + return result; + } + +public Q_SLOTS: // overridden + unsigned promotionZoneWidth() const { return 1; } }; } // namespace MiniShogi diff --git a/src/variants/minishogi/minishogi.cpp b/src/variants/minishogi/minishogi.cpp index 59ab92b..d87b810 100644 --- a/src/variants/minishogi/minishogi.cpp +++ b/src/variants/minishogi/minishogi.cpp @@ -40,17 +40,21 @@ taguaminishogi_initrepo(IVariantLoader* loader) { IState* shogi_state = requestInterface(shogi_state_comp); // base behaviour on shogi - Behaviour* behaviour = - new Behaviour(dynamic_cast(shogi_state->behaviour())->clone()); + Component* shogi_behaviour_comp = shogi->getComponent("behaviour"); + Q_ASSERT(shogi_behaviour_comp); + IBehaviour* behaviour_clone = NULL; + Q_ASSERT(QMetaObject::invokeMethod(shogi_behaviour_comp, "clone", + Q_RETURN_ARG(IBehaviour*, behaviour_clone), + Q_ARG(PromotionManager*, NULL))); + Q_ASSERT(behaviour_clone); + Behaviour* behaviour = new Behaviour(behaviour_clone); // create minishogi state factory Component* state_component = 0; - bool ok = QMetaObject::invokeMethod(shogi_state_comp, "clone", - Q_RETURN_ARG(Component*, state_component), - Q_ARG(const IBehaviour*, behaviour), - Q_ARG(Point, Point(5, 5))); - kDebug() << "cloning prototype state" << ok; - Q_ASSERT(ok); + Q_ASSERT(QMetaObject::invokeMethod(shogi_state_comp, "clone", + Q_RETURN_ARG(Component*, state_component), + Q_ARG(const IBehaviour*, behaviour), + Q_ARG(Point, Point(5, 5)))); Q_ASSERT(state_component); State* state = new State(requestInterface< ::DefaultState>(state_component)); diff --git a/src/variants/shogi/CMakeLists.txt b/src/variants/shogi/CMakeLists.txt index 2a6c30f..81842e9 100644 --- a/src/variants/shogi/CMakeLists.txt +++ b/src/variants/shogi/CMakeLists.txt @@ -1,13 +1,3 @@ -set(taguashogilib_SRCS - behaviour.cpp -) -kde4_add_library(taguashogilib SHARED ${taguashogilib_SRCS}) -target_link_libraries(taguashogilib - taguacore - ${KDE4_KDECORE_LIBS} -) -install(TARGETS taguashogilib DESTINATION ${LIB_INSTALL_DIR}) - set(taguashogi_SRCS shogi.cpp @@ -27,6 +17,7 @@ set(taguashogi_SRCS types/dragonking.cpp types/dragonhorse.cpp + behaviour.cpp colors.cpp moveserializer.cpp # san.cpp @@ -40,7 +31,6 @@ kde4_add_plugin(taguashogi ${taguashogi_SRCS}) target_link_libraries(taguashogi taguacore - taguashogilib ${KDE4_KDECORE_LIBS} ) diff --git a/src/variants/shogi/behaviour.cpp b/src/variants/shogi/behaviour.cpp index da88802..d24d83d 100644 --- a/src/variants/shogi/behaviour.cpp +++ b/src/variants/shogi/behaviour.cpp @@ -27,7 +27,10 @@ Behaviour::Behaviour(IBehaviour* behaviour) , m_delegator(this) { } IBehaviour* Behaviour::clone() const { - Behaviour* b = new Behaviour(m_dgate_behaviour->clone()); + IBehaviour* dgate_clone; + Q_ASSERT(QMetaObject::invokeMethod(m_dgate_behaviour, "clone", + Q_RETURN_ARG(IBehaviour*, dgate_clone))); + Behaviour* b = new Behaviour(dgate_clone); //FIXME: b->m_promotionmanager = m_promotionmanager->clone(); b->m_promotionmanager = m_promotionmanager; return b; @@ -56,11 +59,27 @@ bool Behaviour::mayPromote(const IState* state, const Move& move) const { return false; // move starts or ends on the promotion rows on the other side ? + unsigned pzwidth; + Q_ASSERT(QMetaObject::invokeMethod(m_delegator, "promotionZoneWidth", + Q_RETURN_ARG(unsigned, pzwidth))); return ((state->turn() == Black::self()) ? - (move.src().y >= state->board()->size().y - m_delegator->promotionZoneWidth() || - move.dst().y >= state->board()->size().y - m_delegator->promotionZoneWidth()) : - (move.src().y < m_delegator->promotionZoneWidth() || - move.dst().y < m_delegator->promotionZoneWidth())); + (move.src().y >= state->board()->size().y - pzwidth || + move.dst().y >= state->board()->size().y - pzwidth) : + (move.src().y < pzwidth || + move.dst().y < pzwidth)); +} + +void Behaviour::setDelegator(IBehaviour* behaviour) { + Delegators::Behaviour::setDelegator(behaviour); + m_delegator = behaviour; +} + +IBehaviour* Behaviour::clone(PromotionManager* pm) const { + Behaviour* b = dynamic_cast(clone()); // FIXME: cannot use static_cast ? + Q_ASSERT(b); + if (pm) + b->m_promotionmanager = pm; + return b; } } // namespace Shogi diff --git a/src/variants/shogi/behaviour.h b/src/variants/shogi/behaviour.h index afa0aa7..52313e5 100644 --- a/src/variants/shogi/behaviour.h +++ b/src/variants/shogi/behaviour.h @@ -20,14 +20,14 @@ namespace Shogi { class TAGUA_EXPORT Behaviour : public Delegators::Behaviour { Q_OBJECT PromotionManager* m_promotionmanager; - Behaviour* m_delegator; + IBehaviour* m_delegator; public: Behaviour(IBehaviour* behaviour); - virtual IBehaviour* clone() const; - virtual void captureOn(IState* state, const Point& square) const; + virtual void setDelegator(IBehaviour* behaviour); +public Q_SLOTS: void setPromotionManager(PromotionManager* pm) { m_promotionmanager = pm; kDebug() << this << "m_promotionmanager=" << m_promotionmanager; @@ -38,8 +38,10 @@ public: return m_promotionmanager; } - virtual bool mayPromote(const IState* state, const Move& move) const; - virtual unsigned promotionZoneWidth() const { return 3; } + bool mayPromote(const IState* state, const Move& move) const; + unsigned promotionZoneWidth() const { return 3; } + + virtual IBehaviour* clone(PromotionManager* pm) const; }; } // namespace Shogi diff --git a/src/variants/shogi/delegators/behaviour.h b/src/variants/shogi/delegators/behaviour.h index 57646f5..6feac93 100644 --- a/src/variants/shogi/delegators/behaviour.h +++ b/src/variants/shogi/delegators/behaviour.h @@ -14,6 +14,7 @@ class ShogiBehaviour : public Shogi::Behaviour { protected: Shogi::Behaviour* m_dgate_shogibehaviour; public: +#if 0 ShogiBehaviour(IBehaviour* behaviour) : Delegators::Behaviour(behaviour) , Shogi::Behaviour(behaviour), @@ -29,6 +30,24 @@ public: m_dgate_shogibehaviour->setDelegator(this); kDebug() << this << "m_dgate_shogibehaviour=" << m_dgate_shogibehaviour; } +#endif + + ShogiBehaviour(Component* behaviour) + : Delegators::Behaviour(dynamic_cast(behaviour)) + , Shogi::Behaviour(dynamic_cast(behaviour)), + m_dgate_shogibehaviour(NULL) { + Shogi::Behaviour* shogibehaviour = qobject_cast< Shogi::Behaviour* >(behaviour); + if (shogibehaviour == NULL) { + kError() << "Delegators::ShogiBehaviour needs a Shogi::Behaviour, cannot use a" + << typeid(behaviour).name(); + abort(); + } + + m_dgate_shogibehaviour = shogibehaviour; + m_dgate_shogibehaviour->setDelegator(this); + kDebug() << this << "m_dgate_shogibehaviour=" << m_dgate_shogibehaviour; + } + virtual ~ShogiBehaviour() { delete m_dgate_shogibehaviour; } virtual unsigned promotionZoneWidth() const { diff --git a/src/variants/shogi/shogi.cpp b/src/variants/shogi/shogi.cpp index 9d024d1..6c06e89 100644 --- a/src/variants/shogi/shogi.cpp +++ b/src/variants/shogi/shogi.cpp @@ -43,11 +43,6 @@ taguashogi_initrepo(IVariantLoader* loader) { repo->addComponent("type/dragonhorse", DragonHorse::self()); repo->addComponent("type/tokin", Tokin::self()); -#if 0 - // create crazyhouse behaviour - IState* crazyhouse_state = requestInterface(crazyhouse->getComponent("state")); - repo->addComponent("state", new State(crazyhouse_state->behaviour(), Point(9, 9))); -#else IState* chess_state = requestInterface(chess->getComponent("state")); PromotionManager* promotion_manager = new PromotionManager(); promotion_manager->setPromotion(Silver::self(), Narigin::self()); @@ -59,10 +54,16 @@ taguashogi_initrepo(IVariantLoader* loader) { repo->addComponent("promotion_manager", promotion_manager); // base behaviour on chess - Behaviour* behaviour = new Behaviour(chess_state->behaviour()->clone()); + 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); + Behaviour* behaviour = new Behaviour(behaviour_clone); behaviour->setPromotionManager(promotion_manager); + repo->addComponent("behaviour", behaviour); repo->addComponent("state", new State(behaviour, Point(9, 9))); -#endif Validator* validator = new Validator; repo->addComponent("validator", validator); diff --git a/src/variants/shogi/types/bishop.cpp b/src/variants/shogi/types/bishop.cpp index c893570..40c3dd2 100644 --- a/src/variants/shogi/types/bishop.cpp +++ b/src/variants/shogi/types/bishop.cpp @@ -10,7 +10,8 @@ #include "bishop.h" -#include "../behaviour.h" +#include +#include #include #include @@ -42,10 +43,19 @@ bool Bishop::canMove(const Piece& piece, const Piece& target, if (!valid) return false; - const Behaviour* behaviour = dynamic_cast(state->behaviour()); - if (behaviour->mayPromote(state, move)) { + IBehaviour* behaviour = const_cast(state->behaviour()); + Q_ASSERT(behaviour); + bool may_promote; + Q_ASSERT(QMetaObject::invokeMethod(behaviour, "mayPromote", + Q_RETURN_ARG(bool, may_promote), + Q_ARG(const IState*, state), + Q_ARG(const Move, move))); // FIXME: Move& ? + if (may_promote) { move.setType("promotion"); - const IType* newtype = behaviour->promotionManager()->getPromotion(piece.type()); + const PromotionManager* pm; + Q_ASSERT(QMetaObject::invokeMethod(behaviour, "promotionManager", + Q_RETURN_ARG(const PromotionManager*, pm))); + const IType* newtype = pm->getPromotion(piece.type()); kDebug() << "promoting to" << newtype->name(); move.setPromotion(newtype); } -- 2.11.4.GIT