Variants are not singletons anymore.
[tagua/yd.git] / src / moveserializer.impl.h
blobe172f1f34f27b32aea0498e44907222ca64d6431
1 /*
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.
9 */
11 #ifndef MOVESERIALIZER_IMPL_H
12 #define MOVESERIALIZER_IMPL_H
14 #include "moveserializer.h"
16 /**
17 * Assume the move has already been tested against @a ref.
18 * Calling this function on an untested or illegal move is safe but its return
19 * value is undefined.
20 * Do not try to call this function on an invalid move.
21 * @param ref The position in which this move shall be executed.
22 * @return A compact SAN representation for this move.
24 template <typename Pos>
25 QString MoveSerializerBase<Pos>::SAN() const {
26 Q_ASSERT(m_move.valid());
28 Piece piece = m_ref.get(m_move.from);
29 Piece captured = m_ref.get(m_move.to);
31 if (!piece) {
32 m_ref.dump();
33 std::cout << "Move is: " << m_move.toString(m_ref.size().x, m_ref.size().y) << std::endl;
36 Q_ASSERT(piece);
37 QString res;
38 if (piece.type() == PAWN) {
39 if (captured || m_move.type() == Move::EnPassantCapture)
40 res = m_move.from.col() + "x";
42 res += m_move.to.toString(m_ref.size().y);
45 else {
46 if (m_move.type() == Move::KingSideCastling) {
47 res = "O-O";
49 else if (m_move.type() == Move::QueenSideCastling) {
50 res = "O-O-O";
52 else {
53 res = Piece::typeSymbol(piece.type());
55 AlgebraicNotation temp;
56 temp.from = m_move.from;
57 temp.to = m_move.to;
58 temp.type = piece.type();
59 temp.castling = AlgebraicNotation::NoCastling;
60 minimalNotation(temp, m_ref);
62 res += temp.from.toString(m_ref.size().y);
63 if (captured) res += "x";
64 res += temp.to.toString(m_ref.size().y);
68 if (m_move.type() == Move::Promotion)
69 res += "=" + Piece::typeSymbol(m_move.promotionType);
71 return res + checkSuffix();
74 template <typename Pos>
75 DecoratedMove MoveSerializerBase<Pos>::toDecoratedMove() const {
76 static QRegExp reg("[KQRBNP]");
77 QString move = SAN();
78 DecoratedMove mv;
79 int x = 0;
80 while(reg.indexIn(move, x) != -1) {
81 if(reg.pos() > x)
82 mv.push_back(MovePart(move.mid(x, reg.pos()-x)));
83 switch(move[reg.pos()].toAscii()) {
84 case 'K':
85 mv.push_back(MovePart("king", MovePart::Figurine));
86 break;
87 case 'Q':
88 mv.push_back(MovePart("queen", MovePart::Figurine));
89 break;
90 case 'R':
91 mv.push_back(MovePart("rook", MovePart::Figurine));
92 break;
93 case 'B':
94 mv.push_back(MovePart("bishop", MovePart::Figurine));
95 break;
96 case 'N':
97 mv.push_back(MovePart("knight", MovePart::Figurine));
98 break;
99 case 'P':
100 mv.push_back(MovePart("pawn", MovePart::Figurine));
101 break;
103 x = reg.pos() + reg.matchedLength();
105 if(x<move.length())
106 mv.push_back(MovePart(move.mid(x)));
107 return mv;
110 #endif // MOVESERIALIZER_IMPL_H