From 7fbac45f2a5282dc9234c65d6e7a7e470a2d8610 Mon Sep 17 00:00:00 2001 From: Paolo Capriotti Date: Tue, 31 Jul 2007 00:40:53 +0200 Subject: [PATCH] Initial implementation of a new tagua_wrapped. --- src/common.h | 7 +- tests/hlvariants/CMakeLists.txt | 2 + tests/hlvariants/chesswrappedtest.cpp | 21 +++++ tests/hlvariants/chesswrappedtest.h | 22 +++++ tests/hlvariants/prototype/chess/gamestate.h | 14 +++ tests/hlvariants/prototype/chess/variant.cpp | 11 +++ tests/hlvariants/prototype/chess/variant.h | 18 ++++ tests/hlvariants/prototype/tagua_wrapped.h | 135 ++++++++++++++++++++++++++- 8 files changed, 223 insertions(+), 7 deletions(-) create mode 100644 tests/hlvariants/chesswrappedtest.cpp create mode 100644 tests/hlvariants/chesswrappedtest.h create mode 100644 tests/hlvariants/prototype/chess/variant.cpp create mode 100644 tests/hlvariants/prototype/chess/variant.h diff --git a/src/common.h b/src/common.h index 7e434ae..6c53d69 100644 --- a/src/common.h +++ b/src/common.h @@ -12,6 +12,9 @@ #define COMMON_H #include +#include "export.h" + + class QString; enum ErrorCode { @@ -29,7 +32,7 @@ template class PointerGrid; //typedef PointerGrid PieceGrid; -std::ostream &operator <<(std::ostream &os, const QString& s); +TAGUA_EXPORT std::ostream &operator <<(std::ostream &os, const QString& s); inline const char* wrap_cptr(const char* ptr) { return ptr ? ptr : "[NULL]"; @@ -37,7 +40,7 @@ inline const char* wrap_cptr(const char* ptr) { QString qPrintf(const char* fmt, ...); -QString prettyTypeName(const char* name); +TAGUA_EXPORT QString prettyTypeName(const char* name); inline void TRAP() { #if defined(Q_CC_GNU) diff --git a/tests/hlvariants/CMakeLists.txt b/tests/hlvariants/CMakeLists.txt index 11280a9..06884b5 100644 --- a/tests/hlvariants/CMakeLists.txt +++ b/tests/hlvariants/CMakeLists.txt @@ -4,6 +4,7 @@ SET(hl_SRC prototype/chess/piece.cpp prototype/chess/move.cpp prototype/chess/gamestate.cpp + prototype/chess/variant.cpp ../cppunit_main.cpp boardtest.cpp @@ -11,6 +12,7 @@ SET(hl_SRC chessgamestatetest.cpp chessmovetest.cpp chesslegalitytest.cpp + chesswrappedtest.cpp ) include_directories( diff --git a/tests/hlvariants/chesswrappedtest.cpp b/tests/hlvariants/chesswrappedtest.cpp new file mode 100644 index 0000000..d7ffa91 --- /dev/null +++ b/tests/hlvariants/chesswrappedtest.cpp @@ -0,0 +1,21 @@ +#include "chesswrappedtest.h" +#include "prototype/tagua_wrapped.h" +#include "prototype/chess/variant.h" + +CPPUNIT_TEST_SUITE_REGISTRATION(ChessWrappedTest); + +typedef HLVariant::Chess::Variant Chess; +typedef Chess::LegalityCheck LegalityCheck; +typedef LegalityCheck::GameState GameState; + +void ChessWrappedTest::setUp() { + m_pos = PositionPtr(new HLVariant::WrappedPosition(GameState())); +} + +void ChessWrappedTest::tearDown() { + m_pos.reset(); +} + + + + diff --git a/tests/hlvariants/chesswrappedtest.h b/tests/hlvariants/chesswrappedtest.h new file mode 100644 index 0000000..cda1751 --- /dev/null +++ b/tests/hlvariants/chesswrappedtest.h @@ -0,0 +1,22 @@ +#ifndef CHESSWRAPPEDTEST_H +#define CHESSWRAPPEDTEST_H + +#include +#include +#include +#include +#include + +#include "fwd.h" + +class ChessWrappedTest : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE(ChessWrappedTest); + CPPUNIT_TEST_SUITE_END(); +private: + PositionPtr m_pos; +public: + void setUp(); + void tearDown(); +}; + +#endif // CHESSWRAPPEDTEST_H diff --git a/tests/hlvariants/prototype/chess/gamestate.h b/tests/hlvariants/prototype/chess/gamestate.h index 3db3773..75ab3b0 100644 --- a/tests/hlvariants/prototype/chess/gamestate.h +++ b/tests/hlvariants/prototype/chess/gamestate.h @@ -4,6 +4,7 @@ #include "../board.h" #include "piece.h" #include "move.h" +#include "nopool.h" namespace HLVariant { namespace Chess { @@ -25,6 +26,7 @@ public: typedef _Board Board; typedef _Move Move; typedef typename Board::Piece Piece; + typedef NoPool Pool; private: Board m_board; CastlingData m_castling; @@ -50,6 +52,8 @@ public: virtual void handleCastling(const Piece& piece, const Move& m); virtual void captureOn(const Point& p); + virtual void setTurn(typename Piece::Color color); + virtual typename Piece::Color previousTurn() const; virtual void switchTurn(); virtual typename Piece::Color turn() const; @@ -207,6 +211,16 @@ typename Board::Piece::Color GameState::turn() const { } template +typename Board::Piece::Color GameState::previousTurn() const { + return Piece::oppositeColor(m_turn); +} + +template +void GameState::setTurn(typename Piece::Color color) { + m_turn = color; +} + +template int GameState::startingRank(typename Piece::Color color) const { return color == Piece::WHITE ? m_board.size().y - 1 : 0; } diff --git a/tests/hlvariants/prototype/chess/variant.cpp b/tests/hlvariants/prototype/chess/variant.cpp new file mode 100644 index 0000000..b40d686 --- /dev/null +++ b/tests/hlvariants/prototype/chess/variant.cpp @@ -0,0 +1,11 @@ +#include "variant.h" + +namespace HLVariant { +namespace Chess { + +const char* Variant::m_name = "Chess"; + +} // namespace Chess +} // namespace HLVariant + + diff --git a/tests/hlvariants/prototype/chess/variant.h b/tests/hlvariants/prototype/chess/variant.h new file mode 100644 index 0000000..e3bbe11 --- /dev/null +++ b/tests/hlvariants/prototype/chess/variant.h @@ -0,0 +1,18 @@ +#ifndef HLVARIANT__CHESS__VARIANT_H +#define HLVARIANT__CHESS__VARIANT_H + +#include "legalitycheck.h" + +namespace HLVariant { +namespace Chess { + +struct Variant { + typedef LegalityCheck, Move> > LegalityCheck; + + static const char* m_name; +}; + +} // namespace Chess +} // namespace HLVariant + +#endif // HLVARIANT__CHESS__VARIANT_H diff --git a/tests/hlvariants/prototype/tagua_wrapped.h b/tests/hlvariants/prototype/tagua_wrapped.h index 9ba35ff..877f693 100644 --- a/tests/hlvariants/prototype/tagua_wrapped.h +++ b/tests/hlvariants/prototype/tagua_wrapped.h @@ -3,9 +3,134 @@ #include "tagua.h" #include "fwd.h" +#include "nopool.h" + +#ifdef Q_CC_GNU + #define __FUNC__ __PRETTY_FUNCTION__ +#else + #define __FUNC__ __FUNCTION__ +#endif + +#define MISMATCH(x,y) (std::cout << " --> Error in "<<__FUNC__<<", MISMATCH!" << std::endl \ + << " got type " << prettyTypeName(typeid(x).name()) << std::endl \ + << " instead of " << prettyTypeName(typeid(y).name()) << std::endl \ + << " this is " << prettyTypeName(typeid(*this).name()) << std::endl) namespace HLVariant { + template class WrappedPosition; + + template + class WrappedPool { }; + + template + class WrappedPiece : public AbstractPiece { + typedef typename Variant::LegalityCheck::GameState::Board::Piece Piece; + + Piece m_piece; + public: + const Piece& inner() const { return m_piece; } + + WrappedPiece(const Piece& piece) + : m_piece(piece) { } + + virtual bool equals(const PiecePtr& _other) const { + if (!_other) return false; + WrappedPiece* other = dynamic_cast*>(_other.get()); + + if (other) + return m_piece == other->inner(); + else { + MISMATCH(*_other.get(),WrappedPiece); + return false; + } + } + + virtual QString name() const { + return m_piece.name(); + } + + virtual PiecePtr clone() const { + return PiecePtr(new WrappedPiece(m_piece)); + } + }; + + template + class WrappedMove : public AbstractMove { + typedef typename Variant::LegalityCheck LegalityCheck; + typedef typename LegalityCheck::Move Move; + typedef typename LegalityCheck::GameState GameState; + + Move m_move; + public: + const Move& inner() const { return m_move; } + Move& inner() { return m_move; } + + WrappedMove(const Move& move) + : m_move(move) { } + + virtual QString SAN(const PositionPtr& _ref) const { + WrappedPosition* ref = dynamic_cast*>(_ref.get()); + + if (ref) { +// MoveSerializer serializer(m_move, pos->inner()); + return ""; //BROKEN + } + else { + MISMATCH(*_ref.get(), WrappedPosition); + return "$@%"; + } + } + + virtual DecoratedMove toDecoratedMove(const PositionPtr& _pos) const { + return DecoratedMove(); // BROKEN + } + + virtual QString toString(const PositionPtr& _pos) const { + return ""; // BROKEN + } + + virtual NormalUserMove toUserMove() const { + return NormalUserMove(); // BROKEN + } + + virtual bool equals(const MovePtr& _other) const { + WrappedMove* other = dynamic_cast*>(_other.get()); + + if (other) + return m_move == other->inner(); + else { + MISMATCH(*_other.get(), WrappedMove); + return false; + } + } + }; + + /** + * Metafunction that returns a null pointer when + * its template argument is NoPool. + */ + template + struct ReturnPoolAux { + static PoolPtr apply(typename Variant::GameState& state, int player) { + return PoolPtr(new WrappedPool(state.pool(player))); + } + }; + + template + struct ReturnPoolAux { + static PoolPtr apply(typename Variant::GameState&, int) { + return PoolPtr(); + } + }; + + template + struct ReturnPool { + static PoolPtr apply(typename Variant::GameState& state, int player) { + return ReturnPoolAux(state, player); + } + }; + template class WrappedPosition : public AbstractPosition { typedef typename Variant::LegalityCheck LegalityCheck; @@ -62,15 +187,15 @@ namespace HLVariant { return PoolPtr(); } - virtual void copyPoolFrom(AbstractPosition::Ptr) { + virtual void copyPoolFrom(const PositionPtr&) { // BROKEN } - virtual InteractionType movable(const TurnTest& test, const Point& p) const { + virtual InteractionType movable(const TurnTest&, const Point&) const { return Moving; // BROKEN } - virtual InteractionType droppable(const TurnTest& test, int p) const { + virtual InteractionType droppable(const TurnTest&, int) const { return Moving; // BROKEN } @@ -142,7 +267,7 @@ namespace HLVariant { return MovePtr(); } - virtual AbstractMove::Ptr getMove(const QString&) const { + virtual MovePtr getMove(const QString&) const { // BROKEN return MovePtr(); } @@ -155,7 +280,7 @@ namespace HLVariant { return ""; // BROKEN } - virtual AbstractPiece::Ptr moveHint(const MovePtr&) const { + virtual PiecePtr moveHint(const MovePtr&) const { return PiecePtr(); // BROKEN } -- 2.11.4.GIT