Skip to content

Commit

Permalink
check for kings
Browse files Browse the repository at this point in the history
  • Loading branch information
FSaurenbach committed May 14, 2024
1 parent 6100836 commit 1949fa1
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
43 changes: 42 additions & 1 deletion src/commonMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ var whiteKnight: Bitmap? = null
var whiteBishop: Bitmap? = null
var whiteQueen: Bitmap? = null
var whiteKing: Bitmap? = null
var whiteKingInCheck = false
var whiteKingPosition = Pair(4, 7)

var blackPawn: Bitmap? = null
var blackRook: Bitmap? = null
var blackKnight: Bitmap? = null
var blackBishop: Bitmap? = null
var blackQueen: Bitmap? = null
var blackKing: Bitmap? = null
var blackKingInCheck = false
var blackKingPosition = Pair(4, 0)

var whiteTurn = true

Expand Down Expand Up @@ -54,6 +58,7 @@ class MyScene(private val cont: SceneContainer) : PixelatedScene(512, 512) {
blackKnight = resourcesVfs["b_knight.png"].readBitmap()
blackBishop = resourcesVfs["b_bishop.png"].readBitmap()
blackQueen = resourcesVfs["b_queen.png"].readBitmap()
blackKing = resourcesVfs["b_king.png"].readBitmap()

addAllPieces(cont)
handlePieceMovement()
Expand All @@ -80,6 +85,7 @@ class MyScene(private val cont: SceneContainer) : PixelatedScene(512, 512) {
if (info.start) {
// Iterate through pieces to find the selected piece
// //println("Start dragging...")
// Set king position
val pieceAtCurrentPos =
schachbrett!!.findPiece(newPosition!!.first, newPosition!!.second)

Expand All @@ -106,11 +112,17 @@ class MyScene(private val cont: SceneContainer) : PixelatedScene(512, 512) {
}
// Perform the move if no error
if (!error) {
if (selectedPiece!!.moveChecker(currentPos!!, newPosition!!)) {
if (selectedPiece!!.moveChecker(currentPos!!, newPosition!!, true)) {
figurBewegen(
selectedPiece!!, newPosition!!.first, newPosition!!.second)
selectedPiece = null
}
// Check if king is in Check
inCheck()



// Reset variables
selectedPiece = null
newPosition = null
currentPos = null
Expand All @@ -122,3 +134,32 @@ class MyScene(private val cont: SceneContainer) : PixelatedScene(512, 512) {
}
}
}
fun inCheck(){

println("checking for check...")
for (piece in pieces) {
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 pieces){

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

println("Piece ${bp.kind} at location x:${bp.cx}y:${bp.cy}is attacking the white king")
}
else whiteKingInCheck = false
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")
}
else blackKingInCheck = false
}
}

22 changes: 19 additions & 3 deletions src/commonMain/kotlin/Piece.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class Piece(
*/
fun moveChecker(oldPos: Pair<Int, Int>, newPos: Pair<Int, Int>, performMove:Boolean): Boolean {
val pieceOnNewPos = schachbrett!!.findPiece(newPos.first, newPos.second)
if (whiteTurn) {
if (whiteTurn && performMove) {
return when (pieceKind) {
PieceKind.WhitePawn -> moveWhitePawn(oldPos, newPos, pieceOnNewPos, performMove)
PieceKind.WhiteRook -> moveRook(oldPos, newPos, pieceOnNewPos, true, performMove)
Expand All @@ -105,7 +105,7 @@ class Piece(
else -> false
}
}
if (!whiteTurn) {
if (!whiteTurn && performMove) {
return when (pieceKind) {
PieceKind.BlackPawn -> moveBlackPawn(oldPos, newPos, pieceOnNewPos, performMove)
PieceKind.BlackRook -> moveRook(oldPos, newPos, pieceOnNewPos, false, performMove)
Expand All @@ -116,6 +116,22 @@ class Piece(
else -> false
}
}
if (!performMove){
return when (pieceKind){
PieceKind.WhitePawn -> moveWhitePawn(oldPos, newPos, pieceOnNewPos, performMove)
PieceKind.BlackPawn -> moveBlackPawn(oldPos, newPos, pieceOnNewPos, performMove)
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)
PieceKind.BlackKing -> moveKing(oldPos, newPos, pieceOnNewPos, false, performMove)
}
}

return false
}
Expand Down Expand Up @@ -666,7 +682,7 @@ fun addAllPieces(cont: SceneContainer) {
val whiteBishop1 = Piece(PieceKind.WhiteBishop, Colors.WHITE, 2, 0, cont)
val whiteBishop2 = Piece(PieceKind.WhiteBishop, Colors.WHITE, 5, 0, cont)
val whiteQueen = Piece(PieceKind.WhiteQueen, Colors.WHITE, 3, 0, cont)
val whiteKing = Piece(PieceKind.WhiteKing, Colors.WHITE, 4, 4, cont)
val whiteKing = Piece(PieceKind.WhiteKing, Colors.WHITE, 4, 0, cont)

// black pieces
val blackPawn1 = Piece(PieceKind.BlackPawn, Colors.BLACK, 0, 6, cont)
Expand Down

0 comments on commit 1949fa1

Please sign in to comment.