当前位置: 首页>>代码示例>>C#>>正文


C# Position.legal方法代码示例

本文整理汇总了C#中Position.legal方法的典型用法代码示例。如果您正苦于以下问题:C# Position.legal方法的具体用法?C# Position.legal怎么用?C# Position.legal使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Position的用法示例。


在下文中一共展示了Position.legal方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: qsearch


//.........这里部分代码省略.........
        while ((move = mp.next_move(false)) != Move.MOVE_NONE)
        {
            Debug.Assert(Move.is_ok(move));

            var givesCheck = Move.type_of(move) == MoveType.NORMAL && ci.dcCandidates == 0
                ? Bitboard.AndWithSquare(ci.checkSquares[Piece.type_of(pos.piece_on(Move.from_sq(move)))], Move.to_sq(move))!=0
                : pos.gives_check(move, ci);

            // Futility pruning
            if (!InCheck
                && !givesCheck
                && futilityBase > -Value.VALUE_KNOWN_WIN
                && !pos.advanced_pawn_push(move))
            {
                Debug.Assert(Move.type_of(move) != MoveType.ENPASSANT); // Due to !pos.advanced_pawn_push

                var futilityValue = futilityBase + Value.PieceValue[(int) Phase.EG][pos.piece_on(Move.to_sq(move))];

                if (futilityValue <= alpha)
                {
                    bestValue = Value.Create(Math.Max(bestValue, futilityValue));
                    continue;
                }

                if (futilityBase <= alpha && pos.see(move) <= Value.VALUE_ZERO)
                {
                    bestValue = Value.Create(Math.Max(bestValue, futilityBase));
                    continue;
                }
            }

            // Detect non-capture evasions that are candidates to be pruned
            var evasionPrunable = InCheck
                                   && bestValue > Value.VALUE_MATED_IN_MAX_PLY
                                   && !pos.capture(move);

            // Don't search moves with negative SEE values
            if ((!InCheck || evasionPrunable)
                && Move.type_of(move) != MoveType.PROMOTION
                && pos.see_sign(move) < Value.VALUE_ZERO)
                continue;

            // Speculative prefetch as early as possible
            //prefetch(TT.first_entry(pos.key_after(move)));

            // Check for legality just before making the move
            if (!pos.legal(move, ci.pinned))
                continue;

            ss[ss.current].currentMove = move;

            // Make and search the move
            pos.do_move(move, new StateInfo(), givesCheck);
            var value = givesCheck
                ? -qsearch(NT, true, pos, new StackArrayWrapper(ss.table, ss.current + 1), -beta, -alpha,
                    depth - Depth.ONE_PLY)
                : -qsearch(NT, false, pos, new StackArrayWrapper(ss.table, ss.current + 1), -beta, -alpha,
                    depth - Depth.ONE_PLY);
            pos.undo_move(move);

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

            // Check for new best move
            if (value <= bestValue) continue;

            bestValue = value;

            if (value > alpha)
            {
                if (PvNode) // Update pv even in fail-high case
                    update_pv(currentStack.pv, move, nextStack.pv);

                if (PvNode && value < beta) // Update alpha here! Always alpha < beta
                {
                    alpha = value;
                    bestMove = move;
                }
                else // Fail high
                {
                    tte.save(posKey, value_to_tt(value, currentPly), Bound.BOUND_LOWER,
                        ttDepth, move, currentStack.staticEval, TranspositionTable.generation());

                    return value;
                }
            }
        }

        // All legal moves have been searched. A special case: If we're in check
        // and no legal moves were found, it is checkmate.
        if (InCheck && bestValue == -Value.VALUE_INFINITE)
            return Value.mated_in(currentPly); // Plies to mate from the root

        tte.save(posKey, value_to_tt(bestValue, currentPly),
            PvNode && bestValue > oldAlpha ? Bound.BOUND_EXACT : Bound.BOUND_UPPER,
            ttDepth, bestMove, ss[ss.current].staticEval, TranspositionTable.generation());

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

        return bestValue;
    }
开发者ID:torfranz,项目名称:NetFish,代码行数:101,代码来源:Search.cs

示例2: search


