Added movable and dropped and related tests.
[tagua/yd.git] / tests / hlvariants / prototype / tagua_wrapped.h
blobd50628406f2e10f5c5da1aa32932469786b65d95
1 #ifndef HLVARIANT__TAGUA_WRAPPED_H
2 #define HLVARIANT__TAGUA_WRAPPED_H
4 #include "tagua.h"
5 #include "fwd.h"
6 #include "nopool.h"
8 #ifdef Q_CC_GNU
9 #define __FUNC__ __PRETTY_FUNCTION__
10 #else
11 #define __FUNC__ __FUNCTION__
12 #endif
14 #define MISMATCH(x,y) (std::cout << " --> Error in "<<__FUNC__<<", MISMATCH!" << std::endl \
15 << " got type " << prettyTypeName(typeid(x).name()) << std::endl \
16 << " instead of " << prettyTypeName(typeid(y).name()) << std::endl \
17 << " this is " << prettyTypeName(typeid(*this).name()) << std::endl)
19 namespace HLVariant {
21 template <typename Variant> class WrappedPosition;
23 template <typename Variant>
24 class WrappedPool { };
26 template <typename Variant>
27 class WrappedPiece : public AbstractPiece {
28 typedef typename Variant::LegalityCheck::GameState::Board::Piece Piece;
30 Piece m_piece;
31 public:
32 const Piece& inner() const { return m_piece; }
34 WrappedPiece(const Piece& piece)
35 : m_piece(piece) { }
37 virtual bool equals(const PiecePtr& _other) const {
38 if (!_other) return false;
39 WrappedPiece<Variant>* other = dynamic_cast<WrappedPiece<Variant>*>(_other.get());
41 if (other)
42 return m_piece == other->inner();
43 else {
44 MISMATCH(*_other.get(),WrappedPiece<Variant>);
45 return false;
49 virtual QString name() const {
50 return m_piece.name();
53 virtual PiecePtr clone() const {
54 return PiecePtr(new WrappedPiece<Variant>(m_piece));
58 template <typename Variant>
59 class WrappedMove : public AbstractMove {
60 typedef typename Variant::LegalityCheck LegalityCheck;
61 typedef typename LegalityCheck::Move Move;
62 typedef typename LegalityCheck::GameState GameState;
64 Move m_move;
65 public:
66 const Move& inner() const { return m_move; }
67 Move& inner() { return m_move; }
69 WrappedMove(const Move& move)
70 : m_move(move) { }
72 virtual QString SAN(const PositionPtr& _ref) const {
73 WrappedPosition<Variant>* ref = dynamic_cast<WrappedPosition<Variant>*>(_ref.get());
75 if (ref) {
76 // MoveSerializer<Position> serializer(m_move, pos->inner());
77 return ""; //BROKEN
79 else {
80 MISMATCH(*_ref.get(), WrappedPosition<Variant>);
81 return "$@%";
85 virtual DecoratedMove toDecoratedMove(const PositionPtr&) const {
86 return DecoratedMove(); // BROKEN
89 virtual QString toString(const PositionPtr&) const {
90 return ""; // BROKEN
93 virtual NormalUserMove toUserMove() const {
94 return NormalUserMove(); // BROKEN
97 virtual bool equals(const MovePtr& _other) const {
98 WrappedMove<Variant>* other = dynamic_cast<WrappedMove<Variant>*>(_other.get());
100 if (other)
101 return m_move == other->inner();
102 else {
103 MISMATCH(*_other.get(), WrappedMove<Variant>);
104 return false;
110 * Metafunction that returns a null pointer when
111 * its template argument is NoPool.
113 template <typename Variant, typename Pool>
114 struct ReturnPoolAux {
115 static PoolPtr apply(typename Variant::GameState& state, int player) {
116 return PoolPtr(new WrappedPool<Variant>(state.pool(player)));
120 template <typename Variant>
121 struct ReturnPoolAux<Variant, NoPool> {
122 static PoolPtr apply(typename Variant::GameState&, int) {
123 return PoolPtr();
127 template <typename Variant>
128 struct ReturnPool {
129 static PoolPtr apply(typename Variant::GameState& state, int player) {
130 return ReturnPoolAux<Variant, typename Variant::GameState::Pool>(state, player);
134 template <typename Variant>
135 class WrappedPosition : public AbstractPosition {
136 typedef typename Variant::LegalityCheck LegalityCheck;
137 typedef typename LegalityCheck::GameState GameState;
138 typedef typename GameState::Board Board;
139 typedef typename Board::Piece Piece;
140 typedef typename GameState::Move Move;
142 GameState m_state;
143 public:
144 const GameState& inner() const { return m_state; }
145 GameState& inner() { return m_state; }
147 WrappedPosition(const GameState& state)
148 : m_state(state) { }
150 virtual Point size() const {
151 return m_state.board().size();
154 virtual QStringList borderCoords() const {
155 // BROKEN
156 return QStringList();
159 virtual void setup() {
160 m_state.setup();
163 virtual PiecePtr get(const Point& p) const {
164 Piece piece = m_state.board().get(p);
165 if (piece != Piece())
166 return PiecePtr(new WrappedPiece<Variant>(piece));
167 else
168 return PiecePtr();
171 virtual void set(const Point& p, const PiecePtr& _piece) {
172 if (!_piece) {
173 m_state.board().set(p, Piece());
175 else {
176 WrappedPiece<Variant>* piece = dynamic_cast<WrappedPiece<Variant>*>(_piece.get());
178 if (piece)
179 m_state.board().set(p, piece->inner());
180 else
181 MISMATCH(*_piece.get(), WrappedPiece<Variant>);
185 virtual PoolPtr pool(int) {
186 // BROKEN
187 return PoolPtr();
190 virtual void copyPoolFrom(const PositionPtr&) {
191 // BROKEN
194 virtual InteractionType movable(const TurnTest& test, const Point& p) const {
195 LegalityCheck check(m_state);
196 return check.movable(test, p);
199 virtual InteractionType droppable(const TurnTest& test, int index) const {
200 LegalityCheck check(m_state);
201 return check.droppable(test, index);
204 virtual int turn() const {
205 return static_cast<int>(m_state.turn());
208 virtual void setTurn(int turn) {
209 m_state.setTurn(static_cast<typename Piece::Color>(turn));
212 virtual int previousTurn() const {
213 return static_cast<int>(m_state.previousTurn());
216 virtual void switchTurn() {
217 m_state.switchTurn();
220 virtual bool testMove(const MovePtr& _move) const {
221 WrappedMove<Variant>* move = dynamic_cast<WrappedMove<Variant>*>(_move.get());
223 if (move) {
224 LegalityCheck check(m_state);
225 return check.legal(move->inner());
227 else {
228 MISMATCH(*_move.get(), WrappedMove<Variant>);
229 return false;
233 virtual void move(const MovePtr& _move) {
234 WrappedMove<Variant>* move = dynamic_cast<WrappedMove<Variant>*>(_move.get());
236 if (move)
237 m_state.move(move->inner());
238 else
239 MISMATCH(*_move.get(), WrappedMove<Variant>);
242 virtual PositionPtr clone() const {
243 return PositionPtr(new WrappedPosition<Variant>(m_state));
246 virtual void copyFrom(const PositionPtr& _p) {
247 // TODO: check if this is used somewhere
248 WrappedPosition<Variant>* p = dynamic_cast<WrappedPosition<Variant>*>(_p.get());
250 if (p)
251 m_state = p->inner();
252 else
253 MISMATCH(*_p.get(), WrappedPosition);
256 virtual bool equals(const PositionPtr& _other) const {
257 WrappedPosition<Variant>* other = dynamic_cast<WrappedPosition<Variant>*>(_other.get());
259 if (other)
260 return m_state == other->inner();
261 else {
262 MISMATCH(*_other.get(), WrappedPosition<Variant>);
263 return false;
267 virtual MovePtr getMove(const AlgebraicNotation&) const {
268 // BROKEN
269 return MovePtr();
272 virtual MovePtr getMove(const QString&) const {
273 // BROKEN
274 return MovePtr();
277 virtual QString state() const {
278 return ""; // TODO
281 virtual QString fen(int, int) const {
282 return ""; // BROKEN
285 virtual PiecePtr moveHint(const MovePtr&) const {
286 return PiecePtr(); // BROKEN
289 virtual QString variant() const {
290 return Variant::m_name;
293 virtual void dump() const {
294 // BROKEN
301 #endif // HLVARIANT__TAGUA_WRAPPED_H