push c6c9a0f557d256d471fa4608ad1fd3b70424bdfb
[tagua/yd.git] / src / variants / chess / san.cpp
blobef37decddcacc8808d0ceff91e0964de738c7e43
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"
13 #include <core/type.h>
14 #include "king.h"
16 using namespace Chess;
18 // 1 2 3
19 QRegExp SAN::pattern("^([PRNBKQ])?([a-wyzA-Z]?\\d*|x\\d+)([-x@])?"
20 // 4 5 6
21 "([a-zA-Z]\\d+)(=?([RNBKQrnbkq]))?[+#]?[\?!]*");
22 QRegExp SAN::kingCastlingPattern("^[oO0]-?[oO0][+#]?");
23 QRegExp SAN::queenCastlingPattern("^[oO0]-?[oO0]-?[oO0][+#]?");
24 QRegExp SAN::nonePattern("^none");
26 SAN::SAN()
27 : src(Point::invalid())
28 , dst(Point::invalid())
29 , type(0)
30 , promotion(0)
31 , castling(NoCastling)
32 , drop(false) {
35 const IType* SAN::getType(const QString& letter) {
36 // FIXME: fix types
37 if (letter.isEmpty())
38 return King/*Pawn*/::self();
40 switch(letter[0].toLower().toAscii()) {
41 case 'k':
42 return King::self();
43 case 'q':
44 return King/*Queen*/::self();
45 case 'r':
46 return King/*Rook*/::self();
47 case 'n':
48 return King/*Knight*/::self();
49 case 'b':
50 return King/*Bishop*/::self();
51 case 'p':
52 return King/*Pawn*/::self();
53 default:
54 return 0;
58 void SAN::load(const QString& str, int& offset, int ysize) {
59 if (nonePattern.indexIn(str, offset, QRegExp::CaretAtOffset) != -1) {
60 src = Point::invalid();
61 dst = Point::invalid();
62 offset += nonePattern.matchedLength();
64 else if (pattern.indexIn(str, offset, QRegExp::CaretAtOffset) != -1) {
65 type = getType(pattern.cap(1));
66 drop = pattern.cap(3) == "@";
67 if (drop)
68 src = Point::invalid();
69 else
70 src = Point(pattern.cap(2), ysize);
71 dst = Point(pattern.cap(4), ysize);
72 promotion = pattern.cap(6).isEmpty() ? 0 : getType(pattern.cap(6));
73 castling = NoCastling;
74 offset += pattern.matchedLength();
76 else if (queenCastlingPattern.indexIn(str, offset, QRegExp::CaretAtOffset) != -1) {
77 castling = QueenSide;
79 offset += queenCastlingPattern.matchedLength();
81 else if (kingCastlingPattern.indexIn(str, offset, QRegExp::CaretAtOffset) != -1) {
82 castling = KingSide;
84 offset += kingCastlingPattern.matchedLength();
86 else {
87 //kDebug() << "error!!!! " << str.mid(offset);
88 dst = Point::invalid();
92 void SAN::load(const QString& str, int ysize) {
93 int offset = 0;
94 load(str, offset, ysize);
97 QDebug operator<<(QDebug os, const SAN& move) {
98 if (move.castling == SAN::KingSide)
99 os << "O-O";
100 else if (move.castling == SAN::QueenSide)
101 os << "O-O-O";
102 else
103 os << move.type << ": " << move.src << " -> " << move.dst;
104 return os;