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
Fix Board::hasValidState and update Pawn PSQT
  • Loading branch information
razvanfilea committed Nov 26, 2019
commit bee2ee125319abbc8117e2346e3ff19bf8c80cb4
3 changes: 0 additions & 3 deletions ChessAndroid/app/src/main/cpp/chess/BoardManager.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#include "BoardManager.h"

#include <cassert>

#include "Stats.h"
#include "data/Board.h"
#include "algorithm/Evaluation.h"
#include "algorithm/Hash.h"
#include "algorithm/NegaMax.h"
#include "algorithm/PieceAttacks.h"
Expand Down
86 changes: 31 additions & 55 deletions ChessAndroid/app/src/main/cpp/chess/algorithm/Evaluation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,47 +63,26 @@ constexpr S QUEEN_MOBILITY[] =

short Evaluation::simpleEvaluation(const Board &board) noexcept
{
if (board.state == State::DRAW)
if (board.state == State::INVALID || board.state == State::DRAW)
return 0;
if (board.state == State::WINNER_WHITE)
return VALUE_WINNER_WHITE;
if (board.state == State::WINNER_BLACK)
return VALUE_WINNER_BLACK;

short score{};
short score[2]{};
for (byte i = 0; i < 64u; i++)
if (const Piece &piece = board.getPiece(i); piece)
{
const short points = [&]() -> short {
switch (piece.type)
{
case PAWN:
return Psqt::s_PawnSquares[i].mg;
case KNIGHT:
return Psqt::s_KnightSquares[i].mg;
case BISHOP:
return Psqt::s_BishopSquares[i].mg;
case ROOK:
return Psqt::s_RookSquares[i].mg;
case QUEEN:
return Psqt::s_QueenSquares[i].mg;
case KING:
return Psqt::s_KingSquares[i].mg;
default:
return 0;
}
}();

if (piece.isWhite) score += points; else score -= points;
}
{
const Piece &piece = board.getPiece(i);
score[piece.isWhite] += Psqt::s_Bonus[piece.type][i].mg;
}

return score;
return score[WHITE] - score[BLACK];
}

short Evaluation::evaluate(const Board &board) noexcept
{
assert(board.state != State::INVALID);
if (board.state == State::DRAW)
if (board.state == State::INVALID || board.state == State::DRAW)
return 0;
if (board.state == State::WINNER_WHITE)
return VALUE_WINNER_WHITE;
Expand Down Expand Up @@ -187,7 +166,7 @@ short Evaluation::evaluate(const Board &board) noexcept
case QUEEN:
return evaluateQueen(piece, i, board);
case KING:
return evaluateKing(i);
return evaluateKing(piece, i, board);
default:
return Score();
}
Expand All @@ -196,20 +175,6 @@ short Evaluation::evaluate(const Board &board) noexcept
if (piece.isWhite) totalScore += points; else totalScore -= points;
}

if (board.isCastled(WHITE))
totalScore.mg += 57;
else {
const int count = static_cast<int>(board.canCastleKs(WHITE)) + static_cast<int>(board.canCastleQs(WHITE));
totalScore.mg += count * 23;
}

if (board.isCastled(BLACK))
totalScore.mg -= 57;
else {
const int count = static_cast<int>(board.canCastleKs(BLACK)) + static_cast<int>(board.canCastleQs(BLACK));
totalScore.mg -= count * 23;
}

