Compare commits
3 Commits
a4d5dbbc84
...
main
Author | SHA1 | Date | |
---|---|---|---|
cb0fcc4702 | |||
87adca7e66 | |||
bcf4d66c49 |
22
BasicEngine.cpp
Normal file
22
BasicEngine.cpp
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include "BasicEngine.hpp"
|
||||||
|
#include "Search.hpp"
|
||||||
|
|
||||||
|
std::string BasicEngine::name() const {
|
||||||
|
return mName;
|
||||||
|
}
|
||||||
|
std::string BasicEngine::version() const {
|
||||||
|
return mVersion;
|
||||||
|
}
|
||||||
|
std::string BasicEngine::author() const {
|
||||||
|
return mAuthor;
|
||||||
|
}
|
||||||
|
void BasicEngine::newGame() {
|
||||||
|
|
||||||
|
}
|
||||||
|
PrincipalVariation BasicEngine::pv(const Board &board, const TimeInfo::Optional &timeInfo) {
|
||||||
|
(void)board;
|
||||||
|
(void)timeInfo;
|
||||||
|
auto pv =Search::start(board);
|
||||||
|
return pv;
|
||||||
|
}
|
23
BasicEngine.hpp
Normal file
23
BasicEngine.hpp
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
#ifndef CHESS_ENGINE_BASICENGINE_HPP
|
||||||
|
#define CHESS_ENGINE_BASICENGINE_HPP
|
||||||
|
|
||||||
|
#include "Engine.hpp"
|
||||||
|
class BasicEngine final : public Engine {
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
std::string name() const override;
|
||||||
|
std::string version() const override;
|
||||||
|
std::string author() const override;
|
||||||
|
void newGame() override;
|
||||||
|
PrincipalVariation pv(const Board &board, const TimeInfo::Optional &timeInfo) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
std::string mName = "Egon++";
|
||||||
|
std::string mVersion = "1.0~Egon+C";
|
||||||
|
std::string mAuthor = "Bols";
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif //CHESS_ENGINE_BASICENGINE_HPP
|
23
BitBoard.cpp
23
BitBoard.cpp
@@ -1,4 +1,3 @@
|
|||||||
#include <iostream>
|
|
||||||
#include "BitBoard.hpp"
|
#include "BitBoard.hpp"
|
||||||
|
|
||||||
BitBoard::BitBoard(uint64_t v) {
|
BitBoard::BitBoard(uint64_t v) {
|
||||||
@@ -67,11 +66,12 @@ BitBoard BitBoard::castlingMoves(BitBoard kings, BitBoard rooks, BitBoard empty)
|
|||||||
|
|
||||||
BitBoard BitBoard::bishopAttacks(BitBoard bishops, BitBoard empty) {
|
BitBoard BitBoard::bishopAttacks(BitBoard bishops, BitBoard empty) {
|
||||||
BitBoard result = 0;
|
BitBoard result = 0;
|
||||||
BitBoard diag1 = bishops;
|
|
||||||
BitBoard diag2 = bishops;
|
|
||||||
empty ^= bishops;
|
empty ^= bishops;
|
||||||
|
|
||||||
for (int i = 0; i < 7; i++) {
|
BitBoard diag1 = (bishops.northWest() | bishops.southEast() | bishops) & empty;
|
||||||
|
BitBoard diag2 = (bishops.northEast() | bishops.southWest() | bishops) & empty;
|
||||||
|
|
||||||
|
for (int i = 0; i < 6; i++) {
|
||||||
result |= (diag1 | diag2);
|
result |= (diag1 | diag2);
|
||||||
diag1 = (diag1.northWest() | diag1.southEast()) & empty;
|
diag1 = (diag1.northWest() | diag1.southEast()) & empty;
|
||||||
diag2 = (diag2.northEast() | diag2.southWest()) & empty;
|
diag2 = (diag2.northEast() | diag2.southWest()) & empty;
|
||||||
@@ -82,17 +82,18 @@ BitBoard BitBoard::bishopAttacks(BitBoard bishops, BitBoard empty) {
|
|||||||
|
|
||||||
BitBoard BitBoard::rookAttacks(BitBoard rooks, BitBoard empty) {
|
BitBoard BitBoard::rookAttacks(BitBoard rooks, BitBoard empty) {
|
||||||
BitBoard result = 0;
|
BitBoard result = 0;
|
||||||
BitBoard diag1 = rooks;
|
|
||||||
BitBoard diag2 = rooks;
|
|
||||||
empty ^= rooks;
|
empty ^= rooks;
|
||||||
|
|
||||||
for (int i = 0; i < 7; i++) {
|
BitBoard dir1 = (rooks.north() | rooks.south() | rooks) & empty;
|
||||||
result |= (diag1 | diag2);
|
BitBoard dir2 = (rooks.east() | rooks.west() | rooks) & empty;
|
||||||
diag1 = (diag1.north() | diag1.south()) & empty;
|
|
||||||
diag2 = (diag2.east() | diag2.west()) & empty;
|
for (int i = 0; i < 6; i++) {
|
||||||
|
result |= (dir1 | dir2);
|
||||||
|
dir1 = (dir1.north() | dir1.south()) & empty;
|
||||||
|
dir2 = (dir2.east() | dir2.west()) & empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (result | diag1.north() | diag1.south() | diag2.east() | diag2.west()) & ~rooks;
|
return (result | dir1.north() | dir1.south() | dir2.east() | dir2.west()) & ~rooks;
|
||||||
}
|
}
|
||||||
|
|
||||||
BitBoard BitBoard::queenAttacks(BitBoard queens, BitBoard empty) {
|
BitBoard BitBoard::queenAttacks(BitBoard queens, BitBoard empty) {
|
||||||
|
11
BitBoard.hpp
11
BitBoard.hpp
@@ -39,6 +39,9 @@ public:
|
|||||||
explicit constexpr operator bool() const {
|
explicit constexpr operator bool() const {
|
||||||
return mBoard != 0;
|
return mBoard != 0;
|
||||||
}
|
}
|
||||||
|
explicit constexpr operator unsigned int() const {
|
||||||
|
return mBoard;
|
||||||
|
}
|
||||||
explicit constexpr operator unsigned long() const {
|
explicit constexpr operator unsigned long() const {
|
||||||
return mBoard;
|
return mBoard;
|
||||||
}
|
}
|
||||||
@@ -125,9 +128,10 @@ public:
|
|||||||
// WARN: Check for 0!
|
// WARN: Check for 0!
|
||||||
unsigned lsb() const;
|
unsigned lsb() const;
|
||||||
unsigned pop();
|
unsigned pop();
|
||||||
|
constexpr int count() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
U64 mBoard = {};
|
U64 mBoard = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Relational operators
|
// Relational operators
|
||||||
@@ -282,4 +286,9 @@ inline unsigned BitBoard::pop() {
|
|||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
constexpr int BitBoard::count() const {
|
||||||
|
return __builtin_popcountll(mBoard);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#endif //CHESS_ENGINE_BITBOARD_HPP
|
#endif //CHESS_ENGINE_BITBOARD_HPP
|
||||||
|
65
Board.cpp
65
Board.cpp
@@ -32,6 +32,42 @@ Piece::Optional Board::piece(const Square &square) const {
|
|||||||
return Piece(c, t);
|
return Piece(c, t);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int Board::evaluate() const {
|
||||||
|
int score = evaluate(mTurn);
|
||||||
|
score -= evaluate(!mTurn);
|
||||||
|
|
||||||
|
return score;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Board::evaluate(const PieceColor color) const {
|
||||||
|
int score = 0;
|
||||||
|
|
||||||
|
BitBoard bb;
|
||||||
|
BitBoard colorMask = mPieceBBs[toIndex(color)];
|
||||||
|
for (int i = 0; i < 6; i++) {
|
||||||
|
bb = mPieceBBs[i+2] & colorMask;
|
||||||
|
score += Piece::PieceValue[i] * bb.count();
|
||||||
|
}
|
||||||
|
|
||||||
|
// BoardState bs = BoardState(&mPieceBBs, mOccupiedBB, mTurn, mCR, mEPS);
|
||||||
|
// score += round(mMobilityWeight * MoveGenerator::Mobility(bs, color));
|
||||||
|
|
||||||
|
return score;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Board::isCheckMate() const {
|
||||||
|
auto moves = MoveVec();
|
||||||
|
pseudoLegalMoves(moves);
|
||||||
|
|
||||||
|
if (!moves.empty())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
BoardState bs = BoardState(&mPieceBBs, mOccupiedBB, mTurn, mCR, mEPS);
|
||||||
|
auto attacked = MoveGenerator::generateAttackedSquares(bs, ~mOccupiedBB, !mTurn);
|
||||||
|
|
||||||
|
return attacked & mPieceBBs[toIndex(PieceType::King)] & mPieceBBs[toIndex(mTurn)];
|
||||||
|
}
|
||||||
|
|
||||||
void Board::setTurn(PieceColor turn) {
|
void Board::setTurn(PieceColor turn) {
|
||||||
mTurn = turn;
|
mTurn = turn;
|
||||||
}
|
}
|
||||||
@@ -143,10 +179,11 @@ void Board::handlePawnDoubleAdvance(const Move &move, BitBoard bb, const Piece &
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Board::pseudoLegalMoves(MoveVec &moves) const {
|
void Board::pseudoLegalMoves(MoveVec &moves) const {
|
||||||
auto occupied = mOccupiedBB;
|
auto pieces = mPieceBBs[toIndex(mTurn)];
|
||||||
while(occupied) {
|
|
||||||
auto to = Square(occupied.pop());
|
while (pieces) {
|
||||||
pseudoLegalMovesFrom(to, moves);
|
auto sq = Square(pieces.pop());
|
||||||
|
pseudoLegalMovesFrom(sq, moves);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,29 +195,21 @@ void Board::pseudoLegalMovesFrom(const Square &from, Board::MoveVec &moves) cons
|
|||||||
}
|
}
|
||||||
|
|
||||||
BoardState bs = BoardState(&mPieceBBs, mOccupiedBB, mTurn, mCR, mEPS);
|
BoardState bs = BoardState(&mPieceBBs, mOccupiedBB, mTurn, mCR, mEPS);
|
||||||
auto p = Piece(mTurn, pieceType(fromBB));
|
|
||||||
|
|
||||||
BitBoard movesBB;
|
switch (pieceType(fromBB)) {
|
||||||
switch (p.type()) {
|
|
||||||
case PieceType::Pawn: MoveGenerator::generatePawnMoves(bs, from, moves);
|
case PieceType::Pawn: MoveGenerator::generatePawnMoves(bs, from, moves);
|
||||||
return;
|
break;
|
||||||
case PieceType::Knight: MoveGenerator::generateKnightMoves(bs, from, moves);
|
case PieceType::Knight: MoveGenerator::generateKnightMoves(bs, from, moves);
|
||||||
return;
|
break;
|
||||||
case PieceType::Bishop: MoveGenerator::generateBishopMoves(bs, from, moves);
|
case PieceType::Bishop: MoveGenerator::generateBishopMoves(bs, from, moves);
|
||||||
return;
|
break;
|
||||||
case PieceType::Rook: MoveGenerator::generateRookMoves(bs, from, moves);
|
case PieceType::Rook: MoveGenerator::generateRookMoves(bs, from, moves);
|
||||||
return;
|
break;
|
||||||
case PieceType::Queen: MoveGenerator::generateQueenMoves(bs, from, moves);
|
case PieceType::Queen: MoveGenerator::generateQueenMoves(bs, from, moves);
|
||||||
return;
|
break;
|
||||||
|
|
||||||
case PieceType::King: MoveGenerator::generateKingMoves(bs, from, moves);
|
case PieceType::King: MoveGenerator::generateKingMoves(bs, from, moves);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (movesBB) {
|
|
||||||
auto to = Square(movesBB.pop());
|
|
||||||
moves.emplace_back(from, to, std::nullopt);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
void Board::handleCastlingRights(const Piece &piece, const BitBoard &bb) {
|
void Board::handleCastlingRights(const Piece &piece, const BitBoard &bb) {
|
||||||
if (piece.type() != PieceType::King && piece.type() != PieceType::Rook) {
|
if (piece.type() != PieceType::King && piece.type() != PieceType::Rook) {
|
||||||
|
12
Board.hpp
12
Board.hpp
@@ -21,13 +21,11 @@ public:
|
|||||||
using MoveVec = std::vector<Move>;
|
using MoveVec = std::vector<Move>;
|
||||||
|
|
||||||
Board() = default;
|
Board() = default;
|
||||||
Board(const Board &) = default;
|
|
||||||
Board(Board &&other) = default;
|
|
||||||
Board &operator=(const Board &) = default;
|
|
||||||
Board &operator=(Board &&) = default;
|
|
||||||
|
|
||||||
void setPiece(const Square &square, const Piece::Optional &piece);
|
void setPiece(const Square &square, const Piece::Optional &piece);
|
||||||
Piece::Optional piece(const Square &square) const;
|
Piece::Optional piece(const Square &square) const;
|
||||||
|
std::vector<Piece> pieces(PieceColor color) const;
|
||||||
|
int evaluate() const;
|
||||||
void setTurn(PieceColor turn);
|
void setTurn(PieceColor turn);
|
||||||
PieceColor turn() const;
|
PieceColor turn() const;
|
||||||
void setCastlingRights(CastlingRights cr);
|
void setCastlingRights(CastlingRights cr);
|
||||||
@@ -47,6 +45,9 @@ public:
|
|||||||
return static_cast<int>(c);
|
return static_cast<int>(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool isCheckMate() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
BitBoard mPieceBBs[BB_NUM] = {};
|
BitBoard mPieceBBs[BB_NUM] = {};
|
||||||
@@ -56,6 +57,8 @@ private:
|
|||||||
CastlingRights mCR = CastlingRights::None;
|
CastlingRights mCR = CastlingRights::None;
|
||||||
std::optional<Square> mEPS;
|
std::optional<Square> mEPS;
|
||||||
|
|
||||||
|
constexpr static const double mMobilityWeight = 0.1;
|
||||||
|
|
||||||
void handleCastlingRights(const Piece &piece, const BitBoard &bb);
|
void handleCastlingRights(const Piece &piece, const BitBoard &bb);
|
||||||
|
|
||||||
// Check if the move is castling without checking the rights or validity.
|
// Check if the move is castling without checking the rights or validity.
|
||||||
@@ -83,6 +86,7 @@ private:
|
|||||||
|
|
||||||
void handlePawnDoubleAdvance(const Move &move, BitBoard bb, const Piece &movedPiece);
|
void handlePawnDoubleAdvance(const Move &move, BitBoard bb, const Piece &movedPiece);
|
||||||
void handleEnPassant(const Move &move, const Piece &movedPiece);
|
void handleEnPassant(const Move &move, const Piece &movedPiece);
|
||||||
|
int evaluate(const PieceColor color) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
std::ostream &operator<<(std::ostream &os, const Board &board);
|
std::ostream &operator<<(std::ostream &os, const Board &board);
|
||||||
|
@@ -5,6 +5,8 @@
|
|||||||
#include "BitBoard.hpp"
|
#include "BitBoard.hpp"
|
||||||
#include "Board.hpp"
|
#include "Board.hpp"
|
||||||
|
|
||||||
|
#define BB_NUM 8
|
||||||
|
|
||||||
struct BoardState {
|
struct BoardState {
|
||||||
BoardState(const BitBoard (*const pieceBBs)[8],
|
BoardState(const BitBoard (*const pieceBBs)[8],
|
||||||
const BitBoard occupiedBB,
|
const BitBoard occupiedBB,
|
||||||
@@ -24,6 +26,17 @@ struct BoardState {
|
|||||||
const PieceColor turn;
|
const PieceColor turn;
|
||||||
const CastlingRights cr;
|
const CastlingRights cr;
|
||||||
const std::optional<Square> eps;
|
const std::optional<Square> eps;
|
||||||
|
|
||||||
|
inline PieceType pieceType(const BitBoard &mask) const {
|
||||||
|
for (int i = 2; i < BB_NUM; i++) {
|
||||||
|
if ((*pieceBBs)[i] & mask) {
|
||||||
|
return static_cast<PieceType>(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Should not happen
|
||||||
|
return static_cast<PieceType>(0);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //CHESS_ENGINE_BOARDSTATE_HPP
|
#endif //CHESS_ENGINE_BOARDSTATE_HPP
|
||||||
|
@@ -25,7 +25,7 @@ else ()
|
|||||||
endif ()
|
endif ()
|
||||||
|
|
||||||
if (CMAKE_BUILD_TYPE STREQUAL "Release")
|
if (CMAKE_BUILD_TYPE STREQUAL "Release")
|
||||||
add_compile_options(-O3 -march=native)
|
add_compile_options(-O3 -march=native -funroll-loops)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
add_library(cplchess_lib OBJECT
|
add_library(cplchess_lib OBJECT
|
||||||
@@ -41,7 +41,9 @@ add_library(cplchess_lib OBJECT
|
|||||||
Uci.cpp
|
Uci.cpp
|
||||||
BitBoard.cpp
|
BitBoard.cpp
|
||||||
MoveGenerator.cpp
|
MoveGenerator.cpp
|
||||||
BoardState.hpp)
|
Search.cpp
|
||||||
|
BasicEngine.cpp
|
||||||
|
)
|
||||||
|
|
||||||
target_include_directories(cplchess_lib PUBLIC .)
|
target_include_directories(cplchess_lib PUBLIC .)
|
||||||
|
|
||||||
|
@@ -1,5 +1,8 @@
|
|||||||
#include "EngineFactory.hpp"
|
#include "EngineFactory.hpp"
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include "BasicEngine.hpp"
|
||||||
|
|
||||||
std::unique_ptr<Engine> EngineFactory::createEngine() {
|
std::unique_ptr<Engine> EngineFactory::createEngine() {
|
||||||
return nullptr;
|
return std::make_unique<BasicEngine>();
|
||||||
}
|
}
|
||||||
|
39
Move.cpp
39
Move.cpp
@@ -3,10 +3,21 @@
|
|||||||
#include <ostream>
|
#include <ostream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
Move::Move(const Square &from, const Square &to, const std::optional<PieceType> &promotion)
|
|
||||||
: mFrom(from), mTo(to), mPromotion(promotion) {
|
Move::Move(const Square &from, const Square &to) : mFrom(from), mTo(to) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Move::Move(const Square &from, const Square &to, const std::optional<PieceType> &promotion, unsigned score)
|
||||||
|
: mFrom(from), mTo(to), mPromotion(promotion) {
|
||||||
|
mScore -= score;
|
||||||
|
}
|
||||||
|
|
||||||
|
Move::Move(const Square &from, const Square &to, unsigned int score) : mFrom(from), mTo(to) {
|
||||||
|
mScore -= score;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Move::Optional Move::fromUci(const std::string &uci) {
|
Move::Optional Move::fromUci(const std::string &uci) {
|
||||||
if (uci.length() < 4 || uci.length() > 5)
|
if (uci.length() < 4 || uci.length() > 5)
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
@@ -48,17 +59,31 @@ std::ostream &operator<<(std::ostream &os, const Move &move) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool operator<(const Move &lhs, const Move &rhs) {
|
bool operator<(const Move &lhs, const Move &rhs) {
|
||||||
if (!(lhs.from() == rhs.from()))
|
if (lhs.mScore != rhs.mScore) {
|
||||||
return lhs.from() < rhs.from();
|
return lhs.mScore < rhs.mScore;
|
||||||
|
}
|
||||||
|
|
||||||
if (!(lhs.to() == rhs.to()))
|
if (lhs.mFrom.index() != rhs.mFrom.index()) {
|
||||||
return lhs.to() < rhs.to();
|
return lhs.mFrom.index() < rhs.mFrom.index();
|
||||||
|
}
|
||||||
|
if (lhs.mTo.index() != rhs.mTo.index()) {
|
||||||
|
return lhs.mTo.index() < rhs.mTo.index();
|
||||||
|
}
|
||||||
|
|
||||||
return lhs.promotion() < rhs.promotion();
|
return lhs.promotion() < rhs.promotion();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator==(const Move &lhs, const Move &rhs) {
|
bool operator==(const Move &lhs, const Move &rhs) {
|
||||||
return lhs.from() == rhs.from()
|
return lhs.score() == rhs.score()
|
||||||
|
&& lhs.from() == rhs.from()
|
||||||
&& lhs.to() == rhs.to()
|
&& lhs.to() == rhs.to()
|
||||||
&& lhs.promotion() == rhs.promotion();
|
&& lhs.promotion() == rhs.promotion();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned int Move::score() const {
|
||||||
|
return mScore;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Move::setScore(unsigned int score) {
|
||||||
|
mScore = score;
|
||||||
|
}
|
||||||
|
24
Move.hpp
24
Move.hpp
@@ -7,14 +7,17 @@
|
|||||||
#include <iosfwd>
|
#include <iosfwd>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <limits>
|
||||||
|
|
||||||
class Move {
|
class Move {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
using Optional = std::optional<Move>;
|
using Optional = std::optional<Move>;
|
||||||
|
|
||||||
Move(const Square &from, const Square &to,
|
explicit Move(const Square &from, const Square &to);
|
||||||
const std::optional<PieceType> &promotion = std::nullopt);
|
explicit Move(const Square &from, const Square &to,
|
||||||
|
const std::optional<PieceType> &promotion, unsigned score = 0);
|
||||||
|
explicit Move(const Square &from, const Square &to, unsigned score);
|
||||||
|
|
||||||
static Optional fromUci(const std::string &uci);
|
static Optional fromUci(const std::string &uci);
|
||||||
|
|
||||||
@@ -22,16 +25,23 @@ public:
|
|||||||
Square to() const;
|
Square to() const;
|
||||||
std::optional<PieceType> promotion() const;
|
std::optional<PieceType> promotion() const;
|
||||||
|
|
||||||
|
|
||||||
|
friend bool operator<(const Move &lhs, const Move &rhs);
|
||||||
|
friend bool operator==(const Move &lhs, const Move &rhs);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const Square mFrom;
|
Square mFrom;
|
||||||
const Square mTo;
|
Square mTo;
|
||||||
const std::optional<PieceType> mPromotion;
|
std::optional<PieceType> mPromotion = std::nullopt;
|
||||||
|
unsigned mScore = std::numeric_limits<unsigned>::max();
|
||||||
|
public:
|
||||||
|
unsigned int score() const;
|
||||||
|
|
||||||
|
void setScore(unsigned int mScore);
|
||||||
};
|
};
|
||||||
|
|
||||||
std::ostream &operator<<(std::ostream &os, const Move &move);
|
std::ostream &operator<<(std::ostream &os, const Move &move);
|
||||||
|
|
||||||
// Needed for std::map, std::set
|
// Needed for std::map, std::set
|
||||||
bool operator<(const Move &lhs, const Move &rhs);
|
|
||||||
bool operator==(const Move &lhs, const Move &rhs);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@@ -1,3 +1,4 @@
|
|||||||
|
#include <iostream>
|
||||||
#include "MoveGenerator.hpp"
|
#include "MoveGenerator.hpp"
|
||||||
#include "Board.hpp"
|
#include "Board.hpp"
|
||||||
#include "BoardState.hpp"
|
#include "BoardState.hpp"
|
||||||
@@ -28,9 +29,9 @@ void MoveGenerator::generatePawnMoves(const BoardState &bs, const Square &from,
|
|||||||
|
|
||||||
bool isPromotion = movesBB & (Rank1 | Rank8);
|
bool isPromotion = movesBB & (Rank1 | Rank8);
|
||||||
if (isPromotion) {
|
if (isPromotion) {
|
||||||
generateMovesWithPromotion(from, movesBB, moves);
|
generateMovesWithPromotion(bs, from, movesBB, moves);
|
||||||
} else {
|
} else {
|
||||||
generateMoves(from, movesBB, moves);
|
generateMoves(bs, from, movesBB, PieceType::Pawn, moves);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,24 +39,42 @@ void MoveGenerator::generateBishopMoves(const BoardState &bs, const Square &from
|
|||||||
auto fromBB = BitBoard::fromIndex(from.index());
|
auto fromBB = BitBoard::fromIndex(from.index());
|
||||||
auto movesBB = BitBoard::bishopAttacks(fromBB, ~bs.occupiedBB) & ~(*bs.pieceBBs)[Board::toIndex(bs.turn)];
|
auto movesBB = BitBoard::bishopAttacks(fromBB, ~bs.occupiedBB) & ~(*bs.pieceBBs)[Board::toIndex(bs.turn)];
|
||||||
|
|
||||||
generateMoves(from, movesBB, moves);
|
generateMoves(bs, from, movesBB, PieceType::Bishop, moves);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MoveGenerator::generateMoves(const Square &from, BitBoard movesBB, MoveVec &moves) {
|
void
|
||||||
|
MoveGenerator::generateMoves(const BoardState &bs, const Square &from, BitBoard movesBB, PieceType pt, MoveVec &moves) {
|
||||||
|
auto fromBB = BitBoard::fromIndex(from.index());
|
||||||
while (movesBB) {
|
while (movesBB) {
|
||||||
auto to = Square(movesBB.pop());
|
auto to = Square(movesBB.pop());
|
||||||
moves.emplace_back(from, to, std::nullopt);
|
auto toBB = BitBoard::fromIndex(to.index());
|
||||||
|
if (isCheck(bs, fromBB, toBB)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
moves.emplace_back(from, to, score(bs, toBB, pt));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MoveGenerator::generateMovesWithPromotion(const Square &from, BitBoard movesBB, MoveVec &moves) {
|
void
|
||||||
|
MoveGenerator::generateMovesWithPromotion(const BoardState &bs, const Square &from, BitBoard movesBB, MoveVec &moves) {
|
||||||
|
auto fromBB = BitBoard::fromIndex(from.index());
|
||||||
while (movesBB) {
|
while (movesBB) {
|
||||||
auto to = Square(movesBB.pop());
|
auto to = Square(movesBB.pop());
|
||||||
|
auto toBB = BitBoard::fromIndex(to.index());
|
||||||
|
if (isCheck(bs, fromBB, toBB)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
for (const auto &kItem: Piece::PromotionTypes) {
|
for (const auto &kItem: Piece::PromotionTypes) {
|
||||||
moves.emplace_back(from, to, static_cast<PieceType>(kItem));
|
|
||||||
|
unsigned s = Piece::PromotionScore[Board::toIndex(kItem) - 2] + score(bs, toBB, PieceType::Pawn);
|
||||||
|
moves.emplace_back(from, to, static_cast<PieceType>(kItem), s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MoveGenerator::generateKingMoves(const BoardState &bs, const Square &from, MoveGenerator::MoveVec &moves) {
|
void MoveGenerator::generateKingMoves(const BoardState &bs, const Square &from, MoveGenerator::MoveVec &moves) {
|
||||||
auto fromBB = BitBoard::fromIndex(from.index());
|
auto fromBB = BitBoard::fromIndex(from.index());
|
||||||
auto movesBB = BitBoard::kingMoves(fromBB) & ~(*bs.pieceBBs)[Board::toIndex(bs.turn)];
|
auto movesBB = BitBoard::kingMoves(fromBB) & ~(*bs.pieceBBs)[Board::toIndex(bs.turn)];
|
||||||
@@ -74,7 +93,7 @@ void MoveGenerator::generateKingMoves(const BoardState &bs, const Square &from,
|
|||||||
if (checkCR != CastlingRights::None) {
|
if (checkCR != CastlingRights::None) {
|
||||||
// Generate attacked squares
|
// Generate attacked squares
|
||||||
BitBoard target = CastlingRanks | bs.occupiedBB;
|
BitBoard target = CastlingRanks | bs.occupiedBB;
|
||||||
auto attacked = generateAttackedSquares(bs, target, !bs.turn);
|
auto attacked = generateAttackedSquares(bs, ~target, !bs.turn);
|
||||||
|
|
||||||
movesBB |= BitBoard::castlingMoves(fromBB & ~attacked,
|
movesBB |= BitBoard::castlingMoves(fromBB & ~attacked,
|
||||||
(*bs.pieceBBs)[Board::toIndex(PieceType::Rook)],
|
(*bs.pieceBBs)[Board::toIndex(PieceType::Rook)],
|
||||||
@@ -89,28 +108,28 @@ void MoveGenerator::generateKingMoves(const BoardState &bs, const Square &from,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
generateMoves(from, movesBB, moves);
|
generateMoves(bs, from, movesBB, PieceType::King, moves);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MoveGenerator::generateRookMoves(const BoardState &bs, const Square &from, MoveVec &moves) {
|
void MoveGenerator::generateRookMoves(const BoardState &bs, const Square &from, MoveVec &moves) {
|
||||||
auto fromBB = BitBoard::fromIndex(from.index());
|
auto fromBB = BitBoard::fromIndex(from.index());
|
||||||
auto movesBB = BitBoard::rookAttacks(fromBB, ~bs.occupiedBB) & ~(*bs.pieceBBs)[Board::toIndex(bs.turn)];
|
auto movesBB = BitBoard::rookAttacks(fromBB, ~bs.occupiedBB) & ~(*bs.pieceBBs)[Board::toIndex(bs.turn)];
|
||||||
|
|
||||||
generateMoves(from, movesBB, moves);
|
generateMoves(bs, from, movesBB, PieceType::Rook, moves);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MoveGenerator::generateQueenMoves(const BoardState &bs, const Square &from, MoveVec &moves) {
|
void MoveGenerator::generateQueenMoves(const BoardState &bs, const Square &from, MoveVec &moves) {
|
||||||
auto fromBB = BitBoard::fromIndex(from.index());
|
auto fromBB = BitBoard::fromIndex(from.index());
|
||||||
auto movesBB = BitBoard::queenAttacks(fromBB, ~bs.occupiedBB) & ~(*bs.pieceBBs)[Board::toIndex(bs.turn)];
|
auto movesBB = BitBoard::queenAttacks(fromBB, ~bs.occupiedBB) & ~(*bs.pieceBBs)[Board::toIndex(bs.turn)];
|
||||||
|
|
||||||
generateMoves(from, movesBB, moves);
|
generateMoves(bs, from, movesBB, PieceType::Queen, moves);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MoveGenerator::generateKnightMoves(const BoardState &bs, const Square &from, MoveVec &moves) {
|
void MoveGenerator::generateKnightMoves(const BoardState &bs, const Square &from, MoveVec &moves) {
|
||||||
|
|
||||||
auto fromBB = BitBoard::fromIndex(from.index());
|
auto fromBB = BitBoard::fromIndex(from.index());
|
||||||
auto movesBB = BitBoard::knightMoves(fromBB) & ~(*bs.pieceBBs)[Board::toIndex(bs.turn)];
|
auto movesBB = BitBoard::knightMoves(fromBB) & ~(*bs.pieceBBs)[Board::toIndex(bs.turn)];
|
||||||
generateMoves(from, movesBB, moves);
|
generateMoves(bs, from, movesBB, PieceType::Knight, moves);
|
||||||
}
|
}
|
||||||
|
|
||||||
BitBoard MoveGenerator::generateAttackedSquares(const BoardState &bs, BitBoard target, PieceColor opColor) {
|
BitBoard MoveGenerator::generateAttackedSquares(const BoardState &bs, BitBoard target, PieceColor opColor) {
|
||||||
@@ -125,7 +144,7 @@ BitBoard MoveGenerator::generateAttackedSquares(const BoardState &bs, BitBoard t
|
|||||||
}
|
}
|
||||||
|
|
||||||
// knights
|
// knights
|
||||||
attacked |= BitBoard::knightMoves((*bs.pieceBBs)[Board::toIndex(PieceType::Knight)] & opponentBB) & target;
|
attacked |= BitBoard::knightMoves((*bs.pieceBBs)[Board::toIndex(PieceType::Knight)] & opponentBB) & ~target;
|
||||||
|
|
||||||
// Bishop
|
// Bishop
|
||||||
attacked |= BitBoard::bishopAttacks((*bs.pieceBBs)[Board::toIndex(PieceType::Bishop)] & opponentBB, target);
|
attacked |= BitBoard::bishopAttacks((*bs.pieceBBs)[Board::toIndex(PieceType::Bishop)] & opponentBB, target);
|
||||||
@@ -137,7 +156,78 @@ BitBoard MoveGenerator::generateAttackedSquares(const BoardState &bs, BitBoard t
|
|||||||
attacked |= BitBoard::queenAttacks((*bs.pieceBBs)[Board::toIndex(PieceType::Queen)] & opponentBB, target);
|
attacked |= BitBoard::queenAttacks((*bs.pieceBBs)[Board::toIndex(PieceType::Queen)] & opponentBB, target);
|
||||||
|
|
||||||
// King
|
// King
|
||||||
attacked |= BitBoard::kingMoves((*bs.pieceBBs)[Board::toIndex(PieceType::Queen)] & opponentBB) & target;
|
attacked |= BitBoard::kingMoves((*bs.pieceBBs)[Board::toIndex(PieceType::Queen)] & opponentBB) & ~target;
|
||||||
|
|
||||||
return attacked;
|
return attacked;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline bool MoveGenerator::isCheck(const BoardState &bs, const BitBoard &fromBB, const BitBoard &toBB) {
|
||||||
|
auto kingBB = (*bs.pieceBBs)[Board::toIndex(bs.turn)] & (*bs.pieceBBs)[Board::toIndex(PieceType::King)];
|
||||||
|
auto changeBB = (fromBB ^ toBB);
|
||||||
|
auto target = bs.occupiedBB ^ changeBB;
|
||||||
|
|
||||||
|
if (kingBB & fromBB) {
|
||||||
|
|
||||||
|
if (toBB & bs.occupiedBB) {
|
||||||
|
target ^= toBB;
|
||||||
|
}
|
||||||
|
if ((toBB & ~bs.occupiedBB)) {
|
||||||
|
kingBB ^= changeBB;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
auto attacked = generateAttackedSquares(bs, ~target, !bs.turn);
|
||||||
|
|
||||||
|
return attacked & kingBB;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline unsigned MoveGenerator::score(const BoardState &bs, const BitBoard &toBB, const PieceType moved) {
|
||||||
|
|
||||||
|
unsigned score = 0;
|
||||||
|
auto opponentBB = bs.occupiedBB & ~(*bs.pieceBBs)[Board::toIndex(bs.turn)];
|
||||||
|
|
||||||
|
|
||||||
|
// Check Capture
|
||||||
|
auto captureBB = toBB & opponentBB;
|
||||||
|
if (captureBB) {
|
||||||
|
auto captured = bs.pieceType(captureBB);
|
||||||
|
score = Piece::MVV_LVA[Board::toIndex(captured)][Board::toIndex(moved)];
|
||||||
|
}
|
||||||
|
|
||||||
|
return score;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned MoveGenerator::Mobility(const BoardState &bs, PieceColor color) {
|
||||||
|
unsigned count = 0;
|
||||||
|
auto colorMask = (*bs.pieceBBs)[Board::toIndex(color)];
|
||||||
|
auto targets = (*bs.pieceBBs)[Board::toIndex(!color)];
|
||||||
|
|
||||||
|
// Pawns
|
||||||
|
auto pawnsBB = (*bs.pieceBBs)[Board::toIndex(PieceType::Pawn)] & colorMask;
|
||||||
|
if (color == PieceColor::White) {
|
||||||
|
auto movesBB = BitBoard::pawnNorthAttacks(pawnsBB, targets) & ~colorMask;
|
||||||
|
movesBB |= BitBoard::pawnNorthMoves(pawnsBB, ~bs.occupiedBB);
|
||||||
|
count += movesBB.count();
|
||||||
|
} else {
|
||||||
|
auto movesBB = BitBoard::pawnSouthAttacks(pawnsBB, targets) & ~colorMask;
|
||||||
|
movesBB |= BitBoard::pawnSouthMoves(pawnsBB, ~bs.occupiedBB);
|
||||||
|
count += movesBB.count();
|
||||||
|
}
|
||||||
|
// Bishops
|
||||||
|
count += (BitBoard::bishopAttacks((*bs.pieceBBs)[Board::toIndex(PieceType::Bishop)] & colorMask, ~bs.occupiedBB) &
|
||||||
|
~colorMask).count();
|
||||||
|
// Knights
|
||||||
|
count += (BitBoard::knightMoves((*bs.pieceBBs)[Board::toIndex(PieceType::Knight)] & colorMask) &
|
||||||
|
~colorMask).count();
|
||||||
|
// Rook
|
||||||
|
count += (BitBoard::rookAttacks((*bs.pieceBBs)[Board::toIndex(PieceType::Rook)] & colorMask, ~bs.occupiedBB) &
|
||||||
|
~colorMask).count();
|
||||||
|
// Queen
|
||||||
|
count += (BitBoard::queenAttacks((*bs.pieceBBs)[Board::toIndex(PieceType::Queen)] & colorMask, ~bs.occupiedBB) &
|
||||||
|
~colorMask).count();
|
||||||
|
// King
|
||||||
|
count += (BitBoard::kingMoves((*bs.pieceBBs)[Board::toIndex(PieceType::King)] & colorMask) & ~colorMask).count();
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
@@ -23,13 +23,18 @@ public:
|
|||||||
static void generateRookMoves(const BoardState &bs, const Square &from, MoveVec &moves);
|
static void generateRookMoves(const BoardState &bs, const Square &from, MoveVec &moves);
|
||||||
static void generateQueenMoves(const BoardState &bs, const Square &from, MoveVec &moves);
|
static void generateQueenMoves(const BoardState &bs, const Square &from, MoveVec &moves);
|
||||||
static void generateKnightMoves(const BoardState &bs, const Square &from, MoveVec &moves);
|
static void generateKnightMoves(const BoardState &bs, const Square &from, MoveVec &moves);
|
||||||
|
static BitBoard generateAttackedSquares(const BoardState &bs, BitBoard target, PieceColor opColor);
|
||||||
|
|
||||||
|
static unsigned int Mobility(const BoardState &bs, PieceColor color);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static void generatePawnMoves(const BoardState &bs, const Square &from, BitBoard targets, MoveVec &moves);
|
static void generatePawnMoves(const BoardState &bs, const Square &from, BitBoard targets, MoveVec &moves);
|
||||||
|
|
||||||
static void generateMoves(const Square &from, BitBoard movesBB, MoveVec &moves);
|
static void generateMoves(const BoardState &bs, const Square &from, BitBoard movesBB, PieceType pt, MoveVec &moves);
|
||||||
static void generateMovesWithPromotion(const Square &from, BitBoard movesBB, MoveVec &moves);
|
static void generateMovesWithPromotion(const BoardState &bs, const Square &from, BitBoard movesBB, MoveVec &moves);
|
||||||
static BitBoard generateAttackedSquares(const BoardState &bs, BitBoard target, PieceColor opColor);
|
|
||||||
|
inline static bool isCheck(const BoardState &bs, const BitBoard &fromBB, const BitBoard &toBB);
|
||||||
|
inline static unsigned score(const BoardState &bs, const BitBoard &toBB, PieceType moved);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //CHESS_ENGINE_MOVEGENERATOR_HPP
|
#endif //CHESS_ENGINE_MOVEGENERATOR_HPP
|
||||||
|
18
Piece.hpp
18
Piece.hpp
@@ -24,19 +24,34 @@ class Piece {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
constexpr static const PieceType PromotionTypes[4] = {PieceType::Knight, PieceType::Bishop, PieceType::Rook, PieceType::Queen};
|
constexpr static const PieceType PromotionTypes[4] = {PieceType::Knight, PieceType::Bishop, PieceType::Rook,
|
||||||
|
PieceType::Queen};
|
||||||
|
constexpr static const unsigned PieceValue[6] = {100, 350, 350, 525, 1000, 10000};
|
||||||
|
constexpr static const unsigned PromotionScore[6] = {0, 26, 36, 46, 56, 0};
|
||||||
|
|
||||||
|
constexpr static const unsigned MVV_LVA[6][6] = {
|
||||||
|
{15, 14, 13, 12, 11, 10}, // Pawn
|
||||||
|
{25, 24, 23, 22, 21, 20}, // Knight
|
||||||
|
{35, 34, 33, 32, 31, 30}, // Bishop
|
||||||
|
{45, 44, 43, 42, 41, 40}, // Rook
|
||||||
|
{55, 54, 53, 52, 51, 50}, // Queen
|
||||||
|
{65, 64, 63, 62, 61, 60}, // King
|
||||||
|
};
|
||||||
|
|
||||||
using Optional = std::optional<Piece>;
|
using Optional = std::optional<Piece>;
|
||||||
|
|
||||||
Piece(PieceColor color, PieceType type);
|
Piece(PieceColor color, PieceType type);
|
||||||
|
|
||||||
static Optional fromSymbol(char symbol);
|
static Optional fromSymbol(char symbol);
|
||||||
|
|
||||||
static char toSymbol(PieceType type);
|
static char toSymbol(PieceType type);
|
||||||
|
|
||||||
static std::optional<PieceType> pieceTypeFromSymbol(char symbol);
|
static std::optional<PieceType> pieceTypeFromSymbol(char symbol);
|
||||||
|
|
||||||
PieceColor color() const;
|
PieceColor color() const;
|
||||||
|
|
||||||
PieceType type() const;
|
PieceType type() const;
|
||||||
|
|
||||||
char toSymbol() const;
|
char toSymbol() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -45,6 +60,7 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
bool operator==(const Piece &lhs, const Piece &rhs);
|
bool operator==(const Piece &lhs, const Piece &rhs);
|
||||||
|
|
||||||
std::ostream &operator<<(std::ostream &os, const Piece &piece);
|
std::ostream &operator<<(std::ostream &os, const Piece &piece);
|
||||||
|
|
||||||
// Invert a color (White becomes Black and vice versa)
|
// Invert a color (White becomes Black and vice versa)
|
||||||
|
@@ -1,29 +1,53 @@
|
|||||||
#include "PrincipalVariation.hpp"
|
#include "PrincipalVariation.hpp"
|
||||||
|
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
|
||||||
|
PrincipalVariation::PrincipalVariation(const std::vector<Move> &v, bool isMate): mMoves(v), mIsMate(isMate) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
PrincipalVariation::PrincipalVariation(const std::vector<Move> &v, Board &board): mMoves(v) {
|
||||||
|
for (const auto &kMove : v) {
|
||||||
|
board.makeMove(kMove);
|
||||||
|
}
|
||||||
|
|
||||||
|
mIsMate = board.isCheckMate();
|
||||||
|
if (mIsMate || v.empty()) {
|
||||||
|
mScore = static_cast<int>(v.size());
|
||||||
|
} else {
|
||||||
|
mScore = board.evaluate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
bool PrincipalVariation::isMate() const {
|
bool PrincipalVariation::isMate() const {
|
||||||
return false;
|
return mIsMate;
|
||||||
}
|
}
|
||||||
|
|
||||||
int PrincipalVariation::score() const {
|
int PrincipalVariation::score() const {
|
||||||
return 0;
|
return mScore;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t PrincipalVariation::length() const {
|
std::size_t PrincipalVariation::length() const {
|
||||||
return 0;
|
return mMoves.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
PrincipalVariation::MoveIter PrincipalVariation::begin() const {
|
PrincipalVariation::MoveIter PrincipalVariation::begin() const {
|
||||||
return nullptr;
|
return mMoves.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
PrincipalVariation::MoveIter PrincipalVariation::end() const {
|
PrincipalVariation::MoveIter PrincipalVariation::end() const {
|
||||||
return nullptr;
|
return mMoves.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::ostream &operator<<(std::ostream &os, const PrincipalVariation &pv) {
|
std::ostream &operator<<(std::ostream &os, const PrincipalVariation &pv) {
|
||||||
(void)pv;
|
os << "isMate=" << pv.isMate();
|
||||||
|
os << " score=" << pv.score();
|
||||||
|
os << " length=" << pv.length() << std::endl;
|
||||||
|
os << "Moves:" << std::endl;
|
||||||
|
for (const auto &item : pv) {
|
||||||
|
os << item << std::endl;
|
||||||
|
}
|
||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
@@ -3,21 +3,31 @@
|
|||||||
|
|
||||||
#include "Move.hpp"
|
#include "Move.hpp"
|
||||||
#include "Piece.hpp"
|
#include "Piece.hpp"
|
||||||
|
#include "Board.hpp"
|
||||||
|
|
||||||
#include <iosfwd>
|
#include <iosfwd>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
class PrincipalVariation {
|
class PrincipalVariation {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
using MoveIter = Move*;
|
using MoveIter = std::vector<Move>::const_iterator;
|
||||||
|
|
||||||
|
PrincipalVariation() = default;
|
||||||
|
PrincipalVariation(const std::vector<Move> &v, bool isMate);
|
||||||
|
PrincipalVariation(const std::vector<Move> &v, Board &board);
|
||||||
bool isMate() const;
|
bool isMate() const;
|
||||||
int score() const;
|
int score() const;
|
||||||
|
|
||||||
std::size_t length() const;
|
std::size_t length() const;
|
||||||
MoveIter begin() const;
|
MoveIter begin() const;
|
||||||
MoveIter end() const;
|
MoveIter end() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<Move> mMoves;
|
||||||
|
bool mIsMate = false;
|
||||||
|
int mScore = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
std::ostream &operator<<(std::ostream &os, const PrincipalVariation &pv);
|
std::ostream &operator<<(std::ostream &os, const PrincipalVariation &pv);
|
||||||
|
86
Search.cpp
Normal file
86
Search.cpp
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include "Search.hpp"
|
||||||
|
#include "Move.hpp"
|
||||||
|
#include "Board.hpp"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
int Search::search(Node *node, const Board &board, unsigned depth, int alpha, int beta) {
|
||||||
|
|
||||||
|
if (depth == 0) {
|
||||||
|
return evaluate(board);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto moves = Board::MoveVec();
|
||||||
|
board.pseudoLegalMoves(moves);
|
||||||
|
if (moves.empty()) {
|
||||||
|
return evaluate(board);
|
||||||
|
}
|
||||||
|
std::stable_sort(moves.begin(), moves.end());
|
||||||
|
|
||||||
|
int maxValue = kNegInfinity;
|
||||||
|
for (auto &kMove: moves) {
|
||||||
|
Board nodeBoard = board;
|
||||||
|
nodeBoard.makeMove(kMove);
|
||||||
|
|
||||||
|
Node child(kMove);
|
||||||
|
child.value = search(&child, nodeBoard, depth - 1, -beta, -alpha);
|
||||||
|
|
||||||
|
auto value = -child.value;
|
||||||
|
if (value > maxValue) {
|
||||||
|
maxValue = value;
|
||||||
|
node->next = std::make_unique<Node>(child);
|
||||||
|
}
|
||||||
|
|
||||||
|
alpha = std::max(alpha, value);
|
||||||
|
if (alpha >= beta) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
node->value = maxValue;
|
||||||
|
|
||||||
|
return maxValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
int Search::evaluate(const Board &board) {
|
||||||
|
|
||||||
|
return board.evaluate();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
PrincipalVariation Search::start(const Board &board) {
|
||||||
|
auto rootMoves = Board::MoveVec();
|
||||||
|
board.pseudoLegalMoves(rootMoves);
|
||||||
|
std::stable_sort(rootMoves.begin(), rootMoves.end());
|
||||||
|
|
||||||
|
if (rootMoves.size() <= 1) {
|
||||||
|
auto b = board;
|
||||||
|
return PrincipalVariation(rootMoves, b);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<RootNode> best = std::make_unique<RootNode>();
|
||||||
|
for (const auto &kMove: rootMoves) {
|
||||||
|
|
||||||
|
auto node = Node(kMove);
|
||||||
|
|
||||||
|
Board nodeBoard = board;
|
||||||
|
nodeBoard.makeMove(kMove);
|
||||||
|
|
||||||
|
auto value = -search(&node, nodeBoard, 5, kNegInfinity, kPosInfinity);
|
||||||
|
if (value > best->value) {
|
||||||
|
best->child = std::make_unique<Node>(node);
|
||||||
|
best->board = nodeBoard;
|
||||||
|
best->value = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
auto moves = std::vector<Move>();
|
||||||
|
auto current = std::move(best->child);
|
||||||
|
do {
|
||||||
|
moves.push_back(current->move);
|
||||||
|
current = std::move(current->next);
|
||||||
|
} while (current);
|
||||||
|
|
||||||
|
return PrincipalVariation(moves, best->board);
|
||||||
|
}
|
39
Search.hpp
Normal file
39
Search.hpp
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
#ifndef CHESS_ENGINE_SEARCH_HPP
|
||||||
|
#define CHESS_ENGINE_SEARCH_HPP
|
||||||
|
|
||||||
|
#include "PrincipalVariation.hpp"
|
||||||
|
#include "Board.hpp"
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <limits>
|
||||||
|
|
||||||
|
namespace Search {
|
||||||
|
|
||||||
|
const int kPosInfinity = std::numeric_limits<int>::max();
|
||||||
|
const int kNegInfinity = std::numeric_limits<int>::min();
|
||||||
|
|
||||||
|
struct Node {
|
||||||
|
explicit Node(const Move &move) : move(move) {
|
||||||
|
}
|
||||||
|
|
||||||
|
Node(Node &node) : move(node.move) {
|
||||||
|
next = std::move(node.next);
|
||||||
|
value = node.value;
|
||||||
|
}
|
||||||
|
std::unique_ptr<Node> next;
|
||||||
|
Move move;
|
||||||
|
int value = kNegInfinity;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct RootNode {
|
||||||
|
std::unique_ptr<Node> child;
|
||||||
|
Board board;
|
||||||
|
int value = kNegInfinity;
|
||||||
|
};
|
||||||
|
|
||||||
|
int search(Node *node, const Board &board, unsigned depth, int alpha, int beta);
|
||||||
|
PrincipalVariation start(const Board &board);
|
||||||
|
int evaluate(const Board &board);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //CHESS_ENGINE_SEARCH_HPP
|
@@ -224,6 +224,18 @@ TEST_CASE_PSEUDO_MOVES("Pseudo-legal bishop moves, empty board", "[Bishop]") {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE_PSEUDO_MOVES("Pseudo-legal bishop moves, empty board side", "[Bishop]") {
|
||||||
|
testPseudoLegalMoves(
|
||||||
|
// https://lichess.org/editor/B7/8/8/8/8/8/8/8_w_-_-_0_1
|
||||||
|
"B7/8/8/8/8/8/8/8 w - - 0 1",
|
||||||
|
"a8",
|
||||||
|
{
|
||||||
|
"b7", "c6", "d5",
|
||||||
|
"e4", "f3", "g2", "h1"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE_PSEUDO_MOVES("Pseudo-legal bishop moves, captures", "[Bishop]") {
|
TEST_CASE_PSEUDO_MOVES("Pseudo-legal bishop moves, captures", "[Bishop]") {
|
||||||
testPseudoLegalMoves(
|
testPseudoLegalMoves(
|
||||||
// https://lichess.org/editor/8/8/1p3P2/8/3B4/8/8/8_w_-_-_0_1
|
// https://lichess.org/editor/8/8/1p3P2/8/3B4/8/8/8_w_-_-_0_1
|
||||||
@@ -252,6 +264,41 @@ TEST_CASE_PSEUDO_MOVES("Pseudo-legal rook moves, empty board", "[Rook]") {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE_PSEUDO_MOVES("Pseudo-legal rook moves, empty board side", "[Rook]") {
|
||||||
|
testPseudoLegalMoves(
|
||||||
|
// https://lichess.org/editor/3R4/8/8/8/8/8/8/8_w_-_-_0_1
|
||||||
|
"3R4/8/8/8/8/8/8/8 w - - 0 1",
|
||||||
|
"d8",
|
||||||
|
{
|
||||||
|
"d1", "d2", "d3", "d4", "d5", "d6", "d7",
|
||||||
|
"a8", "b8", "c8",
|
||||||
|
"e8", "f8", "g8",
|
||||||
|
"h8"
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
TEST_CASE_PSEUDO_MOVES("legal rook moves, king check", "[Rook]") {
|
||||||
|
testPseudoLegalMoves(
|
||||||
|
// https://lichess.org/editor/5R1k/6pp/8/8/8/8/8/K7_b_-_-_0_1
|
||||||
|
"5R1k/6pp/8/8/8/8/8/K7 b - - 0 1",
|
||||||
|
"g7",
|
||||||
|
{}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE_PSEUDO_MOVES("legal king moves, king stale", "[Rook]") {
|
||||||
|
testPseudoLegalMoves(
|
||||||
|
// https://lichess.org/editor/k7/8/8/6n1/r7/4K3/2q5/8_w_-_-_0_1
|
||||||
|
"k7/8/8/6n1/r7/4K3/2q5/8 w - - 0 1",
|
||||||
|
"e3",
|
||||||
|
{}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
TEST_CASE_PSEUDO_MOVES("Pseudo-legal rook moves, captures", "[Rook]") {
|
TEST_CASE_PSEUDO_MOVES("Pseudo-legal rook moves, captures", "[Rook]") {
|
||||||
testPseudoLegalMoves(
|
testPseudoLegalMoves(
|
||||||
// https://lichess.org/editor/8/8/3p4/8/3R1P2/8/8/8_w_-_-_0_1
|
// https://lichess.org/editor/8/8/3p4/8/3R1P2/8/8/8_w_-_-_0_1
|
||||||
|
@@ -25,6 +25,23 @@ static void testGameEnd(const char* fen, bool isMate) {
|
|||||||
REQUIRE(pv.length() == 0);
|
REQUIRE(pv.length() == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void testMove(const char *fen, const std::vector<std::string> &expectedMoves) {
|
||||||
|
auto engine = createEngine();
|
||||||
|
REQUIRE(engine != nullptr);
|
||||||
|
|
||||||
|
auto board = Fen::createBoard(fen);
|
||||||
|
REQUIRE(board.has_value());
|
||||||
|
|
||||||
|
for (const auto &moveName : expectedMoves) {
|
||||||
|
auto pv = engine->pv(board.value());
|
||||||
|
Move move = *pv.begin();
|
||||||
|
move.setScore(0);
|
||||||
|
REQUIRE(move == Move::fromUci(moveName));
|
||||||
|
board->makeMove(move);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("Engine detects checkmate", "[Engine][Checkmate]") {
|
TEST_CASE("Engine detects checkmate", "[Engine][Checkmate]") {
|
||||||
auto fen = GENERATE(
|
auto fen = GENERATE(
|
||||||
// https://lichess.org/editor/4R2k/6pp/8/8/8/8/8/K7_b_-_-_0_1
|
// https://lichess.org/editor/4R2k/6pp/8/8/8/8/8/K7_b_-_-_0_1
|
||||||
@@ -46,3 +63,13 @@ TEST_CASE("Engine detects stalemate", "[Engine][Stalemate]") {
|
|||||||
|
|
||||||
testGameEnd(fen, false);
|
testGameEnd(fen, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Puzzles mateIn1_simple", "[Engine][Puzzle]") {
|
||||||
|
auto [fen, moves] = GENERATE(table<const char*, std::vector<std::string>>({
|
||||||
|
{"2bQ1k1r/1p3p2/p4b1p/4pNp1/2q5/8/PPP2PPP/1K1RR3 b - - 3 23",{"f6d8", "d1d8"}}, // https://lichess.org/gQa01loO/black#46
|
||||||
|
{"5rk1/ppp5/2n4p/3b2p1/6R1/P1B1P2P/1Pq5/R2Q2K1 b - - 3 29",{"c2f2"}}, // https://lichess.org/haKJxadR#57
|
||||||
|
{"r1b1k2r/ppp2pp1/2p5/2b1q1Bp/3PP1n1/2P5/PP2BPPP/RN1Q1RK1 b kq - 0 10",{"e5h2"}},
|
||||||
|
{"r3rk2/5ppQ/b1pp1q1p/p5n1/P1P5/2N5/1PB2PP1/R3R1K1 w - - 6 24", {"h7h8"}},
|
||||||
|
}));
|
||||||
|
testMove(fen, moves);
|
||||||
|
}
|
||||||
|
@@ -104,7 +104,7 @@ TEST_CASE("En passant square is correctly parsed", "[Fen][EnPassant]") {
|
|||||||
{
|
{
|
||||||
"rnbqkbnr/ppppp1pp/8/8/2PPPp2/8/PP3PPP/RNBQKBNR b KQkq e3 0 3",
|
"rnbqkbnr/ppppp1pp/8/8/2PPPp2/8/PP3PPP/RNBQKBNR b KQkq e3 0 3",
|
||||||
Square::E3
|
Square::E3
|
||||||
}
|
},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
CAPTURE(fen, ep);
|
CAPTURE(fen, ep);
|
||||||
|
Reference in New Issue
Block a user