Prepare 1.0 alpha3 release.
[tagua/yd.git] / src / positioninfo.cpp
blobe2b6e922792503900939741287ff51281a148a71
1 /*
2 Copyright (c) 2006-2007 Paolo Capriotti <p.capriotti@gmail.com>
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 <KDebug>
13 #include "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 == 1) res++;
72 return res;
75 PositionInfo::PositionInfo()
76 : valid(false) { }
78 bool PositionInfo::load(std::map<int, ICSGameData>& games, const QString& str) {
79 if (pattern.indexIn(str) != 0) {
80 valid = false;
81 return true;
84 bool new_game = false;
86 valid = true;
87 int gn = pattern.cap(CaptureIndexes::GameNumber).toInt();
88 std::map<int, ICSGameData>::iterator gi = games.find(gn);
89 ICSAPIPtr icsapi;
91 if (gi == games.end()) {
92 kWarning() << "Received style12 for unknown game" << gn;
93 // create a gameinfo of type "dummy"
94 gi = games.insert(std::make_pair(gn, ICSGameData(gn, ""))).first;
95 new_game = true;
98 icsapi = gi->second.icsapi;
100 std::vector<PositionRow> rows;
101 for (uint i = 0; i < 8; ++i)
102 rows.push_back(PositionRow(icsapi, pattern.cap(CaptureIndexes::ChessboardStart + i)));
104 gameNumber = pattern.cap(CaptureIndexes::GameNumber).toInt();
105 moveIndex = pattern.cap(CaptureIndexes::MoveOrdinal).toInt();
106 whitePlayer = pattern.cap(CaptureIndexes::WhitePlayer);
107 blackPlayer = pattern.cap(CaptureIndexes::BlackPlayer);
108 turn = pattern.cap(CaptureIndexes::Turn) == "W"? 0 : 1;
110 int ep = pattern.cap(CaptureIndexes::EnPassant).toInt();
111 if (ep == -1)
112 enPassantSquare = Point::invalid();
113 else
114 enPassantSquare = Point(ep, turn == 0? 2 : 5);
116 bool wkCastle = pattern.cap(CaptureIndexes::WhiteKingCastle).toInt() == 1;
117 bool wqCastle = pattern.cap(CaptureIndexes::WhiteQueenCastle).toInt() == 1;
118 bool bkCastle = pattern.cap(CaptureIndexes::BlackKingCastle).toInt() == 1;
119 bool bqCastle = pattern.cap(CaptureIndexes::BlackQueenCastle).toInt() == 1;
121 position = icsapi->createChessboard(turn, wkCastle, wqCastle, bkCastle, bqCastle, enPassantSquare);
122 for (uint i = 0; i < 8; ++i) {
123 for (uint j = 0; j < rows[i].row.size(); ++j) {
124 position->set(Point(j,i), rows[i].row[j]);
128 relation = static_cast<Relation>(pattern.cap(CaptureIndexes::Relation).toInt());
130 whiteTime = pattern.cap(CaptureIndexes::WhiteTime).toInt();
131 blackTime = pattern.cap(CaptureIndexes::BlackTime).toInt();
132 if (pattern.cap(CaptureIndexes::TimeUsed).indexOf('.') == -1) {
133 // time is in seconds
134 whiteTime *= 1000;
135 blackTime *= 1000;
138 lastMoveSAN = pattern.cap(CaptureIndexes::LastMove);
139 lastMove = pattern.cap(CaptureIndexes::LastMoveVerbose);
141 return new_game;