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.
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>
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
;
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
51 // does that piece type can even promote ?
52 if (promotionManager()->getPromotion(state
->board()->get(move
.src())->type()) == NULL
)
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()));