Removed AlgebraicNotation from the variant API.
[tagua/yd.git] / src / positioninfo.cpp
blob4451ec6f41de64e52d66eeea0a8aa1ee8d0f7ed2
1 /*
2 Copyright (c) 2006-2007 Paolo Capriotti <p.capriotti@sns.it>
3 (c) 2006-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 "positioninfo.h"
12 #include "variants/chess.h"
13 #include "variants/variants.h"
14 #include "gameinfo.h"
15 #include "icsapi.h"
17 using namespace boost;
19 // Style 12 was designed by Daniel Sleator (sleator+@cs.cmu.edu) Darooha@ICC
20 QRegExp PositionInfo::pattern(
21 "^<12>\\s+" //header
22 "([qkbnrpQKBNRP-]{8})\\s+" //chessboard
23 "([qkbnrpQKBNRP-]{8})\\s+" //chessboard
24 "([qkbnrpQKBNRP-]{8})\\s+" //chessboard
25 "([qkbnrpQKBNRP-]{8})\\s+" //chessboard
26 "([qkbnrpQKBNRP-]{8})\\s+" //chessboard
27 "([qkbnrpQKBNRP-]{8})\\s+" //chessboard
28 "([qkbnrpQKBNRP-]{8})\\s+" //chessboard
29 "([qkbnrpQKBNRP-]{8})\\s+" //chessboard
30 "([BW])\\s+" //black/white
31 "(-1|[0-7])\\s+" //passing pawn
32 "([01])\\s+" //castle rights
33 "([01])\\s+" //castle rights
34 "([01])\\s+" //castle rights
35 "([01])\\s+" //castle rights
36 "(-?\\d+)\\s+" //50 moves made
37 "(\\d+)\\s+" //game num
38 "(\\S+)\\s+" //white name
39 "(\\S+)\\s+" //black name
40 "(-[1-4]|[0-2])\\s+" //status
41 "(\\d+)\\s+" //time
42 "(\\d+)\\s+" //inc
43 "(\\d+)\\s+" //w material
44 "(\\d+)\\s+" //b material
45 "(-?\\d+)\\s+" //w time
46 "(-?\\d+)\\s+" //b time
47 "(\\d+)\\s+" //move made
48 "(\\S+)\\s+" //coordmove
49 "(\\S+)\\s+" //time used
50 "(\\S+)\\s+" //algmove
51 "([0-1])" //flip
55 PositionInfo::PositionRow::PositionRow(const ICSAPIPtr& icsapi, const QString& str) {
56 Q_ASSERT(str.length() == 8);
58 row.resize(str.length());
59 for (int i = 0; i < str.length(); ++i) {
60 QChar c = str[i];
62 row[i] = icsapi->createPiece(c);
66 /**
67 * @return Position index as a 0-based halfmove count.
69 int PositionInfo::index() const {
70 int res = (moveIndex - 1) * 2;
71 if (turn == BLACK) res++;
72 return res;
75 PositionInfo::PositionInfo(const std::map<int, ICSGameData>& games, const QString& str) {
76 if (pattern.indexIn(str) != 0) {
77 valid = false;
78 return;
81 valid = true;
82 int gn = pattern.cap(CaptureIndexes::GameNumber).toInt();
83 std::map<int, ICSGameData>::const_iterator gi = games.find(gn);
84 ICSAPIPtr icsapi;
86 if (gi == games.end()) {
87 ERROR("Received style12 for unknown game " << gn);
88 icsapi = Variant::variant("Dummy")->icsAPI();
90 else {
91 icsapi = gi->second.icsapi;
94 std::vector<PositionRow> rows;
95 for (uint i = 0; i < 8; ++i)
96 rows.push_back(PositionRow(icsapi, pattern.cap(CaptureIndexes::ChessboardStart + i)));
98 gameNumber = pattern.cap(CaptureIndexes::GameNumber).toInt();
99 moveIndex = pattern.cap(CaptureIndexes::MoveOrdinal).toInt();
100 whitePlayer = pattern.cap(CaptureIndexes::WhitePlayer);
101 blackPlayer = pattern.cap(CaptureIndexes::BlackPlayer);
102 turn = pattern.cap(CaptureIndexes::Turn) == "W"? WHITE : BLACK;
104 int ep = pattern.cap(CaptureIndexes::EnPassant).toInt();
105 if (ep == -1)
106 enPassantSquare = Point::invalid();
107 else
108 enPassantSquare = Point(ep, turn == WHITE? 2 : 5);
110 bool wkCastle = pattern.cap(CaptureIndexes::WhiteKingCastle).toInt() == 1;
111 bool wqCastle = pattern.cap(CaptureIndexes::WhiteQueenCastle).toInt() == 1;
112 bool bkCastle = pattern.cap(CaptureIndexes::BlackKingCastle).toInt() == 1;
113 bool bqCastle = pattern.cap(CaptureIndexes::BlackQueenCastle).toInt() == 1;
115 position = icsapi->createChessboard(turn, wkCastle, wqCastle, bkCastle, bqCastle, enPassantSquare);
116 for (uint i = 0; i < 8; ++i) {
117 for (uint j = 0; j < rows[i].row.size(); ++j) {
118 position->set(Point(j,i), rows[i].row[j]);
122 relation = static_cast<Relation>(pattern.cap(CaptureIndexes::Relation).toInt());
124 whiteTime = pattern.cap(CaptureIndexes::WhiteTime).toInt();
125 blackTime = pattern.cap(CaptureIndexes::BlackTime).toInt();
126 if (pattern.cap(CaptureIndexes::TimeUsed).indexOf('.') == -1) {
127 // time is in seconds
128 whiteTime *= 1000;
129 blackTime *= 1000;
132 lastMoveSAN = pattern.cap(CaptureIndexes::LastMove);
133 lastMove = pattern.cap(CaptureIndexes::LastMoveVerbose);