Skip to content

Commit

Permalink
d
Browse files Browse the repository at this point in the history
  • Loading branch information
FSaurenbach committed Sep 3, 2024
1 parent 561eb43 commit d5e3eb0
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 86 deletions.
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/ChessAi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class ChessAi {
var copy = pieces.toMutableList()
for (piece in copy) {
if (piece.cx == oldX && piece.cy == oldY && oldX != 9 && oldY != 9 && newX != 9 && newY != 9) {
if (!whiteTurn && piece.moveChecker(Pair(oldX, oldY), Pair(newX, newY), true, false) && piece.color == Colors.BLACK) {
if (!whiteTurn && piece.moveChecker(Pair(oldX, oldY), Pair(newX, newY), true) && piece.color == Colors.BLACK) {
println(piece)
figurBewegen(piece, newX, newY)
}
Expand Down
56 changes: 40 additions & 16 deletions src/commonMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class GameScene(private val cont: SceneContainer, private val gameMode: String)
if (!whiteTurn && whiteKingInCheck) {
println("White King is still Check, invalid move")
}
if (selectedPiece!!.moveChecker(currentPos!!, newPosition!!, true, false) && whiteTurnOrPassOn) {
if (selectedPiece!!.moveChecker(currentPos!!, newPosition!!, true) && whiteTurnOrPassOn) {
figurBewegen(
selectedPiece!!, newPosition!!.first, newPosition!!.second
)
Expand Down Expand Up @@ -184,33 +184,57 @@ class GameScene(private val cont: SceneContainer, private val gameMode: String)
}
}

fun inCheck(): Boolean {
fun inCheck(dmrc: MutableList<Piece>? = null): Boolean {
if (dmrc == null){
val piecesCopy = pieces.toMutableList()

val piecesCopy = pieces.toMutableList()
for (piece in piecesCopy) {
if (piece.kind == PieceKind.WhiteKing) {
whiteKingPosition = Pair(piece.cx, piece.cy)
// println("White King Position: $whiteKingPosition")
}
if (piece.kind == PieceKind.BlackKing) {
blackKingPosition = Pair(piece.cx, piece.cy)
// println("Black King Position: $blackKingPosition")
for (bp in piecesCopy) {

if (bp.moveChecker(Pair(bp.cx, bp.cy), whiteKingPosition, false)) {

println("Piece ${bp.kind} at location x:${bp.cx}y:${bp.cy}is attacking the white king")
whiteKingInCheck = true
return true
}
if (bp.moveChecker(Pair(bp.cx, bp.cy), blackKingPosition, false)) {
blackKingInCheck = true
println("Piece ${bp.kind} at location x:${bp.cx}y:${bp.cy}is attacking the black king")
return true
}
}
return false
}
val foundsmt = false
for (bp in piecesCopy) {

if (bp.moveChecker(Pair(bp.cx, bp.cy), whiteKingPosition, false, true)) {
for (bp in dmrc) {

if (bp.moveChecker(Pair(bp.cx, bp.cy), whiteKingPosition, false)) {

println("Piece ${bp.kind} at location x:${bp.cx}y:${bp.cy}is attacking the white king")
whiteKingInCheck = true
return true
} else whiteKingInCheck = foundsmt
if (bp.moveChecker(Pair(bp.cx, bp.cy), blackKingPosition, false, fromInCheck = true)) {
}
if (bp.moveChecker(Pair(bp.cx, bp.cy), blackKingPosition, false)) {
blackKingInCheck = true
println("Piece ${bp.kind} at location x:${bp.cx}y:${bp.cy}is attacking the black king")
return true
}
}
return false

}

fun doesMoveResolveCheck(oldPos: Pair<Int, Int>, newPos: Pair<Int, Int>){
var dmrc = pieces.toMutableList()
for (pieceToBeMoved in dmrc){
if (pieceToBeMoved.cx == oldPos.first && pieceToBeMoved.cy == oldPos.second){
pieceToBeMoved.moveChecker(oldPos, newPos, false)
}
}
inCheck(dmrc)
if (whiteKingInCheck){

}
else if (blackKingInCheck){

}
}
97 changes: 28 additions & 69 deletions src/commonMain/kotlin/Piece.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Piece(
* @return true if the move is valid, false otherwise.
*/
fun moveChecker(
oldPos: Pair<Int, Int>, newPos: Pair<Int, Int>, performMove: Boolean, fromInCheck: Boolean
oldPos: Pair<Int, Int>, newPos: Pair<Int, Int>, performMove: Boolean
): Boolean {
val pieceOnNewPos = schachbrett!!.findPiece(newPos.first, newPos.second)
// Check if white or black king in check
Expand Down Expand Up @@ -117,13 +117,9 @@ class Piece(
PieceKind.WhiteRook -> moveRook(oldPos, newPos, pieceOnNewPos, true, performMove)
PieceKind.BlackRook -> moveRook(oldPos, newPos, pieceOnNewPos, false, performMove)
PieceKind.WhiteKnight -> moveKnight(oldPos, newPos, pieceOnNewPos, true, performMove)

PieceKind.BlackKnight -> moveKnight(oldPos, newPos, pieceOnNewPos, false, performMove)

PieceKind.WhiteBishop -> moveBishop(oldPos, newPos, pieceOnNewPos, true, performMove)

PieceKind.BlackBishop -> moveBishop(oldPos, newPos, pieceOnNewPos, false, performMove)

PieceKind.WhiteQueen -> moveQueen(oldPos, newPos, pieceOnNewPos, true, performMove)
PieceKind.BlackQueen -> moveQueen(oldPos, newPos, pieceOnNewPos, false, performMove)
PieceKind.WhiteKing -> moveKing(oldPos, newPos, pieceOnNewPos, true, performMove)
Expand Down Expand Up @@ -433,91 +429,54 @@ class Piece(
private fun moveQueen(
oldPos: Pair<Int, Int>, newPos: Pair<Int, Int>, pieceOnNewPos: Piece?, isWhite: Boolean, performMove: Boolean
): Boolean {
return moveRook(oldPos, newPos, pieceOnNewPos, isWhite, performMove) || moveBishop(
oldPos, newPos, pieceOnNewPos, isWhite, performMove
)
return moveRook(oldPos, newPos, pieceOnNewPos, isWhite, performMove) ||
moveBishop(oldPos, newPos, pieceOnNewPos, isWhite, performMove)
}

private fun moveKing(
oldPos: Pair<Int, Int>, newPos: Pair<Int, Int>, pieceOnNewPos: Piece?, isWhite: Boolean, performMove: Boolean
): Boolean {
// Determine the move direction
val deltaX = newPos.first - oldPos.first
val deltaY = newPos.second - oldPos.second
val (deltaX, deltaY) = newPos.first - oldPos.first to newPos.second - oldPos.second

// Check if the move is within the valid range for a king
if (abs(deltaX) <= 1 && abs(deltaY) <= 1) {


// Check if there is a piece on the new position and its color
if (pieceOnNewPos != null) {
if (pieceOnNewPos.color == color) {
return false
} else {
if (performMove) {
removePiece(pieceOnNewPos)
}
}
}

// If performMove is true, toggle the turn
if (performMove) {
whiteTurn = !isWhite
pieceOnNewPos?.let {
if (it.color == color) return false
if (performMove) removePiece(it)
}
if (performMove) whiteTurn = !isWhite
println("color: $color , newPosColor: ${pieceOnNewPos?.color}")
println("king position variable: white: ${whiteKingPosition} black: ${blackKingPosition}")
blackKingPosition = Pair(newPos.first, newPos.second)
println("new king position variable: white: ${whiteKingPosition} black: ${blackKingPosition}")
return true
}
// Rochade
if (whiteRochade && isWhite && oldPos.first == 4 && oldPos.second == 7) {
when {
newPos.first == 6 && newPos.second == 7 -> {
if (performMove) {
val rook = schachbrett!!.findPiece(7, 7)
figurBewegen(rook!!, 5, 7)
whiteRochade = false
whiteTurn = false
}
return true
}

newPos.first == 2 && newPos.second == 7 -> {
if (performMove) {
val rook = schachbrett!!.findPiece(0, 7)
figurBewegen(rook!!, 3, 7)
whiteRochade = false
whiteTurn = false
}
return true
}
// Rochade
if (isWhite && whiteRochade && oldPos == 4 to 7) {
when (newPos) {
6 to 7 -> return performRochade(7, 7, 5, 7, false)
2 to 7 -> return performRochade(0, 7, 3, 7, false)
}
}
if (blackRochade && !isWhite && oldPos.first == 4 && oldPos.second == 0) {
when {
newPos.first == 6 && newPos.second == 0 -> {
if (performMove) {
val rook = schachbrett!!.findPiece(7, 0)
figurBewegen(rook!!, 5, 0)
blackRochade = false
whiteTurn = true
}
return true
}

newPos.first == 2 && newPos.second == 0 -> {
if (performMove) {
val rook = schachbrett!!.findPiece(0, 0)
figurBewegen(rook!!, 3, 0)
blackRochade = false
whiteTurn = true
}
return true
}
if (!isWhite && blackRochade && oldPos == 4 to 0) {
when (newPos) {
6 to 0 -> return performRochade(7, 0, 5, 0, true)
2 to 0 -> return performRochade(0, 0, 3, 0, true)
}
}

return false
}

private fun performRochade(rookX: Int, rookY: Int, newX: Int, newY: Int, turnWhite: Boolean): Boolean {
val rook = schachbrett!!.findPiece(rookX, rookY) ?: return false
figurBewegen(rook, newX, newY)
if (turnWhite) blackRochade = false else whiteRochade = false
whiteTurn = turnWhite
return true
}

fun removePiece(piece: Piece) {
pieces.remove(piece)
piece.piece.removeFromParent()
Expand Down

0 comments on commit d5e3eb0

Please sign in to comment.