Skip to content

Commit

Permalink
fixed bishop
Browse files Browse the repository at this point in the history
  • Loading branch information
FSaurenbach committed Mar 18, 2024
1 parent 86bef58 commit 767930a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ plugins { alias(libs.plugins.korge) }

korge {
id = "de.fsaurenbach.sauronchess"

name = "Sauronchess"
fullscreen = false
targetJvm()
targetJs()
targetAndroid()
Expand Down
4 changes: 2 additions & 2 deletions src/commonMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class GameScene(private val cont: SceneContainer) : PixelatedScene(512, 512) {
// When dragging starts
if (info.start) {
// Iterate through pieces to find the selected piece
//println("Start dragging...")
// println("Start dragging...")
if (piece.position ==
board[newPosition!!.second][newPosition!!.first].pos) {
currentPos = newPosition
Expand All @@ -174,7 +174,7 @@ class GameScene(private val cont: SceneContainer) : PixelatedScene(512, 512) {

// When dragging ends
if (info.end && selectedPiece != null) {
//println("End dragging...")
// println("End dragging...")
// Check if newPosition is within the game board
if (newPosition!!.first < 0 ||
newPosition!!.first >= 8 ||
Expand Down
33 changes: 21 additions & 12 deletions src/commonMain/kotlin/Piece.kt
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ class Piece(
*/
fun moveChecker(oldPos: Pair<Int, Int>, newPos: Pair<Int, Int>, withCheck: Boolean): Boolean {
val pieceOnNewPos = pieces.find { it.position == board[newPos.second][newPos.first].pos }

if (pieceOnNewPos != null && pieceOnNewPos.color == color) {
// Prevent moving to a cell occupied by a piece of the same color during the check for
// valid moves
return false
}
return if (whiteTurn) {
when (pieceKind) {
PieceKind.WhitePawn -> moveWhitePawn(oldPos, newPos, pieceOnNewPos, withCheck)
Expand All @@ -131,7 +135,7 @@ class Piece(
PieceKind.BlackPawn -> moveBlackPawn(oldPos, newPos, pieceOnNewPos, withCheck)
PieceKind.BlackRook -> moveRook(oldPos, newPos, pieceOnNewPos, withCheck, false)
PieceKind.BlackKnight -> moveBlackKnight(oldPos, newPos, pieceOnNewPos, withCheck)
PieceKind.BlackBishop -> TODO()
PieceKind.BlackBishop -> moveBishop(oldPos, newPos, pieceOnNewPos, withCheck, false)
PieceKind.BlackQueen -> TODO()
PieceKind.BlackKing -> TODO()
else -> false
Expand Down Expand Up @@ -326,19 +330,21 @@ class Piece(
val p1 = abs(oldPos.first - newPos.first)
val p2 = abs(oldPos.second - newPos.second)
val p = if (p1 == p2) p1 else null
val color = if (isWhite) Colors.WHITE else Colors.BLACK

// Check if piece is moving right or left, up or down
val right = oldPos.first < newPos.first
val left = oldPos.first > newPos.first
val down = oldPos.second > newPos.second
val up = oldPos.second < newPos.second
println("Old position: $oldPos")
println("New position: $newPos")
println("All pieces:")
for (piece in pieces) {
val piecePos = decodePosition(piece.position)
println("Piece at position: $piecePos")
}

if (p != null) {
// Check for obstacles in the bishop's path
for (i in 1 until p) {
val x = if (right) oldPos.first + i else oldPos.first - i
val y = if (down) oldPos.second - i else oldPos.second + i

val x = if (oldPos.first < newPos.first) oldPos.first + i else oldPos.first - i
val y = if (oldPos.second < newPos.second) oldPos.second + i else oldPos.second - i
for (piece in pieces) {
val piecePos = decodePosition(piece.position)
if (piecePos.first == x && piecePos.second == y) {
Expand All @@ -350,7 +356,11 @@ class Piece(
// If there are no obstacles, perform the move
if (withCheck) {
if (pieceOnNewPos != null) {
removePiece(pieceOnNewPos)
if (pieceOnNewPos.color == color) {
return false
} else {
removePiece(pieceOnNewPos)
}
}
whiteTurn = !isWhite
}
Expand All @@ -359,7 +369,6 @@ class Piece(
return false
}


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

0 comments on commit 767930a

Please sign in to comment.