Пример #1
0
        // book_key() returns the PolyGlot hash key of the given position
        private static UInt64 book_key(Position pos)
        {
            UInt64 key = 0;
            Bitboard b = pos.occupied_squares;

            while (b != 0)
            {
                // Piece offset is at 64 * polyPiece where polyPiece is defined as:
                // BP = 0, WP = 1, BN = 2, WN = 3, ... BK = 10, WK = 11
                Square s = Utils.pop_1st_bit(ref b);
                Piece p = pos.piece_on(s);
                int polyPiece = 2 * (Utils.type_of(p) - 1) + (Utils.color_of(p) == ColorC.WHITE ? 1 : 0);
                key ^= PolyGlotRandoms[ZobPieceOffset + (64 * polyPiece + s)];
            }

            b = (ulong)pos.can_castle_CR(CastleRightC.ALL_CASTLES);

            while (b != 0)
                key ^= PolyGlotRandoms[ZobCastleOffset + Utils.pop_1st_bit(ref b)];

            if (pos.st.epSquare != SquareC.SQ_NONE)
                key ^= PolyGlotRandoms[ZobEnPassantOffset + Utils.file_of(pos.st.epSquare)];

            if (pos.sideToMove == ColorC.WHITE)
                key ^= PolyGlotRandoms[ZobTurnOffset + 0];

            return key;
        }
Пример #2
0
        /// move_to_san() takes a position and a legal Move as input and returns its
        /// short algebraic notation representation.
        internal static string move_to_san(Position pos, int m)
        {
            if (m == MoveC.MOVE_NONE)
            {
                return "(none)";
            }

            if (m == MoveC.MOVE_NULL)
            {
                return "(null)";
            }

            Debug.Assert(pos.move_is_legal(m));

            Bitboard others, b;
            Color us = pos.sideToMove;
            var san = new StringBuilder();

            Square from = from_sq(m);
            Square to = to_sq(m);
            Piece pc = pos.piece_on(from);
            PieceType pt = type_of(pc);

            if (type_of_move(m) == MoveTypeC.CASTLING)
            {
                san.Append(to > from ? "O-O" : "O-O-O");
            }
            else
            {
                if (pt != PieceTypeC.PAWN)
                {
                    san.Append(PieceToChar[ColorC.WHITE][pt]); // Upper case

                    // Disambiguation if we have more then one piece of type 'pt' that can
                    // reach 'to' with a legal move.
                    others = b = (pos.attacks_from_PS(pc, to) & pos.pieces_PTC(pt, us)) ^ (ulong)from;
                    while (others != 0)
                    {
                        Move move = make_move(pop_lsb(ref b), to);
                        if (!pos.pl_move_is_legal(move, pos.pinned_pieces()))
                        {
                            others ^= (ulong)from_sq(move);
                        }
                    }

                    if (others != 0)
                    {
                        if ((others & file_bb_S(from)) == 0)
                        {
                            san.Append(file_to_char(file_of(from)));
                        }
                        else if ((others & rank_bb_S(from)) == 0)
                        {
                            san.Append(rank_to_char(rank_of(from)));
                        }
                        else
                        {
                            san.Append(square_to_string(from));
                        }
                    }
                }
                else if (pos.is_capture(m))
                {
                    san.Append(file_to_char(file_of(from)));
                }

                if (pos.is_capture(m))
                {
                    san.Append('x');
                }

                san.Append(square_to_string(to));

                if (type_of_move(m) == MoveTypeC.PROMOTION)
                {
                    san.Append('=');
                    san.Append(PieceToChar[ColorC.WHITE][promotion_type(m)]);
                }
            }

            var ci = CheckInfoBroker.GetObject();
            ci.CreateCheckInfo(pos);
            if (pos.move_gives_check(m, ci))
            {
                var st = new StateInfo();
                pos.do_move(m, st);
                var mlist = MListBroker.GetObject();
                mlist.pos = 0;
                Movegen.generate_legal(pos, mlist.moves, ref mlist.pos);
                san.Append(mlist.pos > 0 ? "+" : "#");
                MListBroker.Free();
                pos.undo_move(m);
            }
            CheckInfoBroker.Free();

            return san.ToString();
        }
Пример #3
0
        /// PawnTable::evaluate_pawns() evaluates each pawn of the given color
        internal static Score evaluate_pawns(Color Us, Position pos, Bitboard ourPawns, Bitboard theirPawns, PawnEntry e)
        {
            Color Them = (Us == ColorC.WHITE ? ColorC.BLACK : ColorC.WHITE);

            Bitboard b;
            Square s;
            File f;
            Rank r;
            bool passed, isolated, doubled, opposed, chain, backward, candidate;
            Score value = ScoreC.SCORE_ZERO;
            Square[] pl = pos.pieceList[Us][PieceTypeC.PAWN];
            int plPos = 0;

            // Loop through all pawns of the current color and score each pawn
            while ((s = pl[plPos++]) != SquareC.SQ_NONE)
            {
                Debug.Assert(pos.piece_on(s) == Utils.make_piece(Us, PieceTypeC.PAWN));

                f = (s & 7);
                r = (s >> 3);

                // This file cannot be half open
                if (Us == ColorC.WHITE)
                {
                    e.halfOpenFilesWHITE &= ~(1 << f);
                }
                else
                {
                    e.halfOpenFilesBLACK &= ~(1 << f);
                }

                // Our rank plus previous one. Used for chain detection
                b = Utils.RankBB[r] | Utils.RankBB[Us == ColorC.WHITE ? r - 1 : r + 1];

                // Flag the pawn as passed, isolated, doubled or member of a pawn
                // chain (but not the backward one).
                chain = (ourPawns & Utils.AdjacentFilesBB[f] & b) != 0;
                isolated = (ourPawns & Utils.AdjacentFilesBB[f]) == 0;
                doubled = (ourPawns & Utils.ForwardBB[Us][s]) != 0;
                opposed = (theirPawns & Utils.ForwardBB[Us][s]) != 0;
                passed = (theirPawns & Utils.PassedPawnMask[Us][s]) == 0;

                // Test for backward pawn
                backward = false;

                // If the pawn is passed, isolated, or member of a pawn chain it cannot
                // be backward. If there are friendly pawns behind on adjacent files
                // or if can capture an enemy pawn it cannot be backward either.
                if (!(passed | isolated | chain)
                    && (((ourPawns & Utils.AttackSpanMask[Them][s])) == 0)
                    && ((((Utils.StepAttacksBB[((Us << 3) | PieceTypeC.PAWN)][s]) & theirPawns)) == 0))
                {
                    // We now know that there are no friendly pawns beside or behind this
                    // pawn on adjacent files. We now check whether the pawn is
                    // backward by looking in the forward direction on the adjacent
                    // files, and seeing whether we meet a friendly or an enemy pawn first.
                    b = Utils.StepAttacksBB[((Us << 3) | PieceTypeC.PAWN)][s];

                    // Note that we are sure to find something because pawn is not passed
                    // nor isolated, so loop is potentially infinite, but it isn't.
                    while ((b & (ourPawns | theirPawns)) == 0)
                    {
                        if (Us == ColorC.WHITE) { b <<= 8; } else { b >>= 8; }
                    }

                    // The friendly pawn needs to be at least two ranks closer than the
                    // enemy pawn in order to help the potentially backward pawn advance.
                    backward = (((b | (Us == ColorC.WHITE ? b << 8 : b >> 8)) & theirPawns) != 0);
                }

                Debug.Assert(opposed | passed | (((Utils.AttackSpanMask[Us][s] & theirPawns)) != 0));

                // A not passed pawn is a candidate to become passed if it is free to
                // advance and if the number of friendly pawns beside or behind this
                // pawn on adjacent files is higher or equal than the number of
                // enemy pawns in the forward direction on the adjacent files.
                candidate = !(opposed | passed | backward | isolated)
                           && (b = Utils.AttackSpanMask[Them][s + (Us == ColorC.WHITE ? SquareC.DELTA_N : SquareC.DELTA_S)] & ourPawns) != 0
                           && Bitcount.popcount_1s_Max15(b) >= Bitcount.popcount_1s_Max15(Utils.AttackSpanMask[Us][s] & theirPawns);

                // Passed pawns will be properly scored in evaluation because we need
                // full attack info to evaluate passed pawns. Only the frontmost passed
                // pawn on each file is considered a true passed pawn.
                if (passed && !doubled)
                {
                    if (Us == ColorC.WHITE)
                    {
                        e.passedPawnsWHITE |= Utils.SquareBB[s];
                    }
                    else
                    {
                        e.passedPawnsBLACK |= Utils.SquareBB[s];
                    }
                }

                // Score this pawn
                if (isolated)
                    value -= IsolatedPawnPenalty[opposed ? 1 : 0][f];

                if (doubled)
                    value -= DoubledPawnPenalty[opposed ? 1 : 0][f];

                if (backward)
                    value -= BackwardPawnPenalty[opposed ? 1 : 0][f];

                if (chain)
                    value += ChainBonus[f];

                if (candidate)
                    value += CandidateBonus[((s >> 3) ^ (Us * 7))];
            }
            return value;
        }
Пример #4
0
        // connected_threat() tests whether it is safe to forward prune a move or if
        // is somehow connected to the threat move returned by null search.
        static bool connected_threat(Position pos, Move m, Move threat)
        {
            Debug.Assert(Utils.is_ok_M(m));
            Debug.Assert(Utils.is_ok_M(threat));
            Debug.Assert(!pos.is_capture_or_promotion(m));
            Debug.Assert(!pos.is_passed_pawn_push(m));

            Square mfrom, mto, tfrom, tto;

            mfrom = Utils.from_sq(m);
            mto = Utils.to_sq(m);
            tfrom = Utils.from_sq(threat);
            tto = Utils.to_sq(threat);

            // Case 1: Don't prune moves which move the threatened piece
            if (mfrom == tto)
                return true;

            // Case 2: If the threatened piece has value less than or equal to the
            // value of the threatening piece, don't prune moves which defend it.
            if (pos.is_capture(threat)
                && (Position.PieceValueMidgame[pos.piece_on(tfrom)] >= Position.PieceValueMidgame[pos.piece_on(tto)]
                    || Utils.type_of(pos.piece_on(tfrom)) == PieceTypeC.KING)
                && pos.move_attacks_square(m, tto))
                return true;

            // Case 3: If the moving piece in the threatened move is a slider, don't
            // prune safe moves which block its ray.
            if (piece_is_slider(pos.piece_on(tfrom))
                && (Utils.bit_is_set(Utils.between_bb(tfrom, tto), mto) != 0)
                && pos.see(m, true) >= 0)
                return true;

            return false;
        }