if (board.state == State::BLACK_IN_CHECK)
{
totalScore.mg += 30;
Expand All @@ -233,7 +198,7 @@ Score Evaluation::evaluatePawn(const Piece &piece, const byte square, const Boar
const AttacksMap &ourAttacks, const AttacksMap &theirAttacks) noexcept
{
const Pos pos(square);
Score value = Psqt::s_PawnSquares[square];
Score value = Psqt::s_Bonus[PAWN][square];

const Color color = toColor(piece.isWhite);
const byte behind = color ? -1 : 1;
Expand Down Expand Up @@ -304,7 +269,7 @@ 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
{
Score value = Psqt::s_KnightSquares[square];
Score value = Psqt::s_Bonus[KNIGHT][square];

const int mobility = Bitboard::popCount(MoveGen<ALL>::generateKnightMoves(piece, square, board));
value += KNIGHT_MOBILITY[mobility];
Expand All @@ -315,7 +280,7 @@ inline Score Evaluation::evaluateKnight(const Piece &piece, const byte square, c
Score Evaluation::evaluateBishop(const Piece &piece, const byte square, const Board &board) noexcept
{
const Pos pos(square);
Score value = Psqt::s_BishopSquares[square];
Score value = Psqt::s_Bonus[BISHOP][square];

const int mobility = Bitboard::popCount(MoveGen<ALL>::generateBishopMoves(piece, square, board));
value += BISHOP_MOBILITY[mobility];
Expand All @@ -324,7 +289,7 @@ Score Evaluation::evaluateBishop(const Piece &piece, const byte square, const Bo
const auto isLongDiagonalBishop = [&] {
if (pos.y - pos.x == 0 || pos.y - (7 - pos.x) == 0)
{
byte x = pos.x, y = pos.y;
auto [x, y] = pos;
if (std::min<byte>(x, 7u - x) > 2) return false;
for (byte i = std::min<byte>(x, 7u - x); i < 4; i++)
{
Expand All @@ -347,7 +312,7 @@ Score Evaluation::evaluateRook(const Piece &piece, const byte square, const Boar
{
const Pos pos(square);
const Color color = toColor(piece.isWhite);
Score value = Psqt::s_RookSquares[pos.toSquare()];
Score value = Psqt::s_Bonus[ROOK][pos.toSquare()];

const int mobility = Bitboard::popCount(MoveGen<ALL>::generateRookMoves(piece, pos.toSquare(), board));
value += ROOK_MOBILITY[mobility];
Expand Down Expand Up @@ -387,14 +352,13 @@ 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 Pos pos(square);
Score value = Psqt::s_QueenSquares[square];
Score value = Psqt::s_Bonus[QUEEN][square];

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

const U64 originalPosition = FILE_D & (piece.isWhite ? RANK_1 : RANK_7);

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

const auto weak = [&] {
Expand Down Expand Up @@ -423,7 +387,19 @@ Score Evaluation::evaluateQueen(const Piece &piece, const byte square, const Boa
return value;
}

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

if (board.isCastled(color))
value.mg += 57;
else {
const int count = static_cast<int>(board.canCastleKs(color))
+ static_cast<int>(board.canCastleQs(color));
value.mg += count * 23;
}

return value;
}
3 changes: 2 additions & 1 deletion ChessAndroid/app/src/main/cpp/chess/algorithm/Evaluation.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ class Evaluation final
static Score evaluateBishop(const Piece &piece, byte square, const Board &board) noexcept;
static Score evaluateRook(const Piece &piece, byte square, const Board &board) noexcept;
static Score evaluateQueen(const Piece &piece, const byte square, const Board &board) noexcept;
inline static Score evaluateKing(byte square) noexcept;
inline static Score
evaluateKing(const Piece &piece, const byte square, const Board &board) noexcept;
};
96 changes: 49 additions & 47 deletions ChessAndroid/app/src/main/cpp/chess/data/Board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "../algorithm/Hash.h"
#include "../persistence/FenParser.h"

void Board::initDefaultBoard() noexcept
void Board::initDefaultBoard()
{
setToFen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
}
Expand Down Expand Up @@ -141,11 +141,13 @@ Phase Board::getPhase() const noexcept

bool Board::hasValidState() const noexcept
{
const Color previousPlayer = oppositeColor(colorToMove);

if (state == State::INVALID)
return false;
if (colorToMove == WHITE && (state == State::WHITE_IN_CHECK || state == State::WINNER_BLACK))
if (previousPlayer == WHITE && (state == State::WHITE_IN_CHECK || state == State::WINNER_BLACK))
return false;
if (colorToMove == BLACK && (state == State::BLACK_IN_CHECK || state == State::WINNER_WHITE))
if (previousPlayer == BLACK && (state == State::BLACK_IN_CHECK || state == State::WINNER_WHITE))
return false;

return true;
Expand All @@ -163,62 +165,21 @@ std::vector<std::pair<Pos, Piece>> Board::getAllPieces() const
return pieces;
}

std::vector<Board> Board::listQuiescenceMoves() const
{
const auto pieces = Player::getAllOwnedPieces(colorToMove, *this);
std::vector<Board> moves;
moves.reserve(50);

for (const auto &pair : pieces)
{
const byte startSq = pair.first;
const Piece &selectedPiece = pair.second;
U64 possibleMoves = selectedPiece.getPossibleCaptures(startSq, *this);

while (possibleMoves)
{
const byte destSq = Bitboard::findNextSquare(possibleMoves);
const Piece &destPiece = getPiece(destSq);
if (!destPiece || destPiece.type == PieceType::KING)
continue;

Board board = *this;
board.doMove(startSq, destSq);

if (!board.hasValidState())
continue;

board.score = Evaluation::simpleEvaluation(board);

moves.push_back(board);
}
}

if (colorToMove)
std::sort(moves.begin(), moves.end(), std::greater<>());
else
std::sort(moves.begin(), moves.end());

return moves;
}

void Board::doMove(const byte startSq, const byte destSq, const bool updateState) noexcept
{
const Color whiteIsMoving = colorToMove;
const Piece &startPiece = getPiece(startSq);
const Piece &destPiece = getPiece(destSq);
const U64 startBb = Bitboard::shiftedBoards[startSq];
const U64 destBb = Bitboard::shiftedBoards[destSq];

score = 0;
state = State::NONE;
colorToMove = oppositeColor(colorToMove);
isPromotion = isCapture = false;
++halfMoveClock;

getType(whiteIsMoving, startPiece.type) &= ~startBb;
getType(oppositeColor(whiteIsMoving), destPiece.type) &= ~destBb;
getType(whiteIsMoving, startPiece.type) |= destBb;
getType(colorToMove, startPiece.type) &= ~startBb;
getType(oppositeColor(colorToMove), destPiece.type) &= ~destBb;
getType(colorToMove, startPiece.type) |= destBb;

Hash::flipSide(zKey);
Hash::makeMove(zKey, startSq, destSq, startPiece, destPiece);
Expand Down Expand Up @@ -249,10 +210,51 @@ void Board::doMove(const byte startSq, const byte destSq, const bool updateState
getPiece(startSq) = Piece();
updateNonPieceBitboards();

colorToMove = oppositeColor(colorToMove);

if (updateState)
this->updateState();
}

std::vector<Board> Board::listQuiescenceMoves() const
{
const auto pieces = Player::getAllOwnedPieces(colorToMove, *this);
std::vector<Board> moves;
moves.reserve(50);

for (const auto &pair : pieces)
{
const byte startSq = pair.first;
const Piece &selectedPiece = pair.second;
U64 possibleMoves = selectedPiece.getPossibleCaptures(startSq, *this);

while (possibleMoves)
{
const byte destSq = Bitboard::findNextSquare(possibleMoves);
const Piece &destPiece = getPiece(destSq);
if (!destPiece || destPiece.type == PieceType::KING)
continue;

Board board = *this;
board.doMove(startSq, destSq);

if (!board.hasValidState())
continue;

board.score = Evaluation::simpleEvaluation(board);

moves.push_back(board);
}
}

if (colorToMove)
std::sort(moves.begin(), moves.end(), std::greater<>());
else
std::sort(moves.begin(), moves.end());

return moves;
}

bool Board::movePawn(const byte startSq, const byte destSq)
{
Piece &pawn = getPiece(startSq);
Expand Down
9 changes: 4 additions & 5 deletions ChessAndroid/app/src/main/cpp/chess/data/Board.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ class Board final
short npm{};

Board() = default;
Board(Board&&) = default;
Board(Board&&) noexcept = default;
Board(const Board &board) = default;
~Board() = default;

Board &operator=(Board&&) = default;
Board &operator=(Board&&) noexcept = default;
Board &operator=(const Board &other) = default;

Piece &operator[](const Pos &pos) noexcept;
Expand All @@ -45,7 +45,7 @@ class Board final
bool operator<(const Board &other) const noexcept;
bool operator>(const Board &other) const noexcept;

void initDefaultBoard() noexcept;
void initDefaultBoard();
void setToFen(const std::string &fen);

bool canCastle(Color color) const noexcept;
Expand All @@ -66,12 +66,11 @@ class Board final
Phase getPhase() const noexcept;
std::vector<std::pair<Pos, Piece>> getAllPieces() const;

void doMove(byte startSq, byte destSq, bool updateState = true) noexcept;
template<class T> // RootMove or Board
std::vector<T> listValidMoves() const noexcept;
std::vector<Board> listQuiescenceMoves() const;

void doMove(byte startSq, byte destSq, bool updateState = true) noexcept;

private:
bool movePawn(byte startSq, byte destSq);
void moveRook(byte startSq);
Expand Down
Loading