-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from nwagu/dev
Dev
- Loading branch information
Showing
67 changed files
with
8,038 additions
and
341 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
androidApp/src/main/java/com/nwagu/android/chessboy/players/GrandPa.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.nwagu.android.chessboy.players | ||
|
||
import com.nwagu.chess.board.Board | ||
import com.nwagu.chess.board.squaresWithPiecesColored | ||
import com.nwagu.chess.board.turn | ||
import com.nwagu.chess.moves.Move | ||
import com.nwagu.chess.moves.getPossibleMovesFrom | ||
|
||
class GrandPa : MoveGenerator { | ||
|
||
override val id = PlayersRegister.GRANDPA.id | ||
override val name = "GrandPa" | ||
|
||
override suspend fun getNextMove(board: Board): Move? { | ||
|
||
val moves = mutableListOf<Move>() | ||
|
||
board.squaresWithPiecesColored(board.turn).forEach { | ||
moves.addAll(board.getPossibleMovesFrom(it)) | ||
} | ||
|
||
// TODO return a decent choice of move | ||
return moves.randomOrNull() | ||
|
||
} | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
androidApp/src/main/java/com/nwagu/android/chessboy/players/JWTC.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.nwagu.android.chessboy.players | ||
|
||
import com.nwagu.chess.board.Board | ||
import com.nwagu.chess.convention.getFen | ||
import com.nwagu.chess.enums.ChessPieceType | ||
import com.nwagu.chess.moves.* | ||
import jwtc.chess.BoardConstants | ||
import jwtc.chess.JNI | ||
import jwtc.chess.Move.* | ||
|
||
class JWTC: AI { | ||
|
||
override val id = PlayersRegister.JWTC.id | ||
override val name = "JWTC" | ||
override var level = 5 | ||
|
||
lateinit var jni: JNI | ||
|
||
override fun init() { | ||
jni = JNI() | ||
} | ||
|
||
override suspend fun getNextMove(board: Board): Move? { | ||
|
||
jni.newGame() | ||
jni.initFEN(board.getFen()) | ||
jni.searchDepth(level) | ||
|
||
val move = jni.move | ||
|
||
if (move == 0) | ||
return null | ||
|
||
val source: Int = getFrom(move) | ||
val destination: Int = getTo(move) | ||
|
||
if (isPromotionMove(move)) { | ||
val promotionPieceType = when (getPromotionPiece(move)) { | ||
BoardConstants.QUEEN -> ChessPieceType.QUEEN | ||
BoardConstants.KNIGHT -> ChessPieceType.KNIGHT | ||
BoardConstants.ROOK -> ChessPieceType.ROOK | ||
BoardConstants.BISHOP -> ChessPieceType.BISHOP | ||
else -> throw IllegalStateException("Invalid Promotion Piece!") | ||
} | ||
return Promotion(source, destination, promotionPieceType) | ||
} | ||
|
||
if (isEP(move)) { | ||
return EnPassant(source, destination) | ||
} | ||
|
||
if (isOOO(move)) { | ||
return Castling(source, destination) | ||
} | ||
|
||
if (isOO(move)) { | ||
return Castling(source, destination) | ||
} | ||
|
||
return RegularMove(source, destination) | ||
|
||
} | ||
|
||
} |
23 changes: 11 additions & 12 deletions
23
...droid/chessboy/movesgenerators/Players.kt → ...nwagu/android/chessboy/players/Players.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,27 @@ | ||
package com.nwagu.android.chessboy.movesgenerators | ||
package com.nwagu.android.chessboy.players | ||
|
||
import com.nwagu.android.chessboy.widgets.SelectableOpponent | ||
import com.nwagu.chess.Player | ||
import com.nwagu.chess.board.Board | ||
import com.nwagu.chess.enums.Level | ||
import com.nwagu.chess.moves.Move | ||
|
||
object User: Player { | ||
override val id = PlayersRegister.USER.id | ||
override val name = "You" | ||
override val level = Level.INDETERMINATE | ||
} | ||
|
||
data class BluetoothOpponent( | ||
override val name: String = "Bluetooth Opponent", | ||
val address: String?, | ||
override val level: Level = Level.INDETERMINATE | ||
): Player, SelectableOpponent { | ||
val address: String, | ||
override val id: String = "${PlayersRegister.BLUETOOTH.id}-${address}" | ||
): SelectableOpponent | ||
|
||
override val displayName = name | ||
override var selected = false | ||
} | ||
|
||
interface AI: Player, SelectableOpponent { | ||
interface MoveGenerator: SelectableOpponent { | ||
override val name: String | ||
override val level: Level | ||
suspend fun getNextMove(board: Board): Move? | ||
} | ||
|
||
interface AI: MoveGenerator { | ||
var level: Int | ||
fun init() | ||
} |
10 changes: 10 additions & 0 deletions
10
androidApp/src/main/java/com/nwagu/android/chessboy/players/PlayersRegister.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.nwagu.android.chessboy.players | ||
|
||
enum class PlayersRegister(val id: String) { | ||
USER(User::class.java.simpleName), | ||
GRANDPA(GrandPa::class.java.simpleName), | ||
RANDOM(RandomMoveGenerator::class.java.simpleName), | ||
JWTC(com.nwagu.android.chessboy.players.JWTC::class.java.simpleName), | ||
BLUETOOTH(BluetoothOpponent::class.java.simpleName), | ||
UCI(UCIChessEngine::class.java.simpleName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
androidApp/src/main/java/com/nwagu/android/chessboy/players/UCIChessEngine.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.nwagu.android.chessboy.players | ||
|
||
import com.nwagu.chess.board.Board | ||
import com.nwagu.chess.convention.convertChessEngineMoveToMove | ||
import com.nwagu.chess.convention.getFen | ||
import com.nwagu.chess.moves.Move | ||
import com.nwagu.chessengineintegration.UCIBridge | ||
import kotlinx.coroutines.GlobalScope | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.collect | ||
import kotlinx.coroutines.launch | ||
import timber.log.Timber | ||
|
||
class UCIChessEngine( | ||
override val name: String, | ||
val pathToBinary: String | ||
): AI { | ||
|
||
override val id = "${PlayersRegister.UCI.id}-${pathToBinary}" | ||
override var level: Int = 5 | ||
|
||
lateinit var uciBridge: UCIBridge | ||
|
||
private val chessEngineMove = MutableStateFlow("") | ||
|
||
override fun init() { | ||
|
||
uciBridge = UCIBridge(pathToBinary, object : UCIBridge.UCIEngineListener { | ||
override fun onUciOk() { | ||
Timber.i("UCI OK") | ||
} | ||
|
||
override fun onReady() { | ||
Timber.i("UCI Ready") | ||
} | ||
|
||
override fun onBestMove(move: String) { | ||
Timber.i(move) | ||
chessEngineMove.value = move | ||
} | ||
|
||
override fun onCopyProtection() { | ||
Timber.i("copy protection") | ||
} | ||
|
||
override fun onId(id: Pair<String, String>) { | ||
Timber.i("ID received: ${id.first} = ${id.second}") | ||
} | ||
|
||
override fun onInfo(info: String) { | ||
Timber.i("INFO received: ${info}") | ||
} | ||
|
||
override fun onOption(option: Pair<String, String>) { | ||
Timber.i("Option received: ${option.first} = ${option.second}") | ||
} | ||
|
||
override fun onError(error: String) { | ||
Timber.e(error) | ||
} | ||
|
||
}) | ||
|
||
uciBridge.init() | ||
} | ||
|
||
override suspend fun getNextMove(board: Board): Move? { | ||
|
||
var move: String? = null | ||
|
||
val job = GlobalScope.launch { | ||
chessEngineMove.collect { | ||
move = it | ||
} | ||
} | ||
|
||
uciBridge.play(board.getFen(), level) | ||
|
||
while (move == null) { | ||
// | ||
} | ||
|
||
job.cancel() | ||
|
||
return board.convertChessEngineMoveToMove(move ?: "") | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.