From 9088efa950b98bc6834dfb586eb182602c6a1cfc Mon Sep 17 00:00:00 2001 From: Paolo Capriotti Date: Sun, 16 Dec 2007 18:34:12 +0100 Subject: [PATCH] Use Validator in chess MoveSerializer. --- src/core/dropanimator.cpp | 2 -- src/variants/chess/chess.cpp | 11 +++++++---- src/variants/chess/moveserializer.cpp | 25 +++++++------------------ src/variants/chess/moveserializer.h | 4 +++- src/variants/chess/state.cpp | 2 +- src/variants/chess/state.h | 3 ++- src/variants/chess/validator.cpp | 2 ++ src/variants/crazyhouse/behaviour.cpp | 2 -- 8 files changed, 22 insertions(+), 29 deletions(-) diff --git a/src/core/dropanimator.cpp b/src/core/dropanimator.cpp index c43cafc..b1b3296 100644 --- a/src/core/dropanimator.cpp +++ b/src/core/dropanimator.cpp @@ -9,8 +9,6 @@ #include "animation.h" -#include - #include "dropanimator.h" #include "move.h" #include "namer.h" diff --git a/src/variants/chess/chess.cpp b/src/variants/chess/chess.cpp index 949745d..5c0e6fb 100644 --- a/src/variants/chess/chess.cpp +++ b/src/variants/chess/chess.cpp @@ -37,14 +37,17 @@ extern "C" KDE_EXPORT Repository* taguachess_initrepo(IVariantLoader*) { repo->addComponent("type/pawn", Pawn::self()); repo->addComponent("state_factory", new StateFactory(new Behaviour, Point(8, 8))); - repo->addComponent("validator", new Validator); + Validator* validator = new Validator; + repo->addComponent("validator", validator); repo->addComponent("animator_factory", new AnimatorFactory); repo->addComponent("namer", new Namer); repo->addComponent("policy", new DefaultPolicy); - repo->addComponent("move_serializer/simple", new MoveSerializer("simple")); - repo->addComponent("move_serializer/decorated", new MoveSerializer("decorated")); - MoveSerializer* san = new MoveSerializer("compact"); + repo->addComponent("move_serializer/simple", + new MoveSerializer("simple", validator)); + repo->addComponent("move_serializer/decorated", + new MoveSerializer("decorated", validator)); + MoveSerializer* san = new MoveSerializer("compact", validator); repo->addComponent("move_serializer/san", san); repo->addComponent("move_serializer/compact", san); diff --git a/src/variants/chess/moveserializer.cpp b/src/variants/chess/moveserializer.cpp index 454f89e..850b993 100644 --- a/src/variants/chess/moveserializer.cpp +++ b/src/variants/chess/moveserializer.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include "icsverbose.h" #include "types.h" @@ -21,8 +22,9 @@ namespace Chess { -MoveSerializer::MoveSerializer(const QString& rep) -: m_rep(rep) { } +MoveSerializer::MoveSerializer(const QString& rep, IValidator* validator) +: m_rep(rep) +, m_validator(validator) { } QString MoveSerializer::san(const Move& move, const IState* ref) const { QString res; @@ -140,16 +142,10 @@ Move MoveSerializer::get_san(const SAN& san, const IState* ref) const { return candidate; else { candidate = Move(from, to); - // FIXME check legality -#if 0 - LegalityCheck check(ref); - if (check.legal(candidate)) + if (m_validator->legal(ref, candidate)) return candidate; else return Move(); -#else - return candidate; -#endif } } @@ -166,11 +162,8 @@ Move MoveSerializer::get_san(const SAN& san, const IState* ref) const { if (p.resembles(san.src) && piece.type() == san.type && piece.color() == ref->turn()) { - -#if 0 - LegalityCheck check(ref); - if (check.legal(mv)) { - if (candidate.valid()) { + if (m_validator->legal(ref, mv)) { + if (candidate != Move()) { // ambiguous! return Move(); } @@ -179,10 +172,6 @@ Move MoveSerializer::get_san(const SAN& san, const IState* ref) const { candidate = mv; } } -#else - if (candidate == Move()) - candidate = mv; -#endif } } } diff --git a/src/variants/chess/moveserializer.h b/src/variants/chess/moveserializer.h index 08a887a..418f7c7 100644 --- a/src/variants/chess/moveserializer.h +++ b/src/variants/chess/moveserializer.h @@ -16,6 +16,7 @@ class IState; class IType; +class IValidator; class SAN; namespace Chess { @@ -23,6 +24,7 @@ namespace Chess { class TAGUA_EXPORT MoveSerializer : public Component, public IMoveSerializer { protected: QString m_rep; + IValidator* m_validator; virtual QString suffix(const Move& move, const IState* ref) const; @@ -40,7 +42,7 @@ public: * Create a serializer to a given string representation for moves. * \param rep A move representation type. */ - MoveSerializer(const QString& rep); + MoveSerializer(const QString& rep, IValidator* validator); virtual QString serialize(const Move& move, const IState* ref) const; diff --git a/src/variants/chess/state.cpp b/src/variants/chess/state.cpp index dde9644..7e1959a 100644 --- a/src/variants/chess/state.cpp +++ b/src/variants/chess/state.cpp @@ -29,7 +29,7 @@ State::State(const State& other) , m_flags(other.m_flags) , m_turn(other.m_turn) , m_behaviour(other.m_behaviour) -, m_delegator(other.m_delegator) { } +, m_delegator(this) { } IState* State::clone() const { return new State(*this); diff --git a/src/variants/chess/state.h b/src/variants/chess/state.h index ea236b9..93f8ab4 100644 --- a/src/variants/chess/state.h +++ b/src/variants/chess/state.h @@ -26,9 +26,10 @@ Q_OBJECT const IColor* m_turn; const IBehaviour* m_behaviour; IState* m_delegator; +protected: + State(const State&); public: State(const IBehaviour* behaviour, const Point& size); - State(const State&); public: virtual IState* clone() const; diff --git a/src/variants/chess/validator.cpp b/src/variants/chess/validator.cpp index 687d279..9fe368d 100644 --- a/src/variants/chess/validator.cpp +++ b/src/variants/chess/validator.cpp @@ -53,6 +53,8 @@ bool Validator::pseudolegal(const IState* state, Move& move) const { } bool Validator::legal(const IState* state, Move& move) const { +// if (!move.type().isEmpty()) return true; + if (!m_delegator->pseudolegal(state, move)) return false; diff --git a/src/variants/crazyhouse/behaviour.cpp b/src/variants/crazyhouse/behaviour.cpp index d52d223..f8bb0ca 100644 --- a/src/variants/crazyhouse/behaviour.cpp +++ b/src/variants/crazyhouse/behaviour.cpp @@ -9,7 +9,6 @@ #include "behaviour.h" -#include #include #include #include @@ -25,7 +24,6 @@ Behaviour::Behaviour(IBehaviour* behaviour) void Behaviour::captureOn(IState* state, const Point& square) const { Piece captured = state->board()->get(square); if (captured != Piece()) { - kDebug() << "captured" << captured.type()->name(); if (captured.get("promoted").toBool()) { captured.setType(pawn); } -- 2.11.4.GIT