Exported QFont to lua, for border text and clocks
[tagua/yd.git] / src / positioninfo.cpp
blob79bc97bb37ed11dd8f2f43b72eb12af6f77ee6a7
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
3 (c) 2006 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"
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(VariantInfo* variant, 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] = variant->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 == BLACK) 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 VariantInfo* variant;
81 valid = true;
82 int gn = pattern.cap(CaptureIndexes::GameNumber).toInt();
83 std::map<int, ICSGameData>::const_iterator gi = games.find(gn);
84 QString var = (gi != games.end() && !gi->second.variant.isEmpty())
85 ? gi->second.variant : QString("chess");
86 variant = Variant::variant(GameInfo::variantCode(var));
88 std::vector<PositionRow> rows;
89 for (uint i = 0; i < 8; ++i)
90 rows.push_back(PositionRow(variant, pattern.cap(CaptureIndexes::ChessboardStart + i)));
92 gameNumber = pattern.cap(CaptureIndexes::GameNumber).toInt();
93 moveIndex = pattern.cap(CaptureIndexes::MoveOrdinal).toInt();
94 whitePlayer = pattern.cap(CaptureIndexes::WhitePlayer);
95 blackPlayer = pattern.cap(CaptureIndexes::BlackPlayer);
96 turn = pattern.cap(CaptureIndexes::Turn) == "W"? WHITE : BLACK;
98 int ep = pattern.cap(CaptureIndexes::EnPassant).toInt();
99 if (ep == -1)
100 enPassantSquare = Point::invalid();
101 else
102 enPassantSquare = Point(ep, turn == WHITE? 2 : 5);
104 bool wkCastle = pattern.cap(CaptureIndexes::WhiteKingCastle).toInt() == 1;
105 bool wqCastle = pattern.cap(CaptureIndexes::WhiteQueenCastle).toInt() == 1;
106 bool bkCastle = pattern.cap(CaptureIndexes::BlackKingCastle).toInt() == 1;
107 bool bqCastle = pattern.cap(CaptureIndexes::BlackQueenCastle).toInt() == 1;
109 position = variant->createChessboard(turn, wkCastle, wqCastle, bkCastle, bqCastle, enPassantSquare);
110 for (uint i = 0; i < 8; ++i) {
111 for (uint j = 0; j < rows[i].row.size(); ++j) {
112 position->set(Point(j,i), rows[i].row[j]);
116 relation = static_cast<Relation>(pattern.cap(CaptureIndexes::Relation).toInt());
118 whiteTime = pattern.cap(CaptureIndexes::WhiteTime).toInt();
119 blackTime = pattern.cap(CaptureIndexes::BlackTime).toInt();
120 if (pattern.cap(CaptureIndexes::TimeUsed).indexOf('.') == -1) {
121 // time is in seconds
122 whiteTime *= 1000;
123 blackTime *= 1000;
126 lastMoveSAN = pattern.cap(CaptureIndexes::LastMove);
127 lastMove = pattern.cap(CaptureIndexes::LastMoveVerbose);