From fd11e28e0e725c115d99abcae83e56f242a905e9 Mon Sep 17 00:00:00 2001
From: Paolo Capriotti
Date: Wed, 1 Aug 2007 03:38:35 +0200
Subject: [PATCH] Fixed DecoratedMove and serialization bug.
---
src/decoratedmove.cpp | 2 +-
src/decoratedmove.h | 4 +++-
src/hlvariant/chess/serializer.h | 20 ++++++++++++++------
tests/hlvariants/chessserializationtest.cpp | 16 ++++++++++++++++
tests/hlvariants/chessserializationtest.h | 4 ++++
5 files changed, 38 insertions(+), 8 deletions(-)
diff --git a/src/decoratedmove.cpp b/src/decoratedmove.cpp
index 5bc137f..0fc2663 100644
--- a/src/decoratedmove.cpp
+++ b/src/decoratedmove.cpp
@@ -22,7 +22,7 @@ DecoratedMove::DecoratedMove(const QString& str) {
}
void DecoratedMove::load(const QString& str) {
- static QRegExp figurine("{[^}]*}");
+ static QRegExp figurine("\\{[^}]*\\}");
int offset = 0;
int begin;
std::cout << "DecoratedMove load" << std::endl;
diff --git a/src/decoratedmove.h b/src/decoratedmove.h
index 8289b50..79fd9b8 100644
--- a/src/decoratedmove.h
+++ b/src/decoratedmove.h
@@ -14,6 +14,8 @@
#include
#include
+#include "export.h"
+
class MovePart {
public:
enum Type {
@@ -28,7 +30,7 @@ public:
: m_string(s), m_type(t) {}
};
-class DecoratedMove {
+class TAGUA_EXPORT DecoratedMove {
typedef QList ElementList;
ElementList m_elements;
diff --git a/src/hlvariant/chess/serializer.h b/src/hlvariant/chess/serializer.h
index 9e78fbe..c8037b7 100644
--- a/src/hlvariant/chess/serializer.h
+++ b/src/hlvariant/chess/serializer.h
@@ -44,6 +44,8 @@ protected:
virtual void minimal_notation(SAN& san, const GameState& ref);
virtual Move get_san(const SAN& san, const GameState& ref);
+
+ virtual QChar symbol(typename Piece::Type type) const;
public:
/**
* Create a serializer to a given string representation for moves.
@@ -110,7 +112,7 @@ QString Serializer::san(const Move& move, const GameState& ref) {
}
else {
if (piece.type() != Piece::PAWN)
- res = piece.typeName()[0].toUpper();
+ res = symbol(piece.type()).toUpper();
SAN tmp;
tmp.from = move.from();
@@ -126,9 +128,7 @@ QString Serializer::san(const Move& move, const GameState& ref) {
}
if (move.promoteTo() != -1)
- res += "=" + QString(Piece::typeName(
- static_cast(move.promoteTo())
- )[0].toUpper());
+ res += "=" + QString(symbol(static_cast(move.promoteTo())).toUpper());
res += suffix(move, ref);
@@ -144,9 +144,9 @@ QString Serializer::serialize(const Move& move, const GameState&
QString res = move.from().toString(ysize) + move.to().toString(ysize);
if (move.promoteTo() != -1)
res = res + "=" +
- Piece::typeName(
+ symbol(
static_cast(move.promoteTo())
- )[0].toUpper();
+ ).toUpper();
return res;
}
case COMPACT:
@@ -284,6 +284,14 @@ void Serializer::minimal_notation(SAN& san, const GameState& ref)
}
#undef TRY
+template
+QChar Serializer::symbol(typename Piece::Type type) const {
+ if (type == Piece::KNIGHT)
+ return 'n';
+ else
+ return Piece::typeName(type)[0];
+}
+
} // namespace Chess
} // namespace HLVariant
diff --git a/tests/hlvariants/chessserializationtest.cpp b/tests/hlvariants/chessserializationtest.cpp
index a158264..045081b 100644
--- a/tests/hlvariants/chessserializationtest.cpp
+++ b/tests/hlvariants/chessserializationtest.cpp
@@ -176,3 +176,19 @@ void ChessSerializationTest::test_castling_q() {
CPPUNIT_ASSERT_EQUAL(QString("O-O-O"), dec.serialize(move, *m_state));
}
+void ChessSerializationTest::regression_knight_king() {
+ m_state->setup();
+
+ ChessMove move(Point(6, 7), Point(5, 5));
+ CPPUNIT_ASSERT(m_check->legal(move));
+
+ ChessSerializer san(ChessSerializer::COMPACT);
+ CPPUNIT_ASSERT_EQUAL(QString("Nf3"), san.serialize(move, *m_state));
+
+ ChessSerializer simple(ChessSerializer::SIMPLE);
+ CPPUNIT_ASSERT_EQUAL(QString("g1f3"), simple.serialize(move, *m_state));
+
+ ChessSerializer dec(ChessSerializer::DECORATED);
+ CPPUNIT_ASSERT_EQUAL(QString("{knight}f3"), dec.serialize(move, *m_state));
+}
+
diff --git a/tests/hlvariants/chessserializationtest.h b/tests/hlvariants/chessserializationtest.h
index 38d23b1..898bc30 100644
--- a/tests/hlvariants/chessserializationtest.h
+++ b/tests/hlvariants/chessserializationtest.h
@@ -28,6 +28,8 @@ class ChessSerializationTest : public CppUnit::TestFixture {
CPPUNIT_TEST(test_promotion_capture_check);
CPPUNIT_TEST(test_castling_k);
CPPUNIT_TEST(test_castling_q);
+
+ CPPUNIT_TEST(regression_knight_king);
CPPUNIT_TEST_SUITE_END();
private:
ChessGameState* m_state;
@@ -45,6 +47,8 @@ public:
void test_promotion_capture_check();
void test_castling_k();
void test_castling_q();
+
+ void regression_knight_king();
};
#endif // CHESSSERIALIZATIONTEST_H
--
2.11.4.GIT