private bool IsInReach(Square square) { if (square.IsOccupied() && square.Piece.Color == Color) { return false; } return IsFreeBetween(square); }
private IEnumerable<Square> GetSquaresInDirection(Square square, Func<Square, Square> directionFunc) { var squareInDirection = directionFunc(square); var squares = new List<Square>(); if (CanMoveTo(squareInDirection)) { squares.Add(squareInDirection); if (squareInDirection.IsFree()) { squares.AddRange(GetSquaresInDirection(squareInDirection, directionFunc)); } } return squares; }
protected bool IsKingInCheckAfterMove(SquareIdentifier squareIdentifier) { var board = CurrentSquare.Board; var squareBefore = CurrentSquare; var squareAfter = board[squareIdentifier]; var pieceAtNewSquare = squareAfter.Piece; CurrentSquare.Piece = null; CurrentSquare = squareAfter; squareAfter.Piece = this; var isInCheckAfterMove = board.IsInCheck(Color); squareBefore.Piece = this; squareAfter.Piece = pieceAtNewSquare; CurrentSquare = squareBefore; return isInCheckAfterMove; }
private bool IsFreeBetween(Square square) { if (CurrentSquare.Identifier.Rank == square.Identifier.Rank) { var oneCloserSquare = square.Identifier.File < CurrentSquare.Identifier.File ? square.SquareToTheRight : square.SquareToTheLeft; return oneCloserSquare.Equals(CurrentSquare) || (oneCloserSquare.IsFree() && IsFreeBetween(oneCloserSquare)); } if (CurrentSquare.Identifier.File == square.Identifier.File) { var oneCloserSquare = square.Identifier.Rank < CurrentSquare.Identifier.Rank ? square.SquareAbove : square.SquareBelow; return oneCloserSquare.Equals(CurrentSquare) || (oneCloserSquare.IsFree() && IsFreeBetween(oneCloserSquare)); } return false; }
public Board() { History = new MoveHistory(); for (var i = 0; i < 8; i++) { for (var j = 0; j < 8; j++) { _squares[i, j] = new Square(); } } InitializePosition("a1", new Rook { Color = Army.White }); InitializePosition("b1", new Knight { Color = Army.White }); InitializePosition("c1", new Bishop { Color = Army.White }); InitializePosition("d1", new Queen { Color = Army.White }); InitializePosition("e1", new King { Color = Army.White }); InitializePosition("f1", new Bishop { Color = Army.White }); InitializePosition("g1", new Knight { Color = Army.White }); InitializePosition("h1", new Rook { Color = Army.White }); InitializePosition("a2", new Pawn { Color = Army.White }); InitializePosition("b2", new Pawn { Color = Army.White }); InitializePosition("c2", new Pawn { Color = Army.White }); InitializePosition("d2", new Pawn { Color = Army.White }); InitializePosition("e2", new Pawn { Color = Army.White }); InitializePosition("f2", new Pawn { Color = Army.White }); InitializePosition("g2", new Pawn { Color = Army.White }); InitializePosition("h2", new Pawn { Color = Army.White }); InitializePosition("a3", null); InitializePosition("b3", null); InitializePosition("c3", null); InitializePosition("d3", null); InitializePosition("e3", null); InitializePosition("f3", null); InitializePosition("g3", null); InitializePosition("h3", null); InitializePosition("a4", null); InitializePosition("b4", null); InitializePosition("c4", null); InitializePosition("d4", null); InitializePosition("e4", null); InitializePosition("f4", null); InitializePosition("g4", null); InitializePosition("h4", null); InitializePosition("a5", null); InitializePosition("b5", null); InitializePosition("c5", null); InitializePosition("d5", null); InitializePosition("e5", null); InitializePosition("f5", null); InitializePosition("g5", null); InitializePosition("h5", null); InitializePosition("a6", null); InitializePosition("b6", null); InitializePosition("c6", null); InitializePosition("d6", null); InitializePosition("e6", null); InitializePosition("f6", null); InitializePosition("g6", null); InitializePosition("h6", null); InitializePosition("a7", new Pawn { Color = Army.Black }); InitializePosition("b7", new Pawn { Color = Army.Black }); InitializePosition("c7", new Pawn { Color = Army.Black }); InitializePosition("d7", new Pawn { Color = Army.Black }); InitializePosition("e7", new Pawn { Color = Army.Black }); InitializePosition("f7", new Pawn { Color = Army.Black }); InitializePosition("g7", new Pawn { Color = Army.Black }); InitializePosition("h7", new Pawn { Color = Army.Black }); InitializePosition("a8", new Rook { Color = Army.Black }); InitializePosition("b8", new Knight { Color = Army.Black }); InitializePosition("c8", new Bishop { Color = Army.Black }); InitializePosition("d8", new Queen { Color = Army.Black }); InitializePosition("e8", new King { Color = Army.Black }); InitializePosition("f8", new Bishop { Color = Army.Black }); InitializePosition("g8", new Knight { Color = Army.Black }); InitializePosition("h8", new Rook { Color = Army.Black }); }
private bool CanMoveTo(Square square) { return square != null && (square.IsFree() || square.Piece.Color != Color); }
private bool IsPromotionSquare(Square square) { return Color == Army.White ? square.Identifier.Rank == '8' : square.Identifier.Rank == '1'; }
private bool IsEnPassantMove(Square move) { if (move.Identifier.Rank == '6') { var lastMove = move.Board.History.Moves.LastOrDefault(); if (lastMove != null) { if (lastMove.MovedPiece is Pawn && lastMove.From.File == move.Identifier.File && lastMove.To.File == move.Identifier.File) { if (Color == Army.White && lastMove.From.Rank == '7' && lastMove.To.Rank == '5') { return true; } if (lastMove.From.Rank == '2' && lastMove.To.Rank == '4') { return true; } } } } return false; }
private Square GetMoveOneForwardSquare(Square from) { var newSquare = Color == Army.White ? from.SquareAbove : from.SquareBelow; return newSquare != null && newSquare.IsFree() ? newSquare : null; }