Beispiel #1
0
        private Square pgnLength4(GameState gameState, IEnumerable <AttackedSquare> potentialPositions, string newPgnMove)
        {
            var        ambiguityResolver = newPgnMove[1];
            var        isRank            = IsRank(ambiguityResolver); // this could be either a rank or a file
            List <int> ambiguityResolutionSet;

            if (isRank)
            {
                int rank = 0;
                Int32.TryParse(ambiguityResolver.ToString(), out rank);
                ambiguityResolutionSet = this._orthogonalService.GetEntireRank(rank - 1);// needs to be using zero-based rank offset
            }
            else
            {
                var iFile = NotationEngine.FileToInt(ambiguityResolver);
                ambiguityResolutionSet = this._orthogonalService.GetEntireFile(iFile);
            }
            var intersection = potentialPositions.Select(a => a.AttackingSquare.Index).Intersect(ambiguityResolutionSet);

            if (intersection.Count() > 1)
            {
                throw new Exception("There should not be more than one item found here.");
            }
            return(gameState.Squares.GetSquare(intersection.First()));
        }
Beispiel #2
0
        private bool isValidKnightMove(int position, int tempPosition, int file, int rank)
        {
            var isValidCoordinate = GeneralEngine.IsValidCoordinate(tempPosition);

            if (!isValidCoordinate)
            {
                return(false);
            }

            var tempCoord = NotationEngine.PositionToCoordinate(tempPosition);
            var tempFile  = NotationEngine.FileToInt(tempCoord[0]);
            var tempRank  = (int)tempCoord[1];

            var fileDiff = Math.Abs(tempFile - file);
            var rankDiff = Math.Abs(tempRank - rank);

            if (fileDiff > 2 || fileDiff < 1)
            {
                return(false);
            }
            if (rankDiff > 2 || rankDiff < 1)
            {
                return(false);
            }

            return(true);
        }
Beispiel #3
0
        private Square pgnLength5(GameState gameState, string newPgnMove)
        {
            var _file = NotationEngine.FileToInt(newPgnMove[1]);
            var _rank = 0;

            Int32.TryParse(newPgnMove[2].ToString(), out _rank);
            var pos = NotationEngine.CoordinatePairToPosition(_file, _rank);

            return(gameState.Squares.GetSquare(pos));
        }
Beispiel #4
0
        private Square pgnLength3(IEnumerable <AttackedSquare> potentialPositions, string newPgnMove)
        {
            var ambiguityResolver = newPgnMove[0];
            var files             = this._orthogonalService.GetEntireFile(NotationEngine.FileToInt(ambiguityResolver)); // this will always be a file if this is a pawn
            var potentialSquares  = potentialPositions.Where(a => files.Contains(a.AttackingSquare.Index)).ToList();

            if (potentialSquares.Count() > 1)
            {
                throw new Exception("There should not be more than one item found here.");
            }
            return(potentialSquares.First().AttackingSquare);
        }
Beispiel #5
0
        private void getPawnAttacks(GameState gameState, Square square, List <AttackedSquare> accumulator)
        {
            var squares    = gameState.Squares;
            var position   = square.Index;
            var pieceColor = square.Piece.Color;
            var coord      = NotationEngine.PositionToCoordinate(position);
            int file       = NotationEngine.FileToInt(coord[0]);
            int rank       = NotationEngine.PositionToRankInt(position);

            var directionIndicator = pieceColor == Color.White ? 1 : -1;
            var homeRankIndicator  = pieceColor == Color.White ? 2 : 7;

            var nextRank             = (rank + directionIndicator);
            var aheadOneRankPosition = NotationEngine.CoordinatePairToPosition(file, nextRank);
            var aheadOneRankSquare   = squares.GetSquare(aheadOneRankPosition);
            var attacks = new List <AttackedSquare>();

            if (!aheadOneRankSquare.Occupied)
            {
                //can't attack going forward
                attacks.Add(new AttackedSquare(square, aheadOneRankSquare, true));
            }

            managePawnAttacks(squares, square, pieceColor, file, rank, directionIndicator, homeRankIndicator, nextRank, attacks, aheadOneRankSquare.Occupied);

            //add en passant position: -1 indicates null here
            if (gameState.EnPassantTargetSquare != "-")
            {
                var enPassantTargetPosition = NotationEngine.CoordinateToPosition(gameState.EnPassantTargetSquare);
                var leftPos  = NotationEngine.CoordinatePairToPosition(file - 1, nextRank);
                var rightPos = NotationEngine.CoordinatePairToPosition(file + 1, nextRank);
                if (enPassantTargetPosition == leftPos || enPassantTargetPosition == rightPos)
                {
                    var enPassantSquare = squares.GetSquare(enPassantTargetPosition);
                    attacks.Add(new AttackedSquare(square, enPassantSquare));
                }
            }
            if (attacks.Any())
            {
                accumulator.AddRange(attacks);
            }
        }
Beispiel #6
0
        private void getKnightAttacks(GameState gameState, Square square, List <AttackedSquare> accumulator)
        {
            var squares            = gameState.Squares;
            var currentPosition    = square.Index;
            var pieceColor         = square.Piece.Color;
            var attacks            = new List <AttackedSquare>();
            var coord              = NotationEngine.PositionToCoordinate(currentPosition);
            var file               = NotationEngine.FileToInt(coord[0]);
            var rank               = (int)coord[1];
            var potentialPositions = new List <int> {
                6, 10, 15, 17, -6, -10, -15, -17
            };

            foreach (var potentialPosition in potentialPositions)
            {
                var position           = currentPosition + potentialPosition;
                var _isValidKnightMove = isValidKnightMove(currentPosition, position, file, rank);
                var _isValidMove       = isValidMove(accumulator, gameState, square, position, pieceColor);
                var _isValidCoordinate = GeneralEngine.IsValidCoordinate(position);

                if (!_isValidKnightMove || !_isValidMove.IsValid || !_isValidCoordinate)
                {
                    continue;
                }

                var attackedSquare = squares.GetSquare(position);
                if (!attackedSquare.Occupied)
                {
                    attacks.Add(new AttackedSquare(square, attackedSquare));
                }
                else
                {
                    attacks.Add(new AttackedSquare(square, attackedSquare, isProtecting: attackedSquare.Piece.Color == pieceColor));
                }
            }
            if (attacks.Any())
            {
                accumulator.AddRange(attacks);
            }
        }