Changed my email in the copyright statements.
[tagua/yd.git] / src / positioninfo.cpp
bloba8818027420f892de7b1ba5b72fb29c469b61cf5
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 "variants.h"
13 #include "gameinfo.h"
14 #include "icsapi.h"
16 using namespace boost;
18 // Style 12 was designed by Daniel Sleator (sleator+@cs.cmu.edu) Darooha@ICC
19 QRegExp PositionInfo::pattern(
20 "^<12>\\s+" //header
21 "([qkbnrpQKBNRP-]{8})\\s+" //chessboard
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 "([BW])\\s+" //black/white
30 "(-1|[0-7])\\s+" //passing pawn
31 "([01])\\s+" //castle rights
32 "([01])\\s+" //castle rights
33 "([01])\\s+" //castle rights
34 "([01])\\s+" //castle rights
35 "(-?\\d+)\\s+" //50 moves made
36 "(\\d+)\\s+" //game num
37 "(\\S+)\\s+" //white name
38 "(\\S+)\\s+" //black name
39 "(-[1-4]|[0-2])\\s+" //status
40 "(\\d+)\\s+" //time
41 "(\\d+)\\s+" //inc
42 "(\\d+)\\s+" //w material
43 "(\\d+)\\s+" //b material
44 "(-?\\d+)\\s+" //w time
45 "(-?\\d+)\\s+" //b time
46 "(\\d+)\\s+" //move made
47 "(\\S+)\\s+" //coordmove
48 "(\\S+)\\s+" //time used
49 "(\\S+)\\s+" //algmove
50 "([0-1])" //flip
54 PositionInfo::PositionRow::PositionRow(const ICSAPIPtr& icsapi, const QString& str) {
55 Q_ASSERT(str.length() == 8);
57 row.resize(str.length());
58 for (int i = 0; i < str.length(); ++i) {
59 QChar c = str[i];
61 row[i] = icsapi->createPiece(c);
65 /**
66 * @return Position index as a 0-based halfmove count.
68 int PositionInfo::index() const {
69 int res = (moveIndex - 1) * 2;
70 if (turn == 1) res++;
71 return res;
74 PositionInfo::PositionInfo()
75 : valid(false) { }
77 bool PositionInfo::load(std::map<int, ICSGameData>& games, const QString& str) {
78 if (pattern.indexIn(str) != 0) {
79 valid = false;
80 return true;
83 bool new_game = false;
85 valid = true;
86 int gn = pattern.cap(CaptureIndexes::GameNumber).toInt();
87 std::map<int, ICSGameData>::iterator gi = games.find(gn);
88 ICSAPIPtr icsapi;
90 if (gi == games.end()) {
91 WARNING("Received style12 for unknown game " << gn);
92 // create a gameinfo of type "dummy"
93 gi = games.insert(std::make_pair(gn, ICSGameData(gn, ""))).first;
94 new_game = true;
97 icsapi = gi->second.icsapi;
99 std::vector<PositionRow> rows;
100 for (uint i = 0; i < 8; ++i)
101 rows.push_back(PositionRow(icsapi, pattern.cap(CaptureIndexes::ChessboardStart + i)));
103 gameNumber = pattern.cap(CaptureIndexes::GameNumber).toInt();
104 moveIndex = pattern.cap(CaptureIndexes::MoveOrdinal).toInt();
105 whitePlayer = pattern.cap(CaptureIndexes::WhitePlayer);
106 blackPlayer = pattern.cap(CaptureIndexes::BlackPlayer);
107 turn = pattern.cap(CaptureIndexes::Turn) == "W"? 0 : 1;
109 int ep = pattern.cap(CaptureIndexes::EnPassant).toInt();
110 if (ep == -1)
111 enPassantSquare = Point::invalid();
112 else
113 enPassantSquare = Point(ep, turn == 0? 2 : 5);
115 bool wkCastle = pattern.cap(CaptureIndexes::WhiteKingCastle).toInt() == 1;
116 bool wqCastle = pattern.cap(CaptureIndexes::WhiteQueenCastle).toInt() == 1;
117 bool bkCastle = pattern.cap(CaptureIndexes::BlackKingCastle).toInt() == 1;
118 bool bqCastle = pattern.cap(CaptureIndexes::BlackQueenCastle).toInt() == 1;
120 position = icsapi->createChessboard(turn, wkCastle, wqCastle, bkCastle, bqCastle, enPassantSquare);
121 for (uint i = 0; i < 8; ++i) {
122 for (uint j = 0; j < rows[i].row.size(); ++j) {
123 position->set(Point(j,i), rows[i].row[j]);
127 relation = static_cast<Relation>(pattern.cap(CaptureIndexes::Relation).toInt());
129 whiteTime = pattern.cap(CaptureIndexes::WhiteTime).toInt();
130 blackTime = pattern.cap(CaptureIndexes::BlackTime).toInt();
131 if (pattern.cap(CaptureIndexes::TimeUsed).indexOf('.') == -1) {
132 // time is in seconds
133 whiteTime *= 1000;
134 blackTime *= 1000;
137 lastMoveSAN = pattern.cap(CaptureIndexes::LastMove);
138 lastMove = pattern.cap(CaptureIndexes::LastMoveVerbose);
140 return new_game;