-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMove.cpp
More file actions
134 lines (111 loc) · 3.68 KB
/
Copy pathMove.cpp
File metadata and controls
134 lines (111 loc) · 3.68 KB
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
//
// Created by Benedikt Hornig on 07.09.22.
//
#include "Move.h"
#include <array>
std::array<char, 8> FILE_NAMES = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
std::array<char, 8> RANK_NAMES = {'1', '2', '3', '4', '5', '6', '7', '8'};
Move::Move() {
this->fromSquare = -1;
this->toSquare = -1;
this->removing = false;
this->crowningPiece = -1;
this->pieceToCrown = -1;
}
Move::Move(const Square fromSquare, const Square toSquare, const bool isRemoving) {
this->fromSquare = fromSquare;
this->toSquare = toSquare;
this->removing = isRemoving;
this->crowningPiece = -1;
this->pieceToCrown = -1;
}
Move::Move(const Square fromSquare, const Square toSquare, const bool isRemoving, const Square crowningPiece, const Square pieceToCrown) {
this->fromSquare = fromSquare;
this->toSquare = toSquare;
this->removing = isRemoving;
this->crowningPiece = crowningPiece;
this->pieceToCrown = pieceToCrown;
}
Move::operator std::string() const {
// parse move object to string
std::string res;
res += FILE_NAMES[squareFile(fromSquare)];
res += RANK_NAMES[squareRank(fromSquare)];
if (isRemoving()) {
res += 'x';
} else {
if (fromSquare != toSquare) {
res += FILE_NAMES[squareFile(toSquare)];
res += RANK_NAMES[squareRank(toSquare)];
}
}
if (isCrowning()) {
res += '^';
res += FILE_NAMES[squareFile(crowningPiece)];
res += RANK_NAMES[squareRank(crowningPiece)];
res += FILE_NAMES[squareFile(pieceToCrown)];
res += RANK_NAMES[squareRank(pieceToCrown)];
}
return res;
}
Move Move::fromNotation(const std::string& notation) {
// parse move from a FEN-like string
long fileIdx = -1, rankIdx = -1;
bool isCrowning = false;
bool isRemoving = false;
Square fromSq = -1, toSq = -1, crownP = -1, pToCrown = -1;
for (const auto &item: notation) {
auto fileName = std::find(FILE_NAMES.begin(), FILE_NAMES.end(), item);
if (fileName != std::end(FILE_NAMES)) {
fileIdx = std::distance(FILE_NAMES.begin(), fileName);
} else {
auto rankName = std::find(RANK_NAMES.begin(), RANK_NAMES.end(), item);
if (rankName != std::end(RANK_NAMES))
rankIdx = std::distance(RANK_NAMES.begin(), rankName);
else if (item == '^')
isCrowning = true;
else if (item == 'x') {
isRemoving = true;
continue;
}
if (fileIdx != -1 && rankIdx != -1) {
auto sq = (int) (rankIdx * 8 + fileIdx);
if (isCrowning)
if (crownP == -1)
crownP = sq;
else
pToCrown = sq;
else if (fromSq != -1)
toSq = sq;
else
fromSq = sq;
fileIdx = -1;
rankIdx = -1;
}
}
}
if (isCrowning)
return {fromSq, toSq, isRemoving, crownP, pToCrown};
return {fromSq, toSq, isRemoving};
}
bool Move::operator==(const Move move) const {
return move.fromSquare == fromSquare && move.toSquare == toSquare && move.removing == removing && move.crowningPiece == crowningPiece && move.pieceToCrown == pieceToCrown;
}
bool Move::isRemoving() const {
return removing;
}
Square Move::getFromSquare() const {
return fromSquare;
}
Square Move::getToSquare() const {
return toSquare;
}
bool Move::isCrowning() const {
return pieceToCrown != -1;
}
Square Move::getPieceToCrown() const {
return pieceToCrown;
}
Square Move::getCrowningPiece() const {
return crowningPiece;
}