Skip to content

Commit

Permalink
d
Browse files Browse the repository at this point in the history
  • Loading branch information
FSaurenbach committed Jul 30, 2024
1 parent b7d7288 commit 1921cd3
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 182 deletions.
14 changes: 3 additions & 11 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,33 @@ korge {
targetJvm()
targetJs()
targetAndroid()

icon = File(rootDir,"icon.png")
serializationJson()
jvmMainClassName = "MainKt"
}
kotlin {
sourceSets {
val commonMain by getting {
dependencies {
implementation("io.ktor:ktor-client-core:2.1.0")
implementation("io.ktor:ktor-client-json:2.1.0")
implementation("io.ktor:ktor-client-serialization:2.1.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.2")
implementation("com.squareup.okhttp3:okhttp:4.9.2")
}
}
val jvmMain by getting {
dependencies {
implementation("io.ktor:ktor-client-cio:2.1.0")
implementation("com.squareup.okhttp3:okhttp:4.9.2")
}
}
val jsMain by getting {
dependencies {
implementation("io.ktor:ktor-http-cio:1.6.0")
implementation("io.ktor:ktor-client-js:1.6.0")
implementation("com.squareup.okhttp3:okhttp:4.9.2")

}
}
}
}
dependencies {
add("commonMainApi", project(":deps"))
add("commonMainApi", "com.squareup.okhttp3:okhttp:4.9.2")

}
repositories {
mavenCentral()
maven("https://jitpack.io")
}
Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
99 changes: 0 additions & 99 deletions src/commonMain/kotlin/Ai.kt

This file was deleted.

148 changes: 148 additions & 0 deletions src/commonMain/kotlin/ChessAi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import korlibs.io.lang.*
import korlibs.io.net.http.*
import korlibs.io.stream.*
import kotlinx.serialization.json.*

class ChessAi {
fun piecesListToFEN(piecesList: List<Pair<String, Pair<Int, Int>>>): String {
val board = Array(8) { Array(8) { '.' } }

// Place pieces on the board
for (piece in piecesList) {
val pieceType = piece.first
val (x, y) = piece.second
board[y][x] =
when (pieceType) {
"WhitePawn" -> 'P'
"WhiteRook" -> 'R'
"WhiteKnight" -> 'N'
"WhiteBishop" -> 'B'
"WhiteQueen" -> 'Q'
"WhiteKing" -> 'K'
"BlackPawn" -> 'p'
"BlackRook" -> 'r'
"BlackKnight" -> 'n'
"BlackBishop" -> 'b'
"BlackQueen" -> 'q'
"BlackKing" -> 'k'
else -> '.'
}
}

// Convert board to FEN
val fen = StringBuilder()
for (row in board) {
var emptyCount = 0
for (cell in row) {
if (cell == '.') {
emptyCount++
} else {
if (emptyCount > 0) {
fen.append(emptyCount)
emptyCount = 0
}
fen.append(cell)
}
}
if (emptyCount > 0) {
fen.append(emptyCount)
}
fen.append('/')
}
fen.setLength(fen.length - 1) // Remove the last '/'

// Add default FEN fields (assuming it's the white's turn and default castling rights and en
// passant)
// fen.append(" w KQkq - 0 1")
if (whiteTurn) {
fen.append(" w KQkq - 0 1")
} else {
fen.append(" b KQkq - 0 1")
}
return fen.toString()
}

suspend fun postBestMove(fen: String): String {
val client = HttpClient()
val url = "http://192.168.178.54:5000/best_move"
// Create JSON object
val json = JsonObject(mapOf("fen" to JsonPrimitive(fen)))
val jsonString = Json.encodeToString(JsonObject.serializer(), json)

return try {
val response = client.requestAsBytes(
url = url,
method = Http.Methods.POST,
content = jsonString.openAsync(),
headers = Http.Headers(
"Content-Type" to "application/json",
"Content-Length" to jsonString.length.toString()
)
)
val responseString = response.content.decodeToString()
val jsonResponse = Json.parseToJsonElement(responseString).jsonObject
jsonResponse["best_move"]?.jsonPrimitive?.content ?: "No best_move found"

} catch (e: IOException) {
"Request failed: ${e.message}"
}
}

fun convertMoveToPosition(move: String) {
var old_x: Int = 9
var old_y: Int = 9
var new_x: Int = 9
var new_y: Int = 9
when (move[0]) {
'a' -> old_x = 0
'b' -> old_x = 1
'c' -> old_x = 2
'd' -> old_x = 3
'e' -> old_x = 4
'f' -> old_x = 5
'g' -> old_x = 6
'h' -> old_x = 7
}
when (move[1]) {
'1' -> old_y = 7
'2' -> old_y = 6
'3' -> old_y = 5
'4' -> old_y = 4
'5' -> old_y = 3
'6' -> old_y = 2
'7' -> old_y = 1
'8' -> old_y = 0
}
when (move[2]) {
'a' -> new_x = 0
'b' -> new_x = 1
'c' -> new_x = 2
'd' -> new_x = 3
'e' -> new_x = 4
'f' -> new_x = 5
'g' -> new_x = 6
'h' -> new_x = 7
}
when (move[3]) {
'1' -> new_y = 7
'2' -> new_y = 6
'3' -> new_y = 5
'4' -> new_y = 4
'5' -> new_y = 3
'6' -> new_y = 2
'7' -> new_y = 1
'8' -> new_y = 0
}
println("Old position: $old_x, $old_y")
println("New position: $new_x, $new_y")
for (piece in pieces){
if (piece.cx == old_x && piece.cy == old_y && old_x != 9 && old_y != 9 && new_x != 9 && new_y != 9){
figurBewegen(piece, new_x, new_y)
whiteTurn = !whiteTurn
}
}

}
}


Loading

0 comments on commit 1921cd3

Please sign in to comment.