Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
6c0054b
Rewrite how the board is stored and moves are made
razvanfilea Nov 20, 2019
1c41cd3
Remove 'ToList' MoveGen template arg, make some fixes
razvanfilea Nov 22, 2019
2bfdff3
Fix a critical error with Board::doMove()
razvanfilea Nov 24, 2019
9db8df9
Fix getPhase always returning ENDGAME
razvanfilea Nov 24, 2019
8fd4e8c
Improve the Evaluation function performance
razvanfilea Nov 24, 2019
bee2ee1
Fix Board::hasValidState and update Pawn PSQT
razvanfilea Nov 26, 2019
68d9853
Switch to the previous Multi Threading model
razvanfilea Nov 28, 2019
7154003
Remove KING_DANGER MoveGen
razvanfilea Nov 28, 2019
eb1963c
Add age attribute to the TranspositionTable
razvanfilea Nov 29, 2019
2da9711
Fix crash when generating legal moves
razvanfilea Nov 30, 2019
f922ceb
Rewrite how the UI processes positions
razvanfilea Nov 30, 2019
220b4d7
Rename NegaMax class to Search
razvanfilea Nov 30, 2019
ee721fd
Work on the ThreadPool
razvanfilea Nov 30, 2019
9a9e5ac
Solve a bug with Castling and disable EnPassant for now
razvanfilea Nov 30, 2019
faba2c2
Add Generated Nodes to Stats
razvanfilea Dec 1, 2019
d73bb5c
Remake the Settings Screen
razvanfilea Dec 1, 2019
9b46c3e
Clean ThreadSafeQueue and ThreadPool
razvanfilea Dec 1, 2019
d5d5ae0
Revert to the old Pawn PSQT
razvanfilea Dec 8, 2019
1b60b38
Re-enable EnPassant
razvanfilea Dec 9, 2019
c0be29b
Add Platform Specific Implementations for BSF and BSR
razvanfilea Dec 10, 2019
8a64bbc
Remove StackVector
razvanfilea Dec 10, 2019
7889cfa
Fix compile errors
razvanfilea Dec 10, 2019
e35ab03
Remake the Pawn Evaluation add Connected Bonus
razvanfilea Dec 11, 2019
459400d
Merge Rays.h into Bitboard.h
razvanfilea Dec 11, 2019
9be2e38
Fix Pawn Move Generation from last commit
razvanfilea Dec 12, 2019
9a37891
Remove Light Theme, add Rook on Queen File Evaluation
razvanfilea Dec 15, 2019
b81a38c
Rewrite Piece class to only use 1 byte
razvanfilea Dec 15, 2019
3a122a9
New icon
razvanfilea Dec 15, 2019
99a93b5
Release 1.0
razvanfilea Dec 15, 2019
b8651a6
Fix issues with the Pull Request
razvanfilea Dec 15, 2019
8c8a8d9
Delete MoveOrdering.h
razvanfilea Dec 15, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Solve a bug with Castling and disable EnPassant for now
  • Loading branch information
razvanfilea committed Nov 30, 2019
commit 9a9e5ac10eaebbaa57759a4ca6ebb2d8cdbab0ce
42 changes: 25 additions & 17 deletions ChessAndroid/app/src/main/cpp/chess/algorithm/Hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ void Hash::init()
std::mt19937_64 mt(rd());
std::uniform_int_distribution<U64> dist(0, UINT64_MAX);

for (byte i = 0; i < 64; ++i)
for (byte p = 0; p < 12; p++)
s_Pieces[i][p] = dist(mt);
for (auto &i : s_Pieces)
for (auto &color : i)
for (auto &piece : color)
piece = dist(mt);

for (auto &rights : s_CastlingRights)
rights = dist(mt);
Expand All @@ -31,7 +32,7 @@ void Hash::init()

U64 Hash::movePiece(const byte sq, const Piece &piece)
{
return s_Pieces[sq][indexOf(piece)];
return s_Pieces[sq][piece.isWhite][piece.type - 1u];
}

