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
Remake the Settings Screen
  • Loading branch information
razvanfilea committed Dec 1, 2019
commit d73bb5c3f8f7cb1bac8f064ff14c59565d2910b1
4 changes: 3 additions & 1 deletion ChessAndroid/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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.2'

implementation 'androidx.preference:preference:1.1.0'
implementation 'com.jaredrummler:colorpicker:1.1.0'
}
29 changes: 19 additions & 10 deletions ChessAndroid/app/src/main/cpp/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,34 @@ 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*/)
{
Expand Down Expand Up @@ -217,12 +232,6 @@ Java_net_theluckycoder_chess_Native_movePiece(JNIEnv */*pEnv*/, jclass /*type*/,
Pos(static_cast<byte>(destX), static_cast<byte>(destY)).toSquare());
}

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


external JNIEXPORT void JNICALL
Java_net_theluckycoder_chess_Native_undoMoves(JNIEnv */*pEnv*/, jclass /*type*/)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import android.widget.FrameLayout
import android.widget.RelativeLayout
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import kotlinx.android.synthetic.main.activity_chess.*
import kotlinx.android.synthetic.main.dialog_restart.view.*
import net.theluckycoder.chess.utils.AppPreferences
import net.theluckycoder.chess.utils.CapturedPieces
import net.theluckycoder.chess.utils.PieceResourceManager
import net.theluckycoder.chess.views.CustomView
Expand All @@ -25,7 +27,7 @@ class ChessActivity : AppCompatActivity(), CustomView.ClickListener, GameManager
private val capturedPieces = CapturedPieces()
private var viewSize = 0

val preferences = Preferences(this)
val preferences = AppPreferences(this)
val pieces = HashMap<Pos, PieceView>(32)

private var selectedPos = Pos()
Expand Down Expand Up @@ -86,6 +88,13 @@ class ChessActivity : AppCompatActivity(), CustomView.ClickListener, GameManager
Settings(4, Runtime.getRuntime().availableProcessors() - 1, 100, true)
}

val delegateThemeValue = if (preferences.darkTheme)
AppCompatDelegate.MODE_NIGHT_YES
else
AppCompatDelegate.MODE_NIGHT_NO

AppCompatDelegate.setDefaultNightMode(delegateThemeValue)

gameManager.initBoard(false)
}

Expand All @@ -95,7 +104,8 @@ class ChessActivity : AppCompatActivity(), CustomView.ClickListener, GameManager
tiles.forEach {
it.value.invalidate()
}
gameManager.statsEnabled = preferences.debugInfo
gameManager.basicStatsEnabled = preferences.basicDebugInfo
gameManager.advancedStatsEnabled = preferences.advancedDebugInfo
gameManager.updateSettings(preferences.settings)
}

