Ejemplo n.º 1
0
 internal Board()
 {
     for ( int i = 0; i < 8; i++ )
     {
         _board[i] = new Piece[8];
     }
 }
Ejemplo n.º 2
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;
 }
Ejemplo n.º 3
0
 public static char GetCharForPiece(Piece piece)
 {
     switch (piece.Type)
     {
         case PieceType.Pawn:
             return piece.Color == PieceColor.White ? 'P' : 'p';
         case PieceType.Rook:
             return piece.Color == PieceColor.White ? 'R' : 'r';
         case PieceType.Knight:
             return piece.Color == PieceColor.White ? 'N' : 'n';
         case PieceType.Bishop:
             return piece.Color == PieceColor.White ? 'B' : 'b';
         case PieceType.Queen:
             return piece.Color == PieceColor.White ? 'Q' : 'q';
         case PieceType.King:
             return piece.Color == PieceColor.White ? 'K' : 'k';
     }
     throw new InvalidOperationException();
 }
Ejemplo n.º 4
0
 private void SetPiece( Location location, Piece piece )
 {
     var file = location.File - 'a';
     var rank = location.Rank - 1;
     _board[file][rank] = piece;
 }
Ejemplo n.º 5
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();
        }
Ejemplo n.º 6
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);
 }
Ejemplo n.º 7
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;
        }
Ejemplo n.º 8
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;
 }
Ejemplo n.º 9
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)));
 }
Ejemplo n.º 10
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);
 }
Ejemplo n.º 11
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;
 }
Ejemplo n.º 12
0
        private void AddMove(string move, Piece piece, string fromLocation, string toLocation)
        {
            if (string.IsNullOrEmpty(fromLocation) && string.IsNullOrEmpty(toLocation))
            {
                if (move == "O-O")
                    _board.AddMove(Move.GetCastle(piece.Color, BoardSide.KingSide));
                else if (move == "O-O-O")
                    _board.AddMove(Move.GetCastle(piece.Color, BoardSide.QueenSide));
                else
                {
                    string result;
                    if (TagPairs.TryGetValue("Result", out result) && result == move)
                        return;
                    throw new InvalidOperationException($"Castle pattern '{move}' is unknown");
                }
            }
            else
            {
                Location targetLocation = Location.Parse(toLocation);

                Location sourceLocation;
                if (false == Location.TryParse(fromLocation, out sourceLocation))
                {
                    sourceLocation = GetSourceLocation(piece, targetLocation, fromLocation);
                }

                if (sourceLocation == null)
                    throw new Exception(string.Format("Could not find {2}'s original location '{0}' going to '{1}'", fromLocation ?? "<unknown>", targetLocation, piece));

                _board.AddMove(new Move(piece, sourceLocation, targetLocation));
            }
        }
Ejemplo n.º 13
0
 private bool Equals(Piece other)
 {
     return _type == other._type && _color == other._color;
 }