refresh d7e71a93da39c621828977f6616ccc46c4b52116
[tagua/yd.git] / src / variants / shogi / behaviour.cpp
blobed4d1b4564c89c62e6700f8aeedf7522cfaf068d
1 /*
2 Copyright (c) 2007 Paolo Capriotti <p.capriotti@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 */
10 #include "behaviour.h"
12 #include <core/board.h>
13 #include <core/color.h>
14 #include <core/move.h>
15 #include <core/piece.h>
16 #include <core/poolcollection.h>
17 #include <core/pool.h>
18 #include <core/state.h>
20 #include <KDebug>
22 namespace Shogi {
24 Behaviour::Behaviour(IBehaviour* behaviour)
25 : Delegators::Behaviour(behaviour)
26 , m_promotionmanager(NULL)
27 , m_delegator(this) { }
29 IBehaviour* Behaviour::clone() const {
30 IBehaviour* dgate_clone;
31 Q_ASSERT(QMetaObject::invokeMethod(m_dgate_behaviour, "clone",
32 Q_RETURN_ARG(IBehaviour*, dgate_clone)));
33 Behaviour* b = new Behaviour(dgate_clone);
34 //FIXME: b->m_promotionmanager = m_promotionmanager->clone();
35 b->m_promotionmanager = m_promotionmanager;
36 return b;
39 void Behaviour::captureOn(IState* state, const Point& square) const {
40 Piece* captured = state->board()->get(square);
41 if (captured != NULL && *captured != Piece()) {
42 if (captured->get("promoted").toBool()) {
43 if (promotionManager())
44 captured->setType(promotionManager()->getDepromotion(captured->type()));
46 state->pools()->pool(opponent(captured->color()))->insert(-1, *captured);
48 m_dgate_behaviour->captureOn(state, square);
51 bool Behaviour::mayPromote(const IState* state, const Move& move) const {
52 // promoted piece cannot promote again
53 if (move.promotion())
54 return false;
56 // does that piece type can even promote ?
57 if (promotionManager() == NULL ||
58 promotionManager()->getPromotion(state->board()->get(move.src())->type()) == NULL)
59 return false;
61 // move starts or ends on the promotion rows on the other side ?
62 unsigned pzwidth;
63 Q_ASSERT(QMetaObject::invokeMethod(m_delegator, "promotionZoneWidth",
64 Q_RETURN_ARG(unsigned, pzwidth)));
65 return ((state->turn() == Black::self()) ?
66 (move.src().y >= state->board()->size().y - pzwidth ||
67 move.dst().y >= state->board()->size().y - pzwidth) :
68 (move.src().y < pzwidth ||
69 move.dst().y < pzwidth));
72 void Behaviour::setDelegator(IBehaviour* behaviour) {
73 Delegators::Behaviour::setDelegator(behaviour);
74 m_delegator = behaviour;
77 IBehaviour* Behaviour::clone(PromotionManager* pm) const {
78 Behaviour* b = dynamic_cast<Behaviour*>(clone()); // FIXME: cannot use static_cast ?
79 Q_ASSERT(b);
80 if (pm)
81 b->m_promotionmanager = pm;
82 return b;
85 } // namespace Shogi