package chess import ( "mchess_server/types" "testing" "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() board.position[types.Coordinate{Col: 5, Row: 1}] = King{Color: types.White} board.position[types.Coordinate{Col: 1, Row: 1}] = Rook{Color: types.White} board.position[types.Coordinate{Col: 4, Row: 5}] = Queen{Color: types.Black} 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_QueenCoversSquareInBetween(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} board.position[types.Coordinate{Col: 6, Row: 5}] = Queen{Color: types.Black} 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_WhiteCastlesLeft_KingInCheck(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} board.position[types.Coordinate{Col: 5, Row: 5}] = Queen{Color: types.Black} castling := types.Move{ StartSquare: types.Coordinate{Col: 5, Row: 1}, EndSquare: types.Coordinate{Col: 3, Row: 1}, ColorMoved: types.White, } valid, violation := board.CheckAndPlay(&castling) assert.False(t, valid) assert.Equal(t, CastlingWhileKingInCheck, violation) } func Test_Board_WhiteCastlesLeft_QueenCoversKingsSquare(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} board.position[types.Coordinate{Col: 3, Row: 5}] = Queen{Color: types.Black} 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_BlackCastlesLeft_QueenCoversSquareInBetween(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} board.position[types.Coordinate{Col: 4, Row: 4}] = Queen{Color: types.White} 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.False(t, valid) } func Test_Board_BlackCastlesRight_QueenCoversSquareInBetween(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} board.position[types.Coordinate{Col: 6, Row: 4}] = Queen{Color: types.White} 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) } func Test_Board_BlackCastlesLeft_KingInCheck(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} board.position[types.Coordinate{Col: 5, Row: 4}] = Queen{Color: types.White} castling := types.Move{ StartSquare: types.Coordinate{Col: 5, Row: 8}, EndSquare: types.Coordinate{Col: 3, Row: 8}, ColorMoved: types.Black, } valid, violation := board.CheckAndPlay(&castling) assert.False(t, valid) assert.Equal(t, CastlingWhileKingInCheck, violation) } func Test_Board_BlackCastlesLeft_QueenCoversKingsSquare(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} board.position[types.Coordinate{Col: 3, Row: 4}] = Queen{Color: types.White} 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.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) }