//.........这里部分代码省略.........

                // Do verification search at high depths
                stack.skipEarlyPruning = true;
                var v = depth - R < Depth.ONE_PLY
                    ? qsearch(NodeType.NonPV, false, pos, ss, beta - 1, beta, Depth.DEPTH_ZERO)
                    : search(NodeType.NonPV, false, pos, ss, beta - 1, beta, depth - R, false);
                stack.skipEarlyPruning = false;

                if (v >= beta)
                    return nullValue;
            }
        }

        // Step 9. ProbCut (skipped when in check)
        // 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
            && depth >= 5*Depth.ONE_PLY_C
            && Math.Abs(beta) < Value.VALUE_MATE_IN_MAX_PLY)
        {
            var rbeta = Value.Create(Math.Min(beta + 200, Value.VALUE_INFINITE));
            var rdepth = depth - 4*Depth.ONE_PLY;

            Debug.Assert(rdepth >= Depth.ONE_PLY_C);
            Debug.Assert(stackMinus1.currentMove != Move.MOVE_NONE);
            Debug.Assert(stackMinus1.currentMove != Move.MOVE_NULL);

            var mp2 = new MovePicker(pos, ttMove, History, CounterMovesHistory,
                Value.PieceValue[(int) Phase.MG][pos.captured_piece_type()]);
            var ci2 = new CheckInfo(pos);

            while ((move = mp2.next_move(false)) != Move.MOVE_NONE)
                if (pos.legal(move, ci2.pinned))
                {
                    stack.currentMove = move;
                    pos.do_move(move, st, pos.gives_check(move, ci2));
                    value =
                        -search(NodeType.NonPV, false, pos, new StackArrayWrapper(ss.table, ss.current + 1), -rbeta,
                            -rbeta + 1, rdepth, !cutNode);
                    pos.undo_move(move);
                    if (value >= rbeta)
                        return value;
                }
        }

        // Step 10. Internal iterative deepening (skipped when in check)
        if (depth >= (PvNode ? 5*Depth.ONE_PLY_C : 8*Depth.ONE_PLY_C)
            && ttMove == 0
            && (PvNode || stack.staticEval + 256 >= beta))
        {
            var d = depth - 2*Depth.ONE_PLY - (PvNode ? Depth.DEPTH_ZERO : depth/4);
            stack.skipEarlyPruning = true;
            search(PvNode ? NodeType.PV : NodeType.NonPV, false, pos, ss, alpha, beta, d, true);
            stack.skipEarlyPruning = false;

            tte = TranspositionTable.probe(posKey, out ttHit);
            ttMove = ttHit ? tte.move() : Move.MOVE_NONE;
        }

 moves_loop: // When in check and at SpNode search starts from here

        var prevMoveSq = Move.to_sq(stackMinus1.currentMove);
        var countermove = Countermoves.table[pos.piece_on(prevMoveSq), prevMoveSq];

        var mp = new MovePicker(pos, ttMove, depth, History, CounterMovesHistory, countermove, ss);
开发者ID:torfranz,项目名称:NetFish,代码行数:67,代码来源:Search.cs

示例3: generate_LEGAL

    /// generate
    /// LEGAL generates all the legal moves in the given position
    private static ExtMoveArrayWrapper generate_LEGAL(Position pos, ExtMoveArrayWrapper moveList)
    {
        var pinned = pos.pinned_pieces(pos.side_to_move());
        var ksq = pos.square(PieceType.KING, pos.side_to_move());
        var cur = moveList.current;

        moveList = pos.checkers() != 0
            ? generate(GenType.EVASIONS, pos, moveList)
            : generate(GenType.NON_EVASIONS, pos, moveList);

        while (cur != moveList.current)
        {
            if ((pinned != 0 || Move.from_sq(moveList[cur]) == ksq || Move.type_of(moveList[cur]) == MoveType.ENPASSANT)
                && !pos.legal(moveList[cur], pinned))
            {
                for (var idx = cur; idx < moveList.current; idx++)
                {
                    moveList.table[idx] = moveList.table[idx + 1];
                }
                --moveList;
            }
            else
            {
                ++cur;
            }
        }

        return moveList;
    }
开发者ID:torfranz,项目名称:NetFish,代码行数:31,代码来源:Movegen.cs


注:本文中的Position.legal方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。