Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Small UI changes
  • Loading branch information
razvanfilea committed Apr 24, 2021
commit f372ccbbd3fa579bc7f7117d6de44755aa73347d
4 changes: 2 additions & 2 deletions ChessAndroid/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {
applicationId "net.theluckycoder.chess"
minSdkVersion 21
targetSdkVersion 30
versionCode 1200
versionCode 1201
versionName "1.2.0"
resConfigs "en"
}
Expand Down Expand Up @@ -80,7 +80,7 @@ dependencies {
implementation("androidx.compose.ui:ui:$compose_version")
implementation("androidx.compose.foundation:foundation:$compose_version")
implementation("androidx.compose.material:material:$compose_version")
debugImplementation("androidx.compose.ui:ui-tooling:$compose_version")
implementation("androidx.compose.ui:ui-tooling:$compose_version")
debugImplementation("org.jetbrains.kotlin:kotlin-reflect:$kotlin_version")
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha04")
}
67 changes: 37 additions & 30 deletions ChessAndroid/app/src/main/cpp/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Java_net_theluckycoder_chess_Native_initBoard(JNIEnv *pEnv, jobject, jobject ins
external jboolean JNICALL
Java_net_theluckycoder_chess_Native_isEngineWorking(JNIEnv *, jobject)
{
return static_cast<jboolean>(BoardManager::isWorking());
return static_cast<jboolean>(BoardManager::isEngineBusy());
}

external jboolean JNICALL
Expand Down Expand Up @@ -166,34 +166,6 @@ Java_net_theluckycoder_chess_Native_getPossibleMoves(JNIEnv *pEnv, jobject, jbyt
return result;
}

external jobject JNICALL
Java_net_theluckycoder_chess_Native_getSearchOptions(JNIEnv *pEnv, jobject)
{
const static auto constructorId = pEnv->GetMethodID(JniCache::searchOptionsClass, "<init>", "(IIJIZ)V");

const auto options = BoardManager::getSearchOptions();

return pEnv->NewObject(JniCache::searchOptionsClass, constructorId,
options.depth(), options.threadCount(),
static_cast<jlong>(options.searchTime()), options.tableSizeMb(),
options.quietSearch());
}

external void JNICALL
Java_net_theluckycoder_chess_Native_setSearchOptions(JNIEnv *, jobject, jint searchDepth,
jboolean quietSearch,
jint threadCount,
jint hashSizeMb,
jlong searchTime)
{
BoardManager::setSearchOptions({ searchDepth,
static_cast<u32>(threadCount),
static_cast<u32>(hashSizeMb),
static_cast<bool>(quietSearch),
static_cast<i64>(searchTime) });
}


external void JNICALL
Java_net_theluckycoder_chess_Native_makeMove(JNIEnv *, jobject, jint move)
{
Expand All @@ -207,9 +179,13 @@ Java_net_theluckycoder_chess_Native_makeEngineMove(JNIEnv *, jobject)
}

external void JNICALL
Java_net_theluckycoder_chess_Native_stopSearch(JNIEnv *, jobject)
Java_net_theluckycoder_chess_Native_stopSearch(JNIEnv *, jobject, jboolean async)
{
Search::stopSearch();
if (!async) {
while (BoardManager::isEngineBusy())
std::this_thread::sleep_for(std::chrono::milliseconds(2));
}
}


Expand Down Expand Up @@ -326,6 +302,37 @@ Java_net_theluckycoder_chess_Native_evaluationTests(JNIEnv *pEnv, jobject)
return pEnv->NewStringUTF(testResults.c_str());
}

// region SearchOptions

external jobject JNICALL
Java_net_theluckycoder_chess_model_SearchOptions_getNativeSearchOptions(JNIEnv *pEnv, jclass)
{
const static auto constructorId = pEnv->GetMethodID(JniCache::searchOptionsClass, "<init>", "(IIJIZ)V");

const auto options = BoardManager::getSearchOptions();

return pEnv->NewObject(JniCache::searchOptionsClass, constructorId,
options.depth(), options.threadCount(),
static_cast<jlong>(options.searchTime()), options.tableSizeMb(),
options.quietSearch());
}

external void JNICALL
Java_net_theluckycoder_chess_model_SearchOptions_setNativeSearchOptions(JNIEnv *, jclass, jint searchDepth,
jboolean quietSearch,
jint threadCount,
jint hashSizeMb,
jlong searchTime)
{
BoardManager::setSearchOptions({ searchDepth,
static_cast<u32>(threadCount),
static_cast<u32>(hashSizeMb),
static_cast<bool>(quietSearch),
static_cast<i64>(searchTime) });
}

// endregion SearchOptions

// region DebugStats

external void JNICALL
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 @@ -52,7 +52,7 @@ class BoardManager final
static void redoLastMoves();

