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
Rewrite Piece class to only use 1 byte
  • Loading branch information
razvanfilea committed Dec 15, 2019
commit b81a38ca91b3a2ed712118eabf33ea0971876a57
4 changes: 2 additions & 2 deletions ChessAndroid/app/src/main/cpp/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ Java_net_theluckycoder_chess_Native_getPieces(JNIEnv *pEnv, jclass /*type*/)
for (const auto &it : pieces)
{
const Pos &pos = it.first;
auto pieceType = static_cast<jbyte>(it.second.type);
if (it.second.color == BLACK)
auto pieceType = static_cast<jbyte>(it.second.type());
if (it.second.color() == BLACK)
pieceType += 6;

jobject obj = pEnv->NewObject(Cache::pieceClass, constructorId, pos.x, pos.y, pieceType);
Expand Down
95 changes: 51 additions & 44 deletions ChessAndroid/app/src/main/cpp/chess/algorithm/Evaluation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ constexpr S PASSED_PAWN_RANK[] =
{
S(0, 0), S(10, 28), S(17, 33), S(15, 41), S(62, 72), S(168, 177), S(276, 260)
};
constexpr S ROOK_ON_QUEEN_FILE(7, 6);
constexpr S ROOK_ON_PAWN(10, 28);
constexpr S ROOK_ON_FILE[] =
{
Expand Down Expand Up @@ -80,7 +81,7 @@ short Evaluation::simpleEvaluation(const Board &board) noexcept
for (byte i = 0; i < SQUARE_NB; i++)
{
const Piece &piece = board.getPiece(i);
score[piece.color] += Psqt::s_Bonus[piece.type][i];
score[piece.color()] += Psqt::s_Bonus[piece.type()][i];
}

const Phase phase = board.getPhase();
Expand Down Expand Up @@ -108,38 +109,38 @@ short Evaluation::evaluate(const Board &board) noexcept
const AttacksMap blackMoves = Player::getAttacksPerColor(false, board);

for (byte i = 0u; i < SQUARE_NB; i++)
if (const auto &piece = board.getPiece(i); piece && piece.type != KING)
if (const auto &piece = board.getPiece(i); piece && piece.type() != KING)
{
if (piece.type == PAWN)
const Color color = piece.color();
if (piece.type() == PAWN)
{
pawnCount[piece.color]++;
pawnCount[color]++;
continue;
}

const bool isWhite = piece.color;
const Pos pos(i);
const auto defendedValue = isWhite ? whiteMoves.map[pos] : blackMoves.map[pos];
const auto attackedValue = isWhite ? blackMoves.map[pos] : whiteMoves.map[pos];
const auto defendedValue = color ? whiteMoves.map[pos] : blackMoves.map[pos];
const auto attackedValue = color ? blackMoves.map[pos] : whiteMoves.map[pos];
Score points;

if (defendedValue < attackedValue)
points -= OVERLOAD * (attackedValue - defendedValue);

if (piece.type == BISHOP)
bishopCount[isWhite]++;
if (piece.type() == BISHOP)
bishopCount[color]++;

const auto &theirAttacks = isWhite ? whiteMoves.board : blackMoves.board;
const auto &theirAttacks = color ? whiteMoves.board : blackMoves.board;
const U64 bb = pos.toBitboard();

for (byte i = KNIGHT; i < BISHOP; i++)
if (theirAttacks[!isWhite][i] & bb)
points -= MINOR_THREATS[piece.type];
if (theirAttacks[!color][i] & bb)
points -= MINOR_THREATS[piece.type()];

if (theirAttacks[!isWhite][ROOK] & bb)
points -= ROOK_THREATS[piece.type];
if (theirAttacks[!color][ROOK] & bb)
points -= ROOK_THREATS[piece.type()];

npm[piece.color] += getPieceValue(piece.type);
totalScore[piece.color] += points;
npm[color] += getPieceValue(piece.type());
totalScore[color] += points;
}

// 2 Bishops receive a bonus
Expand All @@ -152,11 +153,11 @@ short Evaluation::evaluate(const Board &board) noexcept
if (const Piece &piece = board.getPiece(i); piece)
{
const Score points = [&] {
switch (piece.type)
switch (piece.type())
{
case PAWN:
return evaluatePawn(piece, i, board, piece.color ? whiteMoves : blackMoves,
piece.color ? blackMoves : whiteMoves);
return evaluatePawn(piece, i, board, piece.color() ? whiteMoves : blackMoves,
piece.color() ? blackMoves : whiteMoves);
case KNIGHT:
return evaluateKnight(piece, i, board);
case BISHOP:
Expand All @@ -172,7 +173,7 @@ short Evaluation::evaluate(const Board &board) noexcept
}
}();

totalScore[piece.color] += points;
totalScore[piece.color()] += points;
}

if (board.state == State::BLACK_IN_CHECK)
Expand All @@ -192,25 +193,26 @@ Score Evaluation::evaluatePawn(const Piece &piece, const byte square, const Boar
{
using namespace Bitboard;

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

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 Dir forwardDir = color ? NORTH : SOUTH;
const byte rank = (color ? pos.y : 7u - pos.y) - 1u;
const byte behind = color ? -1 : 1;

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 opposed = board.getType(~piece) & getRay(forwardDir, square);
const U64 neighbours = board.getType(piece) & adjacentFiles;
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)
{
const int connectedScore = PAWN_CONNECTED[rank] * (2 + bool(connected) - bool(opposed))
+ 21 * Bitboard::popCount(support);
+ 21 * Bitboard::popCount(support);

value += Score(connectedScore, connectedScore * (rank - 2) / 4);
} else if (!neighbours)
Expand All @@ -222,7 +224,7 @@ Score Evaluation::evaluatePawn(const Piece &piece, const byte square, const Boar
// Threat Safe Pawn
if (ourAttacks.map[square] || theirAttacks.map[square] == 0) // check if the pawn is safe
{
const U64 enemyPieces = MoveGen<CAPTURES>::generatePawnMoves(piece, square, board)
const U64 enemyPieces = MoveGen<CAPTURES>::getPawnMoves(color, square, board)
& ~(board.getType(oppositeColor, PAWN) | board.getType(oppositeColor, KING));

const int count = Bitboard::popCount(enemyPieces);
Expand All @@ -233,7 +235,7 @@ Score Evaluation::evaluatePawn(const Piece &piece, const byte square, const Boar
{ // Passed Pawn
U64 rays = getRay(forwardDir, square);
const U64 adjacentRays = shift<WEST>(rays) | shift<EAST>(rays);
rays |= piece.color ? shift<NORTH>(adjacentRays) : shift<SOUTH>(adjacentRays);
rays |= color ? shift<NORTH>(adjacentRays) : shift<SOUTH>(adjacentRays);

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

Expand All @@ -246,26 +248,28 @@ Score Evaluation::evaluatePawn(const Piece &piece, const byte square, const Boar

inline Score Evaluation::evaluateKnight(const Piece &piece, const byte square, const Board &board) noexcept
{
const Color color = piece.color();
Score value = Psqt::s_Bonus[KNIGHT][square];

const int mobility = Bitboard::popCount(MoveGen<ALL>::generateKnightMoves(piece, square, board));
const int mobility = Bitboard::popCount(MoveGen<ALL>::getKnightMoves(color, square, board));
value += KNIGHT_MOBILITY[mobility];

return value;
}

Score Evaluation::evaluateBishop(const Piece &piece, const byte square, const Board &board) noexcept
{
const Color color = piece.color();
Score value = Psqt::s_Bonus[BISHOP][square];

const int mobility = Bitboard::popCount(MoveGen<ALL>::generateBishopMoves(piece, square, board));
const int mobility = Bitboard::popCount(MoveGen<ALL>::getBishopMoves(color, square, board));
value += BISHOP_MOBILITY[mobility];

constexpr U64 centerFiles = FILE_D | FILE_E;
constexpr U64 center = centerFiles & (RANK_4 | RANK_5);

// Long Diagonal Bishop
const U64 centerAttacks = PieceAttacks::getBishopAttacks(square, board.getType(~piece.color, PAWN)) & center;
const U64 centerAttacks = PieceAttacks::getBishopAttacks(square, board.getType(~piece.color(), PAWN)) & center;
if (Bitboard::popCount(centerAttacks) > 1)
value.mg += 45;

Expand All @@ -274,18 +278,19 @@ Score Evaluation::evaluateBishop(const Piece &piece, const byte square, const Bo

Score Evaluation::evaluateRook(const Piece &piece, const byte square, const Board &board) noexcept
{
const Color color = piece.color();
const Pos pos(square);
Score value = Psqt::s_Bonus[ROOK][pos.toSquare()];

const int mobility = Bitboard::popCount(MoveGen<ALL>::generateRookMoves(piece, pos.toSquare(), board));
const int mobility = Bitboard::popCount(MoveGen<ALL>::getRookMoves(color, pos.toSquare(), board));
value += ROOK_MOBILITY[mobility];

const int rookOnPawn = [&] {
if ((piece.color && pos.y < 5) || (!piece.color && pos.y > 4)) return 0;
if ((color && pos.y < 5) || (!color && pos.y > 4)) return 0;

const U64 rays = Bitboard::getRank(square) | Bitboard::getFile(square);

return Bitboard::popCount(rays & board.getType(~piece.color, PAWN));
return Bitboard::popCount(rays & board.getType(~color, PAWN));
}();
value += ROOK_ON_PAWN * rookOnPawn;

Expand All @@ -294,8 +299,8 @@ Score Evaluation::evaluateRook(const Piece &piece, const byte square, const Boar

for (byte y = 0; y < 8; y++)
{
const auto &other = board.getPiece(pos.x, y);
if (other.type == PAWN)
const Piece &other = board.getPiece(pos.x, y);
if (other.type() == PAWN)
{
if (piece.isSameColor(other))
return 0;
Expand All @@ -307,7 +312,7 @@ Score Evaluation::evaluateRook(const Piece &piece, const byte square, const Boar
}();
value += ROOK_ON_FILE[rookOnFile];

const U64 queens = board.getType(piece.color, QUEEN) | board.getType(~piece.color, QUEEN);
const U64 queens = board.getType(piece.color(), QUEEN) | board.getType(~piece.color(), QUEEN);
if (Bitboard::getFile(square) & queens)
value += ROOK_ON_QUEEN_FILE;

Expand All @@ -316,13 +321,14 @@ Score Evaluation::evaluateRook(const Piece &piece, const byte square, const Boar

Score Evaluation::evaluateQueen(const Piece &piece, const byte square, const Board &board) noexcept
{
const Color color = piece.color();
const Pos pos(square);
Score value = Psqt::s_Bonus[QUEEN][square];

const int mobility = Bitboard::popCount(MoveGen<ALL>::generateQueenMoves(piece, square, board));
const int mobility = Bitboard::popCount(MoveGen<ALL>::getQueenMoves(color, square, board));
value += QUEEN_MOBILITY[mobility];

if (const U64 initialPosition = FILE_D & (piece.color ? RANK_1 : RANK_8);
if (const U64 initialPosition = FILE_D & (color ? RANK_1 : RANK_8);
(initialPosition & Bitboard::shiftedBoards[square]) == 0ull)
value.mg -= 30;

Expand All @@ -336,8 +342,8 @@ Score Evaluation::evaluateQueen(const Piece &piece, const byte square, const Boa
if (dPos.isValid())
{
const Piece &other = board.getPiece(dPos.toSquare());
if (other.type == ROOK && (ix == 0 || iy == 0) && count == 1) return true;
if (other.type == BISHOP && (ix != 0 && iy != 0) && count == 1) return true;
if (other.type() == ROOK && (ix == 0 || iy == 0) && count == 1) return true;
if (other.type() == BISHOP && (ix != 0 && iy != 0) && count == 1) return true;
if (other) count++;
}
else
Expand All @@ -355,11 +361,12 @@ Score Evaluation::evaluateQueen(const Piece &piece, const byte square, const Boa
inline Score Evaluation::evaluateKing(const Piece &piece, const byte square, const Board &board) noexcept
{
Score value = Psqt::s_Bonus[KNIGHT][square];
const Color color = piece.color();

if (board.isCastled(piece.color))
if (board.isCastled(color))
value.mg += 57;
else {
const int count = int(board.canCastleKs(piece.color)) + int(board.canCastleQs(piece.color));
const short count = short(board.canCastleKs(color)) + short(board.canCastleQs(color));
value.mg += count * 20;
}

Expand Down
6 changes: 3 additions & 3 deletions ChessAndroid/app/src/main/cpp/chess/algorithm/Hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void Hash::init()

U64 Hash::movePiece(const byte sq, const Piece &piece)
{
return s_Pieces[sq][piece.color][piece.type];
return s_Pieces[sq][piece.color()][piece.type()];
}

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

for (byte i = 0; i < SQUARE_NB; ++i)
if (const Piece &piece = board.getPiece(i); piece)
hash ^= s_Pieces[i][piece.color][piece.type];
hash ^= s_Pieces[i][piece.color()][piece.type()];

if (board.colorToMove)
hash ^= s_WhiteToMove;
Expand Down Expand Up @@ -73,7 +73,7 @@ void Hash::promotePawn(U64 &key, const byte sq, const Color color, const PieceTy

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

void Hash::flipSide(U64 &key)
Expand Down
12 changes: 6 additions & 6 deletions ChessAndroid/app/src/main/cpp/chess/algorithm/MoveGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ class MoveGen final
MoveGen(const MoveGen&) = delete;
MoveGen(MoveGen&&) = delete;

static U64 generatePawnMoves(const Piece &piece, byte square, const Board &board);
static U64 generateKnightMoves(const Piece &piece, byte square, const Board &board);
static U64 generateBishopMoves(const Piece &piece, byte square, const Board &board);
static U64 generateRookMoves(const Piece &piece, byte square, const Board &board);
static U64 generateQueenMoves(const Piece &piece, byte square, const Board &board);
static U64 generateKingMoves(const Piece &piece, byte square, const Board &board);
static U64 getPawnMoves(Color color, byte square, const Board &board) noexcept;
static U64 getKnightMoves(Color color, byte square, const Board &board) noexcept;
static U64 getBishopMoves(Color color, byte square, const Board &board) noexcept;
static U64 getRookMoves(Color color, byte square, const Board &board) noexcept;
static U64 getQueenMoves(Color color, byte square, const Board &board) noexcept;
static U64 getKingMoves(Color color, byte square, const Board &board) noexcept;
};

#include "MoveGen.inl"
Loading