Пример #5
0
        // search<>() is the main search function for both PV and non-PV nodes and for
        // normal and SplitPoint nodes. When called just after a split point the search
        // is simpler because we have already probed the hash table, done a null move
        // search, and searched the first move before splitting, we don't have to repeat
        // all this work again. We also don't need to store anything to the hash table
        // here: This is taken care of after we return from the split point.
        internal static Value search(NodeType NT, Position pos, Stack[] ss, int ssPos, Value alpha, Value beta, Depth depth)
        {
            bool PvNode = (NT == NodeTypeC.PV || NT == NodeTypeC.Root || NT == NodeTypeC.SplitPointPV || NT == NodeTypeC.SplitPointRoot);
            bool SpNode = (NT == NodeTypeC.SplitPointPV || NT == NodeTypeC.SplitPointNonPV || NT == NodeTypeC.SplitPointRoot);
            bool RootNode = (NT == NodeTypeC.Root || NT == NodeTypeC.SplitPointRoot);

            Debug.Assert(alpha >= -ValueC.VALUE_INFINITE && alpha < beta && beta <= ValueC.VALUE_INFINITE);
            Debug.Assert((alpha == beta - 1) || PvNode);
            Debug.Assert(depth > DepthC.DEPTH_ZERO);

            MovesSearched ms = MovesSearchedBroker.GetObject();
            Move[] movesSearched = ms.movesSearched;

            StateInfo st = null;
            TTEntry tte = TT.StaticEntry;
            bool tteHasValue = false;
            UInt32 ttePos = 0;
            Key posKey = 0;
            Move ttMove, move, excludedMove, bestMove, threatMove;
            Depth ext, newDepth;
            Bound bt;
            Value bestValue, value, oldAlpha, ttValue;
            Value refinedValue, nullValue, futilityBase, futilityValue;
            bool isPvMove, inCheck, singularExtensionNode, givesCheck;
            bool captureOrPromotion, dangerous, doFullDepthSearch;
            int moveCount = 0, playedMoveCount = 0;
            Thread thisThread = pos.this_thread();
            SplitPoint sp = null;

            refinedValue = bestValue = value = -ValueC.VALUE_INFINITE;
            oldAlpha = alpha;
            inCheck = pos.in_check();
            ss[ssPos].ply = ss[ssPos - 1].ply + 1;

            // Used to send selDepth info to GUI
            if (PvNode && thisThread.maxPly < ss[ssPos].ply)
                thisThread.maxPly = ss[ssPos].ply;

            // Step 1. Initialize node
            if (SpNode)
            {
                ttMove = excludedMove = MoveC.MOVE_NONE;
                ttValue = ValueC.VALUE_ZERO;

                sp = ss[ssPos].sp;
                bestMove = sp.bestMove;
                threatMove = sp.threatMove;
                bestValue = sp.bestValue;
                moveCount = sp.moveCount; // Lock must be held here

                Debug.Assert(bestValue > -ValueC.VALUE_INFINITE && moveCount > 0);

                goto split_point_start;
            }
            else
            {
                ss[ssPos].currentMove = threatMove = ss[ssPos + 1].excludedMove = bestMove = MoveC.MOVE_NONE;
                ss[ssPos + 1].skipNullMove = 0; ss[ssPos + 1].reduction = DepthC.DEPTH_ZERO;
                ss[ssPos + 2].killers0 = ss[ssPos + 2].killers1 = MoveC.MOVE_NONE;
            }

            // Step 2. Check for aborted search and immediate draw
            // Enforce node limit here. FIXME: This only works with 1 search thread.
            if ((Limits.nodes != 0) && pos.nodes >= Limits.nodes)
                SignalsStop = true;

            if ((SignalsStop
                 || pos.is_draw(false)
                 || ss[ssPos].ply > Constants.MAX_PLY) && !RootNode)
            {
                MovesSearchedBroker.Free();
                return ValueC.VALUE_DRAW;
            }

            // Step 3. Mate distance pruning. Even if we mate at the next move our score
            // would be at best mate_in(ss[ssPos].ply+1), but if alpha is already bigger because
            // a shorter mate was found upward in the tree then there is no need to search
            // further, we will never beat current alpha. Same logic but with reversed signs
            // applies also in the opposite condition of being mated instead of giving mate,
            // in this case return a fail-high score.
            if (!RootNode)
            {
                alpha = Math.Max(Utils.mated_in(ss[ssPos].ply), alpha);
                beta = Math.Min(Utils.mate_in(ss[ssPos].ply + 1), beta);
                if (alpha >= beta)
                {
                    MovesSearchedBroker.Free();
                    return alpha;
                }
            }

            // Step 4. Transposition table lookup
            // We don't want the score of a partial search to overwrite a previous full search
            // TT value, so we use a different position key in case of an excluded move.
            excludedMove = ss[ssPos].excludedMove;
            posKey = (excludedMove != 0) ? pos.exclusion_key() : pos.key();
            tteHasValue = TT.probe(posKey, ref ttePos, out tte);
            ttMove = RootNode ? RootMoves[PVIdx].pv[0] : tteHasValue ? tte.move() : MoveC.MOVE_NONE;
            ttValue = tteHasValue ? value_from_tt(tte.value(), ss[ssPos].ply) : ValueC.VALUE_ZERO;

            // At PV nodes we check for exact scores, while at non-PV nodes we check for
            // a fail high/low. Biggest advantage at probing at PV nodes is to have a
            // smooth experience in analysis mode. We don't probe at Root nodes otherwise
            // we should also update RootMoveList to avoid bogus output.
            if (!RootNode && tteHasValue && (PvNode ? tte.depth() >= depth && tte.type() == Bound.BOUND_EXACT
                                            : can_return_tt(tte, depth, ttValue, beta)))
            {
                TT.entries[ttePos].set_generation(TT.generation);
                ss[ssPos].currentMove = ttMove; // Can be MOVE_NONE

                if (ttValue >= beta
                    && (ttMove != 0)
                    && !pos.is_capture_or_promotion(ttMove)
                    && ttMove != ss[ssPos].killers0)
                {
                    ss[ssPos].killers1 = ss[ssPos].killers0;
                    ss[ssPos].killers0 = ttMove;
                }

                MovesSearchedBroker.Free();
                return ttValue;
            }

            // Step 5. Evaluate the position statically and update parent's gain statistics
            if (inCheck)
                ss[ssPos].eval = ss[ssPos].evalMargin = ValueC.VALUE_NONE;
            else if (tteHasValue)
            {
                Debug.Assert(tte.static_value() != ValueC.VALUE_NONE);
                ss[ssPos].eval = tte.static_value();
                ss[ssPos].evalMargin = tte.static_value_margin();
                refinedValue = refine_eval(tte, ttValue, ss[ssPos].eval);
            }
            else
            {
                refinedValue = ss[ssPos].eval = Evaluate.do_evaluate(false, pos, ref ss[ssPos].evalMargin);
                TT.store(posKey, ValueC.VALUE_NONE, Bound.BOUND_NONE, DepthC.DEPTH_NONE, MoveC.MOVE_NONE, ss[ssPos].eval, ss[ssPos].evalMargin);
            }

            // Update gain for the parent non-capture move given the static position
            // evaluation before and after the move.
            if ((move = ss[ssPos - 1].currentMove) != MoveC.MOVE_NULL
                && ss[ssPos - 1].eval != ValueC.VALUE_NONE
                && ss[ssPos].eval != ValueC.VALUE_NONE
                && (pos.captured_piece_type() == 0)
                && !Utils.is_special(move))
            {
                Square to = Utils.to_sq(move);
                H.update_gain(pos.piece_on(to), to, -ss[ssPos - 1].eval - ss[ssPos].eval);
            }

            // Step 6. Razoring (is omitted in PV nodes)
            if (!PvNode && !inCheck
                && depth < RazorDepth
                && refinedValue + razor_margin(depth) < beta
                && ttMove == MoveC.MOVE_NONE
                && Math.Abs(beta) < ValueC.VALUE_MATE_IN_MAX_PLY
                && !pos.pawn_on_7th(pos.sideToMove))
            {
                Value rbeta = beta - razor_margin(depth);
                Value v = qsearch(NodeTypeC.NonPV, pos, ss, ssPos, rbeta - 1, rbeta, DepthC.DEPTH_ZERO);
                if (v < rbeta)
                {
                    // Logically we should return (v + razor_margin(depth)), but
                    // surprisingly this did slightly weaker in tests.
                    MovesSearchedBroker.Free();
                    return v;
                }
            }

            // Step 7. Static null move pruning (is omitted in PV nodes)
            // We're betting that the opponent doesn't have a move that will reduce
            // the score by more than futility_margin(depth) if we do a null move.
            if (!PvNode && !inCheck
                && (ss[ssPos].skipNullMove == 0)
                && depth < RazorDepth
                && Math.Abs(beta) < ValueC.VALUE_MATE_IN_MAX_PLY
                && refinedValue - futility_margin(depth, 0) >= beta
                && (pos.non_pawn_material(pos.sideToMove) != 0))
            {
                MovesSearchedBroker.Free();
                return refinedValue - futility_margin(depth, 0);
            }

            // Step 8. Null move search with verification search (is omitted in PV nodes)
            if (!PvNode && !inCheck
                && (ss[ssPos].skipNullMove == 0)
                && depth > DepthC.ONE_PLY
                && refinedValue >= beta
                && Math.Abs(beta) < ValueC.VALUE_MATE_IN_MAX_PLY
                && (pos.non_pawn_material(pos.sideToMove) != 0))
            {
                ss[ssPos].currentMove = MoveC.MOVE_NULL;

                // Null move dynamic reduction based on depth
                int R = 3 + (depth >= 5 * DepthC.ONE_PLY ? depth / 8 : 0);

                // Null move dynamic reduction based on value
                if (refinedValue - Constants.PawnValueMidgame > beta)
                    R++;

                if (st == null) { st = StateInfoBroker.GetObject(); }
                pos.do_null_move(true, st);
                ss[ssPos + 1].skipNullMove = 1;
                nullValue = depth - R * DepthC.ONE_PLY < DepthC.ONE_PLY ? -qsearch(NodeTypeC.NonPV, pos, ss, ssPos + 1, -beta, -alpha, DepthC.DEPTH_ZERO)
                                                      : -search(NodeTypeC.NonPV, pos, ss, ssPos + 1, -beta, -alpha, depth - R * DepthC.ONE_PLY);
                ss[ssPos + 1].skipNullMove = 0;
                pos.do_null_move(false, st);

                if (nullValue >= beta)
                {
                    // Do not return unproven mate scores
                    if (nullValue >= ValueC.VALUE_MATE_IN_MAX_PLY)
                        nullValue = beta;

                    if (depth < 6 * DepthC.ONE_PLY)
                    {
                        if (st != null) { st.previous = null; StateInfoBroker.Free(); }
                        MovesSearchedBroker.Free();
                        return nullValue;
                    }

                    // Do verification search at high depths
                    ss[ssPos].skipNullMove = 1;
                    Value v = search(NodeTypeC.NonPV, pos, ss, ssPos, alpha, beta, depth - R * DepthC.ONE_PLY);
                    ss[ssPos].skipNullMove = 0;

                    if (v >= beta)
                    {
                        if (st != null) { st.previous = null; StateInfoBroker.Free(); }
                        MovesSearchedBroker.Free();
                        return nullValue;
                    }
                }
                else
                {
                    // The null move failed low, which means that we may be faced with
                    // some kind of threat. If the previous move was reduced, check if
                    // the move that refuted the null move was somehow connected to the
                    // move which was reduced. If a connection is found, return a fail
                    // low score (which will cause the reduced move to fail high in the
                    // parent node, which will trigger a re-search with full depth).
                    threatMove = ss[ssPos + 1].currentMove;

                    if (depth < ThreatDepth
                        && (ss[ssPos - 1].reduction != 0)
                        && threatMove != MoveC.MOVE_NONE
                        && connected_moves(pos, ss[ssPos - 1].currentMove, threatMove))
                    {
                        if (st != null) { st.previous = null; StateInfoBroker.Free(); }
                        MovesSearchedBroker.Free();
                        return beta - 1;
                    }
                }
            }

            // Step 9. ProbCut (is omitted in PV nodes)
            // If we have a very good capture (i.e. SEE > seeValues[captured_piece_type])
            // and a reduced search returns a value much above beta, we can (almost) safely
            // prune the previous move.
            if (!PvNode && !inCheck
                && excludedMove == MoveC.MOVE_NONE
                && depth >= RazorDepth + DepthC.ONE_PLY
                && (ss[ssPos].skipNullMove == 0)
                && Math.Abs(beta) < ValueC.VALUE_MATE_IN_MAX_PLY)
            {
                Value rbeta = beta + 200;
                Depth rdepth = depth - DepthC.ONE_PLY - 3 * DepthC.ONE_PLY;

                Debug.Assert(rdepth >= DepthC.ONE_PLY);
                Debug.Assert(ss[ssPos - 1].currentMove != MoveC.MOVE_NONE);
                Debug.Assert(ss[ssPos - 1].currentMove != MoveC.MOVE_NULL);

                MovePicker mp2 = MovePickerBroker.GetObject();
                mp2.MovePickerC(pos, ttMove, H, pos.captured_piece_type());
                CheckInfo ci2 = CheckInfoBroker.GetObject();
                ci2.CreateCheckInfo(pos);

                while ((move = mp2.next_move()) != MoveC.MOVE_NONE)
                {
                    if (pos.pl_move_is_legal(move, ci2.pinned))
                    {
                        ss[ssPos].currentMove = move;
                        if (st == null) { st = StateInfoBroker.GetObject(); }
                        pos.do_move(move, st, ci2, pos.move_gives_check(move, ci2));
                        value = -search(NodeTypeC.NonPV, pos, ss, ssPos + 1, -rbeta, -rbeta + 1, rdepth);
                        pos.undo_move(move);
                        if (value >= rbeta)
                        {
                            if (st != null) { st.previous = null; StateInfoBroker.Free(); }
                            CheckInfoBroker.Free();
                            MovePickerBroker.Free(mp2);
                            MovesSearchedBroker.Free();
                            return value;
                        }
                    }
                }

                CheckInfoBroker.Free();
                MovePickerBroker.Free(mp2);
            }

            // Step 10. Internal iterative deepening
            if (ttMove == MoveC.MOVE_NONE
                && depth >= IIDDepth[PvNode ? 1 : 0]
                && (PvNode || (!inCheck && ss[ssPos].eval + IIDMargin >= beta)))
            {
                Depth d = (PvNode ? depth - 2 * DepthC.ONE_PLY : depth / 2);

                ss[ssPos].skipNullMove = 1;
                search(PvNode ? NodeTypeC.PV : NodeTypeC.NonPV, pos, ss, ssPos, alpha, beta, d);
                ss[ssPos].skipNullMove = 0;

                tteHasValue = TT.probe(posKey, ref ttePos, out tte);
                ttMove = (tteHasValue) ? tte.move() : MoveC.MOVE_NONE;
            }
            else
            {
                // Re-read (needed as TTEntry is a struct in the port)
                if ((tteHasValue) && (TT.entries[ttePos].key == tte.key)) { tte = TT.entries[ttePos]; }
            }

        split_point_start: // At split points actual search starts from here

            MovePicker mp = MovePickerBroker.GetObject();
            mp.MovePickerC(pos, ttMove, depth, H, ss[ssPos], PvNode ? -ValueC.VALUE_INFINITE : beta, SpNode ? ss[ssPos].sp.mp : null);
            CheckInfo ci = CheckInfoBroker.GetObject();
            ci.CreateCheckInfo(pos);
            futilityBase = ss[ssPos].eval + ss[ssPos].evalMargin;
            singularExtensionNode = !RootNode
                                   && !SpNode
                                   && depth >= SingularExtensionDepth[PvNode ? 1 : 0]
                                   && ttMove != MoveC.MOVE_NONE
                                   && (excludedMove == 0) // Recursive singular search is not allowed
                                   && ((tte.type() & Bound.BOUND_LOWER) != 0) // FIXME: uninitialized!
                                   && tte.depth() >= depth - 3 * DepthC.ONE_PLY;

            // Step 11. Loop through moves
            // Loop through all pseudo-legal moves until no moves remain or a beta cutoff occurs
            while (bestValue < beta
                   && (move = mp.next_move()) != MoveC.MOVE_NONE
                    && !thisThread.cutoff_occurred()
                    && !SignalsStop)
            {
                Debug.Assert(Utils.is_ok_M(move));

                if (move == excludedMove)
                    continue;

                // At root obey the "searchmoves" option and skip moves not listed in Root
                // Move List, as a consequence any illegal move is also skipped. In MultiPV
                // mode we also skip PV moves which have been already searched.

                // If we find none, it means !count
                if (RootNode && (find(RootMoves, PVIdx, RootMoves.Count, move) == -1))
                    continue;

                // At PV and SpNode nodes we want all moves to be legal since the beginning
                if ((PvNode || SpNode) && !pos.pl_move_is_legal(move, ci.pinned))
                    continue;

                if (SpNode)
                {
                    moveCount = ++sp.moveCount;
                    ThreadHelper.lock_release(sp.Lock);
                }
                else
                    moveCount++;

                if (RootNode)
                {
                    SignalsFirstRootMove = (moveCount == 1);

                    if (thisThread == Threads.main_thread() && SearchTime.ElapsedMilliseconds > 2000)
                    {
                        Plug.Write("info depth ");
                        Plug.Write((depth / DepthC.ONE_PLY).ToString());
                        Plug.Write(" currmove ");
                        Plug.Write(Utils.move_to_uci(move, Chess960));
                        Plug.Write(" nodes ");
                        Plug.Write(pos.nodes.ToString());
                        Plug.Write(" currmovenumber ");
                        Plug.Write((moveCount + PVIdx).ToString());
                        Plug.Write(Constants.endl);
                    }
                }

                isPvMove = (PvNode && moveCount <= 1);
                captureOrPromotion = pos.is_capture_or_promotion(move);
                givesCheck = pos.move_gives_check(move, ci);
                dangerous = givesCheck || is_dangerous(pos, move, captureOrPromotion);
                ext = DepthC.DEPTH_ZERO;

                // Step 12. Extend checks and, in PV nodes, also dangerous moves
                if (PvNode && dangerous)
                    ext = DepthC.ONE_PLY;

                else if (givesCheck && pos.see(move, true) >= 0)
                    ext = DepthC.ONE_PLY / 2;

                // Singular extension search. If all moves but one fail low on a search of
                // (alpha-s, beta-s), and just one fails high on (alpha, beta), then that move
                // is singular and should be extended. To verify this we do a reduced search
                // on all the other moves but the ttMove, if result is lower than ttValue minus
                // a margin then we extend ttMove.
                if (singularExtensionNode
                    && (ext == 0)
                    && move == ttMove
                    && pos.pl_move_is_legal(move, ci.pinned))
                {
                    if (Math.Abs(ttValue) < ValueC.VALUE_KNOWN_WIN)
                    {
                        Value rBeta = ttValue - (int)(depth);
                        ss[ssPos].excludedMove = move;
                        ss[ssPos].skipNullMove = 1;
                        value = search(NodeTypeC.NonPV, pos, ss, ssPos, rBeta - 1, rBeta, depth / 2);
                        ss[ssPos].skipNullMove = 0;
                        ss[ssPos].excludedMove = MoveC.MOVE_NONE;
                        if (value < rBeta)
                            ext = DepthC.ONE_PLY;
                    }
                }

                // Update current move (this must be done after singular extension search)
                newDepth = depth - DepthC.ONE_PLY + ext;

                // Step 13. Futility pruning (is omitted in PV nodes)
                if (!PvNode && !inCheck
                    && !captureOrPromotion
                    && !dangerous
                    && move != ttMove
                    && (bestValue > ValueC.VALUE_MATED_IN_MAX_PLY || bestValue == -ValueC.VALUE_INFINITE)
                    && !Utils.is_castle(move))
                {
                    // Move count based pruning
                    if (moveCount >= futility_move_count(depth)
                        && ((threatMove == 0) || !connected_threat(pos, move, threatMove)))
                    {
                        if (SpNode)
                            ThreadHelper.lock_grab(sp.Lock);

                        continue;
                    }

                    // Value based pruning
                    // We illogically ignore reduction condition depth >= 3*ONE_PLY for predicted depth,
                    // but fixing this made program slightly weaker.
                    Depth predictedDepth = newDepth - reduction(PvNode, depth, moveCount);
                    futilityValue = futilityBase + futility_margin(predictedDepth, moveCount)
                                                 + H.gain(pos.piece_moved(move), Utils.to_sq(move));

                    if (futilityValue < beta)
                    {
                        if (SpNode)
                            ThreadHelper.lock_grab(sp.Lock);

                        continue;
                    }

                    // Prune moves with negative SEE at low depths
                    if (predictedDepth < 2 * DepthC.ONE_PLY
                        && pos.see(move, true) < 0)
                    {
                        if (SpNode)
                            ThreadHelper.lock_grab(sp.Lock);

                        continue;
                    }
                }

                // Check for legality only before to do the move
                if (!pos.pl_move_is_legal(move, ci.pinned))
                {
                    moveCount--;
                    continue;
                }

                ss[ssPos].currentMove = move;
                if (!SpNode && !captureOrPromotion && playedMoveCount < 64)
                    movesSearched[playedMoveCount++] = move;

                // Step 14. Make the move
                if (st == null) { st = StateInfoBroker.GetObject(); }
                pos.do_move(move, st, ci, givesCheck);

                // Step 15. Reduced depth search (LMR). If the move fails high will be
                // re-searched at full depth.
                if (
                    !isPvMove
                    && !captureOrPromotion
                    && !dangerous
                    && ss[ssPos].killers0 != move
                    && ss[ssPos].killers1 != move
                    && depth > 3 * DepthC.ONE_PLY
                    && !Utils.is_castle(move))
                {
                    ss[ssPos].reduction = reduction(PvNode, depth, moveCount);
                    Depth d = Math.Max(newDepth - ss[ssPos].reduction, DepthC.ONE_PLY);
                    alpha = SpNode ? sp.alpha : alpha;

                    value = -search(NodeTypeC.NonPV, pos, ss, ssPos + 1, -(alpha + 1), -alpha, d);

                    doFullDepthSearch = (value > alpha && ss[ssPos].reduction != DepthC.DEPTH_ZERO);
                    ss[ssPos].reduction = DepthC.DEPTH_ZERO;
                }
                else
                    doFullDepthSearch = !isPvMove;

                // Step 16. Full depth search, when LMR is skipped or fails high
                if (doFullDepthSearch)
                {
                    alpha = SpNode ? sp.alpha : alpha;
                    value = newDepth < DepthC.ONE_PLY ? -qsearch(NodeTypeC.NonPV, pos, ss, ssPos + 1, -(alpha + 1), -alpha, DepthC.DEPTH_ZERO)
                                               : -search(NodeTypeC.NonPV, pos, ss, ssPos + 1, -(alpha + 1), -alpha, newDepth);
                }

                // Only for PV nodes do a full PV search on the first move or after a fail
                // high, in the latter case search only if value < beta, otherwise let the
                // parent node to fail low with value <= alpha and to try another move.
                if (PvNode && (isPvMove || (value > alpha && (RootNode || value < beta))))
                {
                    value = newDepth < DepthC.ONE_PLY ? -qsearch(NodeTypeC.PV, pos, ss, ssPos + 1, -beta, -alpha, DepthC.DEPTH_ZERO)
                                               : -search(NodeTypeC.PV, pos, ss, ssPos + 1, -beta, -alpha, newDepth);
                }

                // Step 17. Undo move
                pos.undo_move(move);

                Debug.Assert(value > -ValueC.VALUE_INFINITE && value < ValueC.VALUE_INFINITE);

                // Step 18. Check for new best move
                if (SpNode)
                {
                    ThreadHelper.lock_grab(sp.Lock);
                    bestValue = sp.bestValue;
                    alpha = sp.alpha;
                }

                // Finished searching the move. If Signals.stop is true, the search
                // was aborted because the user interrupted the search or because we
                // ran out of time. In this case, the return value of the search cannot
                // be trusted, and we don't update the best move and/or PV.
                if (RootNode && !SignalsStop)
                {
                    int rmPos = find(RootMoves, 0, RootMoves.Count, move);

                    // PV move or new best move ?
                    if (isPvMove || value > alpha)
                    {
                        RootMoves[rmPos].score = value;
                        RootMoves[rmPos].extract_pv_from_tt(pos);

                        // We record how often the best move has been changed in each
                        // iteration. This information is used for time management: When
                        // the best move changes frequently, we allocate some more time.
                        if (!isPvMove && MultiPV == 1)
                            BestMoveChanges++;
                    }
                    else
                        // All other moves but the PV are set to the lowest value, this
                        // is not a problem when sorting becuase sort is stable and move
                        // position in the list is preserved, just the PV is pushed up.
                        RootMoves[rmPos].score = -ValueC.VALUE_INFINITE;

                }

                if (value > bestValue)
                {
                    bestValue = value;
                    bestMove = move;

                    if (PvNode
                        && value > alpha
                        && value < beta) // We want always alpha < beta
                        alpha = value;

                    if (SpNode && !thisThread.cutoff_occurred())
                    {
                        sp.bestValue = value;
                        sp.bestMove = move;
                        sp.alpha = alpha;

                        if (value >= beta)
                            sp.cutoff = true;
                    }
                }

                // Step 19. Check for split
                if (!SpNode
                  && depth >= Threads.min_split_depth()
                  && bestValue < beta
                  && Threads.available_slave_exists(thisThread)
                  && !SignalsStop
                  && !thisThread.cutoff_occurred())
                {
                    bestValue = Threads.split(Constants.FakeSplit, pos, ss, ssPos, alpha, beta, bestValue, ref bestMove, depth, threatMove, moveCount, mp, NT);
                }
            }

            // Step 20. Check for mate and stalemate
            // All legal moves have been searched and if there are no legal moves, it
            // must be mate or stalemate. Note that we can have a false positive in
            // case of Signals.stop or thread.cutoff_occurred() are set, but this is
            // harmless because return value is discarded anyhow in the parent nodes.
            // If we are in a singular extension search then return a fail low score.
            if (moveCount == 0)
            {
                if (st != null) { st.previous = null; StateInfoBroker.Free(); }
                CheckInfoBroker.Free();
                MovePickerBroker.Free(mp);
                MovesSearchedBroker.Free();
                return (excludedMove != 0) ? oldAlpha : inCheck ? Utils.mated_in(ss[ssPos].ply) : ValueC.VALUE_DRAW;
            }

            // If we have pruned all the moves without searching return a fail-low score
            if (bestValue == -ValueC.VALUE_INFINITE)
            {
                Debug.Assert(playedMoveCount == 0);
                bestValue = oldAlpha;
            }

            // Step 21. Update tables
            // Update transposition table entry, killers and history
            if (!SpNode && !SignalsStop && !thisThread.cutoff_occurred())
            {
                move = bestValue <= oldAlpha ? MoveC.MOVE_NONE : bestMove;
                bt = bestValue <= oldAlpha ? Bound.BOUND_UPPER
                     : bestValue >= beta ? Bound.BOUND_LOWER : Bound.BOUND_EXACT;

                TT.store(posKey, value_to_tt(bestValue, ss[ssPos].ply), bt, depth, move, ss[ssPos].eval, ss[ssPos].evalMargin);

                // Update killers and history for non capture cut-off moves
                if (!inCheck
                    && bestValue >= beta
                    && !pos.is_capture_or_promotion(move)
                    )
                {
                    if (move != ss[ssPos].killers0)
                    {
                        ss[ssPos].killers1 = ss[ssPos].killers0;
                        ss[ssPos].killers0 = move;
                    }

                    // Increase history value of the cut-off move
                    Value bonus = (depth * depth);
                    H.add(pos.piece_moved(move), Utils.to_sq(move), bonus);

                    // Decrease history of all the other played non-capture moves
                    for (int i = 0; i < playedMoveCount - 1; i++)
                    {
                        Move m = movesSearched[i];
                        H.add(pos.piece_moved(m), Utils.to_sq(m), -bonus);
                    }
                }
            }

            Debug.Assert(bestValue > -ValueC.VALUE_INFINITE && bestValue < ValueC.VALUE_INFINITE);

            if (st != null) { st.previous = null; StateInfoBroker.Free(); }
            CheckInfoBroker.Free();
            MovePickerBroker.Free(mp);
            MovesSearchedBroker.Free();

            return bestValue;
        }