/// Getters and Setters
static bool isWorking() noexcept { return _isWorking; }
static bool isEngineBusy() noexcept { return _isWorking; }

static bool isPlayerWhite() noexcept
{
Expand Down
25 changes: 11 additions & 14 deletions ChessAndroid/app/src/main/cpp/chess/polyglot/PolyBook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,9 @@ namespace PolyKeys
u64(0xF8D626AAAF278509),
};

enum
{
CASTLE_OFFSET = 768,
EN_PASSANT_OFFSET = 772,
TURN_OFFSET = 780,
};
static constexpr usize CastleOffset = 768;
static constexpr usize EnPassantOffset = 772;
static constexpr usize TurnOffset = 780;
}

namespace PolyBook
Expand Down Expand Up @@ -240,9 +237,9 @@ namespace PolyBook
const auto enPass = Bitboard::fromSquare(board.getEnPassantSq());

if (board.colorToMove == WHITE)
return !(Attacks::pawnAttacks<WHITE>(board.getPieces(PAWN, WHITE)) & enPass).empty();
return (Attacks::pawnAttacks<WHITE>(board.getPieces(PAWN, WHITE)) & enPass).notEmpty();
else
return !(Attacks::pawnAttacks<BLACK>(board.getPieces(PAWN, BLACK)) & enPass).empty();
return (Attacks::pawnAttacks<BLACK>(board.getPieces(PAWN, BLACK)) & enPass).notEmpty();
}

static std::optional<std::string> bookPath;
Expand Down Expand Up @@ -310,24 +307,24 @@ namespace PolyBook

// Castling
if (board.canCastleKs<WHITE>())
result ^= PolyKeys::Random64[PolyKeys::CASTLE_OFFSET + 0];
result ^= PolyKeys::Random64[PolyKeys::CastleOffset + 0];
if (board.canCastleQs<WHITE>())
result ^= PolyKeys::Random64[PolyKeys::CASTLE_OFFSET + 1];
result ^= PolyKeys::Random64[PolyKeys::CastleOffset + 1];
if (board.canCastleKs<BLACK>())
result ^= PolyKeys::Random64[PolyKeys::CASTLE_OFFSET + 2];
result ^= PolyKeys::Random64[PolyKeys::CastleOffset + 2];
if (board.canCastleQs<BLACK>())
result ^= PolyKeys::Random64[PolyKeys::CASTLE_OFFSET + 3];
result ^= PolyKeys::Random64[PolyKeys::CastleOffset + 3];

// En Passant
if (board.getEnPassantSq() != SQ_NONE && hasEnPassPawnForCapture(board))
{
result ^= PolyKeys::Random64[PolyKeys::EN_PASSANT_OFFSET + fileOf(board.getEnPassantSq())];
result ^= PolyKeys::Random64[PolyKeys::EnPassantOffset + fileOf(board.getEnPassantSq())];
}

// SideKey
if (board.colorToMove == WHITE)
{
result ^= PolyKeys::Random64[PolyKeys::TURN_OFFSET];
result ^= PolyKeys::Random64[PolyKeys::TurnOffset];
}

// Generate the key and swap it to big endian using flipVertical
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import net.theluckycoder.chess.model.SearchOptions
import net.theluckycoder.chess.utils.SettingsDataStore
import java.io.File
import kotlin.time.ExperimentalTime
import kotlin.time.seconds

