Created SimpleAnimator stub.
[tagua/yd.git] / src / algebraicnotation.cpp
blob06075e6db8eb17280413afd13eb3148bb0420ffa
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 #include <iostream>
12 #include <QRegExp>
13 #include "algebraicnotation.h"
14 #include "variants/xchess/piece.h"
15 #include "variants/xchess/move.h"
17 //BEGIN AlgebraicNotation
19 // 1 2 3
20 QRegExp AlgebraicNotation::pattern("^([PRNBKQ])?([a-wyzA-Z]?\\d*|x\\d+)([-x@])?"
21 // 4 5 6
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())
30 , type(-1)
31 , promotion(-1)
32 , castling(NoCastling)
33 , drop(false) {
36 AlgebraicNotation::AlgebraicNotation(const QString& str, int ysize)
37 : from(Point::invalid())
38 , to(Point::invalid())
39 , type(-1)
40 , promotion(-1)
41 , castling(NoCastling)
42 , drop(false) {
44 int offset = 0;
45 init(str, offset, ysize);
48 AlgebraicNotation::AlgebraicNotation(const QString& str, int& offset, int ysize)
49 : from(Point::invalid())
50 , to(Point::invalid())
51 , type(-1)
52 , promotion(-1)
53 , castling(NoCastling)
54 , drop(false) {
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) == "@";
69 if (drop)
70 from = Point::invalid();
71 else
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) {
81 castling = QueenSide;
83 offset += queenCastlingPattern.matchedLength();
85 else if (kingCastlingPattern.indexIn(str, offset, QRegExp::CaretAtOffset) != -1) {
86 castling = KingSide;
88 offset += kingCastlingPattern.matchedLength();
90 else {
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)
98 return os << "O-O";
99 else if (move.castling == AlgebraicNotation::QueenSide)
100 return os << "O-O-O";
101 else
102 return os << move.type << ": " << move.from << " -> " << move.to;
105 //END AlgebraicNotation
108 //BEGIN VerboseNotation
109 // 1 2 3 5
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();
124 else
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));
132 else
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;
140 else {
141 from = Point::invalid();
142 to = Point::invalid();
143 castling = AlgebraicNotation::NoCastling;
147 //END VerboseNotation