Пример #6
0
        // check_is_dangerous() tests if a checking move can be pruned in qsearch().
        // bestValue is updated only when returning false because in that case move
        // will be pruned.
        static bool check_is_dangerous(Position pos, Move move, Value futilityBase, Value beta)
        {
            Bitboard b, occ, oldAtt, newAtt, kingAtt;
            Square from, to, ksq;
            Piece pc;
            Color them;

            from = Utils.from_sq(move);
            to = Utils.to_sq(move);
            them = Utils.flip_C(pos.sideToMove);
            ksq = pos.king_square(them);
            kingAtt = Position.attacks_from_KING(ksq);
            pc = pos.piece_moved(move);

            occ = pos.occupied_squares ^ Utils.SquareBB[from] ^ Utils.SquareBB[ksq];
            oldAtt = Position.attacks_from(pc, from, occ);
            newAtt = Position.attacks_from(pc, to, occ);

            // Rule 1. Checks which give opponent's king at most one escape square are dangerous
            b = kingAtt & ~pos.pieces_C(them) & ~newAtt & ~(1UL << to);

            if ((b & (b - 1)) == 0) // Catches also !b
                return true;

            // Rule 2. Queen contact check is very dangerous
            if (Utils.type_of(pc) == PieceTypeC.QUEEN
                && (Utils.bit_is_set(kingAtt, to) != 0))
                return true;

            // Rule 3. Creating new double threats with checks
            b = pos.pieces_C(them) & newAtt & ~oldAtt & ~(1UL << ksq);

            while (b != 0)
            {
                // Note that here we generate illegal "double move"!
                if (futilityBase + Position.PieceValueEndgame[pos.piece_on(Utils.pop_1st_bit(ref b))] >= beta)
                    return true;
            }
            return false;
        }
