From 8ff0dd5cfb05fa72a9dd3d32eee6fd0a91000b40 Mon Sep 17 00:00:00 2001 From: Paolo Capriotti Date: Wed, 1 Aug 2007 18:36:01 +0200 Subject: [PATCH] Ported crazyhouse MoveFactory and LegalityChecker. --- src/decoratedmove.cpp | 4 -- src/hlvariant/chess/legalitycheck.h | 24 +++++++-- src/hlvariant/chess/piece.cpp | 10 ++++ src/hlvariant/chess/piece.h | 10 ++++ src/hlvariant/chess/variant.h | 15 +++++- src/hlvariant/crazyhouse/gamestate.h | 4 +- src/hlvariant/crazyhouse/legalitycheck.h | 87 ++++++++++++++++++++++++++++++++ src/hlvariant/crazyhouse/move.h | 19 +++++++ src/hlvariant/crazyhouse/movefactory.h | 27 ++++++++++ src/hlvariant/crazyhouse/piece.cpp | 10 ++++ src/hlvariant/crazyhouse/piece.h | 10 ++++ src/hlvariant/crazyhouse/variant.h | 5 +- src/hlvariant/dropanimator.h | 7 --- src/hlvariant/movefactory.h | 5 +- src/hlvariant/tagua_wrapped.h | 6 +-- src/hlvariant/variantdata.h | 1 + 16 files changed, 221 insertions(+), 23 deletions(-) create mode 100644 src/hlvariant/crazyhouse/legalitycheck.h create mode 100644 src/hlvariant/crazyhouse/movefactory.h diff --git a/src/decoratedmove.cpp b/src/decoratedmove.cpp index 0fc2663..09b5856 100644 --- a/src/decoratedmove.cpp +++ b/src/decoratedmove.cpp @@ -25,15 +25,11 @@ void DecoratedMove::load(const QString& str) { static QRegExp figurine("\\{[^}]*\\}"); int offset = 0; int begin; - std::cout << "DecoratedMove load" << std::endl; while ((begin = figurine.indexIn(str, offset)) != -1) { - std::cout << "begin = " << begin << std::endl; if (begin > offset) { m_elements.push_back(str.mid(offset, begin - offset)); - std::cout << "found text: " << str.mid(offset, begin - offset) << std::endl; } m_elements.push_back(MovePart(str.mid(begin + 1, figurine.matchedLength() - 2), MovePart::Figurine)); - std::cout << "found figurine: " << str.mid(begin + 1, figurine.matchedLength() - 2) << std::endl; offset = begin + figurine.matchedLength(); } diff --git a/src/hlvariant/chess/legalitycheck.h b/src/hlvariant/chess/legalitycheck.h index 9780624..0460a3a 100644 --- a/src/hlvariant/chess/legalitycheck.h +++ b/src/hlvariant/chess/legalitycheck.h @@ -1,3 +1,13 @@ +/* + Copyright (c) 2007 Paolo Capriotti + (c) 2007 Maurizio Monge + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. +*/ + #ifndef HLVARIANT__CHESS__LEGALITYCHECK_H #define HLVARIANT__CHESS__LEGALITYCHECK_H @@ -15,7 +25,7 @@ public: typedef typename GameState::Board Board; typedef typename GameState::Move Move; typedef typename GameState::Piece Piece; -private: +protected: const GameState& m_state; public: LegalityCheck(const GameState& state); @@ -33,6 +43,7 @@ public: const Piece& target = Piece()) const; virtual bool checkPromotion(typename Piece::Type type) const; + virtual typename Piece::Color mover(const Move& move) const; virtual InteractionType movable(const TurnTest&, const Point& x) const; virtual InteractionType droppable(const TurnTest&, int index) const; }; @@ -49,8 +60,7 @@ LegalityCheck::LegalityCheck(const GameState& state) template bool LegalityCheck::legal(Move& move) const { if (pseudolegal(move)) { - Piece piece = m_state.board().get(move.from()); - typename Piece::Color turn = piece.color(); + typename Piece::Color turn = mover(move); GameState tmp(m_state); tmp.move(move); @@ -59,7 +69,7 @@ bool LegalityCheck::legal(Move& move) const { if (kingPos == Point::invalid()) return false; - + LegalityCheck tmpLegality(tmp); if (tmpLegality.attacks(Piece::oppositeColor(turn), kingPos)) return false; @@ -258,6 +268,12 @@ bool LegalityCheck::checkPromotion(typename Piece::Type type) const { } template +typename LegalityCheck::Piece::Color +LegalityCheck::mover(const Move& move) const { + return m_state.board().get(move.from()).color(); +} + +template InteractionType LegalityCheck::movable(const TurnTest& test, const Point& p) const { Piece piece = m_state.board().get(p); if (piece == Piece() || !test(piece.color())) diff --git a/src/hlvariant/chess/piece.cpp b/src/hlvariant/chess/piece.cpp index 7267959..4222136 100644 --- a/src/hlvariant/chess/piece.cpp +++ b/src/hlvariant/chess/piece.cpp @@ -1,3 +1,13 @@ +/* + Copyright (c) 2007 Paolo Capriotti + (c) 2007 Maurizio Monge + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. +*/ + #include "piece.h" namespace HLVariant { diff --git a/src/hlvariant/chess/piece.h b/src/hlvariant/chess/piece.h index b6e6048..3f8aeb4 100644 --- a/src/hlvariant/chess/piece.h +++ b/src/hlvariant/chess/piece.h @@ -1,3 +1,13 @@ +/* + Copyright (c) 2007 Paolo Capriotti + (c) 2007 Maurizio Monge + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. +*/ + #ifndef HLVARIANT__CHESS__PIECE_H #define HLVARIANT__CHESS__PIECE_H diff --git a/src/hlvariant/chess/variant.h b/src/hlvariant/chess/variant.h index ed6f608..061fe20 100644 --- a/src/hlvariant/chess/variant.h +++ b/src/hlvariant/chess/variant.h @@ -1,3 +1,13 @@ +/* + Copyright (c) 2007 Paolo Capriotti + (c) 2007 Maurizio Monge + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. +*/ + #ifndef HLVARIANT__CHESS__VARIANT_H #define HLVARIANT__CHESS__VARIANT_H @@ -6,6 +16,7 @@ #include "export.h" #include "option.h" #include "../animator.h" +#include "../movefactory.h" class VariantInfo; @@ -14,10 +25,12 @@ namespace HLVariant { namespace Chess { struct TAGUA_EXPORT Variant { - typedef LegalityCheck, Move> > LegalityCheck; + typedef GameState, Move> GameState; + typedef LegalityCheck LegalityCheck; typedef MoveGenerator MoveGenerator; typedef Serializer Serializer; typedef SimpleAnimator Animator; + typedef MoveFactory MoveFactory; static const bool hasICS = false; static const bool m_simple_moves = false; diff --git a/src/hlvariant/crazyhouse/gamestate.h b/src/hlvariant/crazyhouse/gamestate.h index 0115c4c..30c80b7 100644 --- a/src/hlvariant/crazyhouse/gamestate.h +++ b/src/hlvariant/crazyhouse/gamestate.h @@ -54,6 +54,7 @@ void GameState::move(const Move& m) { else { m_board.set(m.to(), m.drop()); m_pools.pool(m.drop().color()).remove(m.drop().type()); + this->switchTurn(); } } @@ -61,7 +62,8 @@ template void GameState::captureOn(const Point& p) { Piece captured = m_board.get(p); if (captured != Piece()) { - m_pools.pool(captured.color()).add(captured.actualType()); + m_pools.pool(Piece::oppositeColor(captured.color())) + .add(captured.actualType()); } Base::captureOn(p); } diff --git a/src/hlvariant/crazyhouse/legalitycheck.h b/src/hlvariant/crazyhouse/legalitycheck.h new file mode 100644 index 0000000..52003eb --- /dev/null +++ b/src/hlvariant/crazyhouse/legalitycheck.h @@ -0,0 +1,87 @@ +/* + Copyright (c) 2007 Paolo Capriotti + (c) 2007 Maurizio Monge + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. +*/ + +#ifndef HLVARIANT__CRAZYHOUSE__LEGALITYCHECK_H +#define HLVARIANT__CRAZYHOUSE__LEGALITYCHECK_H + +namespace HLVariant { +namespace Crazyhouse { + +template +class LegalityCheck : public Chess::LegalityCheck<_GameState> { +public: + typedef _GameState GameState; +private: + typedef Chess::LegalityCheck Base; + typedef typename Base::Board Board; + typedef typename Base::Move Move; + typedef typename Base::Piece Piece; +protected: + using Base::m_state; +public: + LegalityCheck(const GameState& state); + + virtual bool pseudolegal(Move& move) const; + virtual typename Piece::Color mover(const Move& move) const; +}; + +// IMPLEMENTATION + +template +LegalityCheck::LegalityCheck(const GameState& state) +: Base(state) { } + +template +bool LegalityCheck::pseudolegal(Move& move) const { + // add drop information to move, if missing + if (move.drop() == Piece() && + move.pool() != Piece::INVALID_COLOR && + move.index() != -1) { + move.setDrop(m_state.pools().pool(move.pool()).get(move.index())); + } + + Piece dropped = move.drop(); + + if (dropped == Piece()) { + return Base::pseudolegal(move); + } + else { + // dropping on a valid square + if (!m_state.board().valid(move.to())) + return false; + + // cannot drop on occupied squares + if (m_state.board().get(move.to()) != Piece()) + return false; + + // cannot drop pawns in first or eighth rank + if (dropped.type() == Piece::PAWN && + (move.to().y == m_state.startingRank(Piece::WHITE) || + move.to().y == m_state.promotionRank(Piece::WHITE))) + return false; + + return true; + } +} + +template +typename LegalityCheck::Piece::Color +LegalityCheck::mover(const Move& move) const { + if (move.drop() != Piece()) + return move.drop().color(); + else + return Base::mover(move); +} + +} // namespace Crazyhouse +} // namespace HLVariant + + +#endif // HLVARIANT__CRAZYHOUSE__LEGALITYCHECK_H diff --git a/src/hlvariant/crazyhouse/move.h b/src/hlvariant/crazyhouse/move.h index 121ac7e..acf2982 100644 --- a/src/hlvariant/crazyhouse/move.h +++ b/src/hlvariant/crazyhouse/move.h @@ -27,6 +27,10 @@ public: Move(Color pool, int index, const Point& to); Piece drop() const; + void setDrop(const Piece& piece); + + Color pool() const; + int index() const; }; template @@ -51,6 +55,21 @@ Piece Move::drop() const { return m_drop; } +template +void Move::setDrop(const Piece& drop) { + m_drop = drop; +} + +template +typename Piece::Color Move::pool() const { + return m_pool; +} + +template +int Move::index() const { + return m_index; +} + }; // namespace Crazyhouse }; // namespace HLVariant diff --git a/src/hlvariant/crazyhouse/movefactory.h b/src/hlvariant/crazyhouse/movefactory.h new file mode 100644 index 0000000..543f079 --- /dev/null +++ b/src/hlvariant/crazyhouse/movefactory.h @@ -0,0 +1,27 @@ +#ifndef HLVARIANT__CRAZYHOUSE__MOVEFACTORY_H +#define HLVARIANT__CRAZYHOUSE__MOVEFACTORY_H + +#include "../movefactory.h" + +namespace HLVariant { +namespace Crazyhouse { + +template +class MoveFactory : public HLVariant::MoveFactory { +public: + typedef typename HLVariant::MoveFactory Base; + typedef typename Base::Move Move; + typedef typename Base::Piece Piece; + + virtual Move createDropMove(const DropUserMove& drop) { + return Move(static_cast(drop.m_pool), + drop.m_piece_index, + drop.m_to); + } +}; + +} // namespace Crazyhouse +} // namespace HLVariant + +#endif // HLVARIANT__CRAZYHOUSE__MOVEFACTORY_H + diff --git a/src/hlvariant/crazyhouse/piece.cpp b/src/hlvariant/crazyhouse/piece.cpp index 9bd478f..b44b1d2 100644 --- a/src/hlvariant/crazyhouse/piece.cpp +++ b/src/hlvariant/crazyhouse/piece.cpp @@ -1,3 +1,13 @@ +/* + Copyright (c) 2007 Paolo Capriotti + (c) 2007 Maurizio Monge + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. +*/ + #include "piece.h" namespace HLVariant { diff --git a/src/hlvariant/crazyhouse/piece.h b/src/hlvariant/crazyhouse/piece.h index 368f2df..ae3b6f7 100644 --- a/src/hlvariant/crazyhouse/piece.h +++ b/src/hlvariant/crazyhouse/piece.h @@ -1,3 +1,13 @@ +/* + Copyright (c) 2007 Paolo Capriotti + (c) 2007 Maurizio Monge + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. +*/ + #ifndef HLVARIANT__CRAZYHOUSE__PIECE_H #define HLVARIANT__CRAZYHOUSE__PIECE_H diff --git a/src/hlvariant/crazyhouse/variant.h b/src/hlvariant/crazyhouse/variant.h index 16afe76..2ee3156 100644 --- a/src/hlvariant/crazyhouse/variant.h +++ b/src/hlvariant/crazyhouse/variant.h @@ -8,8 +8,10 @@ #include "move.h" #include "export.h" #include "option.h" +#include "legalitycheck.h" #include "../animator.h" #include "../dropanimator.h" +#include "movefactory.h" class VariantInfo; @@ -20,10 +22,11 @@ struct TAGUA_EXPORT Variant { typedef Move Move; typedef Board Board; typedef GameState GameState; - typedef Chess::LegalityCheck LegalityCheck; + typedef LegalityCheck LegalityCheck; typedef Chess::MoveGenerator MoveGenerator; typedef Chess::Serializer Serializer; typedef DropAnimatorMixin > Animator; + typedef MoveFactory MoveFactory; static const bool hasICS = false; static const bool m_simple_moves = false; diff --git a/src/hlvariant/dropanimator.h b/src/hlvariant/dropanimator.h index bcd8513..d93232a 100644 --- a/src/hlvariant/dropanimator.h +++ b/src/hlvariant/dropanimator.h @@ -53,10 +53,6 @@ void DropAnimatorMixin::updatePool(const GameState& final) { int pos = 0; - std::cout << "updating pool" << std::endl; - std::cout << m_cinterface->position()->pools().pool(c).size() << std::endl; - std::cout << final.pools().pool(c).size() << std::endl; - // oh, a nice bunch of write-only magic shit while (before_it != before.end() || after_it != after.end()) { bool skip_after = (after_it == after.end() || (before_it != before.end() @@ -66,9 +62,6 @@ void DropAnimatorMixin::updatePool(const GameState& final) { int na = skip_after ? 0 : after_it->second; int nb = skip_before ? 0 : before_it->second; - std::cout << "na = " << na << std::endl; - std::cout << "nb = " << nb << std::endl; - if (nb < na) { for(int i = nb; i < na; i++) m_cinterface->insertPoolPiece(color, pos + (i - nb), Piece(c, after_it->first) ); diff --git a/src/hlvariant/movefactory.h b/src/hlvariant/movefactory.h index 5b4306e..70eec01 100644 --- a/src/hlvariant/movefactory.h +++ b/src/hlvariant/movefactory.h @@ -19,16 +19,17 @@ namespace HLVariant { template class MoveFactory { +public: typedef typename GameState::Move Move; typedef typename GameState::Board::Piece Piece; -public: + virtual ~MoveFactory() { } virtual Move createNormalMove(const NormalUserMove& move) { return Move(move.from, move.to, static_cast(move.promotionType)); } - virtual Move createDropMove(const DropUserMove) { + virtual Move createDropMove(const DropUserMove&) { return Move(); } diff --git a/src/hlvariant/tagua_wrapped.h b/src/hlvariant/tagua_wrapped.h index ac28159..eb2b1a2 100644 --- a/src/hlvariant/tagua_wrapped.h +++ b/src/hlvariant/tagua_wrapped.h @@ -164,7 +164,7 @@ namespace HLVariant { } virtual NormalUserMove toUserMove() const { - MoveFactory factory; + typename VariantData::MoveFactory factory; return factory.toNormal(m_move); } @@ -469,13 +469,13 @@ namespace HLVariant { } virtual MovePtr createNormalMove(const NormalUserMove& move) { - MoveFactory factory; + typename VariantData::MoveFactory factory; Move m = factory.createNormalMove(move); return MovePtr(new WrappedMove(m)); } virtual MovePtr createDropMove(const DropUserMove& move) { - MoveFactory factory; + typename VariantData::MoveFactory factory; Move m = factory.createDropMove(move); return MovePtr(new WrappedMove(m)); } diff --git a/src/hlvariant/variantdata.h b/src/hlvariant/variantdata.h index 336879d..83f4bbe 100644 --- a/src/hlvariant/variantdata.h +++ b/src/hlvariant/variantdata.h @@ -12,6 +12,7 @@ struct VariantData { typedef typename Board::Piece Piece; typedef typename Variant::Animator Animator; typedef typename Variant::Serializer Serializer; + typedef typename Variant::MoveFactory MoveFactory; }; #endif // HLVARIANTS__VARIANTDATA_H -- 2.11.4.GIT