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
Merge Rays.h into Bitboard.h
  • Loading branch information
razvanfilea committed Dec 11, 2019
commit 459400d4ff5598fd82d2a3dd8dfab0fd599fe7de
23 changes: 11 additions & 12 deletions ChessAndroid/app/src/main/cpp/chess/algorithm/Evaluation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "MoveGen.h"
#include "../Stats.h"
#include "../data/Psqt.h"
#include "../data/Rays.h"

#define S Score

Expand Down Expand Up @@ -191,19 +190,21 @@ short Evaluation::evaluate(const Board &board) noexcept
Score Evaluation::evaluatePawn(const Piece &piece, const byte square, const Board &board,
const AttacksMap &ourAttacks, const AttacksMap &theirAttacks) noexcept
{
using namespace Bitboard;

const Color oppositeColor = ~piece.color;
const Pos pos(square);
Score value = Psqt::s_Bonus[PAWN][square];

const Rays::Dir forwardDir = piece.color ? Rays::NORTH : Rays::SOUTH;
const Dir forwardDir = piece.color ? NORTH : SOUTH;
const byte rank = (piece.color ? pos.y : 7u - pos.y) - 1u;
const byte behind = piece.color ? -1 : 1;

const U64 adjacentFiles = Rays::getAdjacentFiles(square);
const U64 opposed = board.getType(oppositeColor, PAWN) & Rays::getRay(forwardDir, square);
const U64 adjacentFiles = getAdjacentFiles(square);
const U64 opposed = board.getType(oppositeColor, PAWN) & getRay(forwardDir, square);
const U64 neighbours = board.getType(piece.color, PAWN) & adjacentFiles;
const U64 connected = neighbours & Rays::getRank(square);
const U64 support = neighbours & Rays::getRank(toSquare(pos.x + behind, pos.y));
const U64 connected = neighbours & getRank(square);
const U64 support = neighbours & getRank(toSquare(pos.x + behind, pos.y));
const bool isDoubled = board.at(pos.x, pos.y - behind) == piece;

if (support | connected)
Expand All @@ -230,9 +231,9 @@ Score Evaluation::evaluatePawn(const Piece &piece, const byte square, const Boar
}

{ // Passed Pawn
U64 rays = Rays::getRay(forwardDir, square);
const U64 adjacentRays = Rays::shift<Rays::WEST>(rays) | Rays::shift<Rays::EAST>(rays);
rays |= piece.color ? Rays::shift<Rays::NORTH>(adjacentRays) : Rays::shift<Rays::SOUTH>(adjacentRays);
U64 rays = getRay(forwardDir, square);
const U64 adjacentRays = shift<WEST>(rays) | shift<EAST>(rays);
rays |= piece.color ? shift<NORTH>(adjacentRays) : shift<SOUTH>(adjacentRays);

const bool isPassedPawn = !static_cast<bool>(rays & board.getType(oppositeColor, PAWN));

Expand Down Expand Up @@ -295,9 +296,7 @@ Score Evaluation::evaluateRook(const Piece &piece, const byte square, const Boar
const int rookOnPawn = [&] {
if ((piece.color && pos.y < 5) || (!piece.color && pos.y > 4)) return 0;

const U64 vertical = Rays::getRay(Rays::NORTH, square) | Rays::getRay(Rays::SOUTH, square);
const U64 horizontal = Rays::getRay(Rays::WEST, square) | Rays::getRay(Rays::EAST, square);
const U64 rays = vertical | horizontal;
const U64 rays = Bitboard::getRank(square) | Bitboard::getFile(square);

return Bitboard::popCount(rays & board.getType(~piece.color, PAWN));
}();
Expand Down
22 changes: 11 additions & 11 deletions ChessAndroid/app/src/main/cpp/chess/algorithm/MoveGen.inl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
template <GenType T>
U64 MoveGen<T>::generatePawnMoves(const Piece &piece, const byte square, const Board &board)
{
using namespace Bitboard;
U64 attacks = PieceAttacks::getPawnAttacks(piece.color, square);

if constexpr (T == ALL || T == CAPTURES)
Expand All @@ -20,7 +21,8 @@ U64 MoveGen<T>::generatePawnMoves(const Piece &piece, const byte square, const B
Pos capturedPos(board.enPassantSq);
capturedPos.y += static_cast<byte>(piece.color ? -1 : 1);
// Keep the en-passant capture if it intersect with one of our potential attacks
captures |= attacks & Bitboard::shiftedBoards[board.enPassantSq];
if (attacks & Bitboard::shiftedBoards[capturedPos.toSquare()])
captures |= Bitboard::shiftedBoards[board.enPassantSq];
}

attacks = captures;
Expand All @@ -30,22 +32,20 @@ U64 MoveGen<T>::generatePawnMoves(const Piece &piece, const byte square, const B

if constexpr (T == ALL)
{
const U64 initialBb = Bitboard::shiftedBoards[square];
Pos pos(square);
piece.color ? pos.y++ : pos.y--;
const U64 bitboard = Bitboard::shiftedBoards[square];
const U64 move = piece.color ? shift<NORTH>(bitboard) : shift<SOUTH>(bitboard);

if (!board[pos])
if (!(board.occupied & move))
{
attacks |= pos.toBitboard();
attacks |= move;

const U64 initialRank = piece.color ? RANK_2 : RANK_7;
if (initialRank & initialBb)
if (initialRank & bitboard)
{
byte y = pos.y;
const U64 doubleMove = piece.color ? shift<NORTH>(bitboard) : shift<SOUTH>(bitboard);

piece.color ? y++ : y--;
if (y < 8u && !board.getPiece(pos.x, y))
attacks |= Pos(pos.x, y).toBitboard();
if (!(board.occupied & doubleMove))
attacks |= move;
}
}
}
Expand Down
79 changes: 38 additions & 41 deletions ChessAndroid/app/src/main/cpp/chess/algorithm/PieceAttacks.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include "PieceAttacks.h"

#include "../data/Pos.h"
#include "../data/Rays.h"

constexpr std::array<byte, 64> rookIndexBits = {
constexpr std::array<byte, SQUARE_NB> rookIndexBits = {
12, 11, 11, 11, 11, 11, 11, 12,
11, 10, 10, 10, 10, 10, 10, 11,
11, 10, 10, 10, 10, 10, 10, 11,
Expand All @@ -14,7 +13,7 @@ constexpr std::array<byte, 64> rookIndexBits = {
12, 11, 11, 11, 11, 11, 11, 12
};

constexpr std::array<byte, 64> bishopIndexBits = {
constexpr std::array<byte, SQUARE_NB> bishopIndexBits = {
6, 5, 5, 5, 5, 5, 5, 6,
5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 7, 7, 7, 7, 5, 5,
Expand Down Expand Up @@ -65,29 +64,33 @@ constexpr std::array<U64, 64> rookMagics = {
* These do not include edge squares
*/
constexpr auto bishopMasks = [] {
using namespace Bitboard;

const U64 edgeSquares = FILE_A | FILE_H | RANK_1 | RANK_8;
std::array<U64, 64> masks{};

for (byte square = 0u; square < 64u; ++square)
for (byte square = 0u; square < SQUARE_NB; ++square)
{
masks[square] = (Rays::getRay(Rays::NORTH_EAST, square)
| Rays::getRay(Rays::NORTH_WEST, square)
| Rays::getRay(Rays::SOUTH_WEST, square)
| Rays::getRay(Rays::SOUTH_EAST, square)) & ~(edgeSquares);
masks[square] = (getRay(NORTH_EAST, square)
| getRay(NORTH_WEST, square)
| getRay(SOUTH_WEST, square)
| getRay(SOUTH_EAST, square)) & ~(edgeSquares);
}

return masks;
}();

constexpr auto rookMasks = [] {
using namespace Bitboard;

std::array<U64, 64> masks{};

for (byte square = 0u; square < 64u; ++square)
{
masks[square] = (Rays::getRay(Rays::NORTH, square) & ~RANK_8)
| (Rays::getRay(Rays::SOUTH, square) & ~RANK_1)
| (Rays::getRay(Rays::EAST, square) & ~FILE_H)
| (Rays::getRay(Rays::WEST, square) & ~FILE_A);
masks[square] = (getRay(NORTH, square) & ~RANK_8)
| (getRay(SOUTH, square) & ~RANK_1)
| (getRay(EAST, square) & ~FILE_H)
| (getRay(WEST, square) & ~FILE_A);
}

return masks;
Expand All @@ -108,44 +111,38 @@ U64 getBlockersFromIndex(const int index, U64 mask)
return blockers;
}

U64 getRayAttacksForwards(const byte square, const U64 occupied, const Rays::Dir direction)
U64 getRayAttacksForwards(const byte square, const U64 occupied, const Dir direction)
{
const U64 attacks = Rays::getRay(direction, square);
const U64 attacks = Bitboard::getRay(direction, square);
const U64 blocker = attacks & occupied;
const byte index = Bitboard::bitScanForward(blocker | 0x8000000000000000);
return attacks ^ Rays::getRay(direction, index);
return attacks ^ Bitboard::getRay(direction, index);
}

U64 getRayAttacksBackwards(const byte square, const U64 occupied, const Rays::Dir direction)
U64 getRayAttacksBackwards(const byte square, const U64 occupied, const Dir direction)
{
const U64 attacks = Rays::getRay(direction, square);
const U64 attacks = Bitboard::getRay(direction, square);
const U64 blocker = attacks & occupied;
const byte index = Bitboard::bitScanReverse(blocker | 1ULL);
return attacks ^ Rays::getRay(direction, index);
return attacks ^ Bitboard::getRay(direction, index);
}

U64 generateBishopAttacks(const byte square, const U64 blockers) noexcept
{
U64 attacks{};

attacks |= getRayAttacksForwards(square, blockers, Rays::NORTH_WEST);
attacks |= getRayAttacksForwards(square, blockers, Rays::NORTH_EAST);
attacks |= getRayAttacksBackwards(square, blockers, Rays::SOUTH_EAST);
attacks |= getRayAttacksBackwards(square, blockers, Rays::SOUTH_WEST);

return attacks;
using namespace Bitboard;
return getRayAttacksForwards(square, blockers, NORTH_WEST)
| getRayAttacksForwards(square, blockers, NORTH_EAST)
| getRayAttacksBackwards(square, blockers, SOUTH_EAST)
| getRayAttacksBackwards(square, blockers, SOUTH_WEST);
}

U64 generateRookAttacks(const byte square, const U64 blockers) noexcept
{
U64 attacks{};

attacks |= getRayAttacksForwards(square, blockers, Rays::NORTH);
attacks |= getRayAttacksForwards(square, blockers, Rays::EAST);
attacks |= getRayAttacksBackwards(square, blockers, Rays::SOUTH);
attacks |= getRayAttacksBackwards(square, blockers, Rays::WEST);

return attacks;
using namespace Bitboard;
return getRayAttacksForwards(square, blockers, NORTH)
| getRayAttacksForwards(square, blockers, EAST)
| getRayAttacksBackwards(square, blockers, SOUTH)
| getRayAttacksBackwards(square, blockers, WEST);
}

const std::array<std::array<U64, 64>, 2> PieceAttacks::s_PawnAttacks = [] {
Expand Down Expand Up @@ -212,12 +209,12 @@ const std::array<U64, 64> PieceAttacks::s_KnightAttacks = [] {
return moves;
}();

std::array<std::array<U64, 1024>, 64> PieceAttacks::s_BishopAttacks{};
std::array<std::array<U64, 1024>, SQUARE_NB> PieceAttacks::s_BishopAttacks{};

std::array<std::array<U64, 4096>, 64> PieceAttacks::s_RookAttacks{};
std::array<std::array<U64, 4096>, SQUARE_NB> PieceAttacks::s_RookAttacks{};

const std::array<U64, 64> PieceAttacks::s_KingAttacks = [] {
std::array<U64, 64> moves{};
const std::array<U64, SQUARE_NB> PieceAttacks::s_KingAttacks = [] {
std::array<U64, SQUARE_NB> moves{};

const auto addAttack = [&](const byte startSquare, const byte x, const byte y) {
const Pos pos(col(startSquare) + x, row(startSquare) + y);
Expand All @@ -226,7 +223,7 @@ const std::array<U64, 64> PieceAttacks::s_KingAttacks = [] {
moves[startSquare] |= pos.toBitboard();
};

for (byte i = 0u; i < 64u; i++)
for (byte i = 0u; i < SQUARE_NB; i++)
{
// Vertical and Horizontal
addAttack(i, -1, 0u);
Expand All @@ -251,7 +248,7 @@ void PieceAttacks::init() noexcept
initialized = true;

// Init Bishop Moves
for (byte square = 0u; square < 64u; square++)
for (byte square = 0u; square < SQUARE_NB; square++)
{
const int indexBit = bishopIndexBits[square];

Expand All @@ -267,7 +264,7 @@ void PieceAttacks::init() noexcept
}

// Init Rook Moves
for (byte square = 0u; square < 64u; square++)
for (byte square = 0u; square < SQUARE_NB; square++)
{
const int indexBit = rookIndexBits[square];

Expand Down
Loading