Пример #7
0
        // connected_moves() tests whether two moves are 'connected' in the sense
        // that the first move somehow made the second move possible (for instance
        // if the moving piece is the same in both moves). The first move is assumed
        // to be the move that was made to reach the current position, while the
        // second move is assumed to be a move from the current position.
        internal static bool connected_moves(Position pos, Move m1, Move m2)
        {
            Square f1, t1, f2, t2;
            Piece p1, p2;
            Square ksq;

            Debug.Assert(Utils.is_ok_M(m1));
            Debug.Assert(Utils.is_ok_M(m2));

            // Case 1: The moving piece is the same in both moves
            f2 = Utils.from_sq(m2);
            t1 = Utils.to_sq(m1);
            if (f2 == t1)
                return true;

            // Case 2: The destination square for m2 was vacated by m1
            t2 = Utils.to_sq(m2);
            f1 = Utils.from_sq(m1);
            if (t2 == f1)
                return true;

            // Case 3: Moving through the vacated square
            p2 = pos.piece_on(f2);
            if (piece_is_slider(p2)
                && (Utils.bit_is_set(Utils.between_bb(f2, t2), f1) != 0))
                return true;

            // Case 4: The destination square for m2 is defended by the moving piece in m1
            p1 = pos.piece_on(t1);
            if ((Utils.bit_is_set(pos.attacks_from_PS(p1, t1), t2)) != 0)
                return true;

            // Case 5: Discovered check, checking piece is the piece moved in m1
            ksq = pos.king_square(pos.sideToMove);
            if (
                piece_is_slider(p1)
                &&
                (Utils.bit_is_set(Utils.between_bb(t1, ksq), f2) != 0)
                &&
                (Utils.bit_is_set(Position.attacks_from(p1, t1, Utils.xor_bit(pos.occupied_squares, f2)), ksq) != 0)
                )
                return true;

            return false;
        }
Пример #8
0
        internal static void generate_quiet_check(Position pos, MoveStack[] ms, ref int mpos)
        {
            /// generate<MV_NON_CAPTURE_CHECK> generates all pseudo-legal non-captures and knight
            /// underpromotions that give check. Returns a pointer to the end of the move list.
            Debug.Assert(!pos.in_check());

            var ci = CheckInfoBroker.GetObject();
            ci.CreateCheckInfo(pos);
            var target = ~pos.occupied_squares;
            var dc = ci.dcCandidates;

            while (dc != 0)
            {
                var from = Utils.pop_lsb(ref dc);
                var pt = Utils.type_of(pos.piece_on(from));

                if (pt == PieceTypeC.PAWN)
                {
                    continue; // Will be generated together with direct checks
                }

                var b = pos.attacks_from_PTS(pt, from) & ~pos.occupied_squares;

                if (pt == PieceTypeC.KING)
                {
                    b &= ~Utils.PseudoAttacks[PieceTypeC.QUEEN][ci.ksq];
                }

                while (b != 0)
                {
                    ms[mpos++].move = Utils.make_move(from, Utils.pop_lsb(ref b));
                }
            }

            generate_all(GenType.QUIET_CHECKS, pos, ms, ref mpos, pos.sideToMove, target, ci);

            CheckInfoBroker.Free();
        }
Пример #9
0
        // evaluate_pieces_of_color<>() assigns bonuses and penalties to all the
        // pieces of a given color.
        private static int evaluate_pieces_of_color(int Us, bool Trace, Position pos, EvalInfo ei, ref int mobility)
        {
            var Them = (Us == ColorC.WHITE ? ColorC.BLACK : ColorC.WHITE);
            mobility = ScoreC.SCORE_ZERO;

            // Do not include in mobility squares protected by enemy pawns or occupied by our pieces
            var mobilityArea = ~(ei.attackedBy[Them][PieceTypeC.PAWN] | pos.byColorBB[Us]);

            #region Evaluate pieces

            ulong between = 0;
            var plPos = 0;
            int s, ksq;
            int mob;
            int f;
            int score, scores = ScoreC.SCORE_ZERO;

            var attackedByThemKing = ei.attackedBy[Them][PieceTypeC.KING];
            var attackedByThemPawn = ei.attackedBy[Them][PieceTypeC.PAWN];
            var kingRingThem = ei.kingRing[Them];

            for (var Piece = PieceTypeC.KNIGHT; Piece < PieceTypeC.KING; Piece++)
            {
                score = ScoreC.SCORE_ZERO;
                ei.attackedBy[Us][Piece] = 0;
                var pl = pos.pieceList[Us][Piece];
                plPos = 0;
                while ((s = pl[plPos++]) != SquareC.SQ_NONE)
                {
                    // Find attacked squares, including x-ray attacks for bishops and rooks
                    if (Piece == PieceTypeC.KNIGHT)
                    {
                        between = Utils.StepAttacksBB_KNIGHT[s];
                    }
                    else if (Piece == PieceTypeC.QUEEN)
                    {
#if X64
                        b = Utils.BAttacks[s][(((pos.occupied_squares & Utils.BMasks[s]) * Utils.BMagics[s]) >> Utils.BShifts[s])] | Utils.RAttacks[s][(((pos.occupied_squares & Utils.RMasks[s]) * Utils.RMagics[s]) >> Utils.RShifts[s])];
#else
                        between = Utils.bishop_attacks_bb(s, pos.occupied_squares)
                            | Utils.rook_attacks_bb(s, pos.occupied_squares);
#endif
                    }
                    else if (Piece == PieceTypeC.BISHOP)
                    {
#if X64
                        b = Utils.BAttacks[s][(((
                              (pos.occupied_squares ^ (pos.byTypeBB[PieceTypeC.QUEEN] & pos.byColorBB[Us]))
                            & Utils.BMasks[s]) * Utils.BMagics[s]) >> Utils.BShifts[s])];
#else
                        between = Utils.bishop_attacks_bb(s, pos.occupied_squares ^ pos.pieces_PTC(PieceTypeC.QUEEN, Us));
#endif
                    }
                    else if (Piece == PieceTypeC.ROOK)
                    {
#if X64
                        b = Utils.RAttacks[s][(((
                              (pos.occupied_squares ^ ((pos.byTypeBB[PieceTypeC.ROOK] | pos.byTypeBB[PieceTypeC.QUEEN]) & pos.byColorBB[Us]))
                            & Utils.RMasks[s]) * Utils.RMagics[s]) >> Utils.RShifts[s])];
#else
                        between = Utils.rook_attacks_bb(
                            s,
                            pos.occupied_squares ^ pos.pieces(PieceTypeC.ROOK, PieceTypeC.QUEEN, Us));
#endif
                    }

                    // Update attack info
                    ei.attackedBy[Us][Piece] |= between;

                    // King attacks
                    if ((between & kingRingThem) != 0)
                    {
                        ei.kingAttackersCount[Us]++;
                        ei.kingAttackersWeight[Us] += KingAttackWeights[Piece];
                        var bb = (between & attackedByThemKing); //ei.attackedBy[Them][PieceTypeC.KING]);
                        if (bb != 0)
                        {
#if X64
                            bb -= (bb >> 1) & 0x5555555555555555UL;
                            bb = ((bb >> 2) & 0x3333333333333333UL) + (bb & 0x3333333333333333UL);
                            ei.kingAdjacentZoneAttacksCount[Us] += (int)((bb * 0x1111111111111111UL) >> 60);
#else
                            ei.kingAdjacentZoneAttacksCount[Us] += Bitcount.popcount_1s_Max15(bb);
#endif
                        }
                    }

                    // Mobility
#if X64
                    Bitboard bmob = b & mobilityArea;
                    if (Piece != PieceTypeC.QUEEN)
                    {
                        bmob -= (bmob >> 1) & 0x5555555555555555UL;
                        bmob = ((bmob >> 2) & 0x3333333333333333UL) + (bmob & 0x3333333333333333UL);
                        mob = (int)((bmob * 0x1111111111111111UL) >> 60);
                    }
                    else
                    {
                        bmob -= ((bmob >> 1) & 0x5555555555555555UL);
                        bmob = ((bmob >> 2) & 0x3333333333333333UL) + (bmob & 0x3333333333333333UL);
                        bmob = ((bmob >> 4) + bmob) & 0x0F0F0F0F0F0F0F0FUL;
                        mob = (int)((bmob * 0x0101010101010101UL) >> 56);
                    }
#else
                    mob = (Piece != PieceTypeC.QUEEN
                               ? Bitcount.popcount_1s_Max15(between & mobilityArea)
                               : Bitcount.popcount_1s_Full(between & mobilityArea));
#endif
                    mobility += MobilityBonus[Piece][mob];

                    // Decrease score if we are attacked by an enemy pawn. Remaining part
                    // of threat evaluation must be done later when we have full attack info.
                    if ((attackedByThemPawn & Utils.SquareBB[s]) != 0)
                    {
                        score -= ThreatenedByPawnPenalty[Piece];
                    }
                    else if ((Piece == PieceTypeC.BISHOP)
                        && ((Utils.PseudoAttacks[Piece][pos.pieceList[Them][PieceTypeC.KING][0]] & Utils.SquareBB[s])
                            != 0))
                    {
                        between = Utils.BetweenBB[s][pos.pieceList[Them][PieceTypeC.KING][0]] & pos.occupied_squares;

                        if (!Utils.more_than_one(between))
                        {
                            score += Utils.make_score(15, 25);
                        }
                    }

                    // Bishop and knight outposts squares
                    if ((Piece == PieceTypeC.BISHOP || Piece == PieceTypeC.KNIGHT)
                        && (((pos.byTypeBB[PieceTypeC.PAWN] & pos.byColorBB[Them]) & Utils.AttackSpanMask[Us][s]) == 0))
                    {
                        #region Evaluate outposts inlined

                        // evaluate_outposts() evaluates bishop and knight outposts squares

                        // Initial bonus based on square
                        var bonus = OutpostBonus[Piece == PieceTypeC.BISHOP ? 1 : 0][s ^ (Us * 56)];

                        // Increase bonus if supported by pawn, especially if the opponent has
                        // no minor piece which can exchange the outpost piece.
                        if ((bonus != 0) && ((ei.attackedBy[Us][PieceTypeC.PAWN] & Utils.SquareBB[s]) != 0))
                        {
                            if (((pos.byTypeBB[PieceTypeC.KNIGHT] & pos.byColorBB[Them]) == 0)
                                && (((((0xAA55AA55AA55AA55UL & Utils.SquareBB[s]) != 0)
                                          ? 0xAA55AA55AA55AA55UL
                                          : ~0xAA55AA55AA55AA55UL)
                                     & (pos.byTypeBB[PieceTypeC.BISHOP] & pos.byColorBB[Them])) == 0))
                            {
                                bonus += bonus + bonus / 2;
                            }
                            else
                            {
                                bonus += bonus / 2;
                            }
                        }
                        score += ((bonus << 16) + bonus); // Utils.make_score(bonus, bonus);

                        #endregion
                    }

                    if ((Piece == PieceTypeC.ROOK || Piece == PieceTypeC.QUEEN) && Utils.relative_rank_CS(Us, s) >= RankC.RANK_5)
                    {
                        // Major piece on 7th rank
                        if (Utils.relative_rank_CS(Us, s) == RankC.RANK_7
                            && Utils.relative_rank_CS(Us, pos.king_square(Them)) == RankC.RANK_8)
                            score += (Piece == PieceTypeC.ROOK ? RookOn7thBonus : QueenOn7thBonus);

                        // Major piece attacking pawns on the same rank
                        Bitboard pawns = pos.pieces_PTC(PieceTypeC.PAWN, Them) & Utils.rank_bb_S(s);
                        if (pawns != 0)
                        {
                            score += (Piece == PieceTypeC.ROOK ? RookOnPawnBonus : QueenOnPawnBonus) * Bitcount.popcount_1s_Max15(pawns);
                        }
                    }

                    // Special extra evaluation for bishops
                    if (pos.chess960 && (Piece == PieceTypeC.BISHOP))
                    {
                        // An important Chess960 pattern: A cornered bishop blocked by
                        // a friendly pawn diagonally in front of it is a very serious
                        // problem, especially when that pawn is also blocked.
                        if (s == Utils.relative_square(Us, SquareC.SQ_A1)
                            || s == Utils.relative_square(Us, SquareC.SQ_H1))
                        {
                            var d = Utils.pawn_push(Us)
                                    + (Utils.file_of(s) == FileC.FILE_A ? SquareC.DELTA_E : SquareC.DELTA_W);
                            if (pos.piece_on(s + d) == Utils.make_piece(Us, PieceTypeC.PAWN))
                            {
                                if (!pos.is_empty(s + d + Utils.pawn_push(Us)))
                                {
                                    score -= 2 * TrappedBishopA1H1Penalty;
                                }
                                else if (pos.piece_on(s + 2 * d) == Utils.make_piece(Us, PieceTypeC.PAWN))
                                {
                                    score -= TrappedBishopA1H1Penalty;
                                }
                                else
                                {
                                    score -= TrappedBishopA1H1Penalty / 2;
                                }
                            }
                        }
                    }

                    // Special extra evaluation for rooks
                    if (Piece == PieceTypeC.ROOK)
                    {
                        // Open and half-open files
                        f = (s & 7);

                        var halfOpenUs = ((Us == ColorC.WHITE)
                                              ? (ei.pi.halfOpenFilesWHITE & (1 << f))
                                              : (ei.pi.halfOpenFilesBLACK & (1 << f))) != 0;

                        if (halfOpenUs)
                        {
                            if (((Them == ColorC.WHITE)
                                     ? (ei.pi.halfOpenFilesWHITE & (1 << f))
                                     : (ei.pi.halfOpenFilesBLACK & (1 << f))) != 0)
                            {
                                score += RookOpenFileBonus;
                            }
                            else
                            {
                                score += RookHalfOpenFileBonus;
                            }
                        }

                        // Penalize rooks which are trapped inside a king. Penalize more if
                        // king has lost right to castle.
                        if (mob > 6 || halfOpenUs)
                        {
                            continue;
                        }

                        ksq = pos.pieceList[Us][PieceTypeC.KING][0];

                        if (((ksq >> 3) ^ (Us * 7)) == RankC.RANK_1 || (ksq >> 3) == (s >> 3))
                        {
                            if ((ksq & 7) >= FileC.FILE_E)
                            {
                                if (f > (ksq & 7))
                                {
                                    // Is there a half-open file between the king and the edge of the board?
                                    if (((Us == ColorC.WHITE)
                                             ? (ei.pi.halfOpenFilesWHITE & ~((1 << ((ksq & 7) + 1)) - 1))
                                             : (ei.pi.halfOpenFilesBLACK & ~((1 << ((ksq & 7) + 1)) - 1))) == 0)
                                    {
                                        score -= ((((pos.st.castleRights & (CastleRightC.WHITE_ANY << (Us << 1))) != 0)
                                                       ? (TrappedRookPenalty - mob * 16) / 2
                                                       : (TrappedRookPenalty - mob * 16)) << 16);
                                    }
                                }
                            }
                            else
                            {
                                if (f < (ksq & 7))
                                {
                                    // Is there a half-open file between the king and the edge of the board?
                                    if (((Us == ColorC.WHITE)
                                             ? (ei.pi.halfOpenFilesWHITE & ((1 << (ksq & 7)) - 1))
                                             : (ei.pi.halfOpenFilesBLACK & ((1 << (ksq & 7)) - 1))) == 0)
                                    {
                                        score -= ((((pos.st.castleRights & (CastleRightC.WHITE_ANY << (Us << 1))) != 0)
                                                       ? (TrappedRookPenalty - mob * 16) / 2
                                                       : (TrappedRookPenalty - mob * 16)) << 16);
                                    }
                                }
                            }
                        }
                    }
                }

                scores += score;

                if (Trace)
                {
                    TracedScores[Us][Piece] = score;
                }
            }

            #endregion

            // Sum up all attacked squares
            ei.attackedBy[Us][0] = ei.attackedBy[Us][PieceTypeC.PAWN] | ei.attackedBy[Us][PieceTypeC.KNIGHT]
                                   | ei.attackedBy[Us][PieceTypeC.BISHOP] | ei.attackedBy[Us][PieceTypeC.ROOK]
                                   | ei.attackedBy[Us][PieceTypeC.QUEEN] | ei.attackedBy[Us][PieceTypeC.KING];

            return scores;
        }
