コード例 #1
0
        internal bool WillCauseCheck(Move move)
        {
            if (move.Castle)
            {
                if (IsThreatened(Board.GetKing(move.Piece.Color)))
                {
                    return(true);
                }

                //Left castle
                if (move.From.Col - move.To.Col > 0)
                {
                    Move firstKingMove = new Move(move.From, new Position(move.From.Row, move.From.Col - 1), move.Piece, move.Capture);

                    MovePiece(firstKingMove);
                    if (IsThreatened(Board.GetKing(move.Piece.Color)))
                    {
                        UnMovePiece(firstKingMove);
                        return(true);
                    }
                    UnMovePiece(firstKingMove);
                }
                //Right castle
                else
                {
                    Move firstKingMove = new Move(move.From, new Position(move.From.Row, move.From.Col + 1), move.Piece, move.Capture);

                    MovePiece(firstKingMove);
                    if (IsThreatened(Board.GetKing(move.Piece.Color)))
                    {
                        UnMovePiece(firstKingMove);
                        return(true);
                    }
                    UnMovePiece(firstKingMove);
                }
            }

            MovePiece(move);
            bool isInCheck = IsThreatened(Board.GetKing(move.Piece.Color));

            UnMovePiece(move);
            return(isInCheck);
        }
コード例 #2
0
 internal bool IsStaleMate()
 {
     return(!IsThreatened(Board.GetKing(CurrentTurn)) && GetAllMoves(CurrentTurn).Count == 0);
 }
コード例 #3
0
        internal void MovePiece(Move move)
        {
            Position to   = move.To;
            Position from = move.From;

            if (move.Capture is King)
            {
                Debug.WriteLine("Not supposed to happen!");
            }

            // Pawn promotion
            if (move.Promotion)
            {
                Board.PromotePawn(move);
            }

            if (move.Piece is Pawn)
            {
                Pawn pawn = move.Piece as Pawn;

                if (move.EnPassantMove && pawn.Color == PieceColor.White)
                {
                    Board.UpdatePieceEnum(pawn, PieceEnum.WhitePawnEnPassant);
                }
                else if (move.EnPassantMove && pawn.Color == PieceColor.Black)
                {
                    Board.UpdatePieceEnum(pawn, PieceEnum.BlackPawnEnPassant);
                }
            }

            if (move.Piece is King)
            {
                King king = move.Piece as King;

                if (move.Castle)
                {
                    Board.MovePiece(move.CastleMove.From, move.CastleMove.To);
                    move.CastleMove.Piece.MoveCount++;
                }

                if (king.Color == PieceColor.White)
                {
                    Board.UpdatePieceEnum(king, PieceEnum.WhiteKingNoCastle);
                }
                else if (king.Color == PieceColor.Black)
                {
                    Board.UpdatePieceEnum(king, PieceEnum.BlackKingNoCastle);
                }
            }

            if (!(move.Piece is Pawn) || move.Capture is Empty)
            {
                if (progress.Count > 0)
                {
                    progress.Push(progress.Peek() + 1);
                }
                else
                {
                    progress.Push(1);
                }
            }
            else
            {
                progress.Push(0);
            }

            if (!(move.Capture is Empty))
            {
                Board.RemovePiece(move.Capture.Position.Row, move.Capture.Position.Col);
            }

            Board.MovePiece(from, to);
            move.Piece.MoveCount++;

            moves.Push(move);
            prevHashes.Push(Board.Hash);

            if (CurrentTurn == PieceColor.White)
            {
                CurrentTurn = PieceColor.Black;
                ClearPassant(PieceColor.Black);
            }
            else
            {
                CurrentTurn = PieceColor.White;
                ClearPassant(PieceColor.White);
            }

            if (IsThreatened(Board.GetKing(CurrentTurn)))
            {
                move.CheckBonus = true;
            }
        }