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
17 class CrazyhousePiece : public ChessPiece {
21 CrazyhousePiece(ChessPiece::Color = INVALID_COLOR, ChessPiece::Type = INVALID_TYPE,
22 bool promoted = false);
23 CrazyhousePiece(const CrazyhousePiece&);
24 CrazyhousePiece(const ChessPiece&);
26 inline bool promoted() const { return m_promoted; }
27 inline void setPromoted(bool value) { m_promoted = value; }
29 bool operator<(const CrazyhousePiece& p) const {
30 return m_promoted != p.m_promoted ?
31 m_promoted < p.m_promoted :
32 this->ChessPiece::operator<(p);
36 // here we use inheritance, but the best approach would be
37 // composition + delegation, since inheriting we get an unwanted
38 // automatic conversion CrazyhouseMove -> ChessMove
39 /* UPDATE: is this still true? Maurizio */
40 class CrazyhouseMove : public ChessMove {
42 CrazyhousePiece m_drop;
44 CrazyhouseMove(const ChessMove& move);
45 CrazyhouseMove(const Point& from, const Point& to, PieceType promotionType = INVALID_TYPE);
46 CrazyhouseMove(CrazyhousePiece piece, const Point& to);
47 CrazyhouseMove(const CrazyhouseMove&);
49 QString toString(int ysize) const {
51 return CrazyhousePiece::typeSymbol(m_drop.type()) + "@" + to.toString(ysize);
52 return ChessMove::toString(ysize);
54 static CrazyhouseMove createDropMove(const CrazyhousePiece& p, const Point& to) {
55 return CrazyhouseMove(p, to);
59 class CrazyhousePosition : public Position<CrazyhouseMove, CrazyhousePiece, Grid<CrazyhousePiece> > {
61 typedef CrazyhouseMove Move;
62 typedef CrazyhousePiece Piece;
63 typedef Position<Move, Piece, Grid<Piece> > Base;
64 typedef std::map<CrazyhousePiece, int> Pool;
67 CrazyhousePosition(const OptList& l);
68 CrazyhousePosition(const CrazyhousePosition&);
69 CrazyhousePosition(const ChessPosition&);
70 CrazyhousePosition(CrazyhousePiece::Color turn, bool wk, bool wq,
71 bool bk, bool bq, const Point& ep);
72 virtual CrazyhousePosition* clone() const;
75 virtual void addToPool(const Piece& p, int n) { m_pool[p] += n; }
76 virtual void removeFromPool(const Piece& p, int n) {
77 if((m_pool[p] -= n) <= 0)
81 virtual CrazyhousePiece::Color moveTurn(const Move&) const;
82 virtual bool pseudolegal(Move&) const;
83 virtual void move(const Move&);
84 virtual void executeCaptureOn(const Point& point);
85 virtual boost::shared_ptr<AbstractGenerator<Move> > createLegalGenerator() const;
87 virtual Move getMove(const AlgebraicNotation& san, bool& ok) const;
89 virtual bool operator==(const CrazyhousePosition& other) const;
91 static Move getVerboseMove(Color turn, const VerboseNotation& m) {
92 Move retv = ChessPosition::getVerboseMove(turn, m);
93 if(retv.from == Point::invalid())
94 retv.m_drop = CrazyhousePiece(turn, static_cast<ChessPiece::Type>(m.type) );
96 retv.m_drop = CrazyhousePiece(INVALID_COLOR, INVALID_TYPE);
103 template <typename MoveTest>
104 class MoveGenerator<CrazyhousePosition, MoveTest>
105 : public Generator<CrazyhousePosition, MoveTest> {
106 typedef Generator<CrazyhousePosition, MoveTest> Base;
111 MoveGenerator(const CrazyhousePosition& pos)
114 std::vector<CrazyhouseMove>& generate() {
116 return Base::generate();
120 void generateDrops() {
121 for (std::map<CrazyhousePiece, int>::const_iterator it = m_pos.pool().begin();
122 it != m_pos.pool().end(); ++it) {
123 if(m_pos.turn() == it->first.color())
124 for (Point to = m_pos.first();
126 to = m_pos.next(to)) {
127 CrazyhouseMove move(it->first, to);
128 if (m_test(move)) m_moves.push_back(move);
135 class MoveSerializer<CrazyhousePosition> : public MoveSerializerBase<CrazyhousePosition> {
136 typedef CrazyhousePosition Position;
137 typedef CrazyhouseMove Move;
138 typedef CrazyhousePiece Piece;
139 typedef MoveSerializerBase<Position> Base;
141 MoveSerializer(const Move& move, const Position& ref)
142 : MoveSerializerBase<Position>(move, ref) { }
144 QString SAN() const {
145 if (m_move.m_drop.valid()) {
147 return QString("%1@%2")
148 .arg(CrazyhousePiece::typeSymbol(m_move.m_drop.type()))
149 .arg(m_move.to.toString(m_ref.size().y)) + checkSuffix();
158 template <typename Variant>
159 class DropAnimator : public SimpleAnimator<Variant> {
161 typedef SimpleAnimator<Variant> Base;
162 typedef typename Base::AnimationPtr AnimationPtr;
163 typedef typename Base::GPosition GPosition;
164 typedef typename Base::GElement GElement;
165 typedef typename Base::Position Position;
166 typedef typename Base::Piece Piece;
167 typedef typename Base::Move Move;
169 virtual boost::shared_ptr<MovementAnimation>
170 createMovementAnimation(const GElement& element, const QPoint& destination);
172 virtual boost::shared_ptr<Animation> createCapture(const Point& p,
173 const GElement& piece,
174 const GElement& captured,
175 const Position& pos);
177 virtual void finalizeBackAnimation(AnimationPtr,
180 virtual void finalizeForwardAnimation(AnimationPtr,
184 DropAnimator(PointConverter* converter, const boost::shared_ptr<GPosition>& position);
185 AnimationPtr warp(const Position&);
186 // AnimationPtr forward(const Position&, const Move& move);
187 // AnimationPtr back(const Position&, const Move& move);
195 template <typename Variant>
196 DropAnimator<Variant>::DropAnimator(PointConverter* converter,
197 const boost::shared_ptr<GPosition>& position)
198 : Base(converter, position) { }
200 template <typename Variant>
201 typename DropAnimator<Variant>::AnimationPtr DropAnimator<Variant>::warp(const Position& final) {
202 this->m_position->updatePool(final.pool());
206 #endif // CRAZYHOUSE_P_H