Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 0 additions & 9 deletions ChessAndroid/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -g -O0 -std=c++17 -Wall -Wextra -pedantic")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -O3 -std=c++17 -Wall -Wextra -pedantic -DNDEBUG")

Expand Down Expand Up @@ -37,10 +32,6 @@ find_library( # Sets the name of the path variable.
# you want CMake to locate.
log)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
chess

Expand Down
8 changes: 5 additions & 3 deletions ChessAndroid/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
applicationId "net.theluckycoder.chess"
minSdkVersion 21
targetSdkVersion 29
versionCode 26
versionName "0.26"
versionCode 100
versionName "1.0"
}

buildTypes {
Expand All @@ -31,6 +31,8 @@ android {

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.3'

implementation 'androidx.preference:preference:1.1.0'
implementation 'com.jaredrummler:colorpicker:1.1.0'
}
128 changes: 77 additions & 51 deletions ChessAndroid/app/src/main/cpp/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,49 @@
#include "chess/BoardManager.h"
#include "chess/Stats.h"
#include "chess/data/Board.h"
#include "chess/algorithm/Evaluation.h"
#include "chess/persistence/MovesPersistence.h"
#include "chess/algorithm/NegaMax.h"
#include "chess/algorithm/PieceAttacks.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 StackVector<PosPair, 2> &moved)
const std::vector<std::pair<byte, byte>> &moved)
{
JNIEnv *env;
int getEnvStat = jvm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6);
if (getEnvStat == JNI_EDETACHED)
{
jvm->AttachCurrentThread(&env, nullptr);
LOGI("ChessCpp", "Attached to Thread");
LOGD("ChessCpp", "Attached to Thread");
}

env->ExceptionClear();

jobjectArray result = env->NewObjectArray(static_cast<jsize>(moved.size()), Cache::posPairClass, nullptr);
jobjectArray result = env->NewObjectArray(static_cast<jsize>(moved.size()), Cache::posPairClass,
nullptr);

const static auto constructorId = env->GetMethodID(Cache::posPairClass, "<init>", "(BBBB)V");
const static auto constructorId = env->GetMethodID(Cache::posPairClass, "<init>", "(IIII)V");

for (unsigned i = 0; i < moved.size(); ++i)
{
const Pos &startPos = moved[i].first;
const Pos &destPos = moved[i].second;
const Pos &startPos = Pos(moved[i].first);
const Pos &destPos = Pos(moved[i].second);
jobject obj = env->NewObject(Cache::posPairClass, constructorId,
startPos.x, startPos.y, destPos.x, destPos.y);
startPos.x, startPos.y, destPos.x, destPos.y);

env->SetObjectArrayElement(result, i, obj);
}

const static auto callbackId = env->GetMethodID(Cache::gameManagerClass, "callback",
"(IZ[Lnet/theluckycoder/chess/PosPair;)V");
env->CallVoidMethod(gameManagerInstance, callbackId,
static_cast<jint>(state), static_cast<jboolean>(shouldRedraw), result);
static_cast<jint>(state), static_cast<jboolean>(shouldRedraw), result);

if (getEnvStat == JNI_EDETACHED)
{
jvm->DetachCurrentThread();
LOGI("ChessCpp", "Detached from Thread");
LOGD("ChessCpp", "Detached from Thread");
}
};

Expand All @@ -61,7 +60,7 @@ external JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void */*reserved*/)
LOGI("ChessCpp", "JNI_OnLoad");

JNIEnv *env;
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK)
if (vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6) != JNI_OK)
return -1;

jvm = vm;
Expand All @@ -87,7 +86,8 @@ external JNIEXPORT void JNI_OnUnload(JavaVM *vm, void */*reserved*/)

