コード例 #1
0
        Result GetGameState()
        {
            MoveGenerator moveGenerator = new MoveGenerator();
            var           moves         = moveGenerator.GenerateMoves(board);

            // Look for mate/stalemate
            if (moves.Count == 0)
            {
                if (moveGenerator.InCheck())
                {
                    return((board.WhiteToMove) ? Result.WhiteIsMated : Result.BlackIsMated);
                }
                return(Result.Stalemate);
            }

            // Fifty move rule
            if (board.fiftyMoveCounter >= 100)
            {
                return(Result.FiftyMoveRule);
            }

            // Threefold repetition
            int repCount = board.RepetitionPositionHistory.Count((x => x == board.ZobristKey));

            if (repCount == 3)
            {
                return(Result.Repetition);
            }

            // Look for insufficient material (not all cases implemented yet)
            int numPawns   = board.pawns[Board.WhiteIndex].Count + board.pawns[Board.BlackIndex].Count;
            int numRooks   = board.rooks[Board.WhiteIndex].Count + board.rooks[Board.BlackIndex].Count;
            int numQueens  = board.queens[Board.WhiteIndex].Count + board.queens[Board.BlackIndex].Count;
            int numKnights = board.knights[Board.WhiteIndex].Count + board.knights[Board.BlackIndex].Count;
            int numBishops = board.bishops[Board.WhiteIndex].Count + board.bishops[Board.BlackIndex].Count;

            if (numPawns + numRooks + numQueens == 0)
            {
                if (numKnights == 1 || numBishops == 1)
                {
                    return(Result.InsufficientMaterial);
                }
            }

            return(Result.Playing);
        }
コード例 #2
0
ファイル: GameManager.cs プロジェクト: zeref80/Diayy-chess-2
        // zasady WDZ
        Result GetGameState()
        {
            MoveGenerator moveGenerator = new MoveGenerator();
            var           moves         = moveGenerator.GenerateMoves(board);

            // Mat
            if (moves.Count == 0)
            {
                if (moveGenerator.InCheck())
                {
                    return((board.WhiteToMove) ? Result.WhiteIsMated : Result.BlackIsMated);
                }
                return(Result.Stalemate);
            }

            // Zasada 50
            if (board.fiftyMoveCounter >= 100)
            {
                return(Result.FiftyMoveRule);
            }

            // Zasada Powtórzeń
            int repCount = board.RepetitionPositionHistory.Count((x => x == board.ZobristKey));

            if (repCount == 3)
            {
                return(Result.Repetition);
            }

            int numPawns   = board.pawns[Board.WhiteIndex].Count + board.pawns[Board.BlackIndex].Count;
            int numRooks   = board.rooks[Board.WhiteIndex].Count + board.rooks[Board.BlackIndex].Count;
            int numQueens  = board.queens[Board.WhiteIndex].Count + board.queens[Board.BlackIndex].Count;
            int numKnights = board.knights[Board.WhiteIndex].Count + board.knights[Board.BlackIndex].Count;
            int numBishops = board.bishops[Board.WhiteIndex].Count + board.bishops[Board.BlackIndex].Count;

            if (numPawns + numRooks + numQueens == 0)
            {
                if (numKnights == 1 || numBishops == 1)
                {
                    return(Result.InsufficientMaterial);
                }
            }

            return(Result.Playing);
        }
コード例 #3
0
        static string NotationFromMove(Board board, Move move)
        {
            MoveGenerator moveGen = new MoveGenerator();

            int movePieceType     = Piece.PieceType(board.Square[move.StartSquare]);
            int capturedPieceType = Piece.PieceType(board.Square[move.TargetSquare]);

            if (move.MoveFlag == Move.Flag.Castling)
            {
                int delta = move.TargetSquare - move.StartSquare;
                if (delta == 2)
                {
                    return("O-O");
                }
                else if (delta == -2)
                {
                    return("O-O-O");
                }
            }

            string moveNotation = GetSymbolFromPieceType(movePieceType);

            // check if any ambiguity exists in notation (e.g if e2 can be reached via Nfe2 and Nbe2)
            if (movePieceType != Piece.Pawn && movePieceType != Piece.King)
            {
                var allMoves = moveGen.GenerateMoves(board);

                foreach (Move altMove in allMoves)
                {
                    if (altMove.StartSquare != move.StartSquare && altMove.TargetSquare == move.TargetSquare)               // if moving to same square from different square
                    {
                        if (Piece.PieceType(board.Square[altMove.StartSquare]) == movePieceType)                            // same piece type
                        {
                            int fromFileIndex          = BoardRepresentation.FileIndex(move.StartSquare);
                            int alternateFromFileIndex = BoardRepresentation.FileIndex(altMove.StartSquare);
                            int fromRankIndex          = BoardRepresentation.RankIndex(move.StartSquare);
                            int alternateFromRankIndex = BoardRepresentation.RankIndex(altMove.StartSquare);

                            if (fromFileIndex != alternateFromFileIndex)                               // pieces on different files, thus ambiguity can be resolved by specifying file
                            {
                                moveNotation += BoardRepresentation.fileNames[fromFileIndex];
                                break;                                 // ambiguity resolved
                            }
                            else if (fromRankIndex != alternateFromRankIndex)
                            {
                                moveNotation += BoardRepresentation.rankNames[fromRankIndex];
                                break;                                 // ambiguity resolved
                            }
                        }
                    }
                }
            }

            if (capturedPieceType != 0)               // add 'x' to indicate capture
            {
                if (movePieceType == Piece.Pawn)
                {
                    moveNotation += BoardRepresentation.fileNames[BoardRepresentation.FileIndex(move.StartSquare)];
                }
                moveNotation += "x";
            }
            else                 // check if capturing ep
            {
                if (move.MoveFlag == Move.Flag.EnPassantCapture)
                {
                    moveNotation += BoardRepresentation.fileNames[BoardRepresentation.FileIndex(move.StartSquare)] + "x";
                }
            }

            moveNotation += BoardRepresentation.fileNames[BoardRepresentation.FileIndex(move.TargetSquare)];
            moveNotation += BoardRepresentation.rankNames[BoardRepresentation.RankIndex(move.TargetSquare)];

            // add promotion piece
            if (move.IsPromotion)
            {
                int promotionPieceType = move.PromotionPieceType;
                moveNotation += "=" + GetSymbolFromPieceType(promotionPieceType);
            }

            board.MakeMove(move, inSearch: true);
            var legalResponses = moveGen.GenerateMoves(board);

            // add check/mate symbol if applicable
            if (moveGen.InCheck())
            {
                if (legalResponses.Count == 0)
                {
                    moveNotation += "#";
                }
                else
                {
                    moveNotation += "+";
                }
            }
            board.UnmakeMove(move, inSearch: true);

            return(moveNotation);
        }