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.
13 #include "algebraicnotation.h"
14 #include "variants/xchess/piece.h"
15 #include "variants/xchess/move.h"
17 //BEGIN AlgebraicNotation
20 QRegExp
AlgebraicNotation::pattern("^([PRNBKQ])?([a-wyzA-Z]?\\d*|x\\d+)([-x@])?"
22 "([a-zA-Z]\\d+)(=([RNBKQ]))?[+#]?[\?!]*");
23 QRegExp
AlgebraicNotation::kingCastlingPattern("^[oO0]-?[oO0][+#]?");
24 QRegExp
AlgebraicNotation::queenCastlingPattern("^[oO0]-?[oO0]-?[oO0][+#]?");
25 QRegExp
AlgebraicNotation::nonePattern("^none");
27 AlgebraicNotation::AlgebraicNotation()
28 : from(Point::invalid())
29 , to(Point::invalid())
32 , castling(NoCastling
)
36 AlgebraicNotation::AlgebraicNotation(const QString
& str
, int ysize
)
37 : from(Point::invalid())
38 , to(Point::invalid())
41 , castling(NoCastling
)
45 init(str
, offset
, ysize
);
48 AlgebraicNotation::AlgebraicNotation(const QString
& str
, int& offset
, int ysize
)
49 : from(Point::invalid())
50 , to(Point::invalid())
53 , castling(NoCastling
)
56 init(str
, offset
, ysize
);
59 void AlgebraicNotation::init(const QString
& str
, int& offset
, int ysize
) {
60 if (nonePattern
.indexIn(str
, offset
, QRegExp::CaretAtOffset
) != -1) {
61 from
= Point::invalid();
62 to
= Point::invalid();
63 offset
+= nonePattern
.matchedLength();
65 else if (pattern
.indexIn(str
, offset
, QRegExp::CaretAtOffset
) != -1) {
66 //std::cout << "matched " << pattern.cap(0) << std::endl;
67 type
= ChessPiece::getType(pattern
.cap(1));
68 drop
= pattern
.cap(3) == "@";
70 from
= Point::invalid();
72 from
= Point(pattern
.cap(2), ysize
);
74 to
= Point(pattern
.cap(4), ysize
);
75 promotion
= pattern
.cap(6).isEmpty() ? -1 : ChessPiece::getType(pattern
.cap(6));
76 castling
= NoCastling
;
78 offset
+= pattern
.matchedLength();
80 else if (queenCastlingPattern
.indexIn(str
, offset
, QRegExp::CaretAtOffset
) != -1) {
83 offset
+= queenCastlingPattern
.matchedLength();
85 else if (kingCastlingPattern
.indexIn(str
, offset
, QRegExp::CaretAtOffset
) != -1) {
88 offset
+= kingCastlingPattern
.matchedLength();
91 //std::cout << "error!!!! " << str.mid(offset) << std::endl;
92 to
= Point::invalid();
96 std::ostream
& operator<<(std::ostream
& os
, const AlgebraicNotation
& move
) {
97 if (move
.castling
== AlgebraicNotation::KingSide
)
99 else if (move
.castling
== AlgebraicNotation::QueenSide
)
100 return os
<< "O-O-O";
102 return os
<< move
.type
<< ": " << move
.from
<< " -> " << move
.to
;
105 //END AlgebraicNotation
108 //BEGIN VerboseNotation
110 QRegExp
VerboseNotation::pattern("([PRNBKQ])/([a-zA-Z]\\d+|@@)-([a-zA-Z]\\d+)(=([PRNBKQ]))?");
111 QRegExp
VerboseNotation::kingCastlingPattern("[oO0]-[oO0]");
112 QRegExp
VerboseNotation::queenCastlingPattern("[oO0]-[oO0]-[oO0]");
113 QRegExp
VerboseNotation::nonePattern("none");
115 VerboseNotation::VerboseNotation(const QString
& str
, int ysize
)
116 : promotion(INVALID_TYPE
) {
117 if (nonePattern
.indexIn(str
) == 0) {
118 from
= Point::invalid();
119 to
= Point::invalid();
121 else if (pattern
.indexIn(str
) == 0) {
122 if (pattern
.cap(2) == "@@")
123 from
= Point::invalid();
125 from
= Point(pattern
.cap(2), ysize
);
127 to
= Point(pattern
.cap(3), ysize
);
129 type
= ChessPiece::getType(pattern
.cap(1));
130 if (!pattern
.cap(5).isEmpty())
131 promotion
= ChessPiece::getType(pattern
.cap(6));
133 promotion
= INVALID_TYPE
;
134 castling
= AlgebraicNotation::NoCastling
;
136 else if (queenCastlingPattern
.indexIn(str
) == 0)
137 castling
= AlgebraicNotation::QueenSide
;
138 else if (kingCastlingPattern
.indexIn(str
) == 0)
139 castling
= AlgebraicNotation::KingSide
;
141 from
= Point::invalid();
142 to
= Point::invalid();
143 castling
= AlgebraicNotation::NoCastling
;
147 //END VerboseNotation