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 #include "crazyhouse_p.h"
12 #include "crazyhouse.h"
13 #include "tagua_wrapped.h"
14 #include "icsapi.impl.h"
15 #include "xchess/dropanimator.impl.h"
16 #include "animation.h"
17 #include "animationfactory.h"
18 #include "moveserializer.impl.h"
20 class CrazyhouseVariantInfo
{
22 typedef CrazyhousePosition Position
;
23 typedef Position::Move Move
;
24 typedef Position::Piece Piece
;
25 typedef Position::PoolReference Pool
;
27 typedef DropAnimatorMixin
<SimpleAnimator
<CrazyhouseVariantInfo
> > Animator
;
29 static const bool hasICS
= true;
30 static const bool m_simple_moves
= false;
31 static void forallPieces(PieceFunction
& f
);
32 static int moveListLayout(){ return 0; }
33 static const char *m_name
;
34 static const char *m_theme_proxy
;
35 static OptList
positionOptions() { return OptList(); }
38 //BEGIN CrazyhousePiece
40 CrazyhousePiece::CrazyhousePiece(ChessPiece::Color color
, ChessPiece::Type type
, bool promoted
)
41 : ChessPiece(color
, type
)
42 , m_promoted(promoted
) { }
44 CrazyhousePiece::CrazyhousePiece(const CrazyhousePiece
& other
)
46 , m_promoted(other
.m_promoted
) { }
48 CrazyhousePiece::CrazyhousePiece(const ChessPiece
& other
)
50 , m_promoted(false) { }
54 //BEGIN CrazyhouseMove
56 CrazyhouseMove::CrazyhouseMove(const ChessMove
& m
)
58 , m_drop(INVALID_COLOR
, INVALID_TYPE
)
60 , m_pool_index(-1) { }
62 CrazyhouseMove::CrazyhouseMove(const Point
& from
, const Point
& to
, PieceType promotionType
)
63 : ChessMove(from
, to
, promotionType
)
64 , m_drop(INVALID_COLOR
, INVALID_TYPE
)
66 , m_pool_index(-1) { }
68 CrazyhouseMove::CrazyhouseMove(const CrazyhouseMove
& other
)
70 , m_drop(other
.m_drop
)
71 , m_pool(other
.m_pool
)
72 , m_pool_index(other
.m_pool_index
) { }
74 CrazyhouseMove::CrazyhouseMove(const CrazyhousePiece
& pc
, const Point
& to
)
75 : ChessMove(Point::invalid(), to
)
78 , m_pool_index(-1) { }
80 CrazyhouseMove::CrazyhouseMove(int pool
, int pool_index
, const Point
& to
)
81 : ChessMove(Point::invalid(), to
)
82 , m_drop(INVALID_COLOR
, INVALID_TYPE
)
84 , m_pool_index(pool_index
) { }
88 //BEGIN CrazyhousePosition
90 CrazyhousePosition::CrazyhousePosition() {
93 CrazyhousePosition::CrazyhousePosition(const OptList
&) {
96 CrazyhousePosition::CrazyhousePosition(const CrazyhousePosition
& other
)
98 , m_pool(other
.m_pool
) {
101 CrazyhousePosition::CrazyhousePosition(const ChessPosition
& other
)
105 CrazyhousePosition::CrazyhousePosition(CrazyhousePiece::Color turn
, bool wk
, bool wq
,
106 bool bk
, bool bq
, const Point
& ep
)
107 : Base(turn
, wk
, wq
, bk
, bq
, ep
) {
110 CrazyhousePosition
* CrazyhousePosition::clone() const {
111 return new CrazyhousePosition(*this);
114 CrazyhousePiece::Color
CrazyhousePosition::moveTurn(const Move
& move
) const {
116 return move
.drop().color();
118 return Base::moveTurn(move
);
121 boost::shared_ptr
<AbstractGenerator
<CrazyhouseMove
> >
122 CrazyhousePosition::createLegalGenerator() const {
123 return boost::shared_ptr
<AbstractGenerator
<CrazyhouseMove
> >(
124 new MoveGenerator
<CrazyhousePosition
,
125 LegalMove
<CrazyhousePosition
> >(*this));
128 CrazyhousePosition::PoolReference
CrazyhousePosition::pool(int index
) {
129 Color c
= static_cast<Color
>(index
);
130 return PoolReference(&m_pool
[c
], c
);
133 CrazyhousePosition::PoolConstReference
CrazyhousePosition::pool(int index
) const {
134 Color c
= static_cast<Color
>(index
);
135 return PoolConstReference(&m_pool
[c
], c
);
138 CrazyhousePosition::PlayerPool
& CrazyhousePosition::rawPool(Piece::Color color
) {
139 return m_pool
[color
];
142 const CrazyhousePosition::PlayerPool
& CrazyhousePosition::rawPool(Piece::Color color
) const {
143 return m_pool
[color
];
146 bool CrazyhousePosition::pseudolegal(Move
& move
) const {
148 if (!move
.drop() && move
.pool() != -1 && move
.poolIndex() != -1) {
149 move
.setDrop(pool(move
.pool()).get(move
.poolIndex()));
151 std::cout
<< move
.drop() << " " << move
.pool() << " " << move
.poolIndex() << " " <<
152 pool(move
.pool()).size() << std::endl
;
156 Q_ASSERT(valid(move
.to
));
158 // cannot drop on occupied squares
159 if (m_board
[move
.to
]) return false;
161 // cannot drop pawns in first or eighth rank
162 if (move
.drop().type() == PAWN
&&
163 (move
.to
.y
== 0 || move
.to
.y
== 7))
170 return Base::pseudolegal(move
);
173 void CrazyhousePosition::move(const Move
& move
) {
176 Q_ASSERT(m_pool
[move
.drop().color()].count(move
.drop().type()));
177 Q_ASSERT(!m_board
[move
.to
]);
179 basicDropPiece(new Piece(move
.drop()), move
.to
);
180 if(!--m_pool
[move
.drop().color()][move
.drop().type()])
181 m_pool
[move
.drop().color()].erase(move
.drop().type());
188 if (move
.type() == Move::Promotion
) {
189 m_board
[move
.to
].setPromoted(true);
194 for(Pool::iterator i
= m_pool
.begin(); i
!= m_pool
.end(); ++i
)
195 std::cout
<< i
->first
.color() << "." << i
->first
.type() << " " << i
->second
<<std::endl
;
199 void CrazyhousePosition::executeCaptureOn(const Point
& point
) {
200 Piece piece
= m_board
[point
];
202 Piece
downgraded( Piece::oppositeColor(piece
.color()),
203 piece
.promoted() ? PAWN
: piece
.type());
204 m_pool
[downgraded
.color()][downgraded
.type()]++;
206 Base::executeCaptureOn(point
);
209 CrazyhouseMove
CrazyhousePosition::getMove(const AlgebraicNotation
& san
, bool& ok
) const {
212 Piece
piece(m_turn
, (PieceType
)san
.type
);
214 if (!m_pool
[piece
.color()].count(piece
.type())) {
216 return CrazyhouseMove::invalid();
220 return CrazyhouseMove( piece
, san
.to
);
224 return Base::getMove(san
, ok
);
227 void CrazyhousePosition::dump() const {
230 for(Pool::const_iterator j
= m_pool
.begin(); j
!= m_pool
.end(); ++j
)
231 for(PlayerPool::const_iterator i
= j
->second
.begin(); i
!= j
->second
.end(); ++i
)
232 std::cout
<< j
->first
<< "." << i
->first
<< " " << i
->second
<<std::endl
;
235 bool CrazyhousePosition::operator==(const CrazyhousePosition
& pos
) const {
236 bool base
= Base::operator==(pos
);
237 bool pools
= m_pool
== pos
.m_pool
;
238 return pools
&& base
;
241 //END CrazyhousePosition
243 //BEGIN PieceFactory<CrazyhouseVariantInfo>
248 class PieceFactory
<CrazyhouseVariantInfo
> {
250 static CrazyhousePiece
createPiece(const QString
& description
) {
251 return ChessPiece::fromDescription(description
);
255 //END PieceFactory<CrazyhouseVariantInfo>
258 //BEGIN CrazyhouseVariant
261 const char *CrazyhouseVariantInfo::m_name
= "Crazyhouse_OLD";
262 const char *CrazyhouseVariantInfo::m_theme_proxy
= "Chess";
264 VariantInfo
* CrazyhouseVariant::static_crazyhouse_variant
= 0;
266 void CrazyhouseVariantInfo::forallPieces(PieceFunction
& f
) {
267 return ChessVariant::forallPieces(f
);
270 VariantInfo
* CrazyhouseVariant::info() {
271 if (!static_crazyhouse_variant
)
272 static_crazyhouse_variant
= new WrappedVariantInfo
<CrazyhouseVariantInfo
>;
273 return static_crazyhouse_variant
;
276 //END CrazyhouseVariant