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 QChar
symbol(typename
Piece::Type type
) const;
41 virtual ~Serializer();
43 QString
serialize(const Move
&, const GameState
& ref
);
44 Move
deserialize(const QString
& str
, const GameState
& ref
);
49 template <typename LegalityCheck
>
50 Serializer
<LegalityCheck
>::Serializer(int 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();
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
++) {
68 if (p
== move
.from() || ref
.board().get(p
) != piece
)
70 Move
mv(p
, move
.to());
71 LegalityCheck
check(ref
);
72 if (check
.legal(mv
)) {
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();
93 piece
= ref
.board().get(move
.from());
95 bool ambiguous
= isAmbiguous(move
, ref
);
101 res
+= symbol(piece
.type());
104 res
+= square(move
.from(), ref
.board().size());
107 if (move
.drop() != Piece())
109 else if (ref
.board().get(move
.to()) != Piece())
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)
127 template <typename LegalityCheck
>
128 QChar Serializer
<LegalityCheck
>::symbol(typename
Piece::Type type
) const {
129 if (type
== Piece::KNIGHT
)
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
141 } // namespace HLVariant
143 #endif // HLVARIANT__SHOGI__SERIALIZER_H