Пример #10
0
        // search<>() is the main search function for both PV and non-PV nodes and for
        // normal and SplitPoint nodes. When called just after a split point the search
        // is simpler because we have already probed the hash table, done a null move
        // search, and searched the first move before splitting, we don't have to repeat
        // all this work again. We also don't need to store anything to the hash table
        // here: This is taken care of after we return from the split point.
        internal static int search(int NT, Position pos, Stack[] ss, int ssPos, int alpha, int beta, int depth)
        {
            var PvNode = (NT == NodeTypeC.PV || NT == NodeTypeC.Root || NT == NodeTypeC.SplitPointPV
                          || NT == NodeTypeC.SplitPointRoot);
            var SpNode = (NT == NodeTypeC.SplitPointPV || NT == NodeTypeC.SplitPointNonPV
                          || NT == NodeTypeC.SplitPointRoot);
            var RootNode = (NT == NodeTypeC.Root || NT == NodeTypeC.SplitPointRoot);

            Debug.Assert(alpha >= -ValueC.VALUE_INFINITE && alpha < beta && beta <= ValueC.VALUE_INFINITE);
            Debug.Assert((PvNode || alpha == beta - 1));
            Debug.Assert(depth > DepthC.DEPTH_ZERO);

            var ms = MovesSearchedBroker.GetObject();
            var movesSearched = ms.movesSearched;

            StateInfo st = null;
            var tte = TT.StaticEntry;
            var tteHasValue = false;
            uint ttePos = 0;
            ulong posKey = 0;
            int ttMove, move, excludedMove, bestMove, threatMove;
            int ext, newDepth;
            int bestValue, value, ttValue;
            int eval = 0, nullValue, futilityValue;
            bool inCheck, givesCheck, pvMove, singularExtensionNode;
            bool captureOrPromotion, dangerous, doFullDepthSearch;
            int moveCount = 0, playedMoveCount = 0;
            SplitPoint sp = null;

            // Step 1. Initialize node
            var thisThread = pos.this_thread();
            //var threatExtension = false;
            inCheck = pos.in_check();
            
            if (SpNode)
            {
                sp = ss[ssPos].sp;
                bestMove = sp.bestMove;
                threatMove = sp.threatMove;
                bestValue = sp.bestValue;
                ttMove = excludedMove = MoveC.MOVE_NONE;
                ttValue = ValueC.VALUE_NONE;

                Debug.Assert(sp.bestValue > -ValueC.VALUE_INFINITE && sp.moveCount > 0);

                goto split_point_start;
            }

            bestValue = -ValueC.VALUE_INFINITE;
            ss[ssPos].currentMove = threatMove = ss[ssPos + 1].excludedMove = bestMove = MoveC.MOVE_NONE;
            ss[ssPos].ply = ss[ssPos - 1].ply + 1;
            ss[ssPos + 1].skipNullMove = 0;
            ss[ssPos + 1].reduction = DepthC.DEPTH_ZERO;
            ss[ssPos + 2].killers0 = ss[ssPos + 2].killers1 = MoveC.MOVE_NONE;

            // Used to send selDepth info to GUI
            if (PvNode && thisThread.maxPly < ss[ssPos].ply)
            {
                thisThread.maxPly = ss[ssPos].ply;
            }
            
            if (!RootNode)
            {
                // Step 2. Check for aborted search and immediate draw
                if ((SignalsStop || pos.is_draw(false) || ss[ssPos].ply > Constants.MAX_PLY))
                {
                    MovesSearchedBroker.Free();
                    return DrawValue[pos.sideToMove];
                }

                // Step 3. Mate distance pruning. Even if we mate at the next move our score
                // would be at best mate_in(ss->ply+1), but if alpha is already bigger because
                // a shorter mate was found upward in the tree then there is no need to search
                // further, we will never beat current alpha. Same logic but with reversed signs
                // applies also in the opposite condition of being mated instead of giving mate,
                // in this case return a fail-high score.
                alpha = Math.Max(Utils.mated_in(ss[ssPos].ply), alpha);
                beta = Math.Min(Utils.mate_in(ss[ssPos].ply + 1), beta);
                if (alpha >= beta)
                {
                    MovesSearchedBroker.Free();
                    return alpha;
                }
            }

            // Step 4. Transposition table lookup
            // We don't want the score of a partial search to overwrite a previous full search
            // TT value, so we use a different position key in case of an excluded move.
            excludedMove = ss[ssPos].excludedMove;
            posKey = (excludedMove != 0) ? pos.exclusion_key() : pos.key();
            tteHasValue = TT.probe(posKey, ref ttePos, out tte);
            ttMove = RootNode ? RootMoves[PVIdx].pv[0] : tteHasValue ? tte.move() : MoveC.MOVE_NONE;
            ttValue = tteHasValue ? value_from_tt(tte.value(), ss[ssPos].ply) : ValueC.VALUE_NONE;

            // At PV nodes we check for exact scores, while at non-PV nodes we check for
            // a fail high/low. Biggest advantage at probing at PV nodes is to have a
            // smooth experience in analysis mode. We don't probe at Root nodes otherwise
            // we should also update RootMoveList to avoid bogus output.
            if (!RootNode 
                && tteHasValue 
                && tte.depth() >= depth
                && ttValue != ValueC.VALUE_NONE // Only in case of TT access race
                && (PvNode ? tte.type() == Bound.BOUND_EXACT 
                            : ttValue >= beta ? ((tte.type() & Bound.BOUND_LOWER) != 0 )
                                              : ((tte.type() & Bound.BOUND_UPPER) != 0)))
            {
                Debug.Assert(ttValue != ValueC.VALUE_NONE); // Due to depth > DEPTH_NONE

                TT.table[ttePos].set_generation(TT.generation);
                ss[ssPos].currentMove = ttMove; // Can be MOVE_NONE

                if (ttValue >= beta && (ttMove != 0) && !pos.is_capture_or_promotion(ttMove)
                    && ttMove != ss[ssPos].killers0)
                {
                    ss[ssPos].killers1 = ss[ssPos].killers0;
                    ss[ssPos].killers0 = ttMove;
                }

                MovesSearchedBroker.Free();
                return ttValue;
            }

            // Step 5. Evaluate the position statically and update parentSplitPoint's gain statistics
            if (inCheck)
            {
                ss[ssPos].staticEval = ss[ssPos].evalMargin = eval = ValueC.VALUE_NONE;
            }
            else if (tteHasValue)
            {
                // Never assume anything on values stored in TT
                if ((ss[ssPos].staticEval = eval = tte.eval_value()) == ValueC.VALUE_NONE
                    || (ss[ssPos].evalMargin = tte.eval_margin()) == ValueC.VALUE_NONE)
                {
                    eval = ss[ssPos].staticEval = Evaluate.do_evaluate(false, pos, ref ss[ssPos].evalMargin);
                }

                // Can ttValue be used as a better position evaluation?
                if (ttValue != ValueC.VALUE_NONE)
                {
                    if ((((tte.type() & Bound.BOUND_LOWER) != 0) && ttValue > eval)
                        || (((tte.type() & Bound.BOUND_UPPER) != 0) && ttValue < eval))
                    {
                        eval = ttValue;
                    }
                }
            }
            else
            {
                eval = ss[ssPos].staticEval = Evaluate.do_evaluate(false, pos, ref ss[ssPos].evalMargin);
                TT.store(
                    posKey,
                    ValueC.VALUE_NONE,
                    Bound.BOUND_NONE,
                    DepthC.DEPTH_NONE,
                    MoveC.MOVE_NONE,
                    ss[ssPos].staticEval,
                    ss[ssPos].evalMargin);
            }

            // Update gain for the parentSplitPoint non-capture move given the static position
            // evaluation before and after the move.
            if ((move = ss[ssPos - 1].currentMove) != MoveC.MOVE_NULL && ss[ssPos - 1].staticEval != ValueC.VALUE_NONE
                && ss[ssPos].staticEval != ValueC.VALUE_NONE && (pos.captured_piece_type() == 0) && Utils.type_of_move(move) == MoveTypeC.NORMAL)
            {
                var to = Utils.to_sq(move);
                H.update_gain(pos.piece_on(to), to, -ss[ssPos - 1].staticEval - ss[ssPos].staticEval);
            }

            // Step 6. Razoring (is omitted in PV nodes)
            if (!PvNode && !inCheck && depth < 4 * DepthC.ONE_PLY && eval + razor_margin(depth) < beta
                && ttMove == MoveC.MOVE_NONE && Math.Abs(beta) < ValueC.VALUE_MATE_IN_MAX_PLY
                && !pos.pawn_on_7th(pos.sideToMove))
            {
                var rbeta = beta - razor_margin(depth);
                var v = qsearch(NodeTypeC.NonPV, false, pos, ss, ssPos, rbeta - 1, rbeta, DepthC.DEPTH_ZERO);
                if (v < rbeta)
                {
                    // Logically we should return (v + razor_margin(depth)), but
                    // surprisingly this did slightly weaker in tests.
                    MovesSearchedBroker.Free();
                    return v;
                }
            }

            // Step 7. Static null move pruning (is omitted in PV nodes)
            // We're betting that the opponent doesn't have a move that will reduce
            // the score by more than futility_margin(depth) if we do a null move.
            if (!PvNode && !inCheck && (ss[ssPos].skipNullMove == 0) && depth < 4 * DepthC.ONE_PLY
                && Math.Abs(beta) < ValueC.VALUE_MATE_IN_MAX_PLY && eval - FutilityMargins[depth][0] >= beta
                && (pos.non_pawn_material(pos.sideToMove) != 0))
            {
                MovesSearchedBroker.Free();
                return eval - FutilityMargins[depth][0];
            }

            // Step 8. Null move search with verification search (is omitted in PV nodes)
            if (!PvNode && !inCheck && (ss[ssPos].skipNullMove == 0) && depth > DepthC.ONE_PLY && eval >= beta
                && Math.Abs(beta) < ValueC.VALUE_MATE_IN_MAX_PLY && (pos.non_pawn_material(pos.sideToMove) != 0))
            {
                ss[ssPos].currentMove = MoveC.MOVE_NULL;

                // Null move dynamic reduction based on depth
                Depth R = 3 * DepthC.ONE_PLY + depth / 4;

                // Null move dynamic reduction based on value
                if (eval - Constants.PawnValueMidgame > beta)
                {
                    R += DepthC.ONE_PLY;
                }

                if (st == null)
                {
                    st = StateInfoBroker.GetObject();
                }
                pos.do_null_move(st);
                ss[ssPos + 1].skipNullMove = 1;
                
                nullValue = depth - R < DepthC.ONE_PLY ? -qsearch(NodeTypeC.NonPV, false, pos, ss, ssPos + 1, -beta, -alpha, DepthC.DEPTH_ZERO)
                                      : -search(NodeTypeC.NonPV, pos, ss, ssPos + 1, -beta, -alpha, depth - R);

                ss[ssPos + 1].skipNullMove = 0;
                pos.undo_null_move(st);

                if (nullValue >= beta)
                {
                    // Do not return unproven mate scores
                    if (nullValue >= ValueC.VALUE_MATE_IN_MAX_PLY)
                    {
                        nullValue = beta;
                    }

                    if (depth < 6 * DepthC.ONE_PLY)
                    {
                        if (st != null)
                        {
                            st.previous = null;
                            StateInfoBroker.Free();
                        }
                        MovesSearchedBroker.Free();
                        return nullValue;
                    }

                    // Do verification search at high depths
                    ss[ssPos].skipNullMove = 1;
                    var v = search(NodeTypeC.NonPV, pos, ss, ssPos, alpha, beta, depth - R);
                    ss[ssPos].skipNullMove = 0;

                    if (v >= beta)
                    {
                        if (st != null)
                        {
                            st.previous = null;
                            StateInfoBroker.Free();
                        }
                        MovesSearchedBroker.Free();
                        return nullValue;
                    }
                }
                else
                {
                    // The null move failed low, which means that we may be faced with
                    // some kind of threat. If the previous move was reduced, check if
                    // the move that refuted the null move was somehow connected to the
                    // the move that refuted the null move was somehow connected to the
                    // move which was reduced. If a connection is found extend moves that
                    // defend against threat.
                    threatMove = ss[ssPos + 1].currentMove;

                    if (depth < 5 * DepthC.ONE_PLY && (ss[ssPos - 1].reduction != 0) && threatMove != MoveC.MOVE_NONE
                        && allows(pos, ss[ssPos - 1].currentMove, threatMove))
                    {
                        //threatExtension = true;
                        
                        if (st != null)
                        {
                            st.previous = null;
                            StateInfoBroker.Free();
                        }
                        MovesSearchedBroker.Free();
                        return beta - 1;
                    }
                }
            }

            // Step 9. ProbCut (is omitted in PV nodes)
            // If we have a very good capture (i.e. SEE > seeValues[captured_piece_type])
            // and a reduced search returns a value much above beta, we can (almost) safely
            // prune the previous move.
            if (!PvNode && !inCheck && excludedMove == MoveC.MOVE_NONE && depth >= 4 * DepthC.ONE_PLY + DepthC.ONE_PLY
                && (ss[ssPos].skipNullMove == 0) && Math.Abs(beta) < ValueC.VALUE_MATE_IN_MAX_PLY)
            {
                var rbeta = beta + 200;
                var rdepth = depth - DepthC.ONE_PLY - 3 * DepthC.ONE_PLY;

                Debug.Assert(rdepth >= DepthC.ONE_PLY);
                Debug.Assert(ss[ssPos - 1].currentMove != MoveC.MOVE_NONE);
                Debug.Assert(ss[ssPos - 1].currentMove != MoveC.MOVE_NULL);

                var mp2 = MovePickerBroker.GetObject();
                mp2.MovePickerC(pos, ttMove, H, pos.captured_piece_type());
                var ci2 = CheckInfoBroker.GetObject();
                ci2.CreateCheckInfo(pos);

                while ((move = mp2.next_move()) != MoveC.MOVE_NONE)
                {
                    if (pos.pl_move_is_legal(move, ci2.pinned))
                    {
                        ss[ssPos].currentMove = move;
                        if (st == null)
                        {
                            st = StateInfoBroker.GetObject();
                        }
                        pos.do_move(move, st, ci2, pos.move_gives_check(move, ci2));
                        value = -search(NodeTypeC.NonPV, pos, ss, ssPos + 1, -rbeta, -rbeta + 1, rdepth);
                        pos.undo_move(move);
                        if (value >= rbeta)
                        {
                            if (st != null)
                            {
                                st.previous = null;
                                StateInfoBroker.Free();
                            }
                            CheckInfoBroker.Free();
                            MovePickerBroker.Free(mp2);
                            MovesSearchedBroker.Free();
                            return value;
                        }
                    }
                }

                CheckInfoBroker.Free();
                MovePickerBroker.Free(mp2);
            }

            // Step 10. Internal iterative deepening
            if (ttMove == MoveC.MOVE_NONE && depth >= (PvNode ? 5 * DepthC.ONE_PLY : 8 * DepthC.ONE_PLY)
                && (PvNode || (!inCheck && ss[ssPos].staticEval + 256 >= beta)))
            {
                var d = (PvNode ? depth - 2 * DepthC.ONE_PLY : depth / 2);

                ss[ssPos].skipNullMove = 1;
                search(PvNode ? NodeTypeC.PV : NodeTypeC.NonPV, pos, ss, ssPos, alpha, beta, d);
                ss[ssPos].skipNullMove = 0;

                tteHasValue = TT.probe(posKey, ref ttePos, out tte);
                ttMove = (tteHasValue) ? tte.move() : MoveC.MOVE_NONE;
            }
            else
            {
                // Re-read (needed as TTEntry is a struct in the port)
                if ((tteHasValue) && (TT.table[ttePos].key == tte.key))
                {
                    tte = TT.table[ttePos];
                }
            }

            split_point_start: // At split points actual search starts from here

            var mp = MovePickerBroker.GetObject();
            mp.MovePickerC(
                pos,
                ttMove,
                depth,
                H,
                ss[ssPos],
                PvNode ? -ValueC.VALUE_INFINITE : beta,
                SpNode ? ss[ssPos].sp.movePicker : null);
            var ci = CheckInfoBroker.GetObject();
            ci.CreateCheckInfo(pos);
            
            value = bestValue; // Workaround a bogus 'uninitialized' warning under gcc
            singularExtensionNode = !RootNode && !SpNode && depth >= (PvNode ? 6 * DepthC.ONE_PLY : 8 * DepthC.ONE_PLY)
                                    && ttMove != MoveC.MOVE_NONE && (excludedMove == 0)
                                    // Recursive singular search is not allowed
                                    && ((tte.type() & Bound.BOUND_LOWER) != 0) // FIXME: uninitialized!
                                    && tte.depth() >= depth - 3 * DepthC.ONE_PLY;

            // Step 11. Loop through moves
            // Loop through all pseudo-legal moves until no moves remain or a beta cutoff occurs
            while ((move = mp.next_move()) != MoveC.MOVE_NONE && !thisThread.cutoff_occurred()
                   && !SignalsStop)
            {
                Debug.Assert(Utils.is_ok_M(move));

                if (move == excludedMove)
                {
                    continue;
                }

                // At root obey the "searchmoves" option and skip moves not listed in Root
                // Move List, as a consequence any illegal move is also skipped. In MultiPV
                // mode we also skip PV moves which have been already searched.

                // If we find none, it means !count
                if (RootNode && (find(RootMoves, PVIdx, RootMoves.Count, move) == -1))
                {
                    continue;
                }

                if (SpNode)
                {
                    // Shared counter cannot be decremented later if move turns out to be illegal
                    if (!pos.pl_move_is_legal(move, ci.pinned))
                    {
                        continue;
                    }

                    moveCount = ++sp.moveCount;
                    ThreadHelper.lock_release(sp.Lock);
                }
                else
                {
                    moveCount++;
                }

                if (RootNode)
                {
                    SignalsFirstRootMove = (moveCount == 1);

                    if (thisThread == Threads.main_thread() && SearchTime.ElapsedMilliseconds > 3000)
                    {
                        Plug.Write("info depth ");
                        Plug.Write((depth / DepthC.ONE_PLY).ToString());
                        Plug.Write(" currmove ");
                        Plug.Write(Utils.move_to_uci(move, pos.chess960));
                        Plug.Write(" nodes ");
                        Plug.Write(pos.nodes.ToString());
                        Plug.Write(" currmovenumber ");
                        Plug.Write((moveCount + PVIdx).ToString());
                        Plug.Write(Constants.endl);
                    }
                }

                ext = DepthC.DEPTH_ZERO;
                captureOrPromotion = pos.is_capture_or_promotion(move);
                givesCheck = pos.move_gives_check(move, ci);
                dangerous = givesCheck 
                            || pos.is_passed_pawn_push(move)
                            || Utils.type_of_move(move) == MoveTypeC.CASTLING
                            || (captureOrPromotion // Entering a pawn endgame?
                                    && Utils.type_of(pos.piece_on(Utils.to_sq(move))) != PieceTypeC.PAWN
                                    && Utils.type_of_move(move) == MoveTypeC.NORMAL
                                    && (pos.non_pawn_material(ColorC.WHITE) + pos.non_pawn_material(ColorC.BLACK) 
                                        - Position.PieceValue[PhaseC.MG][pos.piece_on(Utils.to_sq(move))] == ValueC.VALUE_ZERO));

                // Step 12. Extend checks and, in PV nodes, also dangerous moves
                if (PvNode && dangerous)
                {
                    ext = DepthC.ONE_PLY;
                }
                // else if (threatExtension && refutes(pos, move, threatMove))
                // {
                // ext = DepthC.ONE_PLY;
                // }
                else if (givesCheck && pos.see(move, true) >= 0)
                {
                    ext = DepthC.ONE_PLY / 2;
                }

                // Singular extension search. If all moves but one fail low on a search of
                // (alpha-s, beta-s), and just one fails high on (alpha, beta), then that move
                // is singular and should be extended. To verify this we do a reduced search
                // on all the other moves but the ttMove, if result is lower than ttValue minus
                // a margin then we extend ttreMove.
                if (singularExtensionNode && move == ttMove && (ext == 0) && pos.pl_move_is_legal(move, ci.pinned))
                {
                    Debug.Assert(ttValue != ValueC.VALUE_NONE);

                    var rBeta = ttValue - depth;
                    ss[ssPos].excludedMove = move;
                    ss[ssPos].skipNullMove = 1;
                    value = search(NodeTypeC.NonPV, pos, ss, ssPos, rBeta - 1, rBeta, depth / 2);
                    ss[ssPos].skipNullMove = 0;
                    ss[ssPos].excludedMove = MoveC.MOVE_NONE;
                    if (value < rBeta)
                    {
                        ext = DepthC.ONE_PLY;
                    }
                }

                // Update current move (this must be done after singular extension search)
                newDepth = depth - DepthC.ONE_PLY + ext;

                // Step 13. Futility pruning (is omitted in PV nodes)
                if (!PvNode
                    && !captureOrPromotion
                    && !inCheck 
                    && !dangerous 
                    && move != ttMove
                    && (bestValue > ValueC.VALUE_MATED_IN_MAX_PLY || (bestValue == -ValueC.VALUE_INFINITE && alpha > ValueC.VALUE_MATED_IN_MAX_PLY)))
                {
                    // Move count based pruning
                    if (depth < 16 * DepthC.ONE_PLY
                        && moveCount >= FutilityMoveCounts[depth]
                        && ((threatMove == 0) || !refutes(pos, move, threatMove)))
                    {
                        if (SpNode)
                        {
                            ThreadHelper.lock_grab(sp.Lock);
                        }

                        continue;
                    }

                    // Value based pruning
                    // We illogically ignore reduction condition depth >= 3*ONE_PLY for predicted depth,
                    // but fixing this made program slightly weaker.
                    var predictedDepth = newDepth - reduction(PvNode, depth, moveCount);
                    futilityValue = ss[ssPos].staticEval + ss[ssPos].evalMargin + futility_margin(predictedDepth, moveCount)
                                    + H.gain(pos.piece_moved(move), Utils.to_sq(move));

                    if (futilityValue < beta)
                    {
                        if (SpNode)
                        {
                            ThreadHelper.lock_grab(sp.Lock);
                        }

                        continue;
                    }

                    // Prune moves with negative SEE at low depths
                    if (predictedDepth < 2 * DepthC.ONE_PLY && pos.see(move, true) < 0)
                    {
                        if (SpNode)
                        {
                            ThreadHelper.lock_grab(sp.Lock);
                        }

                        continue;
                    }
                }

                // Check for legality only before to do the move
                if (!RootNode && !SpNode && !pos.pl_move_is_legal(move, ci.pinned))
                {
                    moveCount--;
                    continue;
                }

                pvMove = (PvNode && moveCount == 1);
                ss[ssPos].currentMove = move;
                if (!SpNode && !captureOrPromotion && playedMoveCount < 64)
                {
                    movesSearched[playedMoveCount++] = move;
                }

                // Step 14. Make the move
                if (st == null)
                {
                    st = StateInfoBroker.GetObject();
                }
                pos.do_move(move, st, ci, givesCheck);

                // Step 15. Reduced depth search (LMR). If the move fails high will be
                // re-searched at full depth.
                if (depth > 3 * DepthC.ONE_PLY
                    && !pvMove 
                    && !captureOrPromotion 
                    && !dangerous
                    && move != ttMove
                    && move != ss[ssPos].killers0
                    && move != ss[ssPos].killers1)
                {
                    ss[ssPos].reduction = reduction(PvNode, depth, moveCount);
                    var d = Math.Max(newDepth - ss[ssPos].reduction, DepthC.ONE_PLY);
                    alpha = SpNode ? sp.alpha : alpha;

                    value = -search(NodeTypeC.NonPV, pos, ss, ssPos + 1, -(alpha + 1), -alpha, d);

                    doFullDepthSearch = (value > alpha && ss[ssPos].reduction != DepthC.DEPTH_ZERO);
                    ss[ssPos].reduction = DepthC.DEPTH_ZERO;
                }
                else
                {
                    doFullDepthSearch = !pvMove;
                }

                // Step 16. Full depth search, when LMR is skipped or fails high
                if (doFullDepthSearch)
                {
                    alpha = SpNode ? sp.alpha : alpha;
                    value = newDepth < DepthC.ONE_PLY 
                            ? -qsearch(NodeTypeC.NonPV, givesCheck, pos, ss, ssPos + 1, -(alpha + 1), -alpha, DepthC.DEPTH_ZERO)
                            : -search(NodeTypeC.NonPV, pos, ss, ssPos + 1, -(alpha + 1), -alpha, newDepth);
                }

                // Only for PV nodes do a full PV search on the first move or after a fail
                // high, in the latter case search only if value < beta, otherwise let the
                // parentSplitPoint node to fail low with value <= alpha and to try another move.
                if (PvNode && (pvMove || (value > alpha && (RootNode || value < beta))))
                {
                    value = newDepth < DepthC.ONE_PLY
                                ? -qsearch(NodeTypeC.PV, givesCheck, pos, ss, ssPos + 1, -beta, -alpha, DepthC.DEPTH_ZERO)
                                : -search(NodeTypeC.PV, pos, ss, ssPos + 1, -beta, -alpha, newDepth);
                }

                // Step 17. Undo move
                pos.undo_move(move);

                Debug.Assert(value > -ValueC.VALUE_INFINITE && value < ValueC.VALUE_INFINITE);

                // Step 18. Check for new best move
                if (SpNode)
                {
                    ThreadHelper.lock_grab(sp.Lock);
                    bestValue = sp.bestValue;
                    alpha = sp.alpha;
                }

                // Finished searching the move. If Signals.stop is true, the search
                // was aborted because the user interrupted the search or because we
                // ran out of time. In this case, the return value of the search cannot
                // be trusted, and we don't update the best move and/or PV.
                if (SignalsStop || thisThread.cutoff_occurred())
                {
                    if (st != null)
                    {
                        st.previous = null;
                        StateInfoBroker.Free();
                    }
                    CheckInfoBroker.Free();
                    MovePickerBroker.Free(mp);
                    MovesSearchedBroker.Free();

                    return value; // To avoid returning VALUE_INFINITE
                }

                // Finished searching the move. If Signals.stop is true, the search
                // was aborted because the user interrupted the search or because we
                // ran out of time. In this case, the return value of the search cannot
                // be trusted, and we don't update the best move and/or PV.
                if (RootNode)
                {
                    var rmPos = find(RootMoves, 0, RootMoves.Count, move);

                    // PV move or new best move ?
                    if (pvMove || value > alpha)
                    {
                        RootMoves[rmPos].score = value;
                        RootMoves[rmPos].extract_pv_from_tt(pos);

                        // We record how often the best move has been changed in each
                        // iteration. This information is used for time management: When
                        // the best move changes frequently, we allocate some more time.
                        if (!pvMove)
                        {
                            BestMoveChanges++;
                        }
                    }
                    else
                    {
                        // All other moves but the PV are set to the lowest value, this
                        // is not a problem when sorting becuase sort is stable and move
                        // position in the list is preserved, just the PV is pushed up.
                        RootMoves[rmPos].score = -ValueC.VALUE_INFINITE;
                    }
                }

                if (value > bestValue)
                {
                    bestValue = value;
                    if (SpNode) sp.bestValue = value;

                    if (value > alpha)
                    {
                        bestMove = move;
                        if (SpNode) sp.bestMove = move;

                        if (PvNode && value < beta)
                        {
                            alpha = value; // Update alpha here! Always alpha < beta
                            if (SpNode) sp.alpha = value;
                        }
                        else 
                        {
                            Debug.Assert(value >= beta); // Fail high

                            if (SpNode) sp.cutoff = true;
                            break;
                        }
                    }
                }

                // Step 19. Check for split
                if (!SpNode 
                    && depth >= Threads.minimumSplitDepth 
                    && Threads.available_slave(thisThread) != null
                    && thisThread.splitPointsSize < Constants.MAX_SPLITPOINTS_PER_THREAD)
                {
                    Debug.Assert(bestValue < beta);

                    Threads.split(
                        Constants.FakeSplit,
                        pos,
                        ss,
                        ssPos,
                        alpha,
                        beta,
                        ref bestValue,
                        ref bestMove,
                        depth,
                        threatMove,
                        moveCount,
                        mp,
                        NT);

                    if (bestValue >= beta)
                    {
                        break;
                    }
                }
            }

            // Step 20. Check for mate and stalemate
            // All legal moves have been searched and if there are no legal moves, it
            // must be mate or stalemate. Note that we can have a false positive in
            // case of Signals.stop or thread.cutoff_occurred() are set, but this is
            // harmless because return value is discarded anyhow in the parentSplitPoint nodes.
            // If we are in a singular extension search then return a fail low score.
            // A split node has at least one move, the one tried before to be splitted.
            if (!SpNode && moveCount == 0)
            {
                if (st != null)
                {
                    st.previous = null;
                    StateInfoBroker.Free();
                }
                CheckInfoBroker.Free();
                MovePickerBroker.Free(mp);
                MovesSearchedBroker.Free();
                return (excludedMove != 0) ? alpha : inCheck ? Utils.mated_in(ss[ssPos].ply) : DrawValue[pos.sideToMove];
            }

            // If we have pruned all the moves without searching return a fail-low score
            if (bestValue == -ValueC.VALUE_INFINITE)
            {
                Debug.Assert(playedMoveCount == 0);
                bestValue = alpha;
            }

            if (bestValue >= beta) // Failed high
            {
                TT.store(
                    posKey,
                    value_to_tt(bestValue, ss[ssPos].ply),
                    Bound.BOUND_LOWER,
                    depth,
                    bestMove,
                    ss[ssPos].staticEval,
                    ss[ssPos].evalMargin);

                if (!pos.is_capture_or_promotion(bestMove) && !inCheck)
                {
                    if (bestMove != ss[ssPos].killers0)
                    {
                        ss[ssPos].killers1 = ss[ssPos].killers0;
                        ss[ssPos].killers0 = bestMove;
                    }

                    // Increase history value of the cut-off move
                    var bonus = (depth * depth);
                    H.add(pos.piece_moved(bestMove), Utils.to_sq(bestMove), bonus);

                    // Decrease history of all the other played non-capture moves
                    for (var i = 0; i < playedMoveCount - 1; i++)
                    {
                        var m = movesSearched[i];
                        H.add(pos.piece_moved(m), Utils.to_sq(m), -bonus);
                    }
                }
            }
            else // Failed low or PV search
            {
                TT.store(posKey, value_to_tt(bestValue, ss[ssPos].ply), PvNode && bestMove != MoveC.MOVE_NONE ? Bound.BOUND_EXACT : Bound.BOUND_UPPER, depth, bestMove, ss[ssPos].staticEval, ss[ssPos].evalMargin);
            }

            Debug.Assert(bestValue > -ValueC.VALUE_INFINITE && bestValue < ValueC.VALUE_INFINITE);

            if (st != null)
            {
                st.previous = null;
                StateInfoBroker.Free();
            }
            CheckInfoBroker.Free();
            MovePickerBroker.Free(mp);
            MovesSearchedBroker.Free();

            return bestValue;
        }