external JNIEXPORT void JNICALL
Java_net_theluckycoder_chess_GameManager_initBoardNative(JNIEnv *pEnv, jobject instance,
jboolean restartGame, jboolean isPlayerWhite)
jboolean restartGame,
jboolean isPlayerWhite)
{
static bool boardManagerInitialized = false;
pEnv->ExceptionClear();
Expand All @@ -96,7 +96,8 @@ Java_net_theluckycoder_chess_GameManager_initBoardNative(JNIEnv *pEnv, jobject i
{
pEnv->DeleteGlobalRef(gameManagerInstance);
gameManagerInstance = pEnv->NewGlobalRef(instance);
Cache::gameManagerClass = Cache::cacheClass(pEnv, pEnv->GetObjectClass(gameManagerInstance));
Cache::gameManagerClass = Cache::cacheClass(pEnv,
pEnv->GetObjectClass(gameManagerInstance));

boardManagerInitialized = true;
BoardManager::initBoardManager(listener);
Expand All @@ -120,35 +121,51 @@ Java_net_theluckycoder_chess_Native_isPlayerWhite(JNIEnv */*pEnv*/, jclass /*typ
return static_cast<jboolean>(BoardManager::isPlayerWhite());
}

// region Stats

external JNIEXPORT jstring JNICALL
Java_net_theluckycoder_chess_Native_getStats(JNIEnv *pEnv, jclass /*type*/)
external JNIEXPORT jdouble JNICALL
Java_net_theluckycoder_chess_Native_getSearchTime(JNIEnv */*pEnv*/, jclass /*type*/)
{
return pEnv->NewStringUTF(Stats::formatStats('\n').c_str());
return static_cast<jdouble>(Stats::getElapsedTime());
}

external JNIEXPORT jint JNICALL
Java_net_theluckycoder_chess_Native_getBoardValue(JNIEnv */*pEnv*/, jclass /*type*/)
Java_net_theluckycoder_chess_Native_getCurrentBoardValue(JNIEnv */*pEnv*/, jclass /*type*/)
{
return static_cast<jint>(BoardManager::getBoard().score);
}

external JNIEXPORT jint JNICALL
Java_net_theluckycoder_chess_Native_getBestMoveFound(JNIEnv */*pEnv*/, jclass /*type*/)
{
return static_cast<jint>(Search::getBestMoveFound());
}

external JNIEXPORT jstring JNICALL
Java_net_theluckycoder_chess_Native_getAdvancedStats(JNIEnv *pEnv, jclass /*type*/)
{
return pEnv->NewStringUTF(Stats::formatStats('\n').c_str());
}

// endregion Stats

external JNIEXPORT _jobjectArray *JNICALL
Java_net_theluckycoder_chess_Native_getPieces(JNIEnv *pEnv, jclass /*type*/)
{
pEnv->ExceptionClear();

const static auto constructorId = pEnv->GetMethodID(Cache::pieceClass, "<init>", "(BBB)V");
const static auto constructorId = pEnv->GetMethodID(Cache::pieceClass, "<init>", "(IIB)V");

const auto pieces = BoardManager::getBoard().getAllPieces();
auto *array = pEnv->NewObjectArray(static_cast<jsize>(pieces.size()), Cache::pieceClass, nullptr);
auto *array = pEnv->NewObjectArray(static_cast<jsize>(pieces.size()), Cache::pieceClass,
nullptr);

jsize i = 0;
for (const auto &it : pieces)
{
const Pos &pos = it.first;
auto pieceType = static_cast<jbyte>(it.second.type);
if (!it.second.isWhite)
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 All @@ -165,15 +182,20 @@ Java_net_theluckycoder_chess_Native_getPossibleMoves(JNIEnv *pEnv, jclass /*type
{
pEnv->ExceptionClear();

const static auto constructorId = pEnv->GetMethodID(Cache::posClass, "<init>", "(BB)V");
const static auto constructorId = pEnv->GetMethodID(Cache::posClass, "<init>", "(II)V");

const Pos pos(getByte(pEnv, Cache::posClass, dest, "x"), getByte(pEnv, Cache::posClass, dest, "y"));
const Pos pos(getInt(pEnv, Cache::posClass, dest, "x"),
getInt(pEnv, Cache::posClass, dest, "y"));
const auto possibleMoves = BoardManager::getPossibleMoves(pos);
auto *result = pEnv->NewObjectArray(static_cast<jsize>(possibleMoves.size()), Cache::posClass, nullptr);
auto *result = pEnv->NewObjectArray(static_cast<jsize>(possibleMoves.size()), Cache::posClass,
nullptr);

for (unsigned i = 0; i < possibleMoves.size(); ++i)
{
jobject obj = pEnv->NewObject(Cache::posClass, constructorId, possibleMoves[i].x, possibleMoves[i].y);
jobject obj = pEnv->NewObject(Cache::posClass,
constructorId,
static_cast<int>(possibleMoves[i].x),
static_cast<int>(possibleMoves[i].y));
pEnv->SetObjectArrayElement(result, i, obj);
}

Expand All @@ -188,8 +210,10 @@ Java_net_theluckycoder_chess_Native_enableStats(JNIEnv */*pEnv*/, jclass /*type*
}

external JNIEXPORT void JNICALL
Java_net_theluckycoder_chess_Native_setSettings(JNIEnv */*pEnv*/, jclass /*type*/, jint baseSearchDepth,
jint threadCount, jint cacheSizeMb, jboolean performQuiescenceSearch)
Java_net_theluckycoder_chess_Native_setSettings(JNIEnv */*pEnv*/, jclass /*type*/,
jint baseSearchDepth,
jint threadCount, jint cacheSizeMb,
jboolean performQuiescenceSearch)
{
BoardManager::setSettings(Settings(static_cast<short>(baseSearchDepth),
static_cast<unsigned int>(threadCount),
Expand All @@ -200,16 +224,12 @@ Java_net_theluckycoder_chess_Native_setSettings(JNIEnv */*pEnv*/, jclass /*type*

external JNIEXPORT void JNICALL
Java_net_theluckycoder_chess_Native_movePiece(JNIEnv */*pEnv*/, jclass /*type*/,
jbyte selectedX, jbyte selectedY, jbyte destX, jbyte destY)
{
BoardManager::movePiece(Pos(static_cast<byte>(selectedX), static_cast<byte>(selectedY)),
Pos(static_cast<byte>(destX), static_cast<byte>(destY)));
}

external JNIEXPORT jint JNICALL
Java_net_theluckycoder_chess_Native_getBestMoveFound(JNIEnv */*pEnv*/, jclass /*type*/)
jbyte selectedX, jbyte selectedY, jbyte destX,
jbyte destY)
{
return static_cast<jint>(NegaMax::getBestMoveFound());
BoardManager::movePiece(
toSquare(static_cast<byte>(selectedX), static_cast<byte>(selectedY)),
toSquare(static_cast<byte>(destX), static_cast<byte>(destY)));
}


Expand All @@ -223,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 All @@ -233,11 +253,12 @@ Java_net_theluckycoder_chess_Native_loadMoves(JNIEnv *pEnv, jclass /*type*/, jst
external JNIEXPORT jstring JNICALL
Java_net_theluckycoder_chess_Native_saveMoves(JNIEnv *pEnv, jclass /*type*/)
{
const std::string string = MovesPersistence::saveToString(BoardManager::getMovesHistory(), BoardManager::isPlayerWhite());
const std::string string = MovesPersistence::saveToString(BoardManager::getMovesHistory(),
BoardManager::isPlayerWhite());
return pEnv->NewStringUTF(string.c_str());
}

static U64 perft(const Board &board, int depth)
static U64 perft(const Board &board, const int depth)
{
if (depth == 0) return 1;

Expand All @@ -256,7 +277,10 @@ static U64 perft(const Board &board, int depth)
external JNIEXPORT void JNICALL
Java_net_theluckycoder_chess_Native_perft(JNIEnv */*pEnv*/, jclass /*type*/, jint depth)
{
constexpr std::array perftResults {
using namespace std::chrono;

constexpr auto TAG = "Perft Test";
constexpr std::array<U64, 7> perftResults{
1, 20, 400, 8902, 197281, 4865609, 119060324
};
constexpr int maxSize = static_cast<int>(perftResults.size());
Expand All @@ -269,17 +293,19 @@ 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 = std::chrono::high_resolution_clock::now();
const auto startTime = high_resolution_clock::now();
const U64 nodesCount = perft(board, i);
const auto endTime = high_resolution_clock::now();

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

LOGV("Perft Test", "Time needed: %lf", timeNeeded);
LOGV("Perft Test", "Nodes count: %llu", nodesCount);
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);
}

LOGV(TAG, "Test Finished");
}
Loading