Check which color should be moving and fix knight moves.
This commit is contained in:
parent
f733d9bb08
commit
ca1e87fbfc
@ -17,5 +17,5 @@ func (b Bishop) GetColor() types.ChessColor {
|
||||
}
|
||||
|
||||
func (b Bishop) GetAllNonBlockedMoves(board Board, fromSquare types.Coordinate) []types.Coordinate {
|
||||
return []types.Coordinate{}
|
||||
return board.GetNonBlockedDiagonals(fromSquare)
|
||||
}
|
||||
|
@ -12,12 +12,14 @@ type Position map[types.Coordinate]Piece
|
||||
type Board struct {
|
||||
position Position
|
||||
history []types.Move
|
||||
colorToMove types.ChessColor
|
||||
}
|
||||
|
||||
func newBoard() Board {
|
||||
return Board{
|
||||
position: make(Position),
|
||||
history: make([]types.Move, 0),
|
||||
colorToMove: types.White,
|
||||
}
|
||||
}
|
||||
|
||||
@ -63,7 +65,11 @@ func (b *Board) CheckAndPlay(move types.Move) (bool, string) {
|
||||
if pieceAtStartSquare == nil {
|
||||
return false, "no piece at start square"
|
||||
}
|
||||
|
||||
move.ColorMoved = pieceAtStartSquare.GetColor()
|
||||
if move.ColorMoved != tempBoard.colorToMove {
|
||||
return false, "wrong color moved"
|
||||
}
|
||||
move.PieceMoved = GetShortNameForPiece(pieceAtStartSquare)
|
||||
|
||||
//Check end square of move
|
||||
@ -114,6 +120,7 @@ func (b *Board) CheckAndPlay(move types.Move) (bool, string) {
|
||||
//We play the move on the real board
|
||||
b.position = tempBoard.position
|
||||
b.history = tempBoard.history
|
||||
b.colorToMove = b.colorToMove.Opposite()
|
||||
b.appendMoveToHistory(move)
|
||||
return true, ""
|
||||
}
|
||||
@ -176,6 +183,7 @@ func (b Board) getCopyOfBoard() Board {
|
||||
return Board{
|
||||
position: b.position.getCopyOfPosition(),
|
||||
history: b.history,
|
||||
colorToMove: b.colorToMove,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -8,10 +8,10 @@ func (b *Board) GetNonBlockedRowAndColumn(fromSquare types.Coordinate) []types.C
|
||||
squaresAbove := fromSquare.GetStraightInDirection(types.Coordinate.Up)
|
||||
squaresBelow := fromSquare.GetStraightInDirection(types.Coordinate.Down)
|
||||
|
||||
nonBlocked := b.getNonBlocked(squaresLeft, fromSquare)
|
||||
nonBlocked = append(nonBlocked, b.getNonBlocked(squaresRight, fromSquare)...)
|
||||
nonBlocked = append(nonBlocked, b.getNonBlocked(squaresAbove, fromSquare)...)
|
||||
nonBlocked = append(nonBlocked, b.getNonBlocked(squaresBelow, fromSquare)...)
|
||||
nonBlocked := b.getNonBlockedConsecutive(squaresLeft, fromSquare)
|
||||
nonBlocked = append(nonBlocked, b.getNonBlockedConsecutive(squaresRight, fromSquare)...)
|
||||
nonBlocked = append(nonBlocked, b.getNonBlockedConsecutive(squaresAbove, fromSquare)...)
|
||||
nonBlocked = append(nonBlocked, b.getNonBlockedConsecutive(squaresBelow, fromSquare)...)
|
||||
|
||||
return nonBlocked
|
||||
}
|
||||
@ -22,20 +22,20 @@ func (b *Board) GetNonBlockedDiagonals(fromSquare types.Coordinate) []types.Coor
|
||||
leftDown := fromSquare.GetDiagonalInDirection(types.Coordinate.Left, types.Coordinate.Down)
|
||||
rightDown := fromSquare.GetDiagonalInDirection(types.Coordinate.Right, types.Coordinate.Down)
|
||||
|
||||
nonBlocked := b.getNonBlocked(rightUp, fromSquare)
|
||||
nonBlocked = append(nonBlocked, b.getNonBlocked(leftUp, fromSquare)...)
|
||||
nonBlocked = append(nonBlocked, b.getNonBlocked(leftDown, fromSquare)...)
|
||||
nonBlocked = append(nonBlocked, b.getNonBlocked(rightDown, fromSquare)...)
|
||||
nonBlocked := b.getNonBlockedConsecutive(rightUp, fromSquare)
|
||||
nonBlocked = append(nonBlocked, b.getNonBlockedConsecutive(leftUp, fromSquare)...)
|
||||
nonBlocked = append(nonBlocked, b.getNonBlockedConsecutive(leftDown, fromSquare)...)
|
||||
nonBlocked = append(nonBlocked, b.getNonBlockedConsecutive(rightDown, fromSquare)...)
|
||||
|
||||
return nonBlocked
|
||||
}
|
||||
|
||||
func (b *Board) GetNonBlockedKnightMoves(fromSquare types.Coordinate) []types.Coordinate {
|
||||
allKnightMoves := fromSquare.GetAllKnightMoves()
|
||||
return b.getNonBlocked(allKnightMoves, fromSquare)
|
||||
return b.getNonBlockedForKnights(allKnightMoves, fromSquare)
|
||||
}
|
||||
|
||||
func (b *Board) getNonBlocked(
|
||||
func (b *Board) getNonBlockedConsecutive(
|
||||
squaresToCheck []types.Coordinate,
|
||||
sourceSquare types.Coordinate,
|
||||
) []types.Coordinate {
|
||||
@ -46,6 +46,8 @@ func (b *Board) getNonBlocked(
|
||||
piece := b.getPieceAt(square)
|
||||
if piece != nil {
|
||||
if piece.GetColor() == pieceOnSourceSquare.GetColor() {
|
||||
//We do not append squares with same-colored pieces
|
||||
//and also not the squares behind it
|
||||
break
|
||||
}
|
||||
// if there is an opposite colored piece we append it but
|
||||
@ -57,3 +59,19 @@ func (b *Board) getNonBlocked(
|
||||
}
|
||||
return nonBlocked
|
||||
}
|
||||
|
||||
func (b *Board) getNonBlockedForKnights(
|
||||
squaresToCheck []types.Coordinate,
|
||||
sourceSquare types.Coordinate,
|
||||
) []types.Coordinate {
|
||||
pieceOnSourceSquare := b.getPieceAt(sourceSquare)
|
||||
nonBlocked := []types.Coordinate{}
|
||||
|
||||
for _, square := range squaresToCheck {
|
||||
piece := b.getPieceAt(square)
|
||||
if piece == nil || piece.GetColor() != pieceOnSourceSquare.GetColor() {
|
||||
nonBlocked = append(nonBlocked, square)
|
||||
}
|
||||
}
|
||||
return nonBlocked
|
||||
}
|
||||
|
@ -20,6 +20,12 @@ type CoordinateBuilder struct {
|
||||
coordinate Coordinate
|
||||
}
|
||||
|
||||
func newCoordinateBuilder(c Coordinate) *CoordinateBuilder {
|
||||
return &CoordinateBuilder{
|
||||
coordinate: c,
|
||||
}
|
||||
}
|
||||
|
||||
func (b *CoordinateBuilder) Up(number int) *CoordinateBuilder {
|
||||
b.coordinate.Row += number
|
||||
return b
|
||||
@ -45,27 +51,24 @@ func (b *CoordinateBuilder) Resolve() *Coordinate {
|
||||
if c.Row <= RangeLastValid &&
|
||||
c.Row >= RangeFirstValid &&
|
||||
c.Col <= RangeLastValid &&
|
||||
c.Col >= RangeLastValid {
|
||||
return nil
|
||||
c.Col >= RangeFirstValid {
|
||||
return &c
|
||||
}
|
||||
|
||||
return &c
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Coordinate) GetAllKnightMoves() []Coordinate {
|
||||
unfilteredMoves := make([]*Coordinate, 0, 8)
|
||||
|
||||
builder := CoordinateBuilder{coordinate: *c}
|
||||
|
||||
unfilteredMoves = append(unfilteredMoves, builder.Up(2).Right(1).Resolve())
|
||||
unfilteredMoves = append(unfilteredMoves, builder.Up(1).Right(2).Resolve())
|
||||
unfilteredMoves = append(unfilteredMoves, builder.Down(1).Right(2).Resolve())
|
||||
unfilteredMoves = append(unfilteredMoves, builder.Down(2).Right(1).Resolve())
|
||||
unfilteredMoves = append(unfilteredMoves, builder.Down(2).Left(1).Resolve())
|
||||
unfilteredMoves = append(unfilteredMoves, builder.Down(1).Left(2).Resolve())
|
||||
unfilteredMoves = append(unfilteredMoves, builder.Up(1).Left(2).Resolve())
|
||||
unfilteredMoves = append(unfilteredMoves, builder.Up(2).Left(1).Resolve())
|
||||
|
||||
unfilteredMoves = append(unfilteredMoves, newCoordinateBuilder(*c).Up(2).Right(1).Resolve())
|
||||
unfilteredMoves = append(unfilteredMoves, newCoordinateBuilder(*c).Up(1).Right(2).Resolve())
|
||||
unfilteredMoves = append(unfilteredMoves, newCoordinateBuilder(*c).Down(1).Right(2).Resolve())
|
||||
unfilteredMoves = append(unfilteredMoves, newCoordinateBuilder(*c).Down(2).Right(1).Resolve())
|
||||
unfilteredMoves = append(unfilteredMoves, newCoordinateBuilder(*c).Down(2).Left(1).Resolve())
|
||||
unfilteredMoves = append(unfilteredMoves, newCoordinateBuilder(*c).Down(1).Left(2).Resolve())
|
||||
unfilteredMoves = append(unfilteredMoves, newCoordinateBuilder(*c).Up(1).Left(2).Resolve())
|
||||
unfilteredMoves = append(unfilteredMoves, newCoordinateBuilder(*c).Up(2).Left(1).Resolve())
|
||||
|
||||
return lo.FilterMap(unfilteredMoves, func(unfilteredMove *Coordinate, _ int) (Coordinate, bool) {
|
||||
if unfilteredMove != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user