Working on the shogi serializer.
[tagua.git] / src / hlvariant / shogi / serializer.h
blobd3f3dc7ab57bd70617e52cf14609b6f8472a7f52
1 /*
2 Copyright (c) 2007 Paolo Capriotti <p.capriotti@sns.it>
3 (c) 2007 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 */
11 #ifndef HLVARIANT__SHOGI__SERIALIZER_H
12 #define HLVARIANT__SHOGI__SERIALIZER_H
14 #include <QString>
16 namespace HLVariant {
17 namespace Shogi {
19 template <typename _LegalityCheck>
20 class Serializer {
21 public:
22 enum {
23 SIMPLE = 0,
24 COMPACT = 1,
25 DECORATED = 2
28 typedef _LegalityCheck LegalityCheck;
29 typedef typename LegalityCheck::GameState GameState;
30 typedef typename GameState::Board Board;
31 typedef typename Board::Piece Piece;
32 typedef typename GameState::Move Move;
33 protected:
34 int m_rep;
36 virtual bool isAmbiguous(const Move& move, const GameState& ref) const;
37 virtual QString square(const Point& p, const Point& size) const;
38 virtual QChar symbol(typename Piece::Type type) const;
39 public:
40 Serializer(int rep);
41 virtual ~Serializer();
43 QString serialize(const Move&, const GameState& ref);
44 Move deserialize(const QString& str, const GameState& ref);
47 // IMPLEMENTATION
49 template <typename LegalityCheck>
50 Serializer<LegalityCheck>::Serializer(int rep)
51 : m_rep(rep) { }
54 template <typename LegalityCheck>
55 Serializer<LegalityCheck>::~Serializer() { }
57 template <typename LegalityCheck>
58 bool Serializer<LegalityCheck>::isAmbiguous(const Move& move, const GameState& ref) const {
59 Piece piece = move.drop();
60 if (piece == Piece())
61 piece = ref.board().get(move.from());
63 bool ambiguous = false;
64 if (move.drop() == Piece()) {
65 for (int i = 0; i < ref.board().size().x; i++) {
66 for (int j = 0; j < ref.board().size().x; j++) {
67 Point p(i, j);
68 if (p == move.from() || ref.board().get(p) != piece)
69 continue;
70 Move mv(p, move.to());
71 LegalityCheck check(ref);
72 if (check.legal(mv)) {
73 ambiguous = true;
74 break;
80 return ambiguous;
83 template <typename LegalityCheck>
84 QString Serializer<LegalityCheck>::square(const Point& p, const Point& size) const {
85 return QString::number(size.x - p.x) + QString(p.y + 'a');
89 template <typename LegalityCheck>
90 QString Serializer<LegalityCheck>::serialize(const Move& move, const GameState& ref) {
91 Piece piece = move.drop();
92 if (piece == Piece())
93 piece = ref.board().get(move.from());
95 bool ambiguous = isAmbiguous(move, ref);
97 QString res;
98 if (piece.promoted())
99 res += "+";
101 res += symbol(piece.type());
103 if (ambiguous) {
104 res += square(move.from(), ref.board().size());
107 if (move.drop() != Piece())
108 res += "*";
109 else if (ref.board().get(move.to()) != Piece())
110 res += "x";
111 else
112 res += "-";
114 res += square(move.to(), ref.board().size());
116 if (!piece.promoted() &&
117 move.drop() == Piece() &&
118 ref.promotionZone(ref.turn(), move.to())) {
119 if (move.promoteTo() != -1)
120 res += "+";
121 else
122 res += "=";
124 return res;
127 template <typename LegalityCheck>
128 QChar Serializer<LegalityCheck>::symbol(typename Piece::Type type) const {
129 if (type == Piece::KNIGHT)
130 return 'N';
131 else
132 return Piece::typeName(type)[0].toUpper();
135 template <typename LegalityCheck>
136 typename Serializer<LegalityCheck>::Move Serializer<LegalityCheck>::deserialize(const QString&, const GameState&) {
137 return Move(); // BROKEN
140 } // namespace Shogi
141 } // namespace HLVariant
143 #endif // HLVARIANT__SHOGI__SERIALIZER_H