Skip to content

Commit

Permalink
First look at King implementation #3
Browse files Browse the repository at this point in the history
  • Loading branch information
FSaurenbach committed May 13, 2024
1 parent a7a926f commit a1b4650
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/commonMain/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ var whiteRook: Bitmap? = null
var whiteKnight: Bitmap? = null
var whiteBishop: Bitmap? = null
var whiteQueen: Bitmap? = null
var whiteKing: Bitmap? = null

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 whiteTurn = true

Expand All @@ -45,6 +47,7 @@ class MyScene(private val cont: SceneContainer) : PixelatedScene(512, 512) {
whiteKnight = resourcesVfs["w_knight.png"].readBitmap()
whiteBishop = resourcesVfs["w_bishop.png"].readBitmap()
whiteQueen = resourcesVfs["w_queen.png"].readBitmap()
whiteKing = resourcesVfs["w_king.png"].readBitmap()

blackPawn = resourcesVfs["b_pawn.png"].readBitmap()
blackRook = resourcesVfs["b_rook.png"].readBitmap()
Expand Down
168 changes: 165 additions & 3 deletions src/commonMain/kotlin/Piece.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Piece(
PieceKind.WhiteKnight -> whiteKnight!!
PieceKind.WhiteBishop -> whiteBishop!!
PieceKind.WhiteQueen -> whiteQueen!!
PieceKind.WhiteKing -> whiteKing!!
else -> throw Error("Invalid Piece !?")
})

Expand All @@ -58,6 +59,7 @@ class Piece(
PieceKind.BlackKnight -> blackKnight!!
PieceKind.BlackBishop -> blackBishop!!
PieceKind.BlackQueen -> blackQueen!!
PieceKind.BlackKing -> blackKing!!
else -> throw Error("Invalid Piece !?")
})
piece.size(Size(64, 64))
Expand Down Expand Up @@ -97,6 +99,7 @@ class Piece(
PieceKind.WhiteKnight -> moveKnight(oldPos, newPos, pieceOnNewPos, true)
PieceKind.WhiteBishop -> moveBishop(oldPos, newPos, pieceOnNewPos, true)
PieceKind.WhiteQueen -> moveQueen(oldPos, newPos, pieceOnNewPos, true)
PieceKind.WhiteKing -> moveKing(oldPos, newPos, pieceOnNewPos, true)
else -> false
}
}
Expand All @@ -107,6 +110,7 @@ class Piece(
PieceKind.BlackKnight -> moveKnight(oldPos, newPos, pieceOnNewPos, false)
PieceKind.BlackBishop -> moveBishop(oldPos, newPos, pieceOnNewPos, false)
PieceKind.BlackQueen -> moveQueen(oldPos, newPos, pieceOnNewPos, false)
PieceKind.BlackKing -> moveKing(oldPos, newPos, pieceOnNewPos, false)
else -> false
}
}
Expand Down Expand Up @@ -499,8 +503,163 @@ class Piece(
newPos: Pair<Int, Int>,
pieceOnNewPos: Piece?,
isWhite: Boolean
):Boolean {
return moveRook(oldPos, newPos, pieceOnNewPos, isWhite) || moveBishop(oldPos, newPos, pieceOnNewPos, isWhite)
): Boolean {
return moveRook(oldPos, newPos, pieceOnNewPos, isWhite) ||
moveBishop(oldPos, newPos, pieceOnNewPos, isWhite)
}

