Refactored TurnTest.
[tagua/yd.git] / src / moveserializer.impl.h
blob3eb8b7eeed48fc757c0a321ad2da1b6845b25bd9
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"
15 #include "algebraicnotation.h"
17 /**
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
20 * value is undefined.
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);
32 if (!piece) {
33 m_ref.dump();
34 std::cout << "Move is: " << m_move.toString(m_ref.size().y) << std::endl;
37 Q_ASSERT(piece);
38 QString res;
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);
46 else {
47 if (m_move.type() == Move::KingSideCastling) {
48 res = "O-O";
50 else if (m_move.type() == Move::QueenSideCastling) {
51 res = "O-O-O";
53 else {
54 res = Piece::typeSymbol(piece.type());
56 AlgebraicNotation temp;
57 temp.from = m_move.from;
58 temp.to = m_move.to;
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]");
78 QString move = SAN();
79 DecoratedMove mv;
80 int x = 0;
81 while(reg.indexIn(move, x) != -1) {
82 if(reg.pos() > x)
83 mv.push_back(MovePart(move.mid(x, reg.pos()-x)));
84 switch(move[reg.pos()].toAscii()) {
85 case 'K':
86 mv.push_back(MovePart("king", MovePart::Figurine));
87 break;
88 case 'Q':
89 mv.push_back(MovePart("queen", MovePart::Figurine));
90 break;
91 case 'R':
92 mv.push_back(MovePart("rook", MovePart::Figurine));
93 break;
94 case 'B':
95 mv.push_back(MovePart("bishop", MovePart::Figurine));
96 break;
97 case 'N':
98 mv.push_back(MovePart("knight", MovePart::Figurine));
99 break;
100 case 'P':
101 mv.push_back(MovePart("pawn", MovePart::Figurine));
102 break;
104 x = reg.pos() + reg.matchedLength();
106 if(x<move.length())
107 mv.push_back(MovePart(move.mid(x)));
108 return mv;
111 #endif // MOVESERIALIZER_IMPL_H