Пример #1
0
 public Move(Piece piece, Location from, Location to)
 {
     if (piece == null) throw new ArgumentNullException(nameof(piece));
     if (@from == null) throw new ArgumentNullException(nameof(@from));
     if (to == null) throw new ArgumentNullException(nameof(to));
     From = from;
     To = to;
     Piece = piece;
 }
Пример #2
0
 public Piece GetPiece( Location location )
 {
     var file = location.File - 'a';
     var rank = location.Rank - 1;
     return _board[file][rank];
 }
Пример #3
0
 private void SetPiece( Location location, Piece piece )
 {
     var file = location.File - 'a';
     var rank = location.Rank - 1;
     _board[file][rank] = piece;
 }
Пример #4
0
 public static Location FromOffset(Location location, int fileOffset, int rankOffset)
 {
     var rank = location.Rank + rankOffset;
     var file = (char) (location.File + fileOffset);
     if (rank < 1 || rank > 8)
         return null;
     if (file < 'a' || file > 'h')
         return null;
     return new Location(file, rank);
 }
Пример #5
0
 private bool Equals( Location other )
 {
     return _rank == other._rank && _file == other._file;
 }
Пример #6
0
        public static bool TryParse(string locationString, out Location location)
        {
            location = null;
            if (locationString?.Length != 2) return false;

            int rank;
            if (int.TryParse(locationString[1].ToString(), out rank))
            {
                location = new Location(locationString[0], rank);
                return true;
            }
            return false;
        }
Пример #7
0
        private Location GetSourceLocation(Piece piece, Location destinationLocation, string fromLocationHint)
        {
            if (piece == null) throw new ArgumentNullException(nameof(piece));
            if (destinationLocation == null) throw new ArgumentNullException(nameof(destinationLocation));

            switch (piece.Type)
            {
                case PieceType.Pawn:
                    return GetPawnSourceLocation(piece, destinationLocation, fromLocationHint);
                case PieceType.Rook:
                    return GetRookSourceLocation(piece, destinationLocation, fromLocationHint);
                case PieceType.Knight:
                    return GetKnightSourceLocation(piece, destinationLocation, fromLocationHint);
                case PieceType.Bishop:
                    return GetBishopSourceLocation(piece, destinationLocation, fromLocationHint);
                case PieceType.King:
                    return GetKingSourceLocation(piece, destinationLocation);
                case PieceType.Queen:
                    return GetQueenSourceLocation(piece, destinationLocation, fromLocationHint);
            }
            throw new InvalidOperationException();
        }
Пример #8
0
 private Location GetRookSourceLocation(Piece rook, Location destinationLocation, string fromLocationHint)
 {
     return
         FindSourceLocationForPieceWithStrategy(rook, destinationLocation, l => Location.FromOffset(l, 0, -1), fromLocationHint) ??
         FindSourceLocationForPieceWithStrategy(rook, destinationLocation, l => Location.FromOffset(l, 0, 1), fromLocationHint) ??
         FindSourceLocationForPieceWithStrategy(rook, destinationLocation, l => Location.FromOffset(l, -1, 0), fromLocationHint) ??
         FindSourceLocationForPieceWithStrategy(rook, destinationLocation, l => Location.FromOffset(l, 1, 0), fromLocationHint);
 }
Пример #9
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;
        }
Пример #10
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;
 }
Пример #11
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)));
 }
Пример #12
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);
 }
Пример #13
0
 private Location FindSourceLocationForPieceWithStrategy(Piece piece, Location destinationLocation, Func<Location, Location> strategy, string locationHint)
 {
     for (Location location = strategy(destinationLocation);
         location != null;
         location = strategy(location))
     {
         var foundPiece = GetPiece(location);
         if (foundPiece != null)
         {
             if (piece.Equals(foundPiece) && location.MatchesHint(locationHint))
                 return location;
             break;
         }
     }
     return null;
 }
Пример #14
0
 public Piece GetPiece(Location location)
 {
     //For now it is just the initial starting locations since we have not implemented moving
     return _board.GetPiece(location);
 }