Пример #11
0
        internal static void generate_evasion(Position pos, MoveStack[] ms, ref int mpos)
        {
            /// generate<EVASIONS> generates all pseudo-legal check evasions when the side
            /// to move is in check. Returns a pointer to the end of the move list.
            Debug.Assert(pos.in_check());

            ulong b;
            int from, checksq;
            var checkersCnt = 0;
            var us = pos.sideToMove;
            var ksq = pos.king_square(us);
            ulong sliderAttacks = 0;
            var checkers = pos.st.checkersBB;

            Debug.Assert(checkers != 0);

            // Find squares attacked by slider checkers, we will remove them from the king
            // evasions so to skip known illegal moves avoiding useless legality check later.
            b = checkers;
            do
            {
                checkersCnt++;
                checksq = Utils.pop_lsb(ref b);

                Debug.Assert(Utils.color_of(pos.piece_on(checksq)) == Utils.flip_C(us));

                switch (Utils.type_of(pos.piece_on(checksq)))
                {
                    case PieceTypeC.BISHOP:
                        sliderAttacks |= Utils.PseudoAttacks[PieceTypeC.BISHOP][checksq];
                        break;
                    case PieceTypeC.ROOK:
                        sliderAttacks |= Utils.PseudoAttacks[PieceTypeC.ROOK][checksq];
                        break;
                    case PieceTypeC.QUEEN:
                        // If queen and king are far or not on a diagonal line we can safely
                        // remove all the squares attacked in the other direction becuase are
                        // not reachable by the king anyway.
                        if ((Utils.between_bb(ksq, checksq) != 0)
                            || ((Utils.bit_is_set(Utils.PseudoAttacks[PieceTypeC.BISHOP][checksq], ksq)) == 0))
                        {
                            sliderAttacks |= Utils.PseudoAttacks[PieceTypeC.QUEEN][checksq];
                        }

                        // Otherwise we need to use real rook attacks to check if king is safe
                        // to move in the other direction. For example: king in B2, queen in A1
                        // a knight in B1, and we can safely move to C1.
                        else
                        {
                            sliderAttacks |= Utils.PseudoAttacks[PieceTypeC.BISHOP][checksq]
                                             | pos.attacks_from_ROOK(checksq);
                        }
                        break;
                    default:
                        break;
                }
            }
            while (b != 0);

            // Generate evasions for king, capture and non capture moves
            b = Position.attacks_from_KING(ksq) & ~pos.pieces_C(us) & ~sliderAttacks;
            from = ksq;
            while (b != 0)
            {
                ms[mpos++].move = Utils.make_move(from, Utils.pop_lsb(ref b));
            }

            // Generate evasions for other pieces only if not under a double check
            if (checkersCnt > 1)
            {
                return;
            }

            // Blocking evasions or captures of the checking piece
            var target = Utils.between_bb(checksq, ksq) | checkers;

            generate_all(GenType.EVASIONS, pos, ms, ref mpos, us, target, null);
        }
