Initial implementation of a new tagua_wrapped.
[tagua/yd.git] / tests / hlvariants / prototype / tagua_wrapped.h
blob877f693148d6f41393fc844715940f8bc2deb1c5
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& _pos) const {
86 return DecoratedMove(); // BROKEN
89 virtual QString toString(const PositionPtr& _pos) 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&, const Point&) const {
195 return Moving; // BROKEN
198 virtual InteractionType droppable(const TurnTest&, int) const {
199 return Moving; // BROKEN
202 virtual int turn() const {
203 return static_cast<int>(m_state.turn());
206 virtual void setTurn(int turn) {
207 m_state.setTurn(static_cast<typename Piece::Color>(turn));
210 virtual int previousTurn() const {
211 return static_cast<int>(m_state.previousTurn());
214 virtual void switchTurn() {
215 m_state.switchTurn();
218 virtual bool testMove(const MovePtr& _move) const {
219 WrappedMove<Variant>* move = dynamic_cast<WrappedMove<Variant>*>(_move.get());
221 if (move) {
222 LegalityCheck check(m_state);
223 return check.legal(move->inner());
225 else {
226 MISMATCH(*_move.get(), WrappedMove<Variant>);
227 return false;
231 virtual void move(const MovePtr& _move) {
232 WrappedMove<Variant>* move = dynamic_cast<WrappedMove<Variant>*>(_move.get());
234 if (move)
235 m_state.move(move->inner());
236 else
237 MISMATCH(*_move.get(), WrappedMove<Variant>);
240 virtual PositionPtr clone() const {
241 return PositionPtr(new WrappedPosition<Variant>(m_state));
244 virtual void copyFrom(const PositionPtr& _p) {
245 // TODO: check if this is used somewhere
246 WrappedPosition<Variant>* p = dynamic_cast<WrappedPosition<Variant>*>(_p.get());
248 if (p)
249 m_state = p->inner();
250 else
251 MISMATCH(*_p.get(), WrappedPosition);
254 virtual bool equals(const PositionPtr& _other) const {
255 WrappedPosition<Variant>* other = dynamic_cast<WrappedPosition<Variant>*>(_other.get());
257 if (other)
258 return m_state == other->inner();
259 else {
260 MISMATCH(*_other.get(), WrappedPosition<Variant>);
261 return false;
265 virtual MovePtr getMove(const AlgebraicNotation&) const {
266 // BROKEN
267 return MovePtr();
270 virtual MovePtr getMove(const QString&) const {
271 // BROKEN
272 return MovePtr();
275 virtual QString state() const {
276 return ""; // TODO
279 virtual QString fen(int, int) const {
280 return ""; // BROKEN
283 virtual PiecePtr moveHint(const MovePtr&) const {
284 return PiecePtr(); // BROKEN
287 virtual QString variant() const {
288 return Variant::m_name;
291 virtual void dump() const {
292 // BROKEN
299 #endif // HLVARIANT__TAGUA_WRAPPED_H