-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
move.cpp
executable file
·150 lines (122 loc) · 4.48 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
#include "move.h"
#include "utils.h"
#include "piece.h"
#include "constants.h"
#include "board.h"
#include <cctype>
namespace Napoleon
{
bool Move::IsNull() const
{
return (FromSquare() == ToSquare());
}
bool Move::IsCastleOO() const
{
return ((move >> 12) == KingCastle);
}
bool Move::IsCastleOOO() const
{
return ((move >> 12) == QueenCastle);
}
// convert move object in long algebraic notation as used by uci protocol
std::string Move::ToAlgebraic() const
{
std::string algebraic;
if (IsNull())
return "0000"; // UCI representation for NULL move
if (IsCastle())
{
if (IsCastleOO())
{
if (FromSquare() == Constants::Squares::IntE1)
algebraic += "e1g1";
else
algebraic += "e8g8";
}
else if (IsCastleOOO())
{
if (FromSquare() == Constants::Squares::IntE1)
algebraic += "e1c1";
else
algebraic += "e8c8";
}
}
else
{
algebraic += Utils::Square::ToAlgebraic(FromSquare());
algebraic += Utils::Square::ToAlgebraic(ToSquare());
if (IsPromotion())
algebraic += Utils::Piece::GetInitial(PiecePromoted());
}
return algebraic;
}
// convert move object to Standard algebraic notation as used in most programs
std::string Move::ToSan(Board& board) const
{
if (IsCastle())
{
if (IsCastleOO())
return "O-O";
else
return "O-O-O";
}
std::string san = "";
Square from = FromSquare();
Square to = ToSquare();
int rank = Utils::Square::GetRankIndex(from);
int file = Utils::Square::GetFileIndex(from);
bool sameRank = false;
bool sameFile = false;
Color side = board.SideToMove();
BitBoard occ = board.OccupiedSquares;
Piece piece = board.PieceOnSquare(from);
if (piece.Type != PieceType::Pawn)
{
san += std::toupper(Utils::Piece::GetInitial(piece));
BitBoard attackers = (board.MovesTo(to, side, occ) & board.Pieces(side, piece.Type)) ^ Constants::Masks::SquareMask[from];
BitBoard others = attackers;
while(attackers)
{
Square square = Utils::BitBoard::BitScanForwardReset(attackers);
Move move(square, to);
if (!board.IsMoveLegal(move, board.PinnedPieces()))
{
others ^= square;
continue;
}
if (Utils::Square::GetRankIndex(square) == rank)
sameRank = true;
else if (Utils::Square::GetFileIndex(square) == file)
sameFile = true;
}
// Ambiguities
// If the piece is sufficient to unambiguously determine the origin square, the whole from square is omitted. Otherwise,
// if two (or more) pieces of the same kind can move to the same square, the piece's initial is followed by
// (in descending order of preference):
//
// 1. file of departure if different
// 2. rank of departure if the files are the same but the ranks differ
// 3. the complete origin square coordinate otherwise
if (others)
{
// std::cout << sameRank << " " << sameFile << std::endl;
// assert(sameRank != true && sameFile != true);
// assert((sameRank ^ sameFile) || (!sameRank && !sameFile));
if (!sameFile)
san += Utils::Square::ToAlgebraic(from)[0]; // + file letter
else if (!sameRank)
san += Utils::Square::ToAlgebraic(from)[1]; // + rank number
else
san += Utils::Square::ToAlgebraic(from);
}
}
else if (board.IsCapture(*this))
san = Utils::Square::ToAlgebraic(from)[0];
if (board.IsCapture(*this))
san += "x";
san += Utils::Square::ToAlgebraic(ToSquare());
if (IsPromotion())
san += "=" + std::toupper(Utils::Piece::GetInitial(PiecePromoted()));
return san;
}
}