- castling moves are stored as "king takes own rook" (from = king square, to = castling rook square) - per-colour castling rook squares (Board::castleRook), so DFRC (different back ranks for white/black) works - FEN parsing/printing: KQkq (X-FEN) and Shredder-FEN file letters - Board::CanCastle(): generic castling legality (empty paths, king not attacked, rook x-ray check) - MakeMove handles arbitrary king/rook start squares (king and rook may stay in place or swap through each other's squares) - UCI_Chess960 option: castling printed/parsed as king-takes-rook when on, standard e1g1 style otherwise (both are accepted when parsing) - viri format, perft castle check and castling right updates no longer assume a1/h1/a8/h8
170 lines
4.9 KiB
C++
170 lines
4.9 KiB
C++
#pragma once
|
|
#include <array>
|
|
#include <string_view>
|
|
#include <iostream>
|
|
#include "types.h"
|
|
|
|
constexpr short moveFromMask = 0x3F;
|
|
constexpr short moveToMask = 0xFC0;
|
|
|
|
enum MOVE_FLAGS {
|
|
quiet, doublePawnPush,
|
|
kingCastle, queenCastle,
|
|
capture, epCapture,
|
|
knightPromotion, bishopPromotion, rookPromotion, queenPromotion,
|
|
knightPromoCapture, bishopPromoCapture, rookPromoCapture, queenPromoCapture
|
|
};
|
|
|
|
constexpr std::array<std::string_view, 14> moveTypes {
|
|
"quiet", "doublePawnPush",
|
|
"kingCastle", "QueenCastle",
|
|
"capture", "epCapture",
|
|
"knightPromotion", "bishopPromotion", "rookPromotion", "queenPromotion",
|
|
"knightPromoCapture", "bishopPromoCapture", "rookPromoCapture", "queenPromoCapture"
|
|
};
|
|
|
|
struct Move {
|
|
private:
|
|
uint16_t m_move;
|
|
public:
|
|
Move() {
|
|
m_move = 0;
|
|
}
|
|
|
|
Move(int from, int to, int flags) {
|
|
m_move = from;
|
|
m_move |= to << 6;
|
|
m_move |= flags << 12;
|
|
}
|
|
|
|
int MoveFrom() {
|
|
return (m_move & moveFromMask);
|
|
}
|
|
|
|
int MoveTo() {
|
|
return (m_move & moveToMask) >> 6;
|
|
}
|
|
|
|
int GetFlags() {
|
|
return m_move >> 12;
|
|
}
|
|
|
|
bool IsCapture() {
|
|
return (GetFlags() == capture || GetFlags() == epCapture
|
|
|| GetFlags() == knightPromoCapture || GetFlags() == bishopPromoCapture
|
|
|| GetFlags() == rookPromoCapture || GetFlags() == queenPromoCapture);
|
|
}
|
|
|
|
bool IsQuiet() {
|
|
return (GetFlags() == quiet || GetFlags() == kingCastle
|
|
|| GetFlags() == queenCastle || GetFlags() == doublePawnPush);
|
|
}
|
|
|
|
bool IsPromo() {
|
|
return (GetFlags() >= knightPromotion);
|
|
}
|
|
|
|
bool IsCastle() {
|
|
return (GetFlags() == kingCastle || GetFlags() == queenCastle);
|
|
}
|
|
|
|
// Castling moves are stored as "king takes own castling rook": from = king square, to = rook square.
|
|
// This is the UCI_Chess960 notation and it can't clash with a normal king move.
|
|
// CastleKingDest() is the square the king actually ends up on (g/c file of the back rank).
|
|
int CastleKingDest() {
|
|
return (MoveTo() & ~7) + (GetFlags() == kingCastle ? 6 : 2);
|
|
}
|
|
|
|
// Destination square used when printing the move: standard UCI prints the king's
|
|
// destination (e1g1), UCI_Chess960 prints the rook square (e1h1).
|
|
int PrintTo() {
|
|
return (IsCastle() && !UCIChess960) ? CastleKingDest() : MoveTo();
|
|
}
|
|
|
|
void PrintMove() {
|
|
std::cout << squareCoords[MoveFrom()] << squareCoords[PrintTo()];
|
|
|
|
int flags = GetFlags();
|
|
|
|
if (flags == knightPromotion || flags == knightPromoCapture) {
|
|
std::cout << 'n';
|
|
} else if (flags == bishopPromotion || flags == bishopPromoCapture) {
|
|
std::cout << 'b';
|
|
} else if (flags == rookPromotion || flags == rookPromoCapture) {
|
|
std::cout << 'r';
|
|
} else if (flags == queenPromotion || flags == queenPromoCapture) {
|
|
std::cout << 'q';
|
|
}
|
|
}
|
|
|
|
std::string GetMoveString() {
|
|
std::string result = squareCoords[MoveFrom()];
|
|
result += squareCoords[PrintTo()];
|
|
|
|
int flags = GetFlags();
|
|
|
|
if (flags == knightPromotion || flags == knightPromoCapture) {
|
|
result += 'n';
|
|
} else if (flags == bishopPromotion || flags == bishopPromoCapture) {
|
|
result += 'b';
|
|
} else if (flags == rookPromotion || flags == rookPromoCapture) {
|
|
result += 'r';
|
|
} else if (flags == queenPromotion || flags == queenPromoCapture) {
|
|
result += 'q';
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
uint16_t ConvertToViriMoveFormat() {
|
|
uint16_t viriMove = 0;
|
|
int flags = GetFlags();
|
|
|
|
viriMove = MoveFrom();
|
|
|
|
if (flags == kingCastle || flags == queenCastle) {
|
|
// Castling is already stored as king takes rook, which is what the viri format expects
|
|
viriMove |= (MoveTo() << 6);
|
|
|
|
viriMove |= 0x8000;
|
|
|
|
return viriMove;
|
|
}
|
|
|
|
viriMove |= (MoveTo() << 6);
|
|
|
|
if (flags == epCapture) {
|
|
viriMove |= 0x4000;
|
|
} else if (flags >= knightPromotion && flags <= queenPromoCapture) {
|
|
uint16_t promoPiece = 0;
|
|
|
|
if (flags == knightPromotion || flags == knightPromoCapture) {
|
|
promoPiece = 0;
|
|
} else if (flags == bishopPromotion || flags == bishopPromoCapture) {
|
|
promoPiece = 1;
|
|
} else if (flags == rookPromotion || flags == rookPromoCapture) {
|
|
promoPiece = 2;
|
|
} else if (flags == queenPromotion || flags == queenPromoCapture) {
|
|
promoPiece = 3;
|
|
}
|
|
|
|
viriMove |= 0xC000;
|
|
viriMove |= (promoPiece << 12);
|
|
}
|
|
|
|
return viriMove;
|
|
}
|
|
|
|
int GetPromoPiece() {
|
|
if (IsCapture()) {
|
|
return GetFlags() - 9;
|
|
} else {
|
|
return GetFlags() - 5;
|
|
}
|
|
}
|
|
|
|
operator uint16_t() {
|
|
return m_move;
|
|
}
|
|
};
|