Expand Down Expand Up @@ -152,13 +162,18 @@ class ChessActivity : AppCompatActivity(), CustomView.ClickListener, GameManager
private fun updateState(state: State) {
canMove = true

if (gameManager.statsEnabled) {
if (gameManager.basicStatsEnabled) {
tv_debug.visibility = View.VISIBLE

val advancedStats =
if (gameManager.advancedStatsEnabled) "\n" + Native.getAdvancedStats() else ""

tv_debug.text = getString(
R.string.stats,
Native.getStats(),
Native.getBoardValue(),
Native.getBestMoveFound()
R.string.basic_stats,
Native.getSearchTime(),
Native.getCurrentBoardValue(),
Native.getBestMoveFound(),
advancedStats
)
} else {
tv_debug.visibility = View.GONE
Expand Down Expand Up @@ -227,7 +242,7 @@ class ChessActivity : AppCompatActivity(), CustomView.ClickListener, GameManager

for (i in 0 until 64) {
val pos = Pos(i % 8, i / 8)
val isWhite = (pos.x + pos.y) % 2 == 0
val isWhite = (pos.x + pos.y) % 2 == 1

val xSize = pos.x * viewSize
val ySize = invertIf(isPlayerWhite, pos.y) * viewSize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class GameManager(
private var initialized = false
private var isPlayerWhite = true

var statsEnabled = false
var basicStatsEnabled = false
var advancedStatsEnabled = false
val isWorking
get() = Native.isWorking()

Expand Down Expand Up @@ -67,7 +68,7 @@ class GameManager(
}

fun makeMove(startPos: Pos, destPos: Pos) {
Native.enableStats(statsEnabled)
Native.enableStats(advancedStatsEnabled)
Native.movePiece(
startPos.x.toByte(),
startPos.y.toByte(),
Expand Down
17 changes: 12 additions & 5 deletions ChessAndroid/app/src/main/java/net/theluckycoder/chess/Native.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@ object Native {
@JvmStatic
external fun isPlayerWhite(): Boolean

// region Stats

@JvmStatic
external fun getSearchTime(): Double

@JvmStatic
external fun getCurrentBoardValue(): Int

@JvmStatic
external fun getStats(): String
external fun getBestMoveFound(): Int

@JvmStatic
external fun getBoardValue(): Int
external fun getAdvancedStats(): String

// endregion Stats

@JvmStatic
external fun getPieces(): Array<Piece>
Expand All @@ -33,9 +43,6 @@ object Native {
performQuiescenceSearch: Boolean
)

@JvmStatic
external fun getBestMoveFound(): Int

@JvmStatic
external fun undoMoves()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,39 @@
package net.theluckycoder.chess

import android.os.Bundle
import android.preference.PreferenceFragment
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SeekBarPreference
import androidx.preference.SwitchPreferenceCompat
import net.theluckycoder.chess.utils.AppPreferences
import net.theluckycoder.chess.utils.getColor
import kotlin.concurrent.thread
import kotlin.math.min

class SettingsActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
supportActionBar?.setDisplayHomeAsUpEnabled(true)

fragmentManager
supportFragmentManager
.beginTransaction()
.replace(android.R.id.content, SettingsFragment())
.commit()
}

class SettingsFragment : PreferenceFragment() {
class SettingsFragment : PreferenceFragmentCompat() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.preferences)

findPreference(Preferences.KEY_RESET_COLORS).setOnPreferenceClickListener {
findPreference<Preference>(AppPreferences.KEY_RESET_COLORS)?.setOnPreferenceClickListener {
val activity = activity ?: return@setOnPreferenceClickListener false

Preferences(activity).apply {
AppPreferences(activity).apply {
whiteTileColor = getColor(activity, R.color.tile_white)
blackTileColor = getColor(activity, R.color.tile_black)
possibleTileColor = getColor(activity, R.color.tile_possible)
Expand All @@ -39,7 +44,29 @@ class SettingsActivity : AppCompatActivity() {
true
}

findPreference(Preferences.KEY_PERFT_TEST).setOnPreferenceClickListener {
val threadCountPref = findPreference<SeekBarPreference>(AppPreferences.KEY_THREAD_COUNT)
if (threadCountPref != null) {
val defaultValue = min(Runtime.getRuntime().availableProcessors() - 1, 1)
threadCountPref.setDefaultValue(defaultValue)
threadCountPref.max = Runtime.getRuntime().availableProcessors()
}

val darkThemePref =
findPreference<SwitchPreferenceCompat>(AppPreferences.KEY_DARK_THEME)
if (darkThemePref != null) {
darkThemePref.onPreferenceChangeListener =
Preference.OnPreferenceChangeListener { _, newValue ->
val delegateValue = if (newValue == true)
AppCompatDelegate.MODE_NIGHT_YES
else
AppCompatDelegate.MODE_NIGHT_NO

AppCompatDelegate.setDefaultNightMode(delegateValue)
true
}
}

findPreference<Preference>(AppPreferences.KEY_PERFT_TEST)?.setOnPreferenceClickListener {
thread {
Native.perft(5)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package net.theluckycoder.chess
package net.theluckycoder.chess.utils

import android.content.Context
import android.content.SharedPreferences
import android.preference.PreferenceManager
import net.theluckycoder.chess.utils.getColor
import androidx.preference.PreferenceManager
import net.theluckycoder.chess.R
import net.theluckycoder.chess.Settings

class Preferences(private val context: Context) {
class AppPreferences(private val context: Context) {

companion object {
const val KEY_FIRST_START = "key_first_start"

const val KEY_DARK_THEME = "key_dark_theme"
const val KEY_TILE_WHITE = "key_tile_white"
const val KEY_TILE_BLACK = "key_tile_black"
const val KEY_TILE_POSSIBLE = "key_tile_possible"
Expand All @@ -18,11 +20,12 @@ class Preferences(private val context: Context) {
const val KEY_KING_IN_CHECK = "key_king_in_check"
const val KEY_RESET_COLORS = "key_reset_colors"

const val KEY_DEPTH = "key_depth"
const val KEY_THREADS = "key_threads"
const val KEY_SEARCH_DEPTH = "key_search_depth"
const val KEY_THREAD_COUNT = "key_thread_count"
const val KEY_CACHE_SIZE = "key_cache_size"
const val KEY_QUIET_SEARCH = "key_quiet_search"
const val KEY_DEBUG_INFO = "key_debug_info"
const val KEY_DEBUG_INFO_BASIC = "key_debug_basic"
const val KEY_DEBUG_INFO_ADVANCED = "key_debug_advanced"
const val KEY_PERFT_TEST = "key_perft_test"
}

Expand All @@ -33,6 +36,9 @@ class Preferences(private val context: Context) {
get() = manager.getBoolean(KEY_FIRST_START, true)
set(value) = manager.edit().putBoolean(KEY_FIRST_START, value).apply()

val darkTheme
get() = manager.getBoolean(KEY_DARK_THEME, false)

var whiteTileColor
get() = manager.getInt(KEY_TILE_WHITE, getColor(context, R.color.tile_white))
set(value) = manager.edit().putInt(KEY_TILE_WHITE, value).apply()
Expand All @@ -59,21 +65,29 @@ class Preferences(private val context: Context) {

var settings
get() = Settings(
baseSearchDepth = manager.getString(KEY_DEPTH, null)?.toIntOrNull() ?: 4,
threadCount = manager.getString(KEY_THREADS, null)?.toIntOrNull()
?: Runtime.getRuntime().availableProcessors() - 1,
cacheSize = manager.getString(KEY_CACHE_SIZE, null)?.toIntOrNull() ?: 100,
baseSearchDepth = manager.getInt(KEY_SEARCH_DEPTH, 4),
threadCount = manager.getInt(
KEY_THREAD_COUNT,
Runtime.getRuntime().availableProcessors() - 1
),
cacheSize = manager.getString(
KEY_CACHE_SIZE,
null
)?.toIntOrNull() ?: 100,
performQuiescenceSearch = manager.getBoolean(KEY_QUIET_SEARCH, true)
)
set(value) {
manager.edit()
.putString(KEY_DEPTH, value.baseSearchDepth.toString())
.putString(KEY_THREADS, value.threadCount.toString())
.putInt(KEY_SEARCH_DEPTH, value.baseSearchDepth)
.putInt(KEY_THREAD_COUNT, value.threadCount)
.putString(KEY_CACHE_SIZE, value.cacheSize.toString())
.putBoolean(KEY_QUIET_SEARCH, value.performQuiescenceSearch)
.apply()
}

val debugInfo
get() = manager.getBoolean(KEY_DEBUG_INFO, false)
val basicDebugInfo
get() = manager.getBoolean(KEY_DEBUG_INFO_BASIC, false)

val advancedDebugInfo
get() = manager.getBoolean(KEY_DEBUG_INFO_ADVANCED, false)
}
9 changes: 9 additions & 0 deletions ChessAndroid/app/src/main/res/drawable/ic_pref_cache.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="?android:textColorPrimary"
android:pathData="M15,9L9,9v6h6L15,9zM13,13h-2v-2h2v2zM21,11L21,9h-2L19,7c0,-1.1 -0.9,-2 -2,-2h-2L15,3h-2v2h-2L11,3L9,3v2L7,5c-1.1,0 -2,0.9 -2,2v2L3,9v2h2v2L3,13v2h2v2c0,1.1 0.9,2 2,2h2v2h2v-2h2v2h2v-2h2c1.1,0 2,-0.9 2,-2v-2h2v-2h-2v-2h2zM17,17L7,17L7,7h10v10z" />
</vector>
9 changes: 9 additions & 0 deletions ChessAndroid/app/src/main/res/drawable/ic_pref_debug_info.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="?android:textColorPrimary"
android:pathData="M23,8c0,1.1 -0.9,2 -2,2 -0.18,0 -0.35,-0.02 -0.51,-0.07l-3.56,3.55c0.05,0.16 0.07,0.34 0.07,0.52 0,1.1 -0.9,2 -2,2s-2,-0.9 -2,-2c0,-0.18 0.02,-0.36 0.07,-0.52l-2.55,-2.55c-0.16,0.05 -0.34,0.07 -0.52,0.07s-0.36,-0.02 -0.52,-0.07l-4.55,4.56c0.05,0.16 0.07,0.33 0.07,0.51 0,1.1 -0.9,2 -2,2s-2,-0.9 -2,-2 0.9,-2 2,-2c0.18,0 0.35,0.02 0.51,0.07l4.56,-4.55C8.02,9.36 8,9.18 8,9c0,-1.1 0.9,-2 2,-2s2,0.9 2,2c0,0.18 -0.02,0.36 -0.07,0.52l2.55,2.55c0.16,-0.05 0.34,-0.07 0.52,-0.07s0.36,0.02 0.52,0.07l3.55,-3.56C19.02,8.35 19,8.18 19,8c0,-1.1 0.9,-2 2,-2s2,0.9 2,2z" />
</vector>
9 changes: 9 additions & 0 deletions ChessAndroid/app/src/main/res/drawable/ic_pref_stats.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="?android:textColorPrimary"
android:pathData="M19,3L5,3c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2L21,5c0,-1.1 -0.9,-2 -2,-2zM9,17L7,17v-7h2v7zM13,17h-2L11,7h2v10zM17,17h-2v-4h2v4z" />
</vector>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="?android:textColorPrimary"
android:pathData="M17,16l-4,-4V8.82C14.16,8.4 15,7.3 15,6c0,-1.66 -1.34,-3 -3,-3S9,4.34 9,6c0,1.3 0.84,2.4 2,2.82V12l-4,4H3v5h5v-3.05l4,-4.2 4,4.2V21h5v-5h-4z" />
</vector>
Loading