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.
11 #ifndef HLVARIANT__SHOGI__SERIALIZER_H
12 #define HLVARIANT__SHOGI__SERIALIZER_H
19 template <typename _LegalityCheck
>
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
;
36 virtual bool isAmbiguous(const Move
& move
, const GameState
& ref
) const;
37 virtual QString
square(const Point
& p
, const Point
& size
) const;
38 virtual QString
symbol(const Piece
& piece
) const;
39 virtual typename
Piece::Type
getType(const QChar
& letter
) const;
42 virtual ~Serializer();
44 QString
serialize(const Move
&, const GameState
& ref
);
45 Move
deserialize(const QString
& str
, const GameState
& ref
);
50 template <typename LegalityCheck
>
51 Serializer
<LegalityCheck
>::Serializer(int rep
)
55 template <typename LegalityCheck
>
56 Serializer
<LegalityCheck
>::~Serializer() { }
58 template <typename LegalityCheck
>
59 bool Serializer
<LegalityCheck
>::isAmbiguous(const Move
& move
, const GameState
& ref
) const {
60 Piece piece
= move
.drop();
62 piece
= ref
.board().get(move
.from());
64 bool ambiguous
= false;
65 if (move
.drop() == Piece()) {
66 for (int i
= 0; i
< ref
.board().size().x
; i
++) {
67 for (int j
= 0; j
< ref
.board().size().x
; j
++) {
69 if (p
== move
.from() || ref
.board().get(p
) != piece
)
71 Move
mv(p
, move
.to());
72 LegalityCheck
check(ref
);
73 if (check
.legal(mv
)) {
84 template <typename LegalityCheck
>
85 QString Serializer
<LegalityCheck
>::square(const Point
& p
, const Point
& size
) const {
86 QString res
= QString::number(size
.x
- p
.x
);
87 if (m_rep
== DECORATED
) {
88 res
+= "{num_" + QString::number(p
.y
+ 1) + "}";
91 res
+= QString(p
.y
+ 'a');
98 template <typename LegalityCheck
>
99 QString Serializer
<LegalityCheck
>::serialize(const Move
& move
, const GameState
& ref
) {
100 Piece piece
= move
.drop();
101 if (piece
== Piece())
102 piece
= ref
.board().get(move
.from());
108 if (move
.drop() != Piece()) {
109 res
+= symbol(piece
);
112 res
+= square(move
.from(), ref
.board().size());
113 res
+= square(move
.to(), ref
.board().size());
114 if (move
.promoteTo() != -1)
121 bool ambiguous
= isAmbiguous(move
, ref
);
124 if (piece
.promoted())
127 res
+= symbol(piece
);
130 res
+= square(move
.from(), ref
.board().size());
133 if (move
.drop() != Piece())
135 else if (ref
.board().get(move
.to()) != Piece())
140 res
+= square(move
.to(), ref
.board().size());
142 // if it is a promotion
143 if (move
.promoteTo() != -1)
145 // if it is a refused promotion
146 else if (ref
.canPromote(piece
) &&
147 move
.drop() == Piece() &&
148 ref
.promotionZone(ref
.turn(), move
.to())) {
157 template <typename LegalityCheck
>
158 QString Serializer
<LegalityCheck
>::symbol(const Piece
& piece
) const {
159 if (m_rep
== DECORATED
) {
161 if (piece
.promoted())
163 res
+= piece
.typeName();
164 if (piece
.type() == Piece::KING
)
165 res
+= piece
.color() == Piece::BLACK
? '1' : '2';
169 if (piece
.type() == Piece::KNIGHT
)
172 return piece
.typeName()[0].toUpper();
176 template <typename LegalityCheck
>
177 typename Serializer
<LegalityCheck
>::Piece::Type
178 Serializer
<LegalityCheck
>::getType(const QChar
& letter
) const {
179 switch(letter
.toLower().toAscii()) {
185 return Piece::BISHOP
;
189 return Piece::KNIGHT
;
191 return Piece::SILVER
;
197 return Piece::INVALID_TYPE
;
201 template <typename LegalityCheck
>
202 typename Serializer
<LegalityCheck
>::Move
203 Serializer
<LegalityCheck
>::deserialize(const QString
& str
, const GameState
& ref
) {
204 if (str
[0].isDigit()) {
206 Point
orig(ref
.board().size().x
- str
[0].digitValue(), str
[1].toAscii() - 'a');
207 Point
dest(ref
.board().size().x
- str
[2].digitValue(), str
[3].toAscii() - 'a');
208 return Move(orig
, dest
, ((str
.size() > 4) && (str
[4] == '+')) ? 1 : -1);
212 std::cerr
<< "Expected a star, got a '" << str
[1].toAscii() << "'" << std::endl
;
215 typename
Piece::Type t
= getType(str
[0]);
216 Point
to(ref
.board().size().x
- str
[2].digitValue(), str
[3].toAscii() - 'a');
217 return Move(Piece(ref
.turn(), t
), to
);
223 } // namespace HLVariant
225 #endif // HLVARIANT__SHOGI__SERIALIZER_H