-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiece.cpp
198 lines (180 loc) · 4.04 KB
/
piece.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "piece.h"
#include <QDebug>
#include "boardmodel.h"
Piece::Piece(bool init)
{
if (init)
d = new PieceData(Pawn, White, -1, nullptr);
}
Piece::Piece(Piece::PieceType type, QByteArray position) :
Piece(type, White, positionToIndex(position))
{}
Piece::Piece(Piece::PieceType type, Piece::Color color, int index, BoardModel *board) :
d(new PieceData(type, color, index, board))
{
switch (type) {
case Rook:
setMoves("n+");
break;
case Knight:
setMoves("~1/2");
break;
case Bishop:
setMoves("nX");
break;
case Queen:
setMoves("n*");
break;
case King:
setMoves("1*");
break;
case Pawn:
setMoves("o1>, c1X>, oi2>");
break;
default:
break;
}
}
void Piece::setMoves(QByteArray moves)
{
for (QByteArray m : moves.split(',')) {
d->capabilities.append(parseMove(m.trimmed()));
}
}
QString Piece::sign() const
{
int charCode = d->type >> 8;
return QString(QChar(charCode + (d->color >> 8)));
}
void Piece::setType(char type)
{
if (d->type == Undefined)
d->type = type;
}
void Piece::setBoardIndex(int index)
{
d->index = index;
}
Piece::MoveFlags Piece::parseMove(const QByteArray &move)
{
Piece::MoveFlags ret = 0;
auto it = move.cbegin();
while (it != move.cend()) {
switch (*it++) {
case 'o':
ret |= Move;
break;
case 'i':
ret |= InitialMove;
break;
case 'c':
ret |= Capture;
break;
case '~':
ret |= Leaper;
break;
case '^':
ret |= Locust;
break;
case '1':
ret |= OneSquare;
break;
case '2':
ret |= TwoSquares;
break;
case 'n':
ret |= AnyDistance;
break;
case '*':
ret |= Orthogonally | Diagonally | Sideways | Forwards | Backwards;
break;
case '+':
ret |= Orthogonally | Sideways | Forwards | Backwards;
break;
case '>':
ret |= Orthogonally | Forwards;
break;
case '<':
ret |= Orthogonally | Backwards;
break;
case '=':
ret |= Orthogonally | Sideways;
break;
case 'X': {
if (it == move.cend())
ret |= Diagonally | Forwards | Backwards;
else switch (*it++) {
case '>':
ret |= Diagonally | Forwards;
break;
case '<':
ret |= Diagonally | Backwards;
break;
default:
break;
}
break;
}
case '/':
ret |= OrthogonalPair;
break;
case '&':
ret |= RepeatedMove;
break;
default:
qWarning() << "Unrecognized token" << *(it - 1);
return 0;
break;
}
}
return ret;
}
QByteArray Piece::position() const
{
QByteArray ret;
ret.append('a' + file());
ret.append('1' + rank());
return ret;
}
void Piece::setPosition(QByteArray pos)
{
d->index = positionToIndex(pos);
}
void Piece::setActive(bool active) {
d->active = active;
if (active)
d->board->setActivePiece(d->index);
else
d->board->setActivePiece(-1);
}
bool Piece::inGame() const
{
return d->inGame;
}
void Piece::setInGame(bool inGame)
{
d->inGame = inGame;
}
QByteArray Piece::toByteArray() const
{
QByteArray ret;
ret += d->type;
ret += d->color;
if (d->inGame)
ret += "+";
else
ret += "-";
ret += position();
return ret;
}
int positionToIndex(QByteArray pos)
{
if (pos.size() != 2)
return -1;
QByteArray ba = pos.toLower();
int file = ba[0] - 97;
int rank = 56 - ba[1];
if (qBound(0, file, 7) == file && qBound(0, rank, 7) == rank)
return coordsToIndex(file, rank);
return -1;
}