Beispiel #1
0
        public DivideResults Divide(int depth)
        {
            int[] divideMoves = new int[1000];
            bool  inCheck     = TestInCheck(ToMove);

            int   nodes      = GetMoves(0, divideMoves);
            ulong movescount = 0;

            for (int i = 0; i < nodes; ++i)
            {
                Move(divideMoves[i]);
                PerfResults res = Perft(Math.Max(0, depth - 1), false, null);
                if (null != DividePartialResult)
                {
                    this.CurrentDivideMove      = MovePackHelper.GetAlgebraicString(divideMoves[i]);
                    this.CurrentDivideNodeCount = (long)res.MovesCount;
                    movescount += res.MovesCount;

                    DividePartialResult(this, EventArgs.Empty);
                }
                UndoMove(divideMoves[i]);
            }
            return(new DivideResults()
            {
                NodesCount = (ulong)nodes, MovesCount = movescount
            });
        }
Beispiel #2
0
 static void MakeDict(int n, int[] moves)
 {
     destCells.Clear();
     for (int q = 0; q < n; ++q)
     {
         destCells.Add(MovePackHelper.GetEndSquare(moves[q]));
     }
 }
Beispiel #3
0
 public void UnMove(int move)
 {
     board.BoardArray[HomeSquare] = null;
     board.BoardArray[MovePackHelper.GetStartSquare(move)] = this;
     board.ZKey ^= ZKey;
     HomeSquare  = MovePackHelper.GetStartSquare(move);
     board.ZKey ^= ZKey;
     OnUnMove(move);
 }
Beispiel #4
0
 public void Move(int move)
 {
     OnMove(move);
     board.BoardArray[HomeSquare] = null;
     board.BoardArray[MovePackHelper.GetEndSquare(move)] = this;
     board.ZKey ^= ZKey;
     HomeSquare  = MovePackHelper.GetEndSquare(move);
     board.ZKey ^= ZKey;
 }
Beispiel #5
0
        public void Move(int move)
        {
            halfMovesStack.Push(halfMoves);
            IPiece p = board[MovePackHelper.GetStartSquare(move)];

            if (MovePackHelper.HasCapture(move))
            {
                captured.Push(board[MovePackHelper.GetEndSquare(move)]);
                board[MovePackHelper.GetEndSquare(move)].Capture();
                ZKey           ^= ZKeyForCastling[(int)CastlingStatus];
                CastlingStatus ^= MovePackHelper.GetCastleMask(move);
                ZKey           ^= ZKeyForCastling[(int)CastlingStatus];
                halfMoves       = 0;
            }
            else
            {
                if (p.Type != PieceType.Pawn)
                {
                    halfMoves++;
                }
            }
            p.Move(move);
            if (MovePackHelper.HasCastling(move))
            {
                DoCastling(move);
            }
            PromotionTo to = MovePackHelper.GetPromotion(move);

            if (to != PromotionTo.None)
            {
                DoPromotion(p, move, to);
            }
            moveStack.Push(move);
            if (!epTouched)
            {
                PlaceEnpassant(Square.Invalid);
            }
            epTouched = false;
            if (ToMove == Side.Black)
            {
                movesCount++;
            }
            Flip();
            Debug.Assert(ZKey == CalcZKey());
        }
Beispiel #6
0
        static int GetCaptureCastlingMask(int captureSquare, CastlingAvail status)
        {
            switch (captureSquare)
            {
            case Square.a1:
                return(MovePackHelper.GetCastlingMerge((int)(status & CastlingAvail.QueenSideWhite)));

            case Square.h1:
                return(MovePackHelper.GetCastlingMerge((int)(status & CastlingAvail.KingSideWhite)));

            case Square.a8:
                return(MovePackHelper.GetCastlingMerge((int)(status & CastlingAvail.QueenSideBlack)));

            case Square.h8:
                return(MovePackHelper.GetCastlingMerge((int)(status & CastlingAvail.KingSideBlack)));
            }
            return(0);
        }
Beispiel #7
0
        public void Move(string p)
        {
            int[] probeMoves = new int[256];
            TestInCheck(ToMove);
            int count   = GetMoves(0, probeMoves);
            int decoded = MovePackHelper.DecodeAlgebraic(p);

            for (int i = 0; i < count; ++i)
            {
                if (MovePackHelper.GetCleanedMove(probeMoves[i]) == decoded)
                {
                    Move(probeMoves[i]);
                    return;
                }
            }

            throw new Exception("Invalid move:" + p);
        }
