Prevent castling in case there is no rook on the castling side

This commit is contained in:
Marco 2024-04-16 18:17:28 +02:00
parent 4153f4cc7c
commit 25893a5187
2 changed files with 156 additions and 4 deletions

View File

@ -7,6 +7,76 @@ import (
"github.com/stretchr/testify/assert"
)
func Test_Board_WhiteCastlesLeft(t *testing.T) {
board := newBoard()
board.position[types.Coordinate{Col: 5, Row: 1}] = King{Color: types.White}
board.position[types.Coordinate{Col: 1, Row: 1}] = Rook{Color: types.White}
castling := types.Move{
StartSquare: types.Coordinate{Col: 5, Row: 1},
EndSquare: types.Coordinate{Col: 3, Row: 1},
ColorMoved: types.White,
}
valid, _ := board.CheckAndPlay(&castling)
assert.True(t, valid)
}
func Test_Board_WhiteCastlesRight(t *testing.T) {
board := newBoard()
board.position[types.Coordinate{Col: 5, Row: 1}] = King{Color: types.White}
board.position[types.Coordinate{Col: 8, Row: 1}] = Rook{Color: types.White}
castling := types.Move{
StartSquare: types.Coordinate{Col: 5, Row: 1},
EndSquare: types.Coordinate{Col: 7, Row: 1},
ColorMoved: types.White,
}
valid, _ := board.CheckAndPlay(&castling)
assert.True(t, valid)
}
func Test_Board_BlackCastlesLeft(t *testing.T) {
board := newBoard()
board.colorToMove = types.Black
board.position[types.Coordinate{Col: 5, Row: 8}] = King{Color: types.Black}
board.position[types.Coordinate{Col: 1, Row: 8}] = Rook{Color: types.Black}
castling := types.Move{
StartSquare: types.Coordinate{Col: 5, Row: 8},
EndSquare: types.Coordinate{Col: 3, Row: 8},
ColorMoved: types.Black,
}
valid, _ := board.CheckAndPlay(&castling)
assert.True(t, valid)
}
func Test_Board_BlackCastlesRight(t *testing.T) {
board := newBoard()
board.colorToMove = types.Black
board.position[types.Coordinate{Col: 5, Row: 8}] = King{Color: types.Black}
board.position[types.Coordinate{Col: 8, Row: 8}] = Rook{Color: types.Black}
castling := types.Move{
StartSquare: types.Coordinate{Col: 5, Row: 8},
EndSquare: types.Coordinate{Col: 7, Row: 8},
ColorMoved: types.Black,
}
valid, _ := board.CheckAndPlay(&castling)
assert.True(t, valid)
}
func Test_Board_WhiteCastlesLeft_QueenCoversSquareInBetween(t *testing.T) {
board := newBoard()
@ -156,3 +226,73 @@ func Test_Board_BlackCastlesLeft_QueenCoversKingsSquare(t *testing.T) {
assert.False(t, valid)
}
func Test_Board_WhiteCastlesLeft_NoRook(t *testing.T) {
board := newBoard()
board.position[types.Coordinate{Col: 5, Row: 1}] = King{Color: types.White}
castling := types.Move{
StartSquare: types.Coordinate{Col: 5, Row: 1},
EndSquare: types.Coordinate{Col: 3, Row: 1},
ColorMoved: types.White,
}
valid, _ := board.CheckAndPlay(&castling)
assert.False(t, valid)
}
func Test_Board_WhiteCastlesRight_NoRook(t *testing.T) {
board := newBoard()
board.position[types.Coordinate{Col: 5, Row: 1}] = King{Color: types.White}
castling := types.Move{
StartSquare: types.Coordinate{Col: 5, Row: 1},
EndSquare: types.Coordinate{Col: 7, Row: 1},
ColorMoved: types.White,
}
valid, _ := board.CheckAndPlay(&castling)
assert.False(t, valid)
}
func Test_Board_BlackCastlesLeft_NoRook(t *testing.T) {
board := newBoard()
board.colorToMove = types.Black
kingSquare := types.Coordinate{Col: 5, Row: 8}
kingDestinationSquare := types.Coordinate{Col: 3, Row: 8}
board.position[kingSquare] = King{Color: types.Black}
castling := types.Move{
StartSquare: kingSquare,
EndSquare: kingDestinationSquare,
ColorMoved: types.Black,
}
valid, _ := board.CheckAndPlay(&castling)
assert.False(t, valid)
assert.Equal(t, nil, board.getPieceAt(kingDestinationSquare))
}
func Test_Board_BlackCastlesRight_NoRook(t *testing.T) {
board := newBoard()
board.colorToMove = types.Black
board.position[types.Coordinate{Col: 5, Row: 8}] = King{Color: types.Black}
castling := types.Move{
StartSquare: types.Coordinate{Col: 5, Row: 8},
EndSquare: types.Coordinate{Col: 7, Row: 8},
ColorMoved: types.Black,
}
valid, _ := board.CheckAndPlay(&castling)
assert.False(t, valid)
}

View File

@ -80,8 +80,11 @@ func (k King) isCastlingAllowed(b *Board, move types.Move) (bool, CastlingDirect
!b.isSquareAttacked(destinationSquareForKingRight, types.Black) &&
!b.isSquareAttacked(*destinationSquareForKingRight.Left(1), types.Black) &&
!b.state.WhiteHRookMoved {
_, ok := b.getPieceAt(types.Coordinate{Col: 8, Row: 1}).(Rook)
if ok {
return true, CastlingRight
}
}
if move.EndSquare == destinationSquareForKingLeft &&
b.getPieceAt(destinationSquareForKingLeft) == nil &&
@ -90,8 +93,11 @@ func (k King) isCastlingAllowed(b *Board, move types.Move) (bool, CastlingDirect
!b.isSquareAttacked(destinationSquareForKingLeft, types.Black) &&
!b.isSquareAttacked(*destinationSquareForKingLeft.Right(1), types.Black) &&
!b.state.WhiteARookMoved {
_, ok := b.getPieceAt(types.Coordinate{Col: 1, Row: 1}).(Rook)
if ok {
return true, CastlingLeft
}
}
case types.Black:
destinationSquareForKingRight := types.Coordinate{Col: 7, Row: 8}
destinationSquareForKingLeft := types.Coordinate{Col: 3, Row: 8}
@ -102,8 +108,11 @@ func (k King) isCastlingAllowed(b *Board, move types.Move) (bool, CastlingDirect
!b.isSquareAttacked(destinationSquareForKingRight, types.White) &&
!b.isSquareAttacked(*destinationSquareForKingRight.Left(1), types.White) &&
!b.state.BlackHRookMoved {
_, ok := b.getPieceAt(types.Coordinate{Col: 8, Row: 8}).(Rook)
if ok {
return true, CastlingRight
}
}
if move.EndSquare == destinationSquareForKingLeft &&
b.getPieceAt(destinationSquareForKingLeft) == nil &&
@ -112,9 +121,12 @@ func (k King) isCastlingAllowed(b *Board, move types.Move) (bool, CastlingDirect
!b.isSquareAttacked(destinationSquareForKingLeft, types.White) &&
!b.isSquareAttacked(*destinationSquareForKingLeft.Right(1), types.White) &&
!b.state.BlackARookMoved {
_, ok := b.getPieceAt(types.Coordinate{Col: 1, Row: 8}).(Rook)
if ok {
return true, CastlingLeft
}
}
}
return valid, NoDirection
}