2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
11 #ifndef CRAZYHOUSE_P_H
12 #define CRAZYHOUSE_P_H
16 #include "xchess/pool.h"
18 class CrazyhousePiece
: public ChessPiece
{
22 CrazyhousePiece(ChessPiece::Color
= INVALID_COLOR
, ChessPiece::Type
= INVALID_TYPE
,
23 bool promoted
= false);
24 CrazyhousePiece(const CrazyhousePiece
&);
25 CrazyhousePiece(const ChessPiece
&);
27 inline bool promoted() const { return m_promoted
; }
28 inline void setPromoted(bool value
) { m_promoted
= value
; }
30 bool operator<(const CrazyhousePiece
& p
) const {
31 return m_promoted
!= p
.m_promoted
?
32 m_promoted
< p
.m_promoted
:
33 this->ChessPiece::operator<(p
);
37 class CrazyhouseMove
: public ChessMove
{
38 CrazyhousePiece m_drop
;
42 CrazyhouseMove(const ChessMove
& move
);
43 CrazyhouseMove(const Point
& from
, const Point
& to
, PieceType promotionType
= INVALID_TYPE
);
44 CrazyhouseMove(const CrazyhousePiece
& p
, const Point
& to
);
45 CrazyhouseMove(int pool
, int m_pool_index
, const Point
& to
);
46 CrazyhouseMove(const CrazyhouseMove
&);
48 QString
toString(int ysize
) const {
50 return CrazyhousePiece::typeSymbol(m_drop
.type()) + "@" + to
.toString(ysize
);
51 return ChessMove::toString(ysize
);
53 static CrazyhouseMove
createDropMove(int pool
, int m_pool_index
, const Point
& to
) {
54 return CrazyhouseMove(pool
, m_pool_index
, to
);
57 CrazyhousePiece
drop() const { return m_drop
; }
58 void setDrop(const CrazyhousePiece
& piece
) { m_drop
= piece
; }
59 int pool() const { return m_pool
; }
60 int poolIndex() const { return m_pool_index
; }
63 class CrazyhousePosition
: public Position
<CrazyhouseMove
, CrazyhousePiece
, Grid
<CrazyhousePiece
> > {
65 typedef CrazyhouseMove Move
;
66 typedef CrazyhousePiece Piece
;
67 typedef Position
<Move
, Piece
, Grid
<Piece
> > Base
;
68 typedef PoolReference
<CrazyhousePosition
> PoolReference
;
69 typedef PoolConstReference
<CrazyhousePosition
> PoolConstReference
;
70 typedef PoolReference::Pool Pool
;
71 typedef PoolReference::PlayerPool PlayerPool
;
74 CrazyhousePosition(const OptList
& l
);
75 CrazyhousePosition(const CrazyhousePosition
&);
76 CrazyhousePosition(const ChessPosition
&);
77 CrazyhousePosition(CrazyhousePiece::Color turn
, bool wk
, bool wq
,
78 bool bk
, bool bq
, const Point
& ep
);
79 virtual CrazyhousePosition
* clone() const;
84 virtual CrazyhousePiece::Color
moveTurn(const Move
&) const;
85 virtual bool pseudolegal(Move
&) const;
86 virtual void move(const Move
&);
87 virtual void executeCaptureOn(const Point
& point
);
88 virtual boost::shared_ptr
<AbstractGenerator
<Move
> > createLegalGenerator() const;
90 virtual Move
getMove(const AlgebraicNotation
& san
, bool& ok
) const;
92 virtual bool operator==(const CrazyhousePosition
& other
) const;
94 static Move
getVerboseMove(Color turn
, const VerboseNotation
& m
) {
95 Move retv
= ChessPosition::getVerboseMove(turn
, m
);
96 if(retv
.from
== Point::invalid())
97 retv
.setDrop(CrazyhousePiece(turn
, static_cast<ChessPiece::Type
>(m
.type
)));
99 retv
.setDrop(CrazyhousePiece(INVALID_COLOR
, INVALID_TYPE
));
105 PoolReference
pool(int index
);
106 PoolConstReference
pool(int index
) const;
108 PlayerPool
& rawPool(Piece::Color color
);
109 const PlayerPool
& rawPool(Piece::Color color
) const;
111 Pool
& rawPool() { return m_pool
; }
112 const Pool
& rawPool() const { return m_pool
; }
115 template <typename MoveTest
>
116 class MoveGenerator
<CrazyhousePosition
, MoveTest
>
117 : public Generator
<CrazyhousePosition
, MoveTest
> {
118 typedef Generator
<CrazyhousePosition
, MoveTest
> Base
;
123 MoveGenerator(const CrazyhousePosition
& pos
)
126 std::vector
<CrazyhouseMove
>& generate() {
128 return Base::generate();
132 void generateDrops() {
133 if(m_pos
.rawPool().count(m_pos
.turn())) {
134 const CrazyhousePosition::PlayerPool
& pp
= m_pos
.rawPool().find(m_pos
.turn())->second
;
135 for (CrazyhousePosition::PlayerPool::const_iterator it
= pp
.begin();
136 it
!= pp
.end(); ++it
) {
137 for (Point to
= m_pos
.first();
139 to
= m_pos
.next(to
)) {
140 CrazyhouseMove
move(CrazyhousePiece(m_pos
.turn(),it
->first
), to
);
141 if (m_test(move
)) m_moves
.push_back(move
);
149 class MoveSerializer
<CrazyhousePosition
> : public MoveSerializerBase
<CrazyhousePosition
> {
150 typedef CrazyhousePosition Position
;
151 typedef CrazyhouseMove Move
;
152 typedef CrazyhousePiece Piece
;
153 typedef MoveSerializerBase
<Position
> Base
;
155 MoveSerializer(const Move
& move
, const Position
& ref
)
156 : MoveSerializerBase
<Position
>(move
, ref
) { }
158 QString
SAN() const {
161 return QString("%1@%2")
162 .arg(CrazyhousePiece::typeSymbol(m_move
.drop().type()))
163 .arg(m_move
.to
.toString(m_ref
.size().y
)) + checkSuffix();
172 template <typename Variant
>
173 class DropAnimator
: public SimpleAnimator
<Variant
> {
175 typedef SimpleAnimator
<Variant
> Base
;
176 typedef typename
Base::AnimationPtr AnimationPtr
;
177 typedef typename
Base::GPosition GPosition
;
178 typedef typename
Base::GElement GElement
;
179 typedef typename
Base::Position Position
;
180 typedef typename
Base::Piece Piece
;
181 typedef typename
Base::Move Move
;
183 virtual boost::shared_ptr<MovementAnimation>
184 createMovementAnimation(const GElement& element, const QPoint& destination);
186 virtual boost::shared_ptr<Animation> createCapture(const Point& p,
187 const GElement& piece,
188 const GElement& captured,
189 const Position& pos);
191 virtual void finalizeBackAnimation(AnimationPtr,
194 virtual void finalizeForwardAnimation(AnimationPtr,
198 DropAnimator(PointConverter
* converter
, const boost::shared_ptr
<GPosition
>& position
);
199 AnimationPtr
warp(const Position
&);
200 // AnimationPtr forward(const Position&, const Move& move);
201 // AnimationPtr back(const Position&, const Move& move);
209 template <typename Variant
>
210 DropAnimator
<Variant
>::DropAnimator(PointConverter
* converter
,
211 const boost::shared_ptr
<GPosition
>& position
)
212 : Base(converter
, position
) { }
214 template <typename Variant
>
215 typename DropAnimator
<Variant
>::AnimationPtr DropAnimator
<Variant
>::warp(const Position
& final
) {
216 this->m_position
->updatePool(final
.pool());
220 #endif // CRAZYHOUSE_P_H