Removed all old variants.
[tagua/yd.git] / src / positioninfo.cpp
blob65b050bced4a32c75b718c2f604886c23fa715a4
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.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(const std::map<int, ICSGameData>& games, const QString& str) {
75 if (pattern.indexIn(str) != 0) {
76 valid = false;
77 return;
80 valid = true;
81 int gn = pattern.cap(CaptureIndexes::GameNumber).toInt();
82 std::map<int, ICSGameData>::const_iterator gi = games.find(gn);
83 ICSAPIPtr icsapi;
85 if (gi == games.end()) {
86 ERROR("Received style12 for unknown game " << gn);
87 icsapi = Variant::variant("Dummy")->icsAPI();
89 else {
90 icsapi = gi->second.icsapi;
93 std::vector<PositionRow> rows;
94 for (uint i = 0; i < 8; ++i)
95 rows.push_back(PositionRow(icsapi, pattern.cap(CaptureIndexes::ChessboardStart + i)));
97 gameNumber = pattern.cap(CaptureIndexes::GameNumber).toInt();
98 moveIndex = pattern.cap(CaptureIndexes::MoveOrdinal).toInt();
99 whitePlayer = pattern.cap(CaptureIndexes::WhitePlayer);
100 blackPlayer = pattern.cap(CaptureIndexes::BlackPlayer);
101 turn = pattern.cap(CaptureIndexes::Turn) == "W"? 0 : 1;
103 int ep = pattern.cap(CaptureIndexes::EnPassant).toInt();
104 if (ep == -1)
105 enPassantSquare = Point::invalid();
106 else
107 enPassantSquare = Point(ep, turn == 0? 2 : 5);
109 bool wkCastle = pattern.cap(CaptureIndexes::WhiteKingCastle).toInt() == 1;
110 bool wqCastle = pattern.cap(CaptureIndexes::WhiteQueenCastle).toInt() == 1;
111 bool bkCastle = pattern.cap(CaptureIndexes::BlackKingCastle).toInt() == 1;
112 bool bqCastle = pattern.cap(CaptureIndexes::BlackQueenCastle).toInt() == 1;
114 position = icsapi->createChessboard(turn, wkCastle, wqCastle, bkCastle, bqCastle, enPassantSquare);
115 for (uint i = 0; i < 8; ++i) {
116 for (uint j = 0; j < rows[i].row.size(); ++j) {
117 position->set(Point(j,i), rows[i].row[j]);
121 relation = static_cast<Relation>(pattern.cap(CaptureIndexes::Relation).toInt());
123 whiteTime = pattern.cap(CaptureIndexes::WhiteTime).toInt();
124 blackTime = pattern.cap(CaptureIndexes::BlackTime).toInt();
125 if (pattern.cap(CaptureIndexes::TimeUsed).indexOf('.') == -1) {
126 // time is in seconds
127 whiteTime *= 1000;
128 blackTime *= 1000;
131 lastMoveSAN = pattern.cap(CaptureIndexes::LastMove);
132 lastMove = pattern.cap(CaptureIndexes::LastMoveVerbose);