Пример #12
0
        // refutes() tests whether a 'first' move is able to defend against a 'second'
        // opponent's move. In this case will not be pruned. Normally the second move
        // is the threat (the best move returned from a null search that fails low).
        private static bool refutes(Position pos, int move, int threat)
        {
            Debug.Assert(Utils.is_ok_M(move));
            Debug.Assert(Utils.is_ok_M(threat));
            
            Square mfrom = Utils.from_sq(move);
            Square mto = Utils.to_sq(move);
            Square tfrom = Utils.from_sq(threat);
            Square tto = Utils.to_sq(threat);

            // Don't prune moves of the threatened piece
            if (mfrom == tto)
            {
                return true;
            }

            // If the threatened piece has value less than or equal to the value of the
            // threat piece, don't prune moves which defend it.
            if (pos.is_capture(threat)
                && (Position.PieceValue[PhaseC.MG][pos.piece_on(tfrom)] >= Position.PieceValue[PhaseC.MG][pos.piece_on(tto)]
                    || Utils.type_of(pos.piece_on(tfrom)) == PieceTypeC.KING))
            {
                // Update occupancy as if the piece and the threat are moving
                var occ = Utils.xor_bit(Utils.xor_bit(Utils.xor_bit(pos.occupied_squares, mfrom), mto), tfrom);
                Piece piece = pos.piece_on(mfrom);

                // The piece moved in 'to' attacks the square 's' ?
                if (Utils.bit_is_set(Position.attacks_from(piece, mto, occ), tto) != 0)
                {
                    return true;
                }

                // Scan for possible X-ray attackers behind the moved piece
                var xray = (Utils.rook_attacks_bb(tto, occ)
                        & pos.pieces(PieceTypeC.ROOK, PieceTypeC.QUEEN, Utils.color_of(piece)))
                        | (Utils.bishop_attacks_bb(tto, occ)
                            & pos.pieces(PieceTypeC.BISHOP, PieceTypeC.QUEEN, Utils.color_of(piece)));

                // Verify attackers are triggered by our move and not already existing
                if ((xray != 0) && ((xray ^ (xray & pos.attacks_from_QUEEN(tto))) != 0))
                {
                    return true;
                }
            }

            // Don't prune safe moves which block the threat path
            if ((Utils.bit_is_set(Utils.between_bb(tfrom, tto), mto) != 0) && pos.see(move, true) >= 0)
            {
                return true;
            }

            return false;
        }