@Suppress("unused")
class ChessApp : Application() {

@OptIn(ExperimentalTime::class)
override fun onCreate() {
super.onCreate()

Expand All @@ -27,7 +31,8 @@ class ChessApp : Application() {
launch {
if (dataStore.firstStart().first()) {
// Set the default Engine Settings from native code
val engineSettings = Native.getSearchOptions()
val engineSettings = SearchOptions.getNativeSearchOptions()
.copy(searchTime = SettingsDataStore.DEFAULT_SEARCH_TIME.seconds)
dataStore.setEngineSettings(engineSettings)
dataStore.setFirstStart(false)
}
Expand Down
23 changes: 2 additions & 21 deletions ChessAndroid/app/src/main/java/net/theluckycoder/chess/Native.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package net.theluckycoder.chess

import net.theluckycoder.chess.model.IndexedPiece
import net.theluckycoder.chess.model.Move
import net.theluckycoder.chess.model.SearchOptions
import kotlin.time.ExperimentalTime

object Native {

Expand All @@ -21,27 +19,10 @@ object Native {
external fun getPossibleMoves(square: Byte): Array<Move>

fun makeMove(move: Move) = makeMove(move.content)
external fun makeMove(move: Int)
private external fun makeMove(move: Int)
external fun makeEngineMove()

external fun stopSearch()

external fun getSearchOptions(): SearchOptions

@OptIn(ExperimentalTime::class)
fun setSearchOptions(options: SearchOptions) = setSearchOptions(
options.searchDepth,
options.quietSearch,
options.threadCount,
options.hashSize,
options.searchTime.toLongMilliseconds(),
)

private external fun setSearchOptions(
searchDepth: Int, quietSearch: Boolean,
threadCount: Int, hashSizeMb: Int,
searchTime: Long,
)
external fun stopSearch(async: Boolean)

external fun undoMoves()
external fun redoMoves()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,26 @@ data class SearchOptions(
hashSize: Int,
quietSearch: Boolean,
) : this(searchDepth, threadCount, searchTime.milliseconds, hashSize, quietSearch)

companion object {

@JvmStatic
external fun getNativeSearchOptions(): SearchOptions

@OptIn(ExperimentalTime::class)
fun setNativeSearchOptions(options: SearchOptions) = setNativeSearchOptions(
options.searchDepth,
options.quietSearch,
options.threadCount,
options.hashSize,
options.searchTime.toLongMilliseconds(),
)

@JvmStatic
private external fun setNativeSearchOptions(
searchDepth: Int, quietSearch: Boolean,
threadCount: Int, hashSizeMb: Int,
searchTime: Long,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ data class Tile(
object Selected : State()
object Moved : State()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import net.theluckycoder.chess.Native
import net.theluckycoder.chess.R
import net.theluckycoder.chess.model.*
import net.theluckycoder.chess.utils.SettingsDataStore
import net.theluckycoder.chess.viewmodel.HomeViewModel
import kotlin.math.min

private val PIECES_RESOURCES = intArrayOf(
Expand All @@ -54,6 +52,7 @@ fun ChessBoard(
tiles: List<Tile>,
pieces: List<IndexedPiece>,
gameState: GameState,
onPieceClick: (piece: Piece) -> Unit,
) = BoxWithConstraints(
modifier = modifier
) {
Expand All @@ -70,7 +69,7 @@ fun ChessBoard(

BoardTiles(boardSize, tileSize, isPlayerWhite, tiles, showPossibleMoves)

BoardPieces(tileSize, isPlayerWhite, pieces, gameState)
BoardPieces(tileSize, isPlayerWhite, pieces, gameState, onPieceClick)

if (showCoordinates) {
BoardCoordinates(tileSize)
Expand Down Expand Up @@ -196,7 +195,7 @@ private fun BoardPieces(
isPlayerWhite: Boolean,
pieces: List<IndexedPiece>,
gameState: GameState,
viewModel: HomeViewModel = viewModel()
onPieceClick: (piece: Piece) -> Unit,
) {
val whiteInCheck = gameState == GameState.WHITE_IN_CHECK
val blackInCheck = gameState == GameState.BLACK_IN_CHECK
Expand All @@ -223,11 +222,11 @@ private fun BoardPieces(

IconButton(
modifier = Modifier
.requiredSize(tileDp)
.size(tileDp)
.offset(animatedX, animatedY)
.then(backgroundModifier),
enabled = isPlayerWhite == piece.isWhite,
onClick = { viewModel.getPossibleMoves(piece.square) }
onClick = { onPieceClick(piece) }
) {
Image(painter = getPieceDrawable(piece = piece), contentDescription = null)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fun HomeScreen(
val showMovesHistory by viewModel.dataStore.showMoveHistory().collectAsState(false)
val movesHistory by viewModel.movesHistory.collectAsState()
val currentMoveIndex by viewModel.currentMoveIndex.collectAsState()
val showCaptures by viewModel.dataStore.showCaptures().collectAsState(false)
val showCaptures by viewModel.dataStore.showCapturedPieces().collectAsState(false)

HomeDialogs()

Expand All @@ -70,7 +70,7 @@ fun HomeScreen(
modifier = Modifier.fillMaxHeight(),
show = showCaptures
) {
ActionsBar(Modifier.weight(1f))
ActionsBar(Modifier.weight(1f).padding(8.dp))
}
}
}
Expand Down Expand Up @@ -104,7 +104,6 @@ fun HomeScreen(
private fun HomeChessBoard(
viewModel: HomeViewModel = viewModel()
) {

val isPlayerWhite by viewModel.playerPlayingWhite.collectAsState()
val tiles by viewModel.tiles.collectAsState()
val pieces by viewModel.pieces.collectAsState()
Expand All @@ -114,7 +113,8 @@ private fun HomeChessBoard(
isPlayerWhite = isPlayerWhite,
tiles = tiles,
pieces = pieces,
gameState = gameState
gameState = gameState,
onPieceClick = { viewModel.showPossibleMoves(it.square) }
)
}

Expand Down Expand Up @@ -317,7 +317,7 @@ private fun AppBarActions(viewModel: HomeViewModel = viewModel()) {
if (isEngineThinking) {
DropdownMenuItem(onClick = {
showActionsMenu = false
Native.stopSearch()
Native.stopSearch(true)
}) { Text(text = stringResource(id = R.string.action_stop_search)) }
}
}
Expand Down
Loading