Split theme preferences page to allow more easy different theme types.
[tagua/yd.git] / src / positioninfo.cpp
blob3f4d6b6eb681154026b0db3a891bd142673edef7
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];
60 int color;
61 int type;
63 if (c.category() == QChar::Letter_Uppercase)
64 color = WHITE;
65 else if (c.category() == QChar::Letter_Lowercase)
66 color = BLACK;
67 else {
68 row[i] = AbstractPiece::Ptr();
69 continue;
72 //BROKEN
73 //type = variant->type(c);
74 //row[i] = variant->createPiece(color, type);
78 /**
79 * @return Position index as a 0-based halfmove count.
81 int PositionInfo::index() const {
82 int res = (moveIndex - 1) * 2;
83 if (turn == BLACK) res++;
84 return res;
87 PositionInfo::PositionInfo(const std::map<int, ICSGameData>& games, const QString& str) {
88 if (pattern.indexIn(str) != 0) {
89 valid = false;
90 return;
93 VariantInfo* variant;
94 valid = true;
95 int gn = pattern.cap(CaptureIndexes::GameNumber).toInt();
96 std::map<int, ICSGameData>::const_iterator gi = games.find(gn);
97 QString var = (gi != games.end() && !gi->second.variant.isEmpty())
98 ? gi->second.variant : QString("chess");
99 variant = Variant::variant(GameInfo::variantCode(var));
101 std::vector<PositionRow> rows;
102 for (uint i = 0; i < 8; ++i)
103 rows.push_back(PositionRow(variant, pattern.cap(CaptureIndexes::ChessboardStart + i)));
105 gameNumber = pattern.cap(CaptureIndexes::GameNumber).toInt();
106 moveIndex = pattern.cap(CaptureIndexes::MoveOrdinal).toInt();
107 whitePlayer = pattern.cap(CaptureIndexes::WhitePlayer);
108 blackPlayer = pattern.cap(CaptureIndexes::BlackPlayer);
109 turn = pattern.cap(CaptureIndexes::Turn) == "W"? WHITE : BLACK;
111 int ep = pattern.cap(CaptureIndexes::EnPassant).toInt();
112 if (ep == -1)
113 enPassantSquare = Point::invalid();
114 else
115 enPassantSquare = Point(ep, turn == WHITE? 2 : 5);
117 bool wkCastle = pattern.cap(CaptureIndexes::WhiteKingCastle).toInt() == 1;
118 bool wqCastle = pattern.cap(CaptureIndexes::WhiteQueenCastle).toInt() == 1;
119 bool bkCastle = pattern.cap(CaptureIndexes::BlackKingCastle).toInt() == 1;
120 bool bqCastle = pattern.cap(CaptureIndexes::BlackQueenCastle).toInt() == 1;
122 position = variant->createChessboard(turn, wkCastle, wqCastle, bkCastle, bqCastle, enPassantSquare);
123 for (uint i = 0; i < 8; ++i) {
124 for (uint j = 0; j < rows[i].row.size(); ++j) {
125 position->set(Point(j,i), rows[i].row[j]);
129 relation = static_cast<Relation>(pattern.cap(CaptureIndexes::Relation).toInt());
131 whiteTime = pattern.cap(CaptureIndexes::WhiteTime).toInt();
132 blackTime = pattern.cap(CaptureIndexes::BlackTime).toInt();
133 if (pattern.cap(CaptureIndexes::TimeUsed).indexOf('.') == -1) {
134 // time is in seconds
135 whiteTime *= 1000;
136 blackTime *= 1000;
139 lastMoveSAN = pattern.cap(CaptureIndexes::LastMove);
140 lastMove = pattern.cap(CaptureIndexes::LastMoveVerbose);