Пример #13
0
        // allows() tests whether the 'first' move at previous ply somehow makes the
        // 'second' move possible, for instance if the moving piece is the same in
        // both moves. Normally the second move is the threat (the best move returned
        // from a null search that fails low).
        internal static bool allows(Position pos, int first, int second)
        {
            Debug.Assert(Utils.is_ok_M(first));
            Debug.Assert(Utils.is_ok_M(second));
            Debug.Assert(Utils.color_of(pos.piece_on(Utils.from_sq(second))) == 1 - pos.sideToMove);

            Square m1to = Utils.to_sq(first);
            Square m1from = Utils.from_sq(first);
            Square m2to = Utils.to_sq(second);
            Square m2from = Utils.from_sq(second);


            // The piece is the same or second's destination was vacated by the first move
            if (m1to == m2from || m2to == m1from)
            {
                return true;
            }

            // Second one moves through the square vacated by first one
            if (Utils.bit_is_set(Utils.between_bb(m2from, m2to), m1from) != 0)
            {
                return true;
            }

            // Second's destination is defended by the first move's piece
            Bitboard m1att = Position.attacks_from(pos.piece_on(m1to), m1to, pos.occupied_squares ^ (ulong)m2from);
            if (Utils.bit_is_set(m1att, m2to) != 0)
            {
                return true;
            }

            // Second move gives a discovered check through the first's checking piece
            if (Utils.bit_is_set(m1att, pos.king_square(pos.sideToMove)) != 0 &&
                Utils.bit_is_set(Utils.between_bb(m1to, pos.king_square(pos.sideToMove)), m2from) != 0) // TODO: removing condition asserts below
            {
                Debug.Assert(Utils.bit_is_set(Utils.between_bb(m1to, pos.king_square(pos.sideToMove)), m2from) != 0);
                return true;
            }

            return false;
        }
Пример #14
0
        // check_is_dangerous() tests if a checking move can be pruned in qsearch().
        // bestValue is updated only when returning false because in that case move
        // will be pruned.
        private static bool check_is_dangerous(Position pos, int move, int futilityBase, int beta)
        {
            //ulong b, occ, oldAtt, newAtt, kingAtt;
            //int from, to, ksq;
            //int pc;
            //int them;

            //from = Utils.from_sq(move);
            //to = Utils.to_sq(move);
            //them = Utils.flip_C(pos.sideToMove);
            //ksq = pos.king_square(them);
            //kingAtt = Position.attacks_from_KING(ksq);
            //pc = pos.piece_moved(move);

            //occ = pos.occupied_squares ^ Utils.SquareBB[from] ^ Utils.SquareBB[ksq];
            //oldAtt = Position.attacks_from(pc, from, occ);
            //newAtt = Position.attacks_from(pc, to, occ);

            //// Rule 1. Checks which give opponent's king at most one escape square are dangerous
            //b = kingAtt & ~pos.pieces_C(them) & ~newAtt & ~(1UL << to);

            //if ((b & (b - 1)) == 0) // Catches also !b
            Piece pc = pos.piece_moved(move);
            Square from = Utils.from_sq(move);
            Square to = Utils.to_sq(move);
            Color them = pos.sideToMove ^ 1;
            Square ksq = pos.king_square(them);
            Bitboard enemies = pos.pieces_C(them);
            Bitboard kingAtt = Position.attacks_from_KING(ksq);
            Bitboard occ = pos.occupied_squares ^ Utils.SquareBB[from] ^ Utils.SquareBB[ksq];
            Bitboard oldAtt = Position.attacks_from(pc, from, occ);
            Bitboard newAtt = Position.attacks_from(pc, to, occ);

            // Checks which give opponent's king at most one escape square are dangerous
            if (!Utils.more_than_one(kingAtt & ~(enemies | newAtt | (ulong)to)))
            {
                return true;
            }

            // Queen contact check is very dangerous
            if (Utils.type_of(pc) == PieceTypeC.QUEEN && (Utils.bit_is_set(kingAtt, to) != 0))
            {
                return true;
            }

            // Creating new double threats with checks is dangerous
            Bitboard b = (enemies ^ (ulong)ksq) & newAtt & ~oldAtt;
            while (b != 0)
            {
                // Note that here we generate illegal "double move"!
                if (futilityBase + Position.PieceValue[PhaseC.EG][pos.piece_on(Utils.pop_lsb(ref b))] >= beta)
                {
                    return true;
                }
            }
            return false;
        }
Пример #15
0
        internal static void generate_quiet_check(Position pos, MoveStack[] ms, ref int mpos)
        {
            /// generate<MV_NON_CAPTURE_CHECK> generates all pseudo-legal non-captures and knight
            /// underpromotions that give check. Returns a pointer to the end of the move list.
            Debug.Assert(!pos.in_check());

            Color us = pos.sideToMove;
            CheckInfo ci = CheckInfoBroker.GetObject();
            ci.CreateCheckInfo(pos);
            Bitboard dc = ci.dcCandidates;

            while (dc != 0)
            {
                Square from = Utils.pop_1st_bit(ref dc);
                PieceType pt = Utils.type_of(pos.piece_on(from));

                if (pt == PieceTypeC.PAWN)
                    continue; // Will be generated together with direct checks

                Bitboard b = pos.attacks_from_PTS(pt, from) & ~pos.occupied_squares;

                if (pt == PieceTypeC.KING)
                    b &= ~Utils.PseudoAttacks[PieceTypeC.QUEEN][ci.ksq];

                while (b != 0) { ms[mpos++].move = Utils.make_move(from, Utils.pop_1st_bit(ref b)); }
            }

            generate_pawn_moves(us, MoveType.MV_QUIET_CHECK, pos, ms, ref mpos, ci.dcCandidates, ci.ksq);

            generate_direct_checks(PieceTypeC.KNIGHT, pos, ms, ref mpos, us, ci);
            generate_direct_checks(PieceTypeC.BISHOP, pos, ms, ref mpos, us, ci);
            generate_direct_checks(PieceTypeC.ROOK, pos, ms, ref mpos, us, ci);
            generate_direct_checks(PieceTypeC.QUEEN, pos, ms, ref mpos, us, ci);

            if (pos.can_castle_C(us) != 0)
            {
                generate_castle(CastlingSideC.KING_SIDE, true, pos, ms, ref mpos, us);
                generate_castle(CastlingSideC.QUEEN_SIDE, true, pos, ms, ref mpos, us);
            }

            CheckInfoBroker.Free();
        }
Пример #16
0
        /// flip() flips position with the white and black sides reversed. This
        /// is only useful for debugging especially for finding evaluation symmetry bugs.
        internal void flip()
        {
            // Make a copy of current position before to start changing
            Position pos = new Position(this);
            clear();

            sideToMove = pos.sideToMove ^ 1;
            thisThread = pos.this_thread();
            nodes = pos.nodes;
            chess960 = pos.chess960;
            startPosPly = pos.startpos_ply_counter();

            for (Square s = SquareC.SQ_A1; s <= SquareC.SQ_H8; s++)
                if (!pos.is_empty(s))
                    put_piece((pos.piece_on(s) ^ 8), Utils.flip_S(s));

            if (pos.can_castle_CR(CastleRightC.WHITE_OO) != 0)
                set_castle_right(ColorC.BLACK, Utils.flip_S(pos.castle_rook_square(ColorC.WHITE, CastlingSideC.KING_SIDE)));
            if (pos.can_castle_CR(CastleRightC.WHITE_OOO) != 0)
                set_castle_right(ColorC.BLACK, Utils.flip_S(pos.castle_rook_square(ColorC.WHITE, CastlingSideC.QUEEN_SIDE)));
            if (pos.can_castle_CR(CastleRightC.BLACK_OO) != 0)
                set_castle_right(ColorC.WHITE, Utils.flip_S(pos.castle_rook_square(ColorC.BLACK, CastlingSideC.KING_SIDE)));
            if (pos.can_castle_CR(CastleRightC.BLACK_OOO) != 0)
                set_castle_right(ColorC.WHITE, Utils.flip_S(pos.castle_rook_square(ColorC.BLACK, CastlingSideC.QUEEN_SIDE)));


            if (pos.st.epSquare != SquareC.SQ_NONE)
                st.epSquare = Utils.flip_S(pos.st.epSquare);

            // Checkers
            st.checkersBB = attackers_to(king_square(sideToMove)) & pieces_C(Utils.flip_C(sideToMove));

            // Hash keys
            st.key = compute_key();
            st.pawnKey = compute_pawn_key();
            st.materialKey = compute_material_key();

            // Incremental scores
            st.psqScore = compute_psq_score();

            // Material
            st.npMaterialWHITE = compute_non_pawn_material(ColorC.WHITE);
            st.npMaterialBLACK = compute_non_pawn_material(ColorC.BLACK);

            Debug.Assert(pos_is_ok());
        }