Beispiel #8
0
 private void DoCastling(int move)
 {
     // queen side black castling
     if (MovePackHelper.GetEndSquare(move) == Square.c8)
     {
         ZKey ^= BoardArray[Square.a8].ZKey;
         BoardArray[Square.d8] = BoardArray[Square.a8];
         BoardArray[Square.a8] = null;
         BoardArray[Square.d8].ForceSquare(Square.d8);
         ZKey ^= BoardArray[Square.d8].ZKey;
     }
     else
     // king side black castling
     if (MovePackHelper.GetEndSquare(move) == Square.g8)
     {
         ZKey ^= BoardArray[Square.h8].ZKey;
         BoardArray[Square.f8] = BoardArray[Square.h8];
         BoardArray[Square.h8] = null;
         BoardArray[Square.f8].ForceSquare(Square.f8);
         ZKey ^= BoardArray[Square.f8].ZKey;
     }
     else
     // king side white castling
     if (MovePackHelper.GetEndSquare(move) == Square.g1)
     {
         ZKey ^= BoardArray[Square.h1].ZKey;
         BoardArray[Square.f1] = BoardArray[Square.h1];
         BoardArray[Square.h1] = null;
         BoardArray[Square.f1].ForceSquare(Square.f1);
         ZKey ^= BoardArray[Square.f1].ZKey;
     }
     else
     // queen side white castling
     if (MovePackHelper.GetEndSquare(move) == Square.c1)
     {
         ZKey ^= BoardArray[Square.a1].ZKey;
         BoardArray[Square.d1] = BoardArray[Square.a1];
         BoardArray[Square.a1] = null;
         BoardArray[Square.d1].ForceSquare(Square.d1);
         ZKey ^= BoardArray[Square.d1].ZKey;
     }
 }
Beispiel #9
0
        private void UndoMove(int move)
        {
            IPiece p = board[MovePackHelper.GetEndSquare(move)];

            p.UnMove(move);
            if (MovePackHelper.HasCapture(move))
            {
                IPiece resumed = captured.Pop();
                resumed.UnCapture(MovePackHelper.GetEndSquare(move));
                board[resumed.HomeSquare] = resumed;
                ZKey           ^= ZKeyForCastling[(int)CastlingStatus];
                CastlingStatus ^= MovePackHelper.GetCastleMask(move);
                ZKey           ^= ZKeyForCastling[(int)CastlingStatus];
            }
            if (MovePackHelper.HasCastling(move))
            {
                UndoCastling(move);
            }
            PromotionTo to = MovePackHelper.GetPromotion(move);

            if (to != PromotionTo.None)
            {
                UndoPromotion(p, move, to);
            }
            if (!epTouched)
            {
                UnplaceEnPassant();
            }
            epTouched = false;
            if (ToMove == Side.White)
            {
                movesCount--;
            }
            halfMoves = halfMovesStack.Pop();
            Flip();
            Debug.Assert(ZKey == CalcZKey());
        }
Beispiel #10
0
        public static string ProduceShortAlgebraicString(IChessBoard board, int p)
        {
            board.TestInCheck(board.ToMove);
            int [] moves = new int[200];
            board.GetMoves(0, moves);
            List <int> possibles = new List <int>();
            int        end       = MovePackHelper.GetEndSquare(p);

            foreach (var m in moves)
            {
                if (GetEndSquare(m) == end && board.BoardArray[GetStartSquare(m)].Type == board.BoardArray[GetStartSquare(p)].Type)
                {
                    possibles.Add(p);
                }
            }
            StringBuilder sb     = new StringBuilder();
            var           spiece = board.BoardArray[GetStartSquare(p)];

            if (spiece.Type != PieceType.Pawn)
            {
                sb.Append(spiece.ToString().ToUpper());
            }
            if (possibles.Count > 1)
            {
                var squares = possibles.Select(k => GetSquareString(GetStartSquare(k)));
                if (squares.Select(k => k[0]).Distinct().Count() > 1)
                {
                    sb.Append(GetSquareString(GetStartSquare(p))[0]);
                }
                else if (squares.Select(k => k[1]).Distinct().Count() > 1)
                {
                    sb.Append(GetSquareString(GetStartSquare(p))[1]);
                }
                else
                {
                    sb.Append(GetSquareString(GetStartSquare(p)));
                }
            }
            if (HasCapture(p))
            {
                sb.Append("x");
            }
            sb.Append(GetSquareString(GetEndSquare(p)));
            board.Move(p);
            board.TestInCheck(board.ToMove);
            int cnt = board.GetCheckCount(board.ToMove);

            if (cnt > 0)
            {
                int[] tmp = new int[200];
                if (0 == board.GetMoves(0, tmp))
                {
                    sb.Append("#");
                }
                else
                {
                    sb.Append(new string('+', cnt));
                }
            }
            board.UndoMove();
            return(sb.ToString());
        }