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.
11 #include "positioninfo.h"
16 using namespace boost
;
18 // Style 12 was designed by Daniel Sleator (sleator+@cs.cmu.edu) Darooha@ICC
19 QRegExp
PositionInfo::pattern(
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
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
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
) {
61 row
[i
] = icsapi
->createPiece(c
);
66 * @return Position index as a 0-based halfmove count.
68 int PositionInfo::index() const {
69 int res
= (moveIndex
- 1) * 2;
74 PositionInfo::PositionInfo()
77 bool PositionInfo::load(std::map
<int, ICSGameData
>& games
, const QString
& str
) {
78 if (pattern
.indexIn(str
) != 0) {
83 bool new_game
= false;
86 int gn
= pattern
.cap(CaptureIndexes::GameNumber
).toInt();
87 std::map
<int, ICSGameData
>::iterator gi
= games
.find(gn
);
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
;
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();
111 enPassantSquare
= Point::invalid();
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
137 lastMoveSAN
= pattern
.cap(CaptureIndexes::LastMove
);
138 lastMove
= pattern
.cap(CaptureIndexes::LastMoveVerbose
);