示例#1
0
        private Location GetPawnSourceLocation(Piece pawn, Location destinationLocation, string fromLocationHint)
        {
            //Check if the space was occupied
            var previousPiece = GetPiece(destinationLocation);

            if (previousPiece != null)
            {
                //It was a capture check the diagonals
                //TODO: Validate that both locations do not contain a valid pawn
                var leftLocation = Location.FromOffset(destinationLocation, -1, pawn.Color == PieceColor.White ? -1 : 1);
                if (leftLocation != null && pawn.Equals(GetPiece(leftLocation)) && leftLocation.MatchesHint(fromLocationHint))
                {
                    return(leftLocation);
                }
                var rightLocation = Location.FromOffset(destinationLocation, 1, pawn.Color == PieceColor.White ? -1 : 1);
                if (rightLocation != null && pawn.Equals(GetPiece(rightLocation)) && rightLocation.MatchesHint(fromLocationHint))
                {
                    return(rightLocation);
                }
            }
            else
            {
                //Not a capture look for a pawn in the same file
                var oneBackLocation = Location.FromOffset(destinationLocation, 0, pawn.Color == PieceColor.White ? -1 : 1);
                if (pawn.Equals(GetPiece(oneBackLocation)))
                {
                    return(oneBackLocation);
                }

                //Check if the pawn moved two spaces on its first move
                if (pawn.Color == PieceColor.White && destinationLocation.Rank == 4)
                {
                    var startingLocation = Location.FromOffset(destinationLocation, 0, -2);
                    if (pawn.Equals(GetPiece(startingLocation)))
                    {
                        return(startingLocation);
                    }
                }
                else if (pawn.Color == PieceColor.Black && destinationLocation.Rank == 5)
                {
                    var startingLocation = Location.FromOffset(destinationLocation, 0, 2);
                    if (pawn.Equals(GetPiece(startingLocation)))
                    {
                        return(startingLocation);
                    }
                }
            }
            return(null);
        }
示例#2
0
        private Location GetKingSourceLocation(Piece king, Location destinationLocation)
        {
            var locations = new[]
            {
                Location.FromOffset(destinationLocation, 0, 1),
                Location.FromOffset(destinationLocation, 1, 1),
                Location.FromOffset(destinationLocation, 1, 0),
                Location.FromOffset(destinationLocation, 1, -1),
                Location.FromOffset(destinationLocation, 0, -1),
                Location.FromOffset(destinationLocation, -1, -1),
                Location.FromOffset(destinationLocation, -1, 0),
                Location.FromOffset(destinationLocation, -1, 1)
            };

            return(locations.FirstOrDefault(location => king.Equals(GetPiece(location))));
        }
示例#3
0
        private Location GetKnightSourceLocation(Piece knight, Location destinationLocation, string fromLocationHint)
        {
            var location = Location.FromOffset(destinationLocation, 2, 1);

            if (location != null && knight.Equals(GetPiece(location)) && location.MatchesHint(fromLocationHint))
            {
                return(location);
            }
            location = Location.FromOffset(destinationLocation, 2, -1);
            if (location != null && knight.Equals(GetPiece(location)) && location.MatchesHint(fromLocationHint))
            {
                return(location);
            }
            location = Location.FromOffset(destinationLocation, 1, -2);
            if (location != null && knight.Equals(GetPiece(location)) && location.MatchesHint(fromLocationHint))
            {
                return(location);
            }
            location = Location.FromOffset(destinationLocation, -1, -2);
            if (location != null && knight.Equals(GetPiece(location)) && location.MatchesHint(fromLocationHint))
            {
                return(location);
            }
            location = Location.FromOffset(destinationLocation, -2, -1);
            if (location != null && knight.Equals(GetPiece(location)) && location.MatchesHint(fromLocationHint))
            {
                return(location);
            }
            location = Location.FromOffset(destinationLocation, -2, 1);
            if (location != null && knight.Equals(GetPiece(location)) && location.MatchesHint(fromLocationHint))
            {
                return(location);
            }
            location = Location.FromOffset(destinationLocation, -1, 2);
            if (location != null && knight.Equals(GetPiece(location)) && location.MatchesHint(fromLocationHint))
            {
                return(location);
            }
            location = Location.FromOffset(destinationLocation, 1, 2);
            if (location != null && knight.Equals(GetPiece(location)) && location.MatchesHint(fromLocationHint))
            {
                return(location);
            }
            return(null);
        }
示例#4
0
 private Location GetQueenSourceLocation(Piece queen, Location destinationLocation, string fromLocationHint)
 {
     return
         (FindSourceLocationForPieceWithStrategy(queen, destinationLocation, l => Location.FromOffset(l, 0, 1), fromLocationHint) ??
          FindSourceLocationForPieceWithStrategy(queen, destinationLocation, l => Location.FromOffset(l, 1, 1), fromLocationHint) ??
          FindSourceLocationForPieceWithStrategy(queen, destinationLocation, l => Location.FromOffset(l, 1, 0), fromLocationHint) ??
          FindSourceLocationForPieceWithStrategy(queen, destinationLocation, l => Location.FromOffset(l, 1, -1), fromLocationHint) ??
          FindSourceLocationForPieceWithStrategy(queen, destinationLocation, l => Location.FromOffset(l, 0, -1), fromLocationHint) ??
          FindSourceLocationForPieceWithStrategy(queen, destinationLocation, l => Location.FromOffset(l, -1, -1), fromLocationHint) ??
          FindSourceLocationForPieceWithStrategy(queen, destinationLocation, l => Location.FromOffset(l, -1, 0), fromLocationHint) ??
          FindSourceLocationForPieceWithStrategy(queen, destinationLocation, l => Location.FromOffset(l, -1, 1), fromLocationHint));
 }
示例#5
0
 private Location GetBishopSourceLocation(Piece bishop, Location destinationLocation, string fromLocationHint)
 {
     return
         (FindSourceLocationForPieceWithStrategy(bishop, destinationLocation, l => Location.FromOffset(l, -1, -1), fromLocationHint) ??
          FindSourceLocationForPieceWithStrategy(bishop, destinationLocation, l => Location.FromOffset(l, -1, 1), fromLocationHint) ??
          FindSourceLocationForPieceWithStrategy(bishop, destinationLocation, l => Location.FromOffset(l, 1, 1), fromLocationHint) ??
          FindSourceLocationForPieceWithStrategy(bishop, destinationLocation, l => Location.FromOffset(l, 1, -1), fromLocationHint));
 }