private fun moveKing(
oldPos: Pair<Int, Int>,
newPos: Pair<Int, Int>,
pieceOnNewPos: Piece?,
isWhite: Boolean
): Boolean {
// Case moving down
if (oldPos.first == newPos.first && newPos.second - oldPos.second == 1) {
if (pieceOnNewPos != null && pieceOnNewPos.color == color) {
println("pieceOnNewPos: $pieceOnNewPos")
return false
}
if (pieceOnNewPos != null && pieceOnNewPos.color != color) {
println("pieceOnNewPos: $pieceOnNewPos")
removePiece(pieceOnNewPos)
whiteTurn = !isWhite
return true
}
// Case moving one down
if (pieceOnNewPos == null) {
whiteTurn = !isWhite
return true
}
}
// Case moving up
else if (oldPos.first == newPos.first && newPos.second - oldPos.second == -1) {
if (pieceOnNewPos != null && pieceOnNewPos.color == color) {
println("pieceOnNewPos: $pieceOnNewPos")
return false
}
if (pieceOnNewPos != null && pieceOnNewPos.color != color) {
println("pieceOnNewPos: $pieceOnNewPos")
removePiece(pieceOnNewPos)
whiteTurn = !isWhite
return true
}
// Case moving one up
if (pieceOnNewPos == null) {
whiteTurn = !isWhite
return true
}
}
// Case moving left
else if (oldPos.second == newPos.second && oldPos.first - newPos.first == 1) {
if (pieceOnNewPos != null && pieceOnNewPos.color == color) {
println("pieceOnNewPos: $pieceOnNewPos")
return false
}
if (pieceOnNewPos != null && pieceOnNewPos.color != color) {
println("pieceOnNewPos: $pieceOnNewPos")
removePiece(pieceOnNewPos)
whiteTurn = !isWhite
return true
}
// Case moving one left
if (pieceOnNewPos == null) {
whiteTurn = !isWhite
return true
}
}
// Case moving right
else if (oldPos.second == newPos.second && newPos.first - oldPos.first == 1) {
if (pieceOnNewPos != null && pieceOnNewPos.color == color) {
println("pieceOnNewPos: $pieceOnNewPos")
return false
}
if (pieceOnNewPos != null && pieceOnNewPos.color != color) {
println("pieceOnNewPos: $pieceOnNewPos")
removePiece(pieceOnNewPos)
whiteTurn = !isWhite
return true
}
// Case moving one right
if (pieceOnNewPos == null) {
whiteTurn = !isWhite
return true
}
}
// Case moving down right
else if (newPos.first - oldPos.first == 1 && newPos.second - oldPos.second == 1) {
if (pieceOnNewPos != null && pieceOnNewPos.color == color) {
println("pieceOnNewPos: $pieceOnNewPos")
return false
}
if (pieceOnNewPos != null && pieceOnNewPos.color != color) {
println("pieceOnNewPos: $pieceOnNewPos")
removePiece(pieceOnNewPos)
whiteTurn = !isWhite
return true
}
// Case moving one down right
if (pieceOnNewPos == null) {
whiteTurn = !isWhite
return true
}
}
// Case moving down left
else if (oldPos.first - newPos.first == 1 && newPos.second - oldPos.second == 1) {
if (pieceOnNewPos != null && pieceOnNewPos.color == color) {
println("pieceOnNewPos: $pieceOnNewPos")
return false
}
if (pieceOnNewPos != null && pieceOnNewPos.color != color) {
println("pieceOnNewPos: $pieceOnNewPos")
removePiece(pieceOnNewPos)
whiteTurn = !isWhite
return true
}
// Case moving one down left
if (pieceOnNewPos == null) {
whiteTurn = !isWhite
return true
}
}
// Case moving up right
else if (newPos.first - oldPos.first == 1 && oldPos.second - newPos.second == 1) {
if (pieceOnNewPos != null && pieceOnNewPos.color == color) {
println("pieceOnNewPos: $pieceOnNewPos")
return false
}
if (pieceOnNewPos != null && pieceOnNewPos.color != color) {
println("pieceOnNewPos: $pieceOnNewPos")
removePiece(pieceOnNewPos)
whiteTurn = !isWhite
return true
}
// Case moving one up right
if (pieceOnNewPos == null) {
whiteTurn = !isWhite
return true
}
}
// Case moving up left
else if (oldPos.first - newPos.first == 1 && oldPos.second - newPos.second == 1) {
if (pieceOnNewPos != null && pieceOnNewPos.color == color) {
println("pieceOnNewPos: $pieceOnNewPos")
return false
}
if (pieceOnNewPos != null && pieceOnNewPos.color != color) {
println("pieceOnNewPos: $pieceOnNewPos")
removePiece(pieceOnNewPos)
whiteTurn = !isWhite
return true
}
// Case moving one up left
if (pieceOnNewPos == null) {
whiteTurn = !isWhite
return true
}
}

return false
}

private fun removePiece(piece: Piece) {
Expand Down Expand Up @@ -533,6 +692,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)

// black pieces
val blackPawn1 = Piece(PieceKind.BlackPawn, Colors.BLACK, 0, 6, cont)
Expand All @@ -550,6 +710,7 @@ fun addAllPieces(cont: SceneContainer) {
val blackBishop1 = Piece(PieceKind.BlackBishop, Colors.BLACK, 2, 7, cont)
val blackBishop2 = Piece(PieceKind.BlackBishop, Colors.BLACK, 5, 7, cont)
val blackQueen = Piece(PieceKind.BlackQueen, Colors.BLACK, 3, 7, cont)
val blackKing = Piece(PieceKind.BlackKing, Colors.BLACK, 4, 7, cont)

// Add all pieces to the pieces list
pieces.add(whitePawn1)
Expand Down Expand Up @@ -582,5 +743,6 @@ fun addAllPieces(cont: SceneContainer) {
pieces.add(blackBishop2)
pieces.add(whiteQueen)
pieces.add(blackQueen)

pieces.add(whiteKing)
pieces.add(blackKing)
}

0 comments on commit a1b4650

Please sign in to comment.