Skip to content

Commit

Permalink
d
Browse files Browse the repository at this point in the history
  • Loading branch information
FSaurenbach committed Mar 13, 2024
1 parent f5328e0 commit c546dfa
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 114 deletions.
134 changes: 65 additions & 69 deletions src/commonMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ class GameScene(private val cont: SceneContainer) : PixelatedScene(512, 512) {
val whiteKnight2 = Piece(PieceKind.WhiteKnight, Colors.WHITE, 6, 0, cont = cont)
val blackKnight1 = Piece(PieceKind.BlackKnight, Colors.BLACK, 1, 7, cont = cont)
val blackKnight2 = Piece(PieceKind.BlackKnight, Colors.BLACK, 6, 7, cont = cont)
val whiteKing = Piece(PieceKind.WhiteKing, Colors.WHITE, 3, 0, cont = cont)

pieces.addAll(
whitePawns +
Expand All @@ -101,91 +100,88 @@ class GameScene(private val cont: SceneContainer) : PixelatedScene(512, 512) {
whiteKnight1,
whiteKnight2,
blackKnight1,
blackKnight2
)
)
blackKnight2))
}

private fun SContainer.handlePieceMovement() {

fun SContainer.handlePieceMovement() {
// Variables initialization
var newPosition: Pair<Int, Int>? = null
var currentPos: Pair<Int, Int>? = null
var selectedPiece: Piece? = null
var error = false
println("Stufe -1")
draggableCloseable(onMouseDrag { newPosition = decodePosition(this.globalMousePos) }) { info
->
error = false
if (info.start) {
println("Stufe 0")
for (piece in pieces) {
if (piece.position == board[newPosition!!.second][newPosition!!.first].pos) {
currentPos = newPosition
selectedPiece = piece
for (cell in cells) {
val clxy = Pair(cell.cx, cell.cy)
if (newPosition!! == clxy) {
println("Found Cell where piece is located: ${cell.cx}, ${cell.cy}")
}
if (selectedPiece!!.moveChecker(newPosition!!, clxy, false)) {
println("Can move to: ${cell.cx}, ${cell.cy}")
changeColor(cell.cy, cell.cx, false)
markedCells.add(cell)

// Function to handle piece movement
draggableCloseable(
onMouseDrag {
// When dragging starts, update newPosition and newPositionEncoded
newPosition = decodePosition(this.globalMousePos)
}) { info ->
error = false

// When dragging starts
if (info.start) {
// Iterate through pieces to find the selected piece
println("Start dragging...")
for (piece in pieces) {
if (piece.position ==
board[newPosition!!.second][newPosition!!.first].pos) {
currentPos = newPosition
selectedPiece = piece

// Iterate through cells to mark available moves
for (cell in cells) {
if (newPosition == Pair(cell.cx, cell.cy)) {
println(
"Found Cell where piece is located: ${cell.cx}, ${cell.cy}")
}
if (selectedPiece!!.moveChecker(
newPosition!!, Pair(cell.cx, cell.cy), false)) {
println("Can move to: ${cell.cx}, ${cell.cy}")
changeColor(cell.cy, cell.cx, false)
markedCells.add(cell)
}
}
}
}
}
}
if (info.end && selectedPiece != null) {
// Check if the mouse position is within the game window decode the position and
// check if it's smaller than 8, 8

if (
newPosition!!.first < 0 ||
// When dragging ends
if (info.end && selectedPiece != null) {
println("End dragging...")
// Check if newPosition is within the game board
if (newPosition!!.first < 0 ||
newPosition!!.first >= 8 ||
newPosition!!.second < 0 ||
newPosition!!.second >= 8
) {
error = true
println(
"Invalid move: Position out of bounds. newPosition: (${newPosition!!.first}, ${newPosition!!.second})"
)

// Resetting variables
selectedPiece = null
newPosition!!.second >= 8) {
error = true
println(
"Invalid move: Position out of bounds. newPosition: (${newPosition!!.first}, ${newPosition!!.second})")
// Reset variables
selectedPiece = null
newPosition = null
currentPos = null
}

// Perform the move if no error
if (!error) {
if (selectedPiece!!.moveChecker(currentPos!!, newPosition!!, true)) {
selectedPiece!!.moveTo(newPosition!!.first, newPosition!!.second)
} else {
selectedPiece!!.moveTo(currentPos!!.first, currentPos!!.second)
}
selectedPiece = null
newPosition = null
currentPos = null
}

// Reset colors and variables
for (cell in markedCells) {
changeColor(cell.cy, cell.cx, true)
}
markedCells.clear()
error = false
newPosition = null
currentPos = null
println(
"Invalid move: Position out of bounds. newPosition: (${newPosition!!.first}, ${newPosition!!.second})"
)
}
println(
"ALL STATES: newPosition: $newPosition currentPos: $currentPos selectedPiece: $selectedPiece error: $error \n \n \n \n \n"
)
println("End \n \n \n \n \n")
if (!error) {
if (selectedPiece!!.moveChecker(currentPos!!, newPosition!!, true)) {
selectedPiece!!.moveTo(newPosition!!.first, newPosition!!.second)
} else {
selectedPiece!!.moveTo(currentPos!!.first, currentPos!!.second)
}
}
for (cell in markedCells) {
changeColor(cell.cy, cell.cx, true)
}
selectedPiece = null
markedCells.clear()
error = false
newPosition = null
currentPos = null
println("Stufe 1")
}
println("Stufe 2")
}

println("Stufe 3")
}
}

Expand Down
64 changes: 19 additions & 45 deletions src/commonMain/kotlin/Piece.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ enum class PieceKind {
BlackRook,
WhiteKnight,
BlackKnight,
WhiteKing,
}

/**
Expand All @@ -25,6 +24,7 @@ enum class PieceKind {
fun decodePosition(cxy: Point): Pair<Int, Int> {
val x = cxy.x / 64
val y = cxy.y / 64
println("Encoded: $cxy, Decoded: $x, $y")
return Pair(x.toInt(), y.toInt())
}

Expand Down Expand Up @@ -56,24 +56,19 @@ class Piece(
Image(
if (kind == PieceKind.WhitePawn) whitePawn!!
else if (kind == PieceKind.WhiteRook) whiteRook!!
else if (kind == PieceKind.WhiteKnight) whiteKnight!!
else if (kind == PieceKind.WhiteKing) whiteKing!! else whiteKing!!
)
else if (kind == PieceKind.WhiteKnight) whiteKnight!! else throw Error("bruh"))
piece.size(Size(64, 64))
piece.addTo(cont)
moveTo(cx, cy)
} else {
// If the piece is black, set the piece image to the black pawn or rook or knight
if (
kind == PieceKind.BlackPawn ||
kind == PieceKind.BlackRook ||
kind == PieceKind.BlackKnight
) {
if (kind == PieceKind.BlackPawn ||
kind == PieceKind.BlackRook ||
kind == PieceKind.BlackKnight) {
piece =
Image(
if (kind == PieceKind.BlackPawn) blackPawn!!
else if (kind == PieceKind.BlackRook) blackRook!! else blackKnight!!
)
else if (kind == PieceKind.BlackRook) blackRook!! else blackKnight!!)
piece.size(Size(64, 64))
piece.addTo(cont)
moveTo(cx, cy)
Expand Down Expand Up @@ -108,7 +103,6 @@ class Piece(
PieceKind.WhitePawn -> moveWhitePawn(oldPos, newPos, pieceOnNewPos, withCheck)
PieceKind.WhiteRook -> moveWhiteRook(oldPos, newPos, pieceOnNewPos, withCheck)
PieceKind.WhiteKnight -> moveKnight(oldPos, newPos, pieceOnNewPos, withCheck)
PieceKind.WhiteKing -> moveWhiteKing(oldPos, newPos, pieceOnNewPos, withCheck)
else -> false
}
} else {
Expand All @@ -121,17 +115,6 @@ class Piece(
}
}

private fun moveWhiteKing(
oldPos: Pair<Int, Int>,
newPos: Pair<Int, Int>,
pieceOnNewPos: Piece?,
withCheck: Boolean
): Boolean {

return moveWhiteRook(oldPos, newPos, pieceOnNewPos, withCheck)

}

private fun moveWhitePawn(
oldPos: Pair<Int, Int>,
newPos: Pair<Int, Int>,
Expand All @@ -147,10 +130,8 @@ class Piece(
if (withCheck) whiteTurn = false
return true
}
} else if (
newPos.second - oldPos.second == 1 &&
((newPos.first - oldPos.first == 1) || (newPos.first - oldPos.first == -1))
) {
} else if (newPos.second - oldPos.second == 1 &&
((newPos.first - oldPos.first == 1) || (newPos.first - oldPos.first == -1))) {
if (pieceOnNewPos != null && pieceOnNewPos.color == Colors.BLACK) {
if (withCheck) {
removePiece(pieceOnNewPos)
Expand All @@ -177,10 +158,8 @@ class Piece(
if (withCheck) whiteTurn = true
return true
}
} else if (
newPos.second - oldPos.second == -1 &&
((newPos.first - oldPos.first == 1) || (newPos.first - oldPos.first == -1))
) {
} else if (newPos.second - oldPos.second == -1 &&
((newPos.first - oldPos.first == 1) || (newPos.first - oldPos.first == -1))) {
if (pieceOnNewPos != null && pieceOnNewPos.color == Colors.WHITE) {
if (withCheck) {
removePiece(pieceOnNewPos)
Expand All @@ -205,19 +184,15 @@ class Piece(
val col = decodePosition(piece.position).second

// Skip the current piece if it's at the old or new position
if (
(row == oldPos.first && col == oldPos.second) ||
(row == newPos.first && col == newPos.second)
) {
if ((row == oldPos.first && col == oldPos.second) ||
(row == newPos.first && col == newPos.second)) {
continue
}

// If the piece is blocking the rook's path, return true
if (
(row == oldPos.first || col == oldPos.second) &&
row in minRow..maxRow &&
col in minCol..maxCol
) {
if ((row == oldPos.first || col == oldPos.second) &&
row in minRow..maxRow &&
col in minCol..maxCol) {
return true
}
}
Expand Down Expand Up @@ -246,12 +221,11 @@ class Piece(
return false
}

// If the move is with check and the new position contains a black piece, capture it
// If the move is with check and the new position contains a white piece, capture it
if (withCheck && pieceOnNewPos != null && pieceOnNewPos.color == Colors.BLACK) {
whiteTurn = false
removePiece(pieceOnNewPos)
return true
}
} else if (withCheck) whiteTurn = false

// If the new position is empty, return true
if (pieceOnNewPos == null) {
Expand Down Expand Up @@ -286,10 +260,10 @@ class Piece(

// If the move is with check and the new position contains a white piece, capture it
if (withCheck && pieceOnNewPos != null && pieceOnNewPos.color == Colors.WHITE) {
whiteTurn = true

removePiece(pieceOnNewPos)
return true
}
} else if (withCheck) whiteTurn = true

// If the new position is empty, return true
if (pieceOnNewPos == null) {
Expand Down

0 comments on commit c546dfa

Please sign in to comment.