U64 Hash::compute(const Board &board)
Expand All @@ -40,23 +41,16 @@ U64 Hash::compute(const Board &board)

for (byte i = 0; i < 64; ++i)
if (const Piece &piece = board.getPiece(i); piece)
hash ^= s_Pieces[i][indexOf(piece)];
hash ^= s_Pieces[i][piece.isWhite][piece.type - 1u];

if (board.colorToMove)
hash ^= s_WhiteToMove;

xorCastlingRights(hash, static_cast<CastlingRights>(board.castlingRights));
addCastlingRights(hash, static_cast<CastlingRights>(board.castlingRights));

return hash;
}

byte Hash::indexOf(const Piece& piece)
{
byte type = static_cast<byte>(piece.type - 1u);
if (piece.isWhite) type += 6u;
return type;
}

void Hash::makeMove(U64 &key, const byte selectedSq, const byte destSq, const Piece &selectedPiece, const Piece &destPiece)
{
// Remove Selected Piece
Expand All @@ -72,23 +66,23 @@ void Hash::makeMove(U64 &key, const byte selectedSq, const byte destSq, const Pi
void Hash::promotePawn(U64 &key, const byte sq, const Color color, const PieceType promotedType)
{
// Remove the Pawn
key ^= s_Pieces[sq][indexOf(Piece(PieceType::PAWN, color))];
key ^= s_Pieces[sq][color][PAWN - 1u];

// Add the Promoted Piece
key ^= s_Pieces[sq][indexOf(Piece(promotedType, color))];
key ^= s_Pieces[sq][color][promotedType - 1u];
}

void Hash::xorPiece(U64 &key, const byte sq, const Piece &piece)
{
key ^= s_Pieces[sq][indexOf(piece)];
key ^= s_Pieces[sq][piece.isWhite][piece.type - 1u];
}

void Hash::flipSide(U64 &key)
{
key ^= Hash::s_WhiteToMove;
}

void Hash::xorCastlingRights(U64 &key, const CastlingRights rights)
void Hash::addCastlingRights(U64 &key, const CastlingRights rights)
{
if (rights & CASTLE_WHITE_KING)
key ^= s_CastlingRights[0];
Expand All @@ -100,3 +94,17 @@ void Hash::xorCastlingRights(U64 &key, const CastlingRights rights)
if (rights & CASTLE_BLACK_QUEEN)
key ^= s_CastlingRights[3];
}

void Hash::removeCastlingRights(U64 &key, const CastlingRights rights)
{
// Remove them in the opposite order they were added in
if (rights & CASTLE_BLACK_QUEEN)
key ^= s_CastlingRights[3];
if (rights & CASTLE_BLACK_KING)
key ^= s_CastlingRights[2];

if (rights & CASTLE_WHITE_QUEEN)
key ^= s_CastlingRights[1];
if (rights & CASTLE_WHITE_KING)
key ^= s_CastlingRights[0];
}
12 changes: 6 additions & 6 deletions ChessAndroid/app/src/main/cpp/chess/algorithm/Hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ using U64 = std::uint64_t;

class Hash final
{
private:
using HashArray = std::array<std::array<U64, 12>, 64>;
using HashArray = std::array<std::array<std::array<U64, 6>, 2>, 64>;

static HashArray s_Pieces;
static U64 s_WhiteToMove;
Expand All @@ -21,6 +20,9 @@ class Hash final
Hash(const Hash&) = delete;
Hash(Hash&&) = delete;

Hash &operator=(const Hash&) = delete;
Hash &operator=(Hash&&) = delete;

static void init();
static U64 movePiece(byte sq, const Piece &piece);
static U64 compute(const Board &board);
Expand All @@ -29,8 +31,6 @@ class Hash final
static void promotePawn(U64 &key, byte sq, Color color, PieceType promotedType);
static void xorPiece(U64 &key, byte sq, const Piece &piece);
static void flipSide(U64 &key);
static void xorCastlingRights(U64 &key, CastlingRights rights);

private:
static byte indexOf(const Piece &piece);
static void addCastlingRights(U64 &key, CastlingRights rights);
static void removeCastlingRights(U64 &key, CastlingRights rights);
};
12 changes: 9 additions & 3 deletions ChessAndroid/app/src/main/cpp/chess/algorithm/MoveGen.inl
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@
#include "../data/Bitboard.h"

template <GenType T>
U64 MoveGen<T>::generatePawnMoves(const Piece &piece, byte square, const Board &board)
U64 MoveGen<T>::generatePawnMoves(const Piece &piece, const byte square, const Board &board)
{
U64 attacks = PieceAttacks::getPawnAttacks(piece.isWhite, square);

if constexpr (T == ALL || T == CAPTURES)
{
U64 captures = attacks & board.allPieces[!piece.isWhite];

/*if (board.enPassantSq < 64)
{
Pos capturedPos(board.enPassantSq);
capturedPos.y += static_cast<byte>(piece.isWhite ? -1 : 1);
// Keep the en-passant capture if it intersect with one of our potential attacks
captures |= attacks & Bitboard::shiftedBoards[board.enPassantSq];
}*/

// Keep the en-passant capture if it intersect with one of our potential attacks
captures |= attacks & board.enPassant;
attacks = captures;
}
else if (T == ATTACKS_DEFENSES)
Expand Down
4 changes: 3 additions & 1 deletion ChessAndroid/app/src/main/cpp/chess/algorithm/Search.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#pragma once

#include "../containers/TranspositionTable.h"
#include "../threads/ThreadPool.hpp"

class Board;
class RootMove;
class Settings;

class Search final
{
static bool s_QuiescenceSearchEnabled;
static ThreadPool s_ThreadPool;
static TranspositionTable s_SearchCache;
static bool s_QuiescenceSearchEnabled;
static short s_BestMoveFound;

public:
Expand Down
50 changes: 30 additions & 20 deletions ChessAndroid/app/src/main/cpp/chess/data/Board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ void Board::doMove(const byte startSq, const byte destSq, const bool updateState
}
else
{
enPassant = 0ull;
enPassantSq = 64u;
if (startPiece.type == PieceType::ROOK)
moveRook(startSq);
else if (startPiece.type == PieceType::KING)
Expand Down Expand Up @@ -274,28 +274,32 @@ bool Board::movePawn(const byte startSq, const byte destSq)
return true;
}

if (destSq == enPassant) {
isCapture = true;
if (enPassantSq < 64)
{
if (destSq == enPassantSq)
{
isCapture = true;

Pos capturedPos(enPassant);
capturedPos.y += static_cast<byte>(pawn.isWhite ? -1 : 1);
Piece &capturedPiece = getPiece(capturedPos.toSquare());
Pos capturedPos(enPassantSq);
capturedPos.y += static_cast<byte>(pawn.isWhite ? -1 : 1);
Piece &capturedPiece = getPiece(capturedPos.toSquare());

// Remove the captured Pawn
Hash::xorPiece(zKey, capturedPos.toSquare(), capturedPiece);
capturedPiece = Piece();
getType(toColor(capturedPiece.isWhite), capturedPiece.type);
return true;
// Remove the captured Pawn
Hash::xorPiece(zKey, capturedPos.toSquare(), capturedPiece);
getType(toColor(capturedPiece.isWhite), capturedPiece.type) &= ~capturedPos.toBitboard();
capturedPiece = Piece();
return true;
}
}

const int distance = static_cast<int>(row(destSq)) - static_cast<int>(row(startSq));
if (distance == 2 || distance == -2)
{
Pos newEnPassant(destSq);
newEnPassant.y -= static_cast<byte>(distance / 2);
enPassant = newEnPassant.toSquare();
enPassantSq = newEnPassant.toSquare();
} else
enPassant = 0ull;
enPassantSq = 64u;

return false;
}
Expand All @@ -304,15 +308,19 @@ void Board::moveRook(const byte startSq)
{
const bool isPieceWhite = getPiece(startSq).isWhite;

Hash::removeCastlingRights(zKey, static_cast<CastlingRights>(castlingRights));

if (col(startSq) == 0u)
castlingRights &= isPieceWhite ? ~CASTLE_WHITE_QUEEN : CASTLE_BLACK_QUEEN;
castlingRights &= ~(isPieceWhite ? CASTLE_WHITE_QUEEN : CASTLE_BLACK_QUEEN);
else if (col(startSq) == 7u)
castlingRights &= isPieceWhite ? ~CASTLE_WHITE_KING : CASTLE_BLACK_KING;
castlingRights &= ~(isPieceWhite ? ~CASTLE_WHITE_KING : CASTLE_BLACK_KING);

Hash::addCastlingRights(zKey, static_cast<CastlingRights>(castlingRights));
}

void Board::moveKing(const Piece &king, const byte startSq, const byte destSq)
{
if (!(king & (CastlingRights::CASTLE_WHITE | CastlingRights::CASTLE_BLACK))) return;
if (!canCastle(toColor(king.isWhite))) return;

const Color color = toColor(king.isWhite);
bool castled = false;
Expand All @@ -327,13 +335,13 @@ void Board::moveKing(const Piece &king, const byte startSq, const byte destSq)
{
constexpr byte destX = 5;
getPiece(destX, y) = rook;
rook = Piece::EMPTY;

getType(color, ROOK) &= ~Pos(startX, y).toBitboard();
getType(color, ROOK) |= Pos(destX, y).toBitboard();

castled = true;
Hash::makeMove(zKey, Pos(startX, y).toSquare(), Pos(destX, y).toSquare(), rook);
rook = Piece::EMPTY;
}
}
else if (col(destSq) == 2u)
Expand All @@ -346,19 +354,20 @@ void Board::moveKing(const Piece &king, const byte startSq, const byte destSq)
{
constexpr byte destX = 3u;
getPiece(destX, y) = rook;
rook = Piece::EMPTY;

getType(color, ROOK) &= ~Pos(startX, y).toBitboard();
getType(color, ROOK) |= Pos(destX, y).toBitboard();

castled = true;
Hash::makeMove(zKey, Pos(startX, y).toSquare(), Pos(destX, y).toSquare(), rook);
rook = Piece::EMPTY;
}
}

if (castled)
{
Hash::xorCastlingRights(zKey, static_cast<CastlingRights>(castlingRights));
Hash::removeCastlingRights(zKey, static_cast<CastlingRights>(castlingRights));

if (king.isWhite)
{
castlingRights &= ~CASTLE_WHITE;
Expand All @@ -367,7 +376,8 @@ void Board::moveKing(const Piece &king, const byte startSq, const byte destSq)
castlingRights &= ~CASTLE_BLACK;
castlingRights |= CASTLED_BLACK;
}
Hash::xorCastlingRights(zKey, static_cast<CastlingRights>(castlingRights));

Hash::addCastlingRights(zKey, static_cast<CastlingRights>(castlingRights));
}
}

Expand Down
2 changes: 1 addition & 1 deletion ChessAndroid/app/src/main/cpp/chess/data/Board.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Board final
bool isCapture = false;
byte castlingRights = CastlingRights::CASTLE_WHITE | CastlingRights::CASTLE_BLACK;
byte halfMoveClock{};
U64 enPassant{};
byte enPassantSq{};
U64 occupied{};
std::array<U64, 2> allPieces{};
std::array<std::array<U64, 7>, 2> pieces{};
Expand Down
2 changes: 1 addition & 1 deletion ChessAndroid/app/src/main/cpp/chess/data/Enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ enum Color : bool

constexpr Color toColor(const bool isWhite)
{
return static_cast<Color>(isWhite);
return isWhite ? WHITE : BLACK;
}

constexpr Color oppositeColor(const Color color)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ void FenParser::parseFen(const std::string &fen)

// TODO: En passant target square
fenStream >> token;
board.enPassantSq = 64u;
//board.enPassant = token == "-" ? 0ull : 1 << x;


Expand Down