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.
18 QRegExp
SAN::pattern("^([PRNBKQ])?([a-wyzA-Z]?\\d*|x\\d+)([-x@])?"
20 "([a-zA-Z]\\d+)(=?([RNBKQrnbkq]))?[+#]?[\?!]*");
21 QRegExp
SAN::kingCastlingPattern("^[oO0]-?[oO0][+#]?");
22 QRegExp
SAN::queenCastlingPattern("^[oO0]-?[oO0]-?[oO0][+#]?");
23 QRegExp
SAN::nonePattern("^none");
26 : from(Point::invalid())
27 , to(Point::invalid())
30 , castling(NoCastling
)
34 int SAN::getType(const QString
& letter
) {
38 switch(letter
[0].toLower().toAscii()) {
52 return Piece::INVALID_TYPE
;
56 void SAN::load(const QString
& str
, int& offset
, int ysize
) {
57 if (nonePattern
.indexIn(str
, offset
, QRegExp::CaretAtOffset
) != -1) {
58 from
= Point::invalid();
59 to
= Point::invalid();
60 offset
+= nonePattern
.matchedLength();
62 else if (pattern
.indexIn(str
, offset
, QRegExp::CaretAtOffset
) != -1) {
63 type
= getType(pattern
.cap(1));
64 drop
= pattern
.cap(3) == "@";
66 from
= Point::invalid();
68 from
= Point(pattern
.cap(2), ysize
);
69 to
= Point(pattern
.cap(4), ysize
);
70 promotion
= pattern
.cap(6).isEmpty() ? -1 : getType(pattern
.cap(6));
71 castling
= NoCastling
;
72 offset
+= pattern
.matchedLength();
74 else if (queenCastlingPattern
.indexIn(str
, offset
, QRegExp::CaretAtOffset
) != -1) {
77 offset
+= queenCastlingPattern
.matchedLength();
79 else if (kingCastlingPattern
.indexIn(str
, offset
, QRegExp::CaretAtOffset
) != -1) {
82 offset
+= kingCastlingPattern
.matchedLength();
85 //kDebug() << "error!!!! " << str.mid(offset);
86 to
= Point::invalid();
90 void SAN::load(const QString
& str
, int ysize
) {
92 load(str
, offset
, ysize
);
95 std::ostream
& operator<<(std::ostream
& os
, const SAN
& move
) {
96 if (move
.castling
== SAN::KingSide
)
98 else if (move
.castling
== SAN::QueenSide
)
101 return os
<< move
.type
<< ": " << move
.from
<< " -> " << move
.to
;
106 } // namespace HLVariant