protected static IEnumerable<BoardCoordinate> GetRadialAdjacentFrom(BoardCoordinate startingPosition, int distance) { yield return new BoardCoordinate(startingPosition.X + distance, startingPosition.Y); yield return new BoardCoordinate(startingPosition.X - distance, startingPosition.Y); yield return new BoardCoordinate(startingPosition.X, startingPosition.Y + distance); yield return new BoardCoordinate(startingPosition.X, startingPosition.Y - distance); }
public override IEnumerable<BoardCoordinate> GetMovesFrom(BoardCoordinate startingLocation, int boardSize = Board.DefaultBoardSize) { var quadrantsFromASquare = Enumerable.Range(1, 4); var allPotentialMoves = quadrantsFromASquare.SelectMany(q => GetQuadrantMoves(startingLocation, q)); return allPotentialMoves.Where(bc => bc.IsCoordinateValidForBoardSize(boardSize)); }
public IEnumerable<BoardCoordinate> GetCastlingMovesFor(BoardCoordinate moveStart) { if (!IsUnmovedKing(moveStart)) return Enumerable.Empty<BoardCoordinate>(); return moveStart.Y == WhiteKingRow ? EvaluateRowForCastling(WhiteKingRow) : EvaluateRowForCastling(BlackKingRow); }
public void RemovePiece(BoardCoordinate coordinateForRemoval) { if (!DoesPieceExistAt(coordinateForRemoval)) throw new ArgumentException("coordinateForRemoval"); SetPiece(null, coordinateForRemoval); }
public override bool IsCaptureAllowed(BoardCoordinate origin, BoardCoordinate destination) { var diagonalMovesFromOrigin = GetRadialDiagonalFrom(origin, 1); return diagonalMovesFromOrigin.Any(d => d.Matches(destination)) && ((IsFirstPlayerPiece && origin.Y < destination.Y) || (!IsFirstPlayerPiece && origin.Y > destination.Y)); }
public override IEnumerable<BoardCoordinate> GetMovesFrom(BoardCoordinate startingLocation, int boardSize = Board.DefaultBoardSize) { var allDistancesFromStart = Enumerable.Range(1, boardSize + 1); var allPossibleBoardCoordinates = allDistancesFromStart.SelectMany(sp => GetRadialDiagonalFrom(startingLocation, sp)); var legalBoardCoordinates = allPossibleBoardCoordinates.Where(bc => bc.IsCoordinateValidForBoardSize(boardSize)); return legalBoardCoordinates; }
public IEnumerable<BoardCoordinate> GetCastlingMovesFor(BoardCoordinate moveStart) { if (!IsUnmovedKing(moveStart)) return Enumerable.Empty<BoardCoordinate>(); return moveStart.Y == 1 ? EvaluateRowForCastling(1) : EvaluateRowForCastling(8); }
private IEnumerable<BoardCoordinate> GetQuadrantMoves(BoardCoordinate coordinate, int quadrant) { var xMultiplier = quadrant == 1 || quadrant == 4 ? 1 : -1; var yMultiplier = quadrant == 1 || quadrant == 2 ? 1 : -1; yield return new BoardCoordinate(coordinate.X + 2 * xMultiplier, coordinate.Y + yMultiplier * 1); yield return new BoardCoordinate(coordinate.X + 1 * xMultiplier, coordinate.Y + yMultiplier * 2); }
public bool IsThreatened(BoardCoordinate victim, bool isFirstPlayerAttacking) { var numbers = Enumerable.Range(1, _board.Size); var coordinates = numbers.SelectMany(x => numbers, (x, y) => BoardCoordinate.For(x, y)); return coordinates.Any(c => DoesSquareAttack(c, victim, isFirstPlayerAttacking)); }
public IEnumerable<BoardCoordinate> GetMovesFrom(BoardCoordinate originCoordinate) { var piece = GetPiece(originCoordinate); var checker = new CastlingStatusChecker(this); var castlingMoves = checker.GetCastlingMovesFor(originCoordinate).ToList(); var allPossibleMoves = piece.GetMovesFrom(originCoordinate).Union(castlingMoves); return allPossibleMoves.Where(move => IsMoveLegal(originCoordinate, move)); }
public override IEnumerable<BoardCoordinate> GetMovesFrom(BoardCoordinate startingLocation, int boardSize = Board.DefaultBoardSize) { yield return new BoardCoordinate(startingLocation.X, startingLocation.Y + DirectionalMultiplier); if (!HasMoved) yield return new BoardCoordinate(startingLocation.X, startingLocation.Y + 2 * DirectionalMultiplier); yield return new BoardCoordinate(startingLocation.X + 1, startingLocation.Y + DirectionalMultiplier); yield return new BoardCoordinate(startingLocation.X - 1, startingLocation.Y + DirectionalMultiplier); }
public void AddPiece(Piece piece, BoardCoordinate moveTarget) { if (piece == null) throw new ArgumentNullException("piece"); if (!moveTarget.IsCoordinateValidForBoardSize(_boardSize)) throw new ArgumentException("moveTarget"); SetPiece(piece, moveTarget); }
public void MovePiece(BoardCoordinate origin, BoardCoordinate destination) { VerifyCoordinatesOrThrow(origin, destination); var pieceToMove = GetPiece(origin); AddPiece(pieceToMove, destination); RemovePiece(origin); pieceToMove.HasMoved = true; ReconcileEnPassant(origin, destination, pieceToMove); }
public override IEnumerable<BoardCoordinate> GetMovesFrom(BoardCoordinate startingLocation, int boardSize = Board.DefaultBoardSize) { var availableCoordinates = Enumerable.Range(1, boardSize); var verticalMoves = availableCoordinates.Where(y => startingLocation.Y != y). Select(y => new BoardCoordinate(startingLocation.X, y)); var horizontalMoves = availableCoordinates.Where(x => startingLocation.X != x). Select(x => new BoardCoordinate(x, startingLocation.Y)); return verticalMoves.Union(horizontalMoves); }
public void AddPiece(BoardCoordinate square, string pieceString) { VerifyNonEmptyPieceStringOrThrow(pieceString); var pieceCode = pieceString.Substring(1); var playerCode = pieceString[0]; var isFirstPlayer = DetermineIsFirstPlayer(playerCode); var pieceToAdd = BuildPiece(pieceCode, isFirstPlayer); _board.AddPiece(pieceToAdd, square); }
private IEnumerable<BoardCoordinate> GetCastleMoveIfAvailable(BoardCoordinate rookStart, BoardCoordinate moveIfSuccess) { var piece = _board.GetPiece(rookStart); var kingCoordinate = BoardCoordinate.For(KingColumn, rookStart.Y); var pathMaker = new PathMaker(kingCoordinate, rookStart); var spacesBetweenKingAndRook = pathMaker.GetPathToDestination().Where(bc => bc.X != rookStart.X && bc.X != KingColumn); var isBlocked = spacesBetweenKingAndRook.Any(bc => _board.GetPiece(bc) != null); if (piece != null && !piece.HasMoved && !isBlocked) yield return moveIfSuccess; }
public override IEnumerable<BoardCoordinate> GetMovesFrom(BoardCoordinate startingLocation, int boardSize = Board.DefaultBoardSize) { var oneSquareAwayMoves = GetAllRadialMovesFrom(startingLocation, 1); var validOneSquareAway = oneSquareAwayMoves.Where(bc => bc.IsCoordinateValidForBoardSize(boardSize)); if (!HasMoved) return validOneSquareAway.Union(new List<BoardCoordinate>() { BoardCoordinate.For(startingLocation.X + 2, startingLocation.Y), BoardCoordinate.For(startingLocation.X - 2, startingLocation.Y) }); return validOneSquareAway; }
private static bool IsEnPassantApplicable(BoardCoordinate origin, BoardCoordinate destination, Piece pieceToMove) { const int enPassantTrigger = 2; var movementFactor = pieceToMove.IsFirstPlayerPiece ? -enPassantTrigger : enPassantTrigger; return origin.Y == destination.Y + movementFactor; }
private IEnumerable<BoardCoordinate> GetCastleMoveIfAvailable(BoardCoordinate rookStart, BoardCoordinate moveIfSuccess) { var piece = _board.GetPiece(rookStart); if (piece != null && !piece.HasMoved) yield return moveIfSuccess; }
private bool IsUnmovedKing(BoardCoordinate moveStart) { var kingToMove = _board.GetPiece(moveStart) as King; return kingToMove != null && !kingToMove.HasMoved; }
private bool DoesSquareAttack(BoardCoordinate attacker, BoardCoordinate victim, bool isFirstPlayerAttacking) { var piece = _board.GetPiece(attacker); return piece != null && piece.IsCaptureAllowed(attacker, victim) && isFirstPlayerAttacking != piece.IsFirstPlayerPiece; }
public bool DoesPieceExistAt(BoardCoordinate coordinateToCheck) { return(GetPiece(coordinateToCheck) != null); }
private void ReconcileEnPassant(BoardCoordinate origin, BoardCoordinate destination, Piece pieceToMove) { if (IsEnPassantApplicable(origin, destination, pieceToMove)) SetEnPassantIfCandidatePawnsArePresent(destination); CleanEnPassantForPlayerThatJustMoved(pieceToMove); }
public abstract IEnumerable<BoardCoordinate> GetMovesFrom(BoardCoordinate startingLocation, int boardSize = Board.DefaultBoardSize);
public virtual bool IsNonCaptureAllowed(BoardCoordinate origin, BoardCoordinate destination) { return true; }
protected static IEnumerable<BoardCoordinate> GetRadialAdjecentFromInclusive(BoardCoordinate startingPosition, int distance) { var squaresToRadiate = Enumerable.Range(1, distance); return squaresToRadiate.SelectMany(sq => GetRadialAdjacentFrom(startingPosition, sq)); }
public Piece GetPiece(BoardCoordinate coordinateToRetrieve) { return(_pieces[coordinateToRetrieve.X - 1, coordinateToRetrieve.Y - 1]); }
public virtual bool IsCaptureAllowed(BoardCoordinate origin, BoardCoordinate destination) { return GetMovesFrom(origin).Any(m => m.Matches(destination)); }
private void SetEnPassantIfCandidatePawnsArePresent(BoardCoordinate destination) { var leftTarget = BoardCoordinate.For(destination.X - 1, destination.Y); var rightTarget = BoardCoordinate.For(destination.X + 1, destination.Y); SetEnPassantIfPawnExistsAtTarget(destination, leftTarget); SetEnPassantIfPawnExistsAtTarget(destination, rightTarget); }
protected static IEnumerable<BoardCoordinate> GetAllRadialMovesFrom(BoardCoordinate startingLocation, int distance) { return GetRadialDiagonalFromInclusive(startingLocation, distance).Union(GetRadialAdjecentFromInclusive(startingLocation, distance)); }
public PathMaker(BoardCoordinate origin, BoardCoordinate destination) { _origin = origin; _destination = destination; }
private bool DoesFriendlyPieceExistAt(BoardCoordinate origin, BoardCoordinate destination) { var piece = GetPiece(destination); return(piece != null && piece.IsFirstPlayerPiece == GetPiece(origin).IsFirstPlayerPiece); }
private void SetPiece(Piece piece, BoardCoordinate location) { _pieces[location.X - 1, location.Y - 1] = piece; }
public override IEnumerable<BoardCoordinate> GetMovesFrom(BoardCoordinate startingLocation, int boardSize = Board.DefaultBoardSize) { var oneSquareAwayMoves = GetAllRadialMovesFrom(startingLocation, 1); return oneSquareAwayMoves.Where(bc => bc.IsCoordinateValidForBoardSize(boardSize)); }
public override IEnumerable <BoardCoordinate> GetMovesFrom(BoardCoordinate startingLocation, int boardSize = Board.DefaultBoardSize) { var oneSquareAwayMoves = GetAllRadialMovesFrom(startingLocation, 1); return(oneSquareAwayMoves.Where(bc => bc.IsCoordinateValidForBoardSize(boardSize))); }