2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
3 (c) 2006 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 MOVESERIALIZER_IMPL_H
12 #define MOVESERIALIZER_IMPL_H
14 #include "moveserializer.h"
15 #include "algebraicnotation.h"
18 * Assume the move has already been tested against @a ref.
19 * Calling this function on an untested or illegal move is safe but its return
21 * Do not try to call this function on an invalid move.
22 * @param ref The position in which this move shall be executed.
23 * @return A compact SAN representation for this move.
25 template <typename Pos
>
26 QString MoveSerializerBase
<Pos
>::SAN() const {
27 Q_ASSERT(m_move
.valid());
29 Piece piece
= m_ref
.get(m_move
.from
);
30 Piece captured
= m_ref
.get(m_move
.to
);
34 std::cout
<< "Move is: " << m_move
.toString(m_ref
.size().y
) << std::endl
;
39 if (piece
.type() == PAWN
) {
40 if (captured
|| m_move
.type() == Move::EnPassantCapture
)
41 res
= m_move
.from
.col() + "x";
43 res
+= m_move
.to
.toString(m_ref
.size().y
);
47 if (m_move
.type() == Move::KingSideCastling
) {
50 else if (m_move
.type() == Move::QueenSideCastling
) {
54 res
= Piece::typeSymbol(piece
.type());
56 AlgebraicNotation temp
;
57 temp
.from
= m_move
.from
;
59 temp
.type
= piece
.type();
60 temp
.castling
= AlgebraicNotation::NoCastling
;
61 minimalNotation(temp
, m_ref
);
63 res
+= temp
.from
.toString(m_ref
.size().y
);
64 if (captured
) res
+= "x";
65 res
+= temp
.to
.toString(m_ref
.size().y
);
69 if (m_move
.type() == Move::Promotion
)
70 res
+= "=" + Piece::typeSymbol(m_move
.promotionType
);
72 return res
+ checkSuffix();
75 template <typename Pos
>
76 DecoratedMove MoveSerializerBase
<Pos
>::toDecoratedMove() const {
77 static QRegExp
reg("[KQRBNP]");
81 while(reg
.indexIn(move
, x
) != -1) {
83 mv
.push_back(MovePart(move
.mid(x
, reg
.pos()-x
)));
84 switch(move
[reg
.pos()].toAscii()) {
86 mv
.push_back(MovePart("king", MovePart::Figurine
));
89 mv
.push_back(MovePart("queen", MovePart::Figurine
));
92 mv
.push_back(MovePart("rook", MovePart::Figurine
));
95 mv
.push_back(MovePart("bishop", MovePart::Figurine
));
98 mv
.push_back(MovePart("knight", MovePart::Figurine
));
101 mv
.push_back(MovePart("pawn", MovePart::Figurine
));
104 x
= reg
.pos() + reg
.matchedLength();
107 mv
.push_back(MovePart(move
.mid(x
)));
111 #endif // MOVESERIALIZER_IMPL_H