-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMove.cpp
184 lines (166 loc) · 4.65 KB
/
Move.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
#include "Move.hpp"
#include <ostream>
int angle4(int x,int y);
bool equalDistAndAngle(const Move& lhs, const Move& rhs);
Move::Move(const Square& from, const Square& to,
const std::optional<PieceType>& promotion):f(from),t(to),prom(promotion)
{
}
Move::Optional Move::fromUci(const std::string& uci) {
if(uci.length()>=4 && uci.length()<=5){
char fr[3] ={uci[0],uci[1],'\0'};
char too[3] ={uci[2],uci[3],'\0'};
std::optional<Square> F = Square::fromName(fr);
std::optional<Square> T = Square::fromName(too);
if(F == std::nullopt || T ==std::nullopt){
return std::nullopt;
}
Square Fr = (Square)F.value();
Square To = (Square)T.value();
if(uci.length()==5){
if(Piece::fromSymbol(uci[4]).has_value()){
std::optional<PieceType> pr = Piece::fromSymbol(uci[4]).value().type();
return Move(Fr,To,pr);
}
return std::nullopt;
}
return Move(Fr,To);
}
return std::nullopt;
}
std::string Move::toUci(){
std::string s = "";
s += this->from().getName();
s += this->to().getName();
if(this->promotion().has_value()){
s += Piece::typeChar(this->promotion().value());
}
//const std::string a =s;
return s;
}
Square Move::from() const {
return f;
}
Square Move::to() const {
return t;
}
std::optional<PieceType> Move::promotion() const {
return prom;
}
std::ostream& operator<<(std::ostream& os, const Move& move) {
Square F = move.from();
Square T = move.to();
auto t= T.getName();
auto f= F.getName();
if(move.promotion().has_value()){
char p = Piece::typeChar(move.promotion().value());
return os << (f+t+p);
}
return os<<(f+t);
}
bool equalDistAndAngle(const Move& lhs, const Move& rhs){
if(lhs.to().rank()< rhs.to().rank()){
return true;
}
else if (lhs.to().rank()> rhs.to().rank())
{
return false;
}
else{
if(lhs.to().file()< rhs.to().file()){
return true;
}
return false;
}
}
bool operator<(const Move& lhs, const Move& rhs) {
int DistLhs = Square::HammingDist(lhs.from(),lhs.to());
int DistRhs = Square::HammingDist(rhs.from(),rhs.to());
if(DistLhs<DistRhs){
return true;
}
if(DistLhs==DistRhs){
if(lhs.from() == rhs.from() && lhs.to()==rhs.to() ){
//Other possibilities for promotions?????
return false;
}
//first compare rank : the higher the rank higher the move, if equal compare their file.
if(DistLhs==0){
return equalDistAndAngle(lhs,rhs);
}
int lhsXDist = lhs.to().file()-lhs.from().file();
int lhsYDist = lhs.to().rank()-lhs.from().rank();
int rhsXDist = rhs.to().file()-rhs.from().file();
int rhsYDist = rhs.to().rank()-rhs.from().rank();
int lhsAngle = angle4(lhsXDist,lhsYDist);
int rhsAngle = angle4(rhsXDist,rhsYDist);
// The lower the angle number the higher the move.
if(lhsAngle>rhsAngle){
return true;
}
else if(rhsAngle>lhsAngle){
return false;
}
else{
if(lhsAngle ==1 || lhsAngle == 2){
if(lhsXDist<rhsXDist){
return true;
}
else if (lhsXDist>rhsXDist)
{
return false;
}
else{
return equalDistAndAngle(lhs,rhs);
}
}
else{
if(lhsXDist>rhsXDist){
return true;
}
else if (lhsXDist<rhsXDist)
{
return false;
}
else{
return equalDistAndAngle(lhs,rhs);
}
}
}
}
return false;
}
int angle4(int x,int y){
if(x>0 && y>=0){
return 1;
}
if(x<=0 && y>0){
return 2;
}
if(x<0 && y<=0){
return 3;
}
if(x>=0 && y<0){
return 4;
}
if (x==0 && y==0)
{
return 0;
}
return 0;
}
bool operator==(const Move& lhs, const Move& rhs) {
if(lhs.from() == rhs.from() && lhs.to()==rhs.to() ){
if(lhs.promotion().has_value() != rhs.promotion().has_value()){
return false;
}
if(lhs.promotion().has_value()){
if(lhs.promotion().value() == rhs.promotion().value()){
return true;
}
return false;
}
return true;
}
return false;
}