예제 #1
0
파일: Board.cs 프로젝트: Keboo/PGNSharp
 internal Board()
 {
     for ( int i = 0; i < 8; i++ )
     {
         _board[i] = new Piece[8];
     }
 }
예제 #2
0
파일: Move.cs 프로젝트: Keboo/PGNSharp
 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;
 }
예제 #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();
 }
예제 #4
0
파일: Board.cs 프로젝트: Keboo/PGNSharp
 private void SetPiece( Location location, Piece piece )
 {
     var file = location.File - 'a';
     var rank = location.Rank - 1;
     _board[file][rank] = piece;
 }
예제 #5
0
파일: Game.cs 프로젝트: Keboo/PGNSharp
        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();
        }
예제 #6
0
파일: Game.cs 프로젝트: Keboo/PGNSharp
 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);
 }
예제 #7
0
파일: Game.cs 프로젝트: Keboo/PGNSharp
        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;
        }
예제 #8
0
파일: Game.cs 프로젝트: Keboo/PGNSharp
 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;
 }
예제 #9
0
파일: Game.cs 프로젝트: Keboo/PGNSharp
 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)));
 }
예제 #10
0
파일: Game.cs 프로젝트: Keboo/PGNSharp
 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);
 }
예제 #11
0
파일: Game.cs 프로젝트: Keboo/PGNSharp
 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;
 }
예제 #12
0
파일: Game.cs 프로젝트: Keboo/PGNSharp
        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));
            }
        }
예제 #13
0
파일: Piece.cs 프로젝트: Keboo/PGNSharp
 private bool Equals(Piece other)
 {
     return _type == other._type && _color == other._color;
 }