2023-06-26 21:58:40 +00:00
|
|
|
package chess
|
|
|
|
|
|
|
|
import (
|
|
|
|
"mchess_server/types"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/samber/lo"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_Rook_GetNonBlockedSquares(t *testing.T) {
|
|
|
|
t.Run("free row and column", func(t *testing.T) {
|
|
|
|
board := newBoard()
|
|
|
|
rook := Rook{Color: types.Black}
|
|
|
|
|
|
|
|
board.position[types.Coordinate{Col: 5, Row: 5}] = rook
|
2023-06-27 20:32:24 +00:00
|
|
|
squares := rook.GetAllNonBlockedMoves(board, types.Coordinate{Col: 5, Row: 5})
|
2023-06-26 21:58:40 +00:00
|
|
|
assert.Len(t, squares, 14)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("free row and column", func(t *testing.T) {
|
|
|
|
board := newBoard()
|
|
|
|
rook := Rook{Color: types.Black}
|
|
|
|
rookCoordinate := types.Coordinate{Col: 5, Row: 5}
|
|
|
|
|
|
|
|
board.position[rookCoordinate] = rook
|
|
|
|
board.position[types.Coordinate{Col: 3, Row: 5}] = Pawn{Color: types.White}
|
|
|
|
board.position[types.Coordinate{Col: 5, Row: 6}] = Pawn{Color: types.White}
|
|
|
|
board.position[types.Coordinate{Col: 6, Row: 5}] = Pawn{Color: types.Black}
|
|
|
|
|
2023-06-27 20:32:24 +00:00
|
|
|
squares := rook.GetAllNonBlockedMoves(board, rookCoordinate)
|
2023-06-26 21:58:40 +00:00
|
|
|
|
|
|
|
squaresOnLeft := lo.Filter(squares, func(square types.Coordinate, _ int) bool {
|
|
|
|
return square.Row == rookCoordinate.Row && square.Col < rookCoordinate.Col
|
|
|
|
})
|
|
|
|
assert.Equal(t,
|
|
|
|
[]types.Coordinate{
|
|
|
|
{Col: 4, Row: 5},
|
|
|
|
{Col: 3, Row: 5},
|
|
|
|
},
|
|
|
|
squaresOnLeft)
|
|
|
|
|
|
|
|
squaresOnRight := lo.Filter(squares, func(square types.Coordinate, _ int) bool {
|
|
|
|
return square.Row == rookCoordinate.Row && square.Col > rookCoordinate.Col
|
|
|
|
})
|
|
|
|
assert.Equal(t,
|
|
|
|
[]types.Coordinate{},
|
|
|
|
squaresOnRight)
|
|
|
|
|
|
|
|
squaresAbove := lo.Filter(squares, func(square types.Coordinate, _ int) bool {
|
|
|
|
return square.Col == rookCoordinate.Col && square.Row > rookCoordinate.Row
|
|
|
|
})
|
|
|
|
assert.Equal(t,
|
|
|
|
[]types.Coordinate{
|
|
|
|
{Col: 5, Row: 6},
|
|
|
|
},
|
|
|
|
squaresAbove)
|
|
|
|
|
|
|
|
squaresBelow := lo.Filter(squares, func(square types.Coordinate, _ int) bool {
|
|
|
|
return square.Col == rookCoordinate.Col && square.Row < rookCoordinate.Row
|
|
|
|
})
|
|
|
|
assert.Equal(t,
|
|
|
|
[]types.Coordinate{
|
|
|
|
{Col: 5, Row: 4},
|
|
|
|
{Col: 5, Row: 3},
|
|
|
|
{Col: 5, Row: 2},
|
|
|
|
{Col: 5, Row: 1},
|
|
|
|
},
|
|
|
|
squaresBelow)
|
|
|
|
})
|
|
|
|
}
|