From 4e864375834afaa9355b3d1555613572c79751b9 Mon Sep 17 00:00:00 2001
From: Paolo Capriotti
Date: Fri, 17 Aug 2007 11:27:03 +0200
Subject: [PATCH] Serializers use strings instead of integers to represent
serialization types.
---
src/hlvariant/chess/serializer.h | 91 ++++++++++++----------------
src/hlvariant/crazyhouse/serializer.h | 13 ++--
src/hlvariant/icsapi_wrapped.h | 3 +-
src/hlvariant/shogi/serializer.h | 84 ++++++++++++--------------
src/hlvariant/tagua_wrapped.h | 8 +--
src/moveserializer.h | 66 --------------------
src/moveserializer.impl.h | 110 ----------------------------------
7 files changed, 86 insertions(+), 289 deletions(-)
delete mode 100644 src/moveserializer.h
delete mode 100644 src/moveserializer.impl.h
diff --git a/src/hlvariant/chess/serializer.h b/src/hlvariant/chess/serializer.h
index a7005a6..e4d39fe 100644
--- a/src/hlvariant/chess/serializer.h
+++ b/src/hlvariant/chess/serializer.h
@@ -11,6 +11,8 @@
#ifndef HLVARIANT__CHESS__SERIALIZER_H
#define HLVARIANT__CHESS__SERIALIZER_H
+#include
+
#include "legalitycheck.h"
#include "san.h"
#include "icsverbose.h"
@@ -20,23 +22,13 @@ namespace Chess {
template
class Serializer {
-public:
- /**
- * Possible string representations for moves.
- */
- enum {
- SIMPLE = 0, /// The most direct way of representing a move.
- COMPACT = 1, /// Compact notation. This corresponds to SAN notation for games that support it.
- DECORATED = 2, /// Symbolic figurine notation. Figurine names are enclosed in braces.
- ICS_VERBOSE = 3 /// Verbose notation as defined by ICS.
- } MoveRepresentation;
protected:
typedef typename MoveGenerator::LegalityCheck LegalityCheck;
typedef typename LegalityCheck::GameState GameState;
typedef typename GameState::Move Move;
typedef typename GameState::Board::Piece Piece;
- int m_rep;
+ QString m_rep;
protected:
virtual QString suffix(const Move& move, const GameState& ref);
@@ -53,10 +45,9 @@ protected:
public:
/**
* Create a serializer to a given string representation for moves.
- * \param rep A move representation.
- * \sa MoveRepresentation.
+ * \param rep A move representation type.
*/
- Serializer(int rep);
+ Serializer(const QString& rep);
virtual ~Serializer();
@@ -83,7 +74,7 @@ public:
// IMPLEMENTATION
template
-Serializer::Serializer(int rep)
+Serializer::Serializer(const QString& rep)
: m_rep(rep) { }
template
@@ -141,33 +132,30 @@ QString Serializer::san(const Move& move, const GameState& ref) {
template
QString Serializer::serialize(const Move& move, const GameState& ref) {
- switch (m_rep) {
- case SIMPLE:
- {
- int ysize = ref.board().size().y;
- QString res = move.from().toString(ysize) + move.to().toString(ysize);
- if (move.promoteTo() != -1)
- res = res + "=" +
- symbol(
- static_cast(move.promoteTo())
- ).toUpper();
- return res;
- }
- case COMPACT:
+ if (m_rep == "simple") {
+ int ysize = ref.board().size().y;
+ QString res = move.from().toString(ysize) + move.to().toString(ysize);
+ if (move.promoteTo() != -1)
+ res = res + "=" +
+ symbol(
+ static_cast(move.promoteTo())
+ ).toUpper();
+ return res;
+ }
+ else if (m_rep == "compact") {
return san(move, ref);
- case DECORATED:
- {
- QString res = san(move, ref);
- res.replace('K', "{king}");
- res.replace('Q', "{queen}");
- res.replace('R', "{rook}");
- res.replace('N', "{knight}");
- res.replace('B', "{bishop}");
- res.replace('P', "{pawn}");
- return res;
- }
- case ICS_VERBOSE:
- default:
+ }
+ else if (m_rep == "decorated") {
+ QString res = san(move, ref);
+ res.replace('K', "{king}");
+ res.replace('Q', "{queen}");
+ res.replace('R', "{rook}");
+ res.replace('N', "{knight}");
+ res.replace('B', "{bishop}");
+ res.replace('P', "{pawn}");
+ return res;
+ }
+ else {
return "";
}
}
@@ -249,19 +237,16 @@ Serializer::get_san(const SAN& san, const GameState& ref) {
template
typename Serializer::Move
Serializer::deserialize(const QString& str, const GameState& ref) {
- switch (m_rep) {
- case COMPACT:
- {
- SAN tmp;
- tmp.load(str, ref.board().size().y);
- return get_san(tmp, ref);
- }
- case ICS_VERBOSE:
+ if (m_rep == "compact") {
+ SAN tmp;
+ tmp.load(str, ref.board().size().y);
+ return get_san(tmp, ref);
+ }
+ else if (m_rep == "ics-verbose") {
return parse_ics_verbose(str, ref);
- case SIMPLE:
- case DECORATED:
- default:
- // no need to parse decorated moves
+ }
+ else {
+ // no need to parse simple or decorated moves
return Move();
}
}
diff --git a/src/hlvariant/crazyhouse/serializer.h b/src/hlvariant/crazyhouse/serializer.h
index 8d352e3..ceb81bd 100644
--- a/src/hlvariant/crazyhouse/serializer.h
+++ b/src/hlvariant/crazyhouse/serializer.h
@@ -25,14 +25,14 @@ class Serializer : public Chess::Serializer {
protected:
using Base::m_rep;
public:
- Serializer(int rep);
+ Serializer(const QString& rep);
virtual QString serialize(const Move& move, const GameState& ref);
};
// IMPLEMENTATION
template
-Serializer::Serializer(int rep)
+Serializer::Serializer(const QString& rep)
: Base(rep) { }
template
@@ -42,13 +42,12 @@ QString Serializer::serialize(const Move& move, const GameState&
}
QString res;
- switch (m_rep) {
- case Base::SIMPLE:
- case Base::COMPACT:
- res = symbol(move.drop().type()).toUpper();
- case Base::DECORATED:
+ if (m_rep == "decorated") {
res = "{" + move.drop().typeName() + '}';
}
+ else {
+ res = symbol(move.drop().type()).toUpper();
+ }
return res + '@' + move.to().toString(ref.board().size().y) + suffix(move, ref);
}
diff --git a/src/hlvariant/icsapi_wrapped.h b/src/hlvariant/icsapi_wrapped.h
index 7740a66..8bfaf93 100644
--- a/src/hlvariant/icsapi_wrapped.h
+++ b/src/hlvariant/icsapi_wrapped.h
@@ -47,8 +47,7 @@ MovePtr WrappedICSAPI::parseVerbose(const QString& str, const PositionP
WrappedPosition* ref = dynamic_cast*>(_ref.get());
if (ref) {
- typename VariantData::Serializer serializer(
- VariantData::Serializer::ICS_VERBOSE);
+ typename VariantData::Serializer serializer("ics-verbose");
return MovePtr(new WrappedMove(
serializer.deserialize(str, ref->inner())));
}
diff --git a/src/hlvariant/shogi/serializer.h b/src/hlvariant/shogi/serializer.h
index 971a54f..c5fa273 100644
--- a/src/hlvariant/shogi/serializer.h
+++ b/src/hlvariant/shogi/serializer.h
@@ -19,26 +19,20 @@ namespace Shogi {
template
class Serializer {
public:
- enum {
- SIMPLE = 0,
- COMPACT = 1,
- DECORATED = 2
- };
-
typedef _LegalityCheck LegalityCheck;
typedef typename LegalityCheck::GameState GameState;
typedef typename GameState::Board Board;
typedef typename Board::Piece Piece;
typedef typename GameState::Move Move;
protected:
- int m_rep;
+ QString m_rep;
virtual bool isAmbiguous(const Move& move, const GameState& ref) const;
virtual QString square(const Point& p, const Point& size) const;
virtual QString symbol(const Piece& piece) const;
virtual typename Piece::Type getType(const QChar& letter) const;
public:
- Serializer(int rep);
+ Serializer(const QString& rep);
virtual ~Serializer();
QString serialize(const Move&, const GameState& ref);
@@ -48,7 +42,7 @@ public:
// IMPLEMENTATION
template
-Serializer::Serializer(int rep)
+Serializer::Serializer(const QString& rep)
: m_rep(rep) { }
@@ -84,7 +78,7 @@ bool Serializer::isAmbiguous(const Move& move, const GameState& r
template
QString Serializer::square(const Point& p, const Point& size) const {
QString res = QString::number(size.x - p.x);
- if (m_rep == DECORATED) {
+ if (m_rep == "decorated") {
res += "{num_" + QString::number(p.y + 1) + "}";
}
else {
@@ -103,8 +97,7 @@ QString Serializer::serialize(const Move& move, const GameState&
QString res;
- switch (m_rep) {
- case SIMPLE:
+ if (m_rep == "simple") {
if (move.drop() != Piece()) {
res += symbol(piece);
res += '*';
@@ -114,49 +107,46 @@ QString Serializer::serialize(const Move& move, const GameState&
if (move.promoteTo() != -1)
res += "+";
return res;
- case COMPACT:
- case DECORATED:
- default:
- {
- bool ambiguous = isAmbiguous(move, ref);
-
- QString res;
- if (piece.promoted())
- res += "+";
+ }
+ else {
+ bool ambiguous = isAmbiguous(move, ref);
- res += symbol(piece);
-
- if (ambiguous) {
- res += square(move.from(), ref.board().size());
- }
-
- if (move.drop() != Piece())
- res += "*";
- else if (ref.board().get(move.to()) != Piece())
- res += "x";
- else
- res += "-";
-
- res += square(move.to(), ref.board().size());
-
- // if it is a promotion
- if (move.promoteTo() != -1)
- res += "+";
- // if it is a refused promotion
- else if (ref.canPromote(piece) &&
- move.drop() == Piece() &&
- ref.promotionZone(ref.turn(), move.to())) {
- res += "=";
- }
+ QString res;
+ if (piece.promoted())
+ res += "+";
+
+ res += symbol(piece);
+
+ if (ambiguous) {
+ res += square(move.from(), ref.board().size());
+ }
+
+ if (move.drop() != Piece())
+ res += "*";
+ else if (ref.board().get(move.to()) != Piece())
+ res += "x";
+ else
+ res += "-";
- return res;
+ res += square(move.to(), ref.board().size());
+
+ // if it is a promotion
+ if (move.promoteTo() != -1)
+ res += "+";
+ // if it is a refused promotion
+ else if (ref.canPromote(piece) &&
+ move.drop() == Piece() &&
+ ref.promotionZone(ref.turn(), move.to())) {
+ res += "=";
}
+
+ return res;
}
}
template
QString Serializer::symbol(const Piece& piece) const {
- if (m_rep == DECORATED) {
+ if (m_rep == "decorated") {
QString res = "{";
if (piece.promoted())
res += "p_";
diff --git a/src/hlvariant/tagua_wrapped.h b/src/hlvariant/tagua_wrapped.h
index 9911985..4b7883f 100644
--- a/src/hlvariant/tagua_wrapped.h
+++ b/src/hlvariant/tagua_wrapped.h
@@ -128,7 +128,7 @@ namespace HLVariant {
WrappedPosition* ref = dynamic_cast*>(_ref.get());
if (ref) {
- Serializer serializer(Serializer::COMPACT);
+ Serializer serializer("compact");
return serializer.serialize(m_move, ref->inner());
}
else {
@@ -141,7 +141,7 @@ namespace HLVariant {
WrappedPosition* ref = dynamic_cast*>(_ref.get());
if (ref) {
- Serializer serializer(Serializer::DECORATED);
+ Serializer serializer("decorated");
return DecoratedMove(serializer.serialize(m_move, ref->inner()));
}
else {
@@ -154,7 +154,7 @@ namespace HLVariant {
WrappedPosition* ref = dynamic_cast*>(_ref.get());
if (ref) {
- Serializer serializer(Serializer::SIMPLE);
+ Serializer serializer("simple");
return serializer.serialize(m_move, ref->inner());
}
else {
@@ -345,7 +345,7 @@ namespace HLVariant {
}
virtual MovePtr getMove(const QString& san) const {
- Serializer serializer(Serializer::COMPACT);
+ Serializer serializer("compact");
Move res = serializer.deserialize(san, m_state);
if (res.valid()) {
return MovePtr(new WrappedMove(res));
diff --git a/src/moveserializer.h b/src/moveserializer.h
deleted file mode 100644
index 81e62d6..0000000
--- a/src/moveserializer.h
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- Copyright (c) 2006 Paolo Capriotti
- (c) 2006 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 MOVESERIALIZER_H
-#define MOVESERIALIZER_H
-
-#include
-#include "decoratedmove.h"
-
-class AbstractMoveSerializer {
-public:
- virtual ~AbstractMoveSerializer() { }
- virtual QString SAN() const = 0;
- virtual DecoratedMove toDecoratedMove() const = 0;
-};
-
-template
-class MoveSerializerBase : public AbstractMoveSerializer {
-public:
- typedef Pos Position;
- typedef typename Position::Move Move;
- typedef typename Position::Piece Piece;
-protected:
- const Move& m_move;
- const Position& m_ref;
-
- virtual QString checkSuffix() const {
- Pos temp(m_ref);
- temp.move(m_move);
- if (temp.check()) {
- if (temp.stalled())
- return "#";
- else
- return "+";
- }
- else
- return "";
- }
-
-public:
- MoveSerializerBase(const Move& move, const Position& ref)
- : m_move(move)
- , m_ref(ref) { }
- virtual ~MoveSerializerBase() { }
-
- virtual QString SAN() const;
- virtual DecoratedMove toDecoratedMove() const;
-};
-
-template
-class MoveSerializer : public MoveSerializerBase {
- typedef Pos Position;
- typedef typename Position::Move Move;
-public:
- MoveSerializer(const Move& move, const Position& ref)
- : MoveSerializerBase(move, ref) { }
-};
-
-#endif // MOVESERIALIZER_H
diff --git a/src/moveserializer.impl.h b/src/moveserializer.impl.h
deleted file mode 100644
index 4e809d6..0000000
--- a/src/moveserializer.impl.h
+++ /dev/null
@@ -1,110 +0,0 @@
-/*
- Copyright (c) 2006 Paolo Capriotti
- (c) 2006 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 MOVESERIALIZER_IMPL_H
-#define MOVESERIALIZER_IMPL_H
-
-#include "moveserializer.h"
-
-/**
- * Assume the move has already been tested against @a ref.
- * Calling this function on an untested or illegal move is safe but its return
- * value is undefined.
- * Do not try to call this function on an invalid move.
- * @param ref The position in which this move shall be executed.
- * @return A compact SAN representation for this move.
- */
-template
-QString MoveSerializerBase::SAN() const {
- Q_ASSERT(m_move.valid());
-
- Piece piece = m_ref.get(m_move.from);
- Piece captured = m_ref.get(m_move.to);
-
- if (!piece) {
- m_ref.dump();
- std::cout << "Move is: " << m_move.toString(m_ref.size().x, m_ref.size().y) << std::endl;
- }
-
- Q_ASSERT(piece);
- QString res;
- if (piece.type() == PAWN) {
- if (captured || m_move.type() == Move::EnPassantCapture)
- res = m_move.from.col() + "x";
-
- res += m_move.to.toString(m_ref.size().y);
- }
-
- else {
- if (m_move.type() == Move::KingSideCastling) {
- res = "O-O";
- }
- else if (m_move.type() == Move::QueenSideCastling) {
- res = "O-O-O";
- }
- else {
- res = Piece::typeSymbol(piece.type());
-
- AlgebraicNotation temp;
- temp.from = m_move.from;
- temp.to = m_move.to;
- temp.type = piece.type();
- temp.castling = AlgebraicNotation::NoCastling;
- minimalNotation(temp, m_ref);
-
- res += temp.from.toString(m_ref.size().y);
- if (captured) res += "x";
- res += temp.to.toString(m_ref.size().y);
- }
- }
-
- if (m_move.type() == Move::Promotion)
- res += "=" + Piece::typeSymbol(m_move.promotionType);
-
- return res + checkSuffix();
-}
-
-template
-DecoratedMove MoveSerializerBase::toDecoratedMove() const {
- static QRegExp reg("[KQRBNP]");
- QString move = SAN();
- DecoratedMove mv;
- int x = 0;
- while(reg.indexIn(move, x) != -1) {
- if(reg.pos() > x)
- mv.push_back(MovePart(move.mid(x, reg.pos()-x)));
- switch(move[reg.pos()].toAscii()) {
- case 'K':
- mv.push_back(MovePart("king", MovePart::Figurine));
- break;
- case 'Q':
- mv.push_back(MovePart("queen", MovePart::Figurine));
- break;
- case 'R':
- mv.push_back(MovePart("rook", MovePart::Figurine));
- break;
- case 'B':
- mv.push_back(MovePart("bishop", MovePart::Figurine));
- break;
- case 'N':
- mv.push_back(MovePart("knight", MovePart::Figurine));
- break;
- case 'P':
- mv.push_back(MovePart("pawn", MovePart::Figurine));
- break;
- }
- x = reg.pos() + reg.matchedLength();
- }
- if(x