Removed AlgebraicNotation from the variant API.
[tagua/yd.git] / src / algebraicnotation.cpp
blob996dd6eba1304610c553bc5a0ecc6696ac2e433f
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+)(=?([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())
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 type = ChessPiece::getType(pattern.cap(1));
67 drop = pattern.cap(3) == "@";
68 if (drop)
69 from = Point::invalid();
70 else
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) {
78 castling = QueenSide;
80 offset += queenCastlingPattern.matchedLength();
82 else if (kingCastlingPattern.indexIn(str, offset, QRegExp::CaretAtOffset) != -1) {
83 castling = KingSide;
85 offset += kingCastlingPattern.matchedLength();
87 else {
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)
95 return os << "O-O";
96 else if (move.castling == AlgebraicNotation::QueenSide)
97 return os << "O-O-O";
98 else
99 return os << move.type << ": " << move.from << " -> " << move.to;
102 //END AlgebraicNotation
105 //BEGIN VerboseNotation
106 // 1 2 3 5
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();
121 else
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));
129 else
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;
137 else {
138 from = Point::invalid();
139 to = Point::invalid();
140 castling = AlgebraicNotation::NoCastling;
144 //END VerboseNotation