From c0cec5e17e3be1940098cf9af778c3204cb59d16 Mon Sep 17 00:00:00 2001
From: Paolo Capriotti
Date: Thu, 2 Aug 2007 02:09:46 +0200
Subject: [PATCH] Working on the shogi serializer.
---
src/hlvariant/shogi/gamestate.h | 6 +++
src/hlvariant/shogi/serializer.h | 88 ++++++++++++++++++++++++++++++++++++++--
2 files changed, 91 insertions(+), 3 deletions(-)
diff --git a/src/hlvariant/shogi/gamestate.h b/src/hlvariant/shogi/gamestate.h
index 4ca83ac..43cbe79 100644
--- a/src/hlvariant/shogi/gamestate.h
+++ b/src/hlvariant/shogi/gamestate.h
@@ -47,6 +47,7 @@ public:
virtual void move(const Move& m);
virtual void basicMove(const Move& m);
virtual void captureOn(const Point& p);
+ virtual bool promotionZone(typename Piece::Color player, const Point& p) const;
virtual void setTurn(typename Piece::Color color);
virtual typename Piece::Color previousTurn() const;
@@ -109,6 +110,11 @@ void GameState::setup() {
#undef COL
template
+bool GameState::promotionZone(typename Piece::Color player, const Point& p) const {
+ return player == Piece::WHITE ? p.y >= 6 : p.y <= 2;
+}
+
+template
bool GameState::operator==(const GameState& other) const {
return m_turn == other.m_turn &&
m_board == other.m_board;
diff --git a/src/hlvariant/shogi/serializer.h b/src/hlvariant/shogi/serializer.h
index e7c2177..d3f3dc7 100644
--- a/src/hlvariant/shogi/serializer.h
+++ b/src/hlvariant/shogi/serializer.h
@@ -30,10 +30,15 @@ public:
typedef typename GameState::Board Board;
typedef typename Board::Piece Piece;
typedef typename GameState::Move Move;
-private:
+protected:
int m_rep;
+
+ virtual bool isAmbiguous(const Move& move, const GameState& ref) const;
+ virtual QString square(const Point& p, const Point& size) const;
+ virtual QChar symbol(typename Piece::Type type) const;
public:
Serializer(int rep);
+ virtual ~Serializer();
QString serialize(const Move&, const GameState& ref);
Move deserialize(const QString& str, const GameState& ref);
@@ -45,9 +50,86 @@ template
Serializer::Serializer(int rep)
: m_rep(rep) { }
+
+template
+Serializer::~Serializer() { }
+
+template
+bool Serializer::isAmbiguous(const Move& move, const GameState& ref) const {
+ Piece piece = move.drop();
+ if (piece == Piece())
+ piece = ref.board().get(move.from());
+
+ bool ambiguous = false;
+ if (move.drop() == Piece()) {
+ for (int i = 0; i < ref.board().size().x; i++) {
+ for (int j = 0; j < ref.board().size().x; j++) {
+ Point p(i, j);
+ if (p == move.from() || ref.board().get(p) != piece)
+ continue;
+ Move mv(p, move.to());
+ LegalityCheck check(ref);
+ if (check.legal(mv)) {
+ ambiguous = true;
+ break;
+ }
+ }
+ }
+ }
+
+ return ambiguous;
+}
+
+template
+QString Serializer::square(const Point& p, const Point& size) const {
+ return QString::number(size.x - p.x) + QString(p.y + 'a');
+}
+
+
+template
+QString Serializer::serialize(const Move& move, const GameState& ref) {
+ Piece piece = move.drop();
+ if (piece == Piece())
+ piece = ref.board().get(move.from());
+
+ bool ambiguous = isAmbiguous(move, ref);
+
+ QString res;
+ if (piece.promoted())
+ res += "+";
+
+ res += symbol(piece.type());
+
+ 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 (!piece.promoted() &&
+ move.drop() == Piece() &&
+ ref.promotionZone(ref.turn(), move.to())) {
+ if (move.promoteTo() != -1)
+ res += "+";
+ else
+ res += "=";
+ }
+ return res;
+}
+
template
-QString Serializer::serialize(const Move&, const GameState&) {
- return "N/A"; // BROKEN
+QChar Serializer::symbol(typename Piece::Type type) const {
+ if (type == Piece::KNIGHT)
+ return 'N';
+ else
+ return Piece::typeName(type)[0].toUpper();
}
template
--
2.11.4.GIT