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+)(=?([RNBKQrnbkq]))?[+#]?[\?!]*");
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 type
= ChessPiece::getType(pattern
.cap(1));
67 drop
= pattern
.cap(3) == "@";
69 from
= Point::invalid();
71 from
= Point(pattern
.cap(2), ysize
);
72 to
= Point(pattern
.cap(4), ysize
);
73 promotion
= pattern
.cap(6).isEmpty() ? -1 : ChessPiece::getType(pattern
.cap(6));
74 castling
= NoCastling
;
75 offset
+= pattern
.matchedLength();
77 else if (queenCastlingPattern
.indexIn(str
, offset
, QRegExp::CaretAtOffset
) != -1) {
80 offset
+= queenCastlingPattern
.matchedLength();
82 else if (kingCastlingPattern
.indexIn(str
, offset
, QRegExp::CaretAtOffset
) != -1) {
85 offset
+= kingCastlingPattern
.matchedLength();
88 //std::cout << "error!!!! " << str.mid(offset) << std::endl;
89 to
= Point::invalid();
93 std::ostream
& operator<<(std::ostream
& os
, const AlgebraicNotation
& move
) {
94 if (move
.castling
== AlgebraicNotation::KingSide
)
96 else if (move
.castling
== AlgebraicNotation::QueenSide
)
99 return os
<< move
.type
<< ": " << move
.from
<< " -> " << move
.to
;
102 //END AlgebraicNotation
105 //BEGIN VerboseNotation
107 QRegExp
VerboseNotation::pattern("([PRNBKQ])/([a-zA-Z]\\d+|@@)-([a-zA-Z]\\d+)(=([PRNBKQ]))?");
108 QRegExp
VerboseNotation::kingCastlingPattern("[oO0]-[oO0]");
109 QRegExp
VerboseNotation::queenCastlingPattern("[oO0]-[oO0]-[oO0]");
110 QRegExp
VerboseNotation::nonePattern("none");
112 VerboseNotation::VerboseNotation(const QString
& str
, int ysize
)
113 : promotion(INVALID_TYPE
) {
114 if (nonePattern
.indexIn(str
) == 0) {
115 from
= Point::invalid();
116 to
= Point::invalid();
118 else if (pattern
.indexIn(str
) == 0) {
119 if (pattern
.cap(2) == "@@")
120 from
= Point::invalid();
122 from
= Point(pattern
.cap(2), ysize
);
124 to
= Point(pattern
.cap(3), ysize
);
126 type
= ChessPiece::getType(pattern
.cap(1));
127 if (!pattern
.cap(5).isEmpty())
128 promotion
= ChessPiece::getType(pattern
.cap(6));
130 promotion
= INVALID_TYPE
;
131 castling
= AlgebraicNotation::NoCastling
;
133 else if (queenCastlingPattern
.indexIn(str
) == 0)
134 castling
= AlgebraicNotation::QueenSide
;
135 else if (kingCastlingPattern
.indexIn(str
) == 0)
136 castling
= AlgebraicNotation::KingSide
;
138 from
= Point::invalid();
139 to
= Point::invalid();
140 castling
= AlgebraicNotation::NoCastling
;
144 //END VerboseNotation