new 4475edb243ed4627f4c5f2c470ca40b3def034d4
[tagua/yd.git] / src / positioninfo.cpp
blobf6a3c864229288032cd44dc4c75a693fe6646b79
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>
14 #include <core/board.h>
15 #include <core/state.h>
17 #include "variants.h"
18 #include "gameinfo.h"
19 #include "icsapi.h"
21 using namespace boost;
23 // Style 12 was designed by Daniel Sleator (sleator+@cs.cmu.edu) Darooha@ICC
24 QRegExp PositionInfo::pattern(
25 "^<12>\\s+" //header
26 "([qkbnrpQKBNRP-]{8})\\s+" //chessboard
27 "([qkbnrpQKBNRP-]{8})\\s+" //chessboard
28 "([qkbnrpQKBNRP-]{8})\\s+" //chessboard
29 "([qkbnrpQKBNRP-]{8})\\s+" //chessboard
30 "([qkbnrpQKBNRP-]{8})\\s+" //chessboard
31 "([qkbnrpQKBNRP-]{8})\\s+" //chessboard
32 "([qkbnrpQKBNRP-]{8})\\s+" //chessboard
33 "([qkbnrpQKBNRP-]{8})\\s+" //chessboard
34 "([BW])\\s+" //black/white
35 "(-1|[0-7])\\s+" //passing pawn
36 "([01])\\s+" //castle rights
37 "([01])\\s+" //castle rights
38 "([01])\\s+" //castle rights
39 "([01])\\s+" //castle rights
40 "(-?\\d+)\\s+" //50 moves made
41 "(\\d+)\\s+" //game num
42 "(\\S+)\\s+" //white name
43 "(\\S+)\\s+" //black name
44 "(-[1-4]|[0-2])\\s+" //status
45 "(\\d+)\\s+" //time
46 "(\\d+)\\s+" //inc
47 "(\\d+)\\s+" //w material
48 "(\\d+)\\s+" //b material
49 "(-?\\d+)\\s+" //w time
50 "(-?\\d+)\\s+" //b time
51 "(\\d+)\\s+" //move made
52 "(\\S+)\\s+" //coordmove
53 "(\\S+)\\s+" //time used
54 "(\\S+)\\s+" //algmove
55 "([0-1])" //flip
59 PositionInfo::PositionRow::PositionRow(const ICSAPIPtr& icsapi, const QString& str) {
60 Q_ASSERT(str.length() == 8);
62 row.resize(str.length());
63 for (int i = 0; i < str.length(); ++i) {
64 QChar c = str[i];
66 row[i] = icsapi->createPiece(c);
70 /**
71 * @return Position index as a 0-based halfmove count.
73 int PositionInfo::index() const {
74 int res = (moveIndex - 1) * 2;
75 if (turn == 1) res++;
76 return res;
79 PositionInfo::PositionInfo()
80 : valid(false) { }
82 bool PositionInfo::load(std::map<int, ICSGameData>& games, const QString& str) {
83 if (pattern.indexIn(str) != 0) {
84 valid = false;
85 return true;
88 bool new_game = false;
90 valid = true;
91 int gn = pattern.cap(CaptureIndexes::GameNumber).toInt();
92 std::map<int, ICSGameData>::iterator gi = games.find(gn);
93 ICSAPIPtr icsapi;
95 if (gi == games.end()) {
96 kWarning() << "Received style12 for unknown game" << gn;
97 // create a gameinfo of type "dummy"
98 gi = games.insert(std::make_pair(gn, ICSGameData(gn, ""))).first;
99 new_game = true;
102 // FIXME load icsapi
103 // icsapi = gi->second.icsapi;
105 std::vector<PositionRow> rows;
106 for (uint i = 0; i < 8; ++i)
107 rows.push_back(PositionRow(icsapi, pattern.cap(CaptureIndexes::ChessboardStart + i)));
109 gameNumber = pattern.cap(CaptureIndexes::GameNumber).toInt();
110 moveIndex = pattern.cap(CaptureIndexes::MoveOrdinal).toInt();
111 whitePlayer = pattern.cap(CaptureIndexes::WhitePlayer);
112 blackPlayer = pattern.cap(CaptureIndexes::BlackPlayer);
113 turn = pattern.cap(CaptureIndexes::Turn) == "W"? 0 : 1;
115 int ep = pattern.cap(CaptureIndexes::EnPassant).toInt();
116 if (ep == -1)
117 enPassantSquare = Point::invalid();
118 else
119 enPassantSquare = Point(ep, turn == 0? 2 : 5);
121 bool wkCastle = pattern.cap(CaptureIndexes::WhiteKingCastle).toInt() == 1;
122 bool wqCastle = pattern.cap(CaptureIndexes::WhiteQueenCastle).toInt() == 1;
123 bool bkCastle = pattern.cap(CaptureIndexes::BlackKingCastle).toInt() == 1;
124 bool bqCastle = pattern.cap(CaptureIndexes::BlackQueenCastle).toInt() == 1;
126 position = icsapi->createChessboard(turn, wkCastle, wqCastle, bkCastle, bqCastle, enPassantSquare);
127 for (uint i = 0; i < 8; ++i) {
128 for (uint j = 0; j < rows[i].row.size(); ++j) {
129 position->board()->set(Point(j,i), rows[i].row[j]);
133 relation = static_cast<Relation>(pattern.cap(CaptureIndexes::Relation).toInt());
135 whiteTime = pattern.cap(CaptureIndexes::WhiteTime).toInt();
136 blackTime = pattern.cap(CaptureIndexes::BlackTime).toInt();
137 if (pattern.cap(CaptureIndexes::TimeUsed).indexOf('.') == -1) {
138 // time is in seconds
139 whiteTime *= 1000;
140 blackTime *= 1000;
143 lastMoveSAN = pattern.cap(CaptureIndexes::LastMove);
144 lastMove = pattern.cap(CaptureIndexes::LastMoveVerbose);
146 return new_game;