Exemplo n.º 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);
 }
Exemplo n.º 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));
        }
Exemplo n.º 3
0
        public IEnumerable<BoardCoordinate> GetCastlingMovesFor(BoardCoordinate moveStart)
        {
            if (!IsUnmovedKing(moveStart))
                return Enumerable.Empty<BoardCoordinate>();

            return moveStart.Y == WhiteKingRow ? EvaluateRowForCastling(WhiteKingRow) : EvaluateRowForCastling(BlackKingRow);
        }
Exemplo n.º 4
0
        public void RemovePiece(BoardCoordinate coordinateForRemoval)
        {
            if (!DoesPieceExistAt(coordinateForRemoval))
                throw new ArgumentException("coordinateForRemoval");

            SetPiece(null, coordinateForRemoval);
        }
Exemplo n.º 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));
        }
Exemplo n.º 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;
 }
Exemplo n.º 7
0
        public IEnumerable<BoardCoordinate> GetCastlingMovesFor(BoardCoordinate moveStart)
        {
            if (!IsUnmovedKing(moveStart))
                return Enumerable.Empty<BoardCoordinate>();

            return moveStart.Y == 1 ? EvaluateRowForCastling(1) : EvaluateRowForCastling(8);
        }
Exemplo n.º 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);
 }
Exemplo n.º 9
0
        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));
        }
Exemplo n.º 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));
 }
Exemplo n.º 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);
 }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 15
0
        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);
        }
Exemplo n.º 16
0
        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;
        }
Exemplo n.º 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;
        }
Exemplo n.º 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;
 }
Exemplo n.º 19
0
 private IEnumerable<BoardCoordinate> GetCastleMoveIfAvailable(BoardCoordinate rookStart, BoardCoordinate moveIfSuccess)
 {
     var piece = _board.GetPiece(rookStart);
     if (piece != null && !piece.HasMoved)
         yield return moveIfSuccess;
 }
Exemplo n.º 20
0
 private bool IsUnmovedKing(BoardCoordinate moveStart)
 {
     var kingToMove = _board.GetPiece(moveStart) as King;
     return kingToMove != null && !kingToMove.HasMoved;
 }
Exemplo n.º 21
0
        private bool DoesSquareAttack(BoardCoordinate attacker, BoardCoordinate victim, bool isFirstPlayerAttacking)
        {
            var piece = _board.GetPiece(attacker);

            return piece != null && piece.IsCaptureAllowed(attacker, victim) && isFirstPlayerAttacking != piece.IsFirstPlayerPiece;
        }
Exemplo n.º 22
0
 public bool DoesPieceExistAt(BoardCoordinate coordinateToCheck)
 {
     return(GetPiece(coordinateToCheck) != null);
 }
Exemplo n.º 23
0
 private void ReconcileEnPassant(BoardCoordinate origin, BoardCoordinate destination, Piece pieceToMove)
 {
     if (IsEnPassantApplicable(origin, destination, pieceToMove))
         SetEnPassantIfCandidatePawnsArePresent(destination);
     CleanEnPassantForPlayerThatJustMoved(pieceToMove);
 }
Exemplo n.º 24
0
 public abstract IEnumerable<BoardCoordinate> GetMovesFrom(BoardCoordinate startingLocation, int boardSize = Board.DefaultBoardSize);
Exemplo n.º 25
0
 public virtual bool IsNonCaptureAllowed(BoardCoordinate origin, BoardCoordinate destination)
 {
     return true;
 }
Exemplo n.º 26
0
 protected static IEnumerable<BoardCoordinate> GetRadialAdjecentFromInclusive(BoardCoordinate startingPosition, int distance)
 {
     var squaresToRadiate = Enumerable.Range(1, distance);
     return squaresToRadiate.SelectMany(sq => GetRadialAdjacentFrom(startingPosition, sq));
 }
Exemplo n.º 27
0
 public Piece GetPiece(BoardCoordinate coordinateToRetrieve)
 {
     return(_pieces[coordinateToRetrieve.X - 1, coordinateToRetrieve.Y - 1]);
 }
Exemplo n.º 28
0
 public virtual bool IsCaptureAllowed(BoardCoordinate origin, BoardCoordinate destination)
 {
     return GetMovesFrom(origin).Any(m => m.Matches(destination));
 }
Exemplo n.º 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);
        }
Exemplo n.º 30
0
 protected static IEnumerable<BoardCoordinate> GetAllRadialMovesFrom(BoardCoordinate startingLocation, int distance)
 {
     return GetRadialDiagonalFromInclusive(startingLocation, distance).Union(GetRadialAdjecentFromInclusive(startingLocation, distance));
 }
Exemplo n.º 31
0
 public PathMaker(BoardCoordinate origin, BoardCoordinate destination)
 {
     _origin = origin;
     _destination = destination;
 }
Exemplo n.º 32
0
        private bool DoesFriendlyPieceExistAt(BoardCoordinate origin, BoardCoordinate destination)
        {
            var piece = GetPiece(destination);

            return(piece != null && piece.IsFirstPlayerPiece == GetPiece(origin).IsFirstPlayerPiece);
        }
Exemplo n.º 33
0
 private void SetPiece(Piece piece, BoardCoordinate location)
 {
     _pieces[location.X - 1, location.Y - 1] = piece;
 }
Exemplo n.º 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));
 }
Exemplo n.º 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)));
        }