2 Copyright (c) 2007 Paolo Capriotti <p.capriotti@gmail.com>
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.
13 #include <core/type.h>
16 using namespace Chess
;
19 QRegExp
SAN::pattern("^([PRNBKQ])?([a-wyzA-Z]?\\d*|x\\d+)([-x@])?"
21 "([a-zA-Z]\\d+)(=?([RNBKQrnbkq]))?[+#]?[\?!]*");
22 QRegExp
SAN::kingCastlingPattern("^[oO0]-?[oO0][+#]?");
23 QRegExp
SAN::queenCastlingPattern("^[oO0]-?[oO0]-?[oO0][+#]?");
24 QRegExp
SAN::nonePattern("^none");
27 : src(Point::invalid())
28 , dst(Point::invalid())
31 , castling(NoCastling
)
35 const IType
* SAN::getType(const QString
& letter
) {
38 return King
/*Pawn*/::self();
40 switch(letter
[0].toLower().toAscii()) {
44 return King
/*Queen*/::self();
46 return King
/*Rook*/::self();
48 return King
/*Knight*/::self();
50 return King
/*Bishop*/::self();
52 return King
/*Pawn*/::self();
58 void SAN::load(const QString
& str
, int& offset
, int ysize
) {
59 if (nonePattern
.indexIn(str
, offset
, QRegExp::CaretAtOffset
) != -1) {
60 src
= Point::invalid();
61 dst
= Point::invalid();
62 offset
+= nonePattern
.matchedLength();
64 else if (pattern
.indexIn(str
, offset
, QRegExp::CaretAtOffset
) != -1) {
65 type
= getType(pattern
.cap(1));
66 drop
= pattern
.cap(3) == "@";
68 src
= Point::invalid();
70 src
= Point(pattern
.cap(2), ysize
);
71 dst
= Point(pattern
.cap(4), ysize
);
72 promotion
= pattern
.cap(6).isEmpty() ? 0 : getType(pattern
.cap(6));
73 castling
= NoCastling
;
74 offset
+= pattern
.matchedLength();
76 else if (queenCastlingPattern
.indexIn(str
, offset
, QRegExp::CaretAtOffset
) != -1) {
79 offset
+= queenCastlingPattern
.matchedLength();
81 else if (kingCastlingPattern
.indexIn(str
, offset
, QRegExp::CaretAtOffset
) != -1) {
84 offset
+= kingCastlingPattern
.matchedLength();
87 //kDebug() << "error!!!! " << str.mid(offset);
88 dst
= Point::invalid();
92 void SAN::load(const QString
& str
, int ysize
) {
94 load(str
, offset
, ysize
);
97 QDebug
operator<<(QDebug os
, const SAN
& move
) {
98 if (move
.castling
== SAN::KingSide
)
100 else if (move
.castling
== SAN::QueenSide
)
103 os
<< move
.type
<< ": " << move
.src
<< " -> " << move
.dst
;