refresh e50354d3ef3d92779d3796094388f4161b20460c
[tagua/yd.git] / src / variants / shogi / behaviour.cpp
blob46942f62ba9fcc574bde3a37716ea5d190f790e7
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_delegator(this), m_promotionmanager(NULL) { }
28 IBehaviour* Behaviour::clone() const {
29 Behaviour* b = new Behaviour(m_behaviour->clone());
30 //FIXME: b->m_promotionmanager = m_promotionmanager->clone();
31 b->m_promotionmanager = m_promotionmanager;
32 return b;
35 void Behaviour::captureOn(IState* state, const Point& square) const {
36 Piece* captured = state->board()->get(square);
37 if (captured != NULL && *captured != Piece()) {
38 if (captured->get("promoted").toBool()) {
39 captured->setType(m_promotionmanager->getDepromotion(captured->type()));
41 state->pools()->pool(opponent(captured->color()))->insert(-1, *captured);
43 m_behaviour->captureOn(state, square);
46 bool Behaviour::mayPromote(const IState* state, const Move& move) const {
47 // promoted piece cannot promote again
48 if (move.promotion())
49 return false;
51 // does that piece type can even promote ?
52 if (promotionManager()->getPromotion(state->board()->get(move.src())->type()) == NULL)
53 return false;
55 // move starts or ends on the promotion rows on the other side ?
56 return ((state->turn() == Black::self()) ?
57 (move.src().y >= state->board()->size().y - m_delegator->promotionZoneWidth() ||
58 move.dst().y >= state->board()->size().y - m_delegator->promotionZoneWidth()) :
59 (move.src().y < m_delegator->promotionZoneWidth() ||
60 move.dst().y < m_delegator->promotionZoneWidth()));
63 } // namespace Shogi