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
Re-enable EnPassant
  • Loading branch information
razvanfilea committed Dec 9, 2019
commit 1b60b3876b647af01c111ba4a3a0c33f21655c49
25 changes: 13 additions & 12 deletions ChessAndroid/app/src/main/cpp/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
#include "chess/persistence/MovesPersistence.h"
#include "chess/algorithm/Search.h"

JavaVM *jvm = nullptr;
jobject gameManagerInstance;
static JavaVM *jvm = nullptr;
static jobject gameManagerInstance;

const BoardManager::PieceChangeListener listener = [](State state, bool shouldRedraw,
const std::vector<std::pair<byte, byte>> &moved)
Expand All @@ -23,7 +23,7 @@ const BoardManager::PieceChangeListener listener = [](State state, bool shouldRe
if (getEnvStat == JNI_EDETACHED)
{
jvm->AttachCurrentThread(&env, nullptr);
LOGI("ChessCpp", "Attached to Thread");
LOGD("ChessCpp", "Attached to Thread");
}

env->ExceptionClear();
Expand Down Expand Up @@ -51,7 +51,7 @@ const BoardManager::PieceChangeListener listener = [](State state, bool shouldRe
if (getEnvStat == JNI_EDETACHED)
{
jvm->DetachCurrentThread();
LOGI("ChessCpp", "Detached from Thread");
LOGD("ChessCpp", "Detached from Thread");
}
};

Expand Down Expand Up @@ -124,7 +124,7 @@ Java_net_theluckycoder_chess_Native_isPlayerWhite(JNIEnv */*pEnv*/, jclass /*typ
// region Stats

external JNIEXPORT jdouble JNICALL
Java_net_theluckycoder_chess_Native_getSearchTime(JNIEnv *pEnv, jclass /*type*/)
Java_net_theluckycoder_chess_Native_getSearchTime(JNIEnv */*pEnv*/, jclass /*type*/)
{
return static_cast<jdouble>(Stats::getElapsedTime());
}
Expand Down Expand Up @@ -228,8 +228,8 @@ Java_net_theluckycoder_chess_Native_movePiece(JNIEnv */*pEnv*/, jclass /*type*/,
jbyte destY)
{
BoardManager::movePiece(
Pos(static_cast<byte>(selectedX), static_cast<byte>(selectedY)).toSquare(),
Pos(static_cast<byte>(destX), static_cast<byte>(destY)).toSquare());
toSquare(static_cast<byte>(selectedX), static_cast<byte>(selectedY)),
toSquare(static_cast<byte>(destX), static_cast<byte>(destY)));
}


