Exemple #1
0
 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);
 }
Exemple #2
0
        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);
        }
Exemple #4
0
        public void RemovePiece(BoardCoordinate coordinateForRemoval)
        {
            if (!DoesPieceExistAt(coordinateForRemoval))
                throw new ArgumentException("coordinateForRemoval");

            SetPiece(null, coordinateForRemoval);
        }
Exemple #5
0
        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));
        }
Exemple #6
0
 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);
        }
Exemple #8
0
 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));
        }
Exemple #10
0
 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));
 }
Exemple #11
0
 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);
 }
Exemple #12
0
        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);
        }
Exemple #13
0
        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);
        }
Exemple #14
0
        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;
        }
Exemple #17
0
        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;
        }
Exemple #18
0
 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;
        }
Exemple #22
0
 public bool DoesPieceExistAt(BoardCoordinate coordinateToCheck)
 {
     return(GetPiece(coordinateToCheck) != null);
 }
Exemple #23
0
 private void ReconcileEnPassant(BoardCoordinate origin, BoardCoordinate destination, Piece pieceToMove)
 {
     if (IsEnPassantApplicable(origin, destination, pieceToMove))
         SetEnPassantIfCandidatePawnsArePresent(destination);
     CleanEnPassantForPlayerThatJustMoved(pieceToMove);
 }
Exemple #24
0
 public abstract IEnumerable<BoardCoordinate> GetMovesFrom(BoardCoordinate startingLocation, int boardSize = Board.DefaultBoardSize);
Exemple #25
0
 public virtual bool IsNonCaptureAllowed(BoardCoordinate origin, BoardCoordinate destination)
 {
     return true;
 }
Exemple #26
0
 protected static IEnumerable<BoardCoordinate> GetRadialAdjecentFromInclusive(BoardCoordinate startingPosition, int distance)
 {
     var squaresToRadiate = Enumerable.Range(1, distance);
     return squaresToRadiate.SelectMany(sq => GetRadialAdjacentFrom(startingPosition, sq));
 }
Exemple #27
0
 public Piece GetPiece(BoardCoordinate coordinateToRetrieve)
 {
     return(_pieces[coordinateToRetrieve.X - 1, coordinateToRetrieve.Y - 1]);
 }
Exemple #28
0
 public virtual bool IsCaptureAllowed(BoardCoordinate origin, BoardCoordinate destination)
 {
     return GetMovesFrom(origin).Any(m => m.Matches(destination));
 }
Exemple #29
0
        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);
        }
Exemple #30
0
 protected static IEnumerable<BoardCoordinate> GetAllRadialMovesFrom(BoardCoordinate startingLocation, int distance)
 {
     return GetRadialDiagonalFromInclusive(startingLocation, distance).Union(GetRadialAdjecentFromInclusive(startingLocation, distance));
 }
Exemple #31
0
 public PathMaker(BoardCoordinate origin, BoardCoordinate destination)
 {
     _origin = origin;
     _destination = destination;
 }
Exemple #32
0
        private bool DoesFriendlyPieceExistAt(BoardCoordinate origin, BoardCoordinate destination)
        {
            var piece = GetPiece(destination);

            return(piece != null && piece.IsFirstPlayerPiece == GetPiece(origin).IsFirstPlayerPiece);
        }
Exemple #33
0
 private void SetPiece(Piece piece, BoardCoordinate location)
 {
     _pieces[location.X - 1, location.Y - 1] = piece;
 }
Exemple #34
0
 public override IEnumerable<BoardCoordinate> GetMovesFrom(BoardCoordinate startingLocation, int boardSize = Board.DefaultBoardSize)
 {
     var oneSquareAwayMoves = GetAllRadialMovesFrom(startingLocation, 1);
     return oneSquareAwayMoves.Where(bc => bc.IsCoordinateValidForBoardSize(boardSize));
 }
Exemple #35
0
        public override IEnumerable <BoardCoordinate> GetMovesFrom(BoardCoordinate startingLocation, int boardSize = Board.DefaultBoardSize)
        {
            var oneSquareAwayMoves = GetAllRadialMovesFrom(startingLocation, 1);

            return(oneSquareAwayMoves.Where(bc => bc.IsCoordinateValidForBoardSize(boardSize)));
        }