Replaced all std::cout with kDebug.
[tagua/yd.git] / src / hlvariant / chess / san.cpp
blobc88596e2a2d466ebd3dc5d6d267377639e42e811
1 /*
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.
9 */
11 #include "san.h"
12 #include "piece.h"
14 namespace HLVariant {
15 namespace Chess {
17 // 1 2 3
18 QRegExp SAN::pattern("^([PRNBKQ])?([a-wyzA-Z]?\\d*|x\\d+)([-x@])?"
19 // 4 5 6
20 "([a-zA-Z]\\d+)(=?([RNBKQrnbkq]))?[+#]?[\?!]*");
21 QRegExp SAN::kingCastlingPattern("^[oO0]-?[oO0][+#]?");
22 QRegExp SAN::queenCastlingPattern("^[oO0]-?[oO0]-?[oO0][+#]?");
23 QRegExp SAN::nonePattern("^none");
25 SAN::SAN()
26 : from(Point::invalid())
27 , to(Point::invalid())
28 , type(-1)
29 , promotion(-1)
30 , castling(NoCastling)
31 , drop(false) {
34 int SAN::getType(const QString& letter) {
35 if (letter.isEmpty())
36 return Piece::PAWN;
38 switch(letter[0].toLower().toAscii()) {
39 case 'k':
40 return Piece::KING;
41 case 'q':
42 return Piece::QUEEN;
43 case 'r':
44 return Piece::ROOK;
45 case 'n':
46 return Piece::KNIGHT;
47 case 'b':
48 return Piece::BISHOP;
49 case 'p':
50 return Piece::PAWN;
51 default:
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) == "@";
65 if (drop)
66 from = Point::invalid();
67 else
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) {
75 castling = QueenSide;
77 offset += queenCastlingPattern.matchedLength();
79 else if (kingCastlingPattern.indexIn(str, offset, QRegExp::CaretAtOffset) != -1) {
80 castling = KingSide;
82 offset += kingCastlingPattern.matchedLength();
84 else {
85 //kDebug() << "error!!!! " << str.mid(offset);
86 to = Point::invalid();
90 void SAN::load(const QString& str, int ysize) {
91 int offset = 0;
92 load(str, offset, ysize);
95 std::ostream& operator<<(std::ostream& os, const SAN& move) {
96 if (move.castling == SAN::KingSide)
97 return os << "O-O";
98 else if (move.castling == SAN::QueenSide)
99 return os << "O-O-O";
100 else
101 return os << move.type << ": " << move.from << " -> " << move.to;
105 } // namespace Chess
106 } // namespace HLVariant