From d5e3eb010eaa2f92197535228b40b88a8eb6f884 Mon Sep 17 00:00:00 2001 From: Ferdinand Saurenbach Date: Tue, 3 Sep 2024 11:53:18 +0200 Subject: [PATCH] d --- src/commonMain/kotlin/ChessAi.kt | 2 +- src/commonMain/kotlin/Main.kt | 56 ++++++++++++------ src/commonMain/kotlin/Piece.kt | 97 +++++++++----------------------- 3 files changed, 69 insertions(+), 86 deletions(-) diff --git a/src/commonMain/kotlin/ChessAi.kt b/src/commonMain/kotlin/ChessAi.kt index edc9600..f07662c 100644 --- a/src/commonMain/kotlin/ChessAi.kt +++ b/src/commonMain/kotlin/ChessAi.kt @@ -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) } diff --git a/src/commonMain/kotlin/Main.kt b/src/commonMain/kotlin/Main.kt index 75e89f3..7165157 100644 --- a/src/commonMain/kotlin/Main.kt +++ b/src/commonMain/kotlin/Main.kt @@ -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 ) @@ -184,33 +184,57 @@ class GameScene(private val cont: SceneContainer, private val gameMode: String) } } -fun inCheck(): Boolean { +fun inCheck(dmrc: MutableList? = 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, newPos: Pair){ + 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){ + + } } diff --git a/src/commonMain/kotlin/Piece.kt b/src/commonMain/kotlin/Piece.kt index 7123f88..6f47725 100644 --- a/src/commonMain/kotlin/Piece.kt +++ b/src/commonMain/kotlin/Piece.kt @@ -79,7 +79,7 @@ class Piece( * @return true if the move is valid, false otherwise. */ fun moveChecker( - oldPos: Pair, newPos: Pair, performMove: Boolean, fromInCheck: Boolean + oldPos: Pair, newPos: Pair, performMove: Boolean ): Boolean { val pieceOnNewPos = schachbrett!!.findPiece(newPos.first, newPos.second) // Check if white or black king in check @@ -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) @@ -433,91 +429,54 @@ class Piece( private fun moveQueen( oldPos: Pair, newPos: Pair, 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, newPos: Pair, 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()