GNUShogi engine.
[tagua/yd.git] / src / variants / crazyhouse_p.h
blobabd63ca1c1dc22883b6ebbc1caf843484d89b5e8
1 /*
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.
9 */
11 #ifndef CRAZYHOUSE_P_H
12 #define CRAZYHOUSE_P_H
14 #include <map>
15 #include "chess.h"
16 #include "xchess/pool.h"
18 class CrazyhousePiece : public ChessPiece {
19 bool m_promoted;
21 public:
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;
39 int m_pool;
40 int m_pool_index;
41 public:
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 xsize, int ysize) const {
49 if(m_drop.valid())
50 return CrazyhousePiece::typeSymbol(m_drop.type()) + "@" + to.toString(ysize);
51 return ChessMove::toString(xsize, 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> > {
64 public:
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;
73 CrazyhousePosition();
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;
82 Pool m_pool;
83 public:
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)));
98 else
99 retv.setDrop(CrazyhousePiece(INVALID_COLOR, INVALID_TYPE));
100 return retv;
103 void dump() const;
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 void setRawPool(const Pool& p) { m_pool = p; }
112 Pool& rawPool() { return m_pool; }
113 const Pool& rawPool() const { return m_pool; }
116 template <typename MoveTest>
117 class MoveGenerator<CrazyhousePosition, MoveTest>
118 : public Generator<CrazyhousePosition, MoveTest> {
119 typedef Generator<CrazyhousePosition, MoveTest> Base;
120 using Base::m_pos;
121 using Base::m_test;
122 using Base::m_moves;
123 public:
124 MoveGenerator(const CrazyhousePosition& pos)
125 : Base(pos) { }
127 std::vector<CrazyhouseMove>& generate() {
128 generateDrops();
129 return Base::generate();
132 private:
133 void generateDrops() {
134 const CrazyhousePosition::PlayerPool& pp = m_pos.rawPool()[m_pos.turn()];
135 for (CrazyhousePosition::PlayerPool::const_iterator it = pp.begin();
136 it != pp.end(); ++it) {
137 for (Point to = m_pos.first();
138 to <= m_pos.last();
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);
147 template <>
148 class MoveSerializer<CrazyhousePosition> : public MoveSerializerBase<CrazyhousePosition> {
149 typedef CrazyhousePosition Position;
150 typedef CrazyhouseMove Move;
151 typedef CrazyhousePiece Piece;
152 typedef MoveSerializerBase<Position> Base;
153 public:
154 MoveSerializer(const Move& move, const Position& ref)
155 : MoveSerializerBase<Position>(move, ref) { }
157 QString SAN() const {
158 if (m_move.drop()) {
160 return QString("%1@%2")
161 .arg(CrazyhousePiece::typeSymbol(m_move.drop().type()))
162 .arg(m_move.to.toString(m_ref.size().y)) + checkSuffix();
164 else
165 return Base::SAN();
169 #endif // CRAZYHOUSE_P_H