Expand All @@ -243,7 +243,7 @@ external JNIEXPORT void JNICALL
Java_net_theluckycoder_chess_Native_loadMoves(JNIEnv *pEnv, jclass /*type*/, jstring moves)
{
const char *nativeString = pEnv->GetStringUTFChars(moves, nullptr);
const MovesPersistence savedMoves = MovesPersistence(nativeString);
const MovesPersistence savedMoves(nativeString);

BoardManager::loadGame(savedMoves.getMoves(), savedMoves.isPlayerWhite());

Expand Down Expand Up @@ -279,6 +279,7 @@ Java_net_theluckycoder_chess_Native_perft(JNIEnv */*pEnv*/, jclass /*type*/, jin
{
using namespace std::chrono;

constexpr auto TAG = "Perft Test";
constexpr std::array<U64, 7> perftResults{
1, 20, 400, 8902, 197281, 4865609, 119060324
};
Expand All @@ -292,17 +293,17 @@ Java_net_theluckycoder_chess_Native_perft(JNIEnv */*pEnv*/, jclass /*type*/, jin

for (int i = 0; i <= depth; ++i)
{
LOGV("Perft Test", "Starting Depth %d Test", i);
LOGV(TAG, "Starting Depth %d Test", i);

const auto startTime = high_resolution_clock::now();
const U64 nodesCount = perft(board, i);
const auto endTime = high_resolution_clock::now();

const auto timeNeeded = duration<double, std::milli>(endTime - startTime).count();

LOGV("Perft Test", "Time needed: %lf", timeNeeded);
LOGV("Perft Test", "Nodes count: %llu/%llu", nodesCount, perftResults[i]);
LOGV(TAG, "Time needed: %lf", timeNeeded);
LOGV(TAG, "Nodes count: %llu/%llu", nodesCount, perftResults[i]);
if (nodesCount != perftResults[i])
LOGE("Perft Test Error", "Nodes count do not match at depth %d", i);
LOGE(TAG, "Nodes count do not match at depth %d", i);
}
}
2 changes: 2 additions & 0 deletions ChessAndroid/app/src/main/cpp/chess/BoardManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ void BoardManager::initBoardManager(const PieceChangeListener &listener, const b
{
Hash::init();
PieceAttacks::init();
//MoveOrdering::init();

s_Board.initDefaultBoard();
//s_Board.setToFen("r1b1k2r/ppppnppp/2n2q2/2b5/3NP3/2P1B3/PP3PPP/RN1QKB1R w KQkq - 0 1");
s_Listener = listener;

s_MovesHistory.clear();
Expand Down
2 changes: 1 addition & 1 deletion ChessAndroid/app/src/main/cpp/chess/BoardManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include "Settings.h"
#include "containers/StackVector.h"
#include "data/Enums.h"
#include "data/Defs.h"
#include "data/Piece.h"

class RootMove;
Expand Down
19 changes: 17 additions & 2 deletions ChessAndroid/app/src/main/cpp/chess/Stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,31 @@ void Stats::resetStats() noexcept
s_NodesGenerated = 0;
}

size_t Stats::getBoardsEvaluated() noexcept
{
return s_BoardsEvaluated;
}

size_t Stats::getNodesSearched() noexcept
{
return s_NodesSearched;
}

size_t Stats::getNodesGenerated() noexcept
{
return s_NodesGenerated;
}

void Stats::incrementBoardsEvaluated() noexcept
{
if (s_StatsEnabled)
++s_BoardsEvaluated;
}

void Stats::incrementNodesSearched() noexcept
void Stats::incrementNodesSearched(const std::size_t amount) noexcept
{
if (s_StatsEnabled)
++s_NodesSearched;
s_NodesSearched += amount;
}

void Stats::incrementNodesGenerated(const std::size_t amount) noexcept
Expand Down
8 changes: 6 additions & 2 deletions ChessAndroid/app/src/main/cpp/chess/Stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ class Stats final
static void setEnabled(bool enabled) noexcept;
static void resetStats() noexcept;

static size_t getBoardsEvaluated() noexcept;
static size_t getNodesSearched() noexcept;
static size_t getNodesGenerated() noexcept;

static void incrementBoardsEvaluated() noexcept;
static void incrementNodesSearched() noexcept;
static void incrementNodesGenerated(std::size_t amount) noexcept;
static void incrementNodesSearched(std::size_t amount = 1u) noexcept;
static void incrementNodesGenerated(std::size_t amount = 1u) noexcept;

static void startTimer() noexcept;
static void stopTimer() noexcept;
Expand Down
10 changes: 5 additions & 5 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.isWhite][piece.type - 1u];
return s_Pieces[sq][piece.isWhite][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 < 64; ++i)
if (const Piece &piece = board.getPiece(i); piece)
hash ^= s_Pieces[i][piece.isWhite][piece.type - 1u];
hash ^= s_Pieces[i][piece.isWhite][piece.type];

if (board.colorToMove)
hash ^= s_WhiteToMove;
Expand All @@ -65,15 +65,15 @@ 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][color][PAWN - 1u];
key ^= s_Pieces[sq][color][PAWN];

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

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

void Hash::flipSide(U64 &key)
Expand Down
4 changes: 2 additions & 2 deletions ChessAndroid/app/src/main/cpp/chess/algorithm/Hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

#include <array>

#include "../data/Enums.h"
#include "../data/Defs.h"
#include "../data/Piece.h"

using U64 = std::uint64_t;

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

static HashArray s_Pieces;
static U64 s_WhiteToMove;
Expand Down
19 changes: 10 additions & 9 deletions ChessAndroid/app/src/main/cpp/chess/algorithm/MoveGen.inl
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ U64 MoveGen<T>::generatePawnMoves(const Piece &piece, const byte square, const B
{
U64 captures = attacks & board.allPieces[!piece.isWhite];

/*if (board.enPassantSq < 64)
{
if (board.enPassantSq < 64u)
{
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];
}*/
}

attacks = captures;
}
else if (T == ATTACKS_DEFENSES)
attacks &= board.occupied;

const U64 initialBb = Bitboard::shiftedBoards[square];
Pos pos(square);
piece.isWhite ? pos.y++ : pos.y--;

if constexpr (T == ALL)
{
const U64 initialBb = Bitboard::shiftedBoards[square];
Pos pos(square);
piece.isWhite ? pos.y++ : pos.y--;

if (!board[pos])
{
attacks |= pos.toBitboard();
Expand Down Expand Up @@ -157,10 +157,11 @@ U64 MoveGen<T>::generateKingMoves(const Piece &piece, const byte square, const B

const byte y = row(square);
const auto isEmptyAndCheckFree = [&, y](const byte x) {
const byte sq = Pos(x, y).toSquare();
const byte sq = toSquare(x, y);
const U64 bb = Bitboard::shiftedBoards[sq];

return !board.getPiece(x, y) && !(opponentsMoves & bb) && !Player::isAttacked(oppositeColor(color), sq, board);
return !board.getPiece(x, y) && !(opponentsMoves & bb)
&& !Player::isAttacked(oppositeColor(color), sq, board);
};

// King Side
Expand Down
4 changes: 2 additions & 2 deletions ChessAndroid/app/src/main/cpp/chess/algorithm/MoveOrdering.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class MoveOrdering
if (initialized) return;
initialized = true;

for (int attacker = PAWN; attacker <= KING; ++attacker)
for (byte attacker = PAWN; attacker <= KING; ++attacker)
{
for (int victim = PAWN; victim <= KING; ++victim)
for (byte victim = PAWN; victim <= KING; ++victim)
{
MvvLvaScore[victim][attacker] = victimScore[victim] + 6 - (victimScore[attacker] / 100);
}
Expand Down
Loading