Beispiel #1
0
        private string GetSANSourceString(Board preMoveBoard, Move move)
        {
            var src = BoardHelpers.GetPieceAtIndex(preMoveBoard.Occupancy, move.SourceIndex);

            Debug.Assert(src.HasValue);
            if (src == Piece.King)
            {
                return("K");
            }

            if (src == Piece.Pawn)
            {
                var source = "";

                //if the move was an En Passant or a capture, return the file letter
                if (move.MoveType == MoveType.EnPassant ||
                    move.SourceIndex.GetFile() != move.DestinationIndex.GetFile())
                {
                    source = move.SourceIndex.IndexToFileDisplay().ToString();
                }

                return(source);
            }

            var strSrcPiece              = src.Value.GetCharRepresentation().ToString().ToUpper();
            var otherLikePieces          = preMoveBoard.Occupancy.Occupancy(preMoveBoard.ActivePlayer, src);
            var duplicateAttackerIndexes = new List <ushort>();

            foreach (var attackerIndex in otherLikePieces.GetSetBits())
            {
                var legalMoves = Bitboard.Instance.GetLegalMoves(attackerIndex,
                                                                 preMoveBoard.Occupancy, preMoveBoard.EnPassantIndex, preMoveBoard.CastlingAvailability);
                var canPieceMoveToDestination = legalMoves.Any(x => x.DestinationIndex == move.DestinationIndex);
                if (canPieceMoveToDestination)
                {
                    duplicateAttackerIndexes.Add(attackerIndex);
                }
            }

            if (duplicateAttackerIndexes.Count == 1)
            {
                return(strSrcPiece);
            }
            var duplicateFiles = duplicateAttackerIndexes.Select(x => x.GetFile()).GroupBy(x => x)
                                 .Any(x => x.Count() > 1);
            var duplicateRanks = duplicateAttackerIndexes.Select(x => x.GetRank()).GroupBy(x => x)
                                 .Any(x => x.Count() > 1);

            if (!duplicateFiles)
            {
                return(strSrcPiece + move.SourceIndex.IndexToFileDisplay());
            }

            if (!duplicateRanks)
            {
                return(strSrcPiece + move.SourceIndex.IndexToRankDisplay());
            }

            return(strSrcPiece + move.SourceIndex.ToSquareString());
        }
Beispiel #2
0
        public void GetPieceAtIndexTest(TestCase <Piece?, ushort> testCase)
        {
            var actual = BoardHelpers.GetPieceAtIndex(BoardHelpers.InitialBoard, testCase.TestMethodInputValue);

            Assert.AreEqual(testCase.ExpectedValue, actual, testCase.Description);
        }