本文整理汇总了C#中Portfish.Position.king_square方法的典型用法代码示例。如果您正苦于以下问题:C# Position.king_square方法的具体用法?C# Position.king_square怎么用?C# Position.king_square使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Portfish.Position
的用法示例。
在下文中一共展示了Position.king_square方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: generate_castle
private static void generate_castle(
int Side,
bool Checks,
Position pos,
MoveStack[] ms,
ref int mpos,
int us)
{
if (pos.castle_impeded(us, Side) || (pos.can_castle_CR(Utils.make_castle_right(us, Side)) == 0))
{
return;
}
// After castling, the rook and king final positions are the same in Chess960
// as they would be in standard chess.
var kfrom = pos.king_square(us);
var rfrom = pos.castle_rook_square(us, Side);
var kto = Utils.relative_square(us, Side == CastlingSideC.KING_SIDE ? SquareC.SQ_G1 : SquareC.SQ_C1);
var enemies = pos.pieces_C(us ^ 1);
Debug.Assert(!pos.in_check());
int K = pos.chess960 ? kto > kfrom ? -1 : 1 : Side == CastlingSideC.KING_SIDE ? -1 : 1;
for (Square s = kto; s != kfrom; s += (Square)K)
{
if ((pos.attackers_to(s) & enemies) != 0)
{
return;
}
}
// Because we generate only legal castling moves we need to verify that
// when moving the castling rook we do not discover some hidden checker.
// For instance an enemy queen in SQ_A1 when castling rook is in SQ_B1.
if (pos.chess960 && ((pos.attackers_to(kto, Utils.xor_bit(pos.occupied_squares, rfrom)) & enemies) != 0))
{
return;
}
var m = Utils.make(kfrom, rfrom, MoveTypeC.CASTLING);
if (Checks)
{
var ci = CheckInfoBroker.GetObject();
ci.CreateCheckInfo(pos);
var givesCheck = pos.move_gives_check(m, ci);
CheckInfoBroker.Free();
if (!givesCheck)
{
return;
}
}
ms[mpos++].move = m;
}
示例2: generate_castle
private static void generate_castle(CastlingSide Side, bool OnlyChecks, Position pos, MoveStack[] ms, ref int mpos, Color us)
{
if (pos.castle_impeded(us, Side) || (pos.can_castle_CR(Utils.make_castle_right(us, Side))==0) )
return;
// After castling, the rook and king final positions are the same in Chess960
// as they would be in standard chess.
Square kfrom = pos.king_square(us);
Square rfrom = pos.castle_rook_square(us, Side);
Square kto = Utils.relative_square(us, Side == CastlingSideC.KING_SIDE ? SquareC.SQ_G1 : SquareC.SQ_C1);
Bitboard enemies = pos.pieces_C(us ^ 1);
Debug.Assert(!pos.in_check());
for (Square s = Math.Min(kfrom, kto), e = Math.Max(kfrom, kto); s <= e; s++)
if (s != kfrom // We are not in check
&& ((pos.attackers_to(s) & enemies) != 0))
return;
// Because we generate only legal castling moves we need to verify that
// when moving the castling rook we do not discover some hidden checker.
// For instance an enemy queen in SQ_A1 when castling rook is in SQ_B1.
if (pos.chess960
&& ((pos.attackers_to(kto, Utils.xor_bit(pos.occupied_squares, rfrom)) & enemies) != 0))
return;
Move m = Utils.make_castle(kfrom, rfrom);
if (OnlyChecks)
{
CheckInfo ci = CheckInfoBroker.GetObject();
ci.CreateCheckInfo(pos);
bool givesCheck = pos.move_gives_check(m, ci);
CheckInfoBroker.Free();
if (!givesCheck) return;
}
ms[mpos++].move = m;
}
示例3: Endgame_KQKP
/// KQ vs KP. In general, a win for the stronger side, however, there are a few
/// important exceptions. Pawn on 7th rank, A,C,F or H file, with king next can
/// be a draw, so we scale down to distance between kings only.
internal static int Endgame_KQKP(int strongerSide, Position pos)
{
var weakerSide = strongerSide ^ 1;
Debug.Assert(pos.non_pawn_material(strongerSide) == Constants.QueenValueMidgame);
Debug.Assert(pos.piece_count(strongerSide, PieceTypeC.PAWN) == 0);
Debug.Assert(pos.non_pawn_material(weakerSide) == 0);
Debug.Assert(pos.piece_count(weakerSide, PieceTypeC.PAWN) == 1);
Square winnerKSq = pos.king_square(strongerSide);
Square loserKSq = pos.king_square(weakerSide);
Square pawnSq = pos.pieceList[weakerSide][PieceTypeC.PAWN][0];
var result = Constants.QueenValueEndgame - Constants.PawnValueEndgame + DistanceBonus[Utils.square_distance(winnerKSq, loserKSq)];
if ( Utils.square_distance(loserKSq, pawnSq) == 1
&& Utils.relative_rank_CS(weakerSide, pawnSq) == RankC.RANK_7)
{
File f = Utils.file_of(pawnSq);
if (f == FileC.FILE_A || f == FileC.FILE_C || f == FileC.FILE_F || f == FileC.FILE_H)
{
result = DistanceBonus[Utils.square_distance(winnerKSq, loserKSq)];
}
}
return strongerSide == pos.sideToMove ? result : -result;
}
示例4: check_is_dangerous
// 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;
}
示例5: evaluate_unstoppable_pawns
// evaluate_unstoppable_pawns() evaluates the unstoppable passed pawns for both sides, this is quite
// conservative and returns a winning score only when we are very sure that the pawn is winning.
private static int evaluate_unstoppable_pawns(Position pos, EvalInfo ei)
{
ulong b, b2, blockers, supporters, queeningPath, candidates;
int s, blockSq, queeningSquare;
int c, winnerSide, loserSide;
bool pathDefended, opposed;
int pliesToGo = 0, movesToGo, oppMovesToGo = 0, sacptg, blockersCount, minKingDist, kingptg, d;
int pliesToQueenWHITE = 256, pliesToQueenBLACK = 256, pliesToQueenWinner = 256;
// Step 1. Hunt for unstoppable passed pawns. If we find at least one,
// record how many plies are required for promotion.
for (c = ColorC.WHITE; c <= ColorC.BLACK; c++)
{
// Skip if other side has non-pawn pieces
if (pos.non_pawn_material(Utils.flip_C(c)) != 0)
{
continue;
}
b = ei.pi.passed_pawns(c);
while (b != 0)
{
s = Utils.pop_lsb(ref b);
queeningSquare = Utils.relative_square(c, Utils.make_square(Utils.file_of(s), RankC.RANK_8));
queeningPath = Utils.forward_bb(c, s);
// Compute plies to queening and check direct advancement
movesToGo = Utils.rank_distance(s, queeningSquare)
- (Utils.relative_rank_CS(c, s) == RankC.RANK_2 ? 1 : 0);
oppMovesToGo = Utils.square_distance(pos.king_square(Utils.flip_C(c)), queeningSquare)
- ((c != pos.sideToMove) ? 1 : 0);
pathDefended = ((ei.attackedBy[c][0] & queeningPath) == queeningPath);
if (movesToGo >= oppMovesToGo && !pathDefended)
{
continue;
}
// Opponent king cannot block because path is defended and position
// is not in check. So only friendly pieces can be blockers.
Debug.Assert(!pos.in_check());
Debug.Assert((queeningPath & pos.occupied_squares) == (queeningPath & pos.pieces_C(c)));
// Add moves needed to free the path from friendly pieces and retest condition
movesToGo += Bitcount.popcount_1s_Max15(queeningPath & pos.pieces_C(c));
if (movesToGo >= oppMovesToGo && !pathDefended)
{
continue;
}
pliesToGo = 2 * movesToGo - ((c == pos.sideToMove) ? 1 : 0);
if (c == ColorC.WHITE)
{
pliesToQueenWHITE = Math.Min(pliesToQueenWHITE, pliesToGo);
}
else
{
pliesToQueenBLACK = Math.Min(pliesToQueenBLACK, pliesToGo);
}
}
}
// Step 2. If either side cannot promote at least three plies before the other side then situation
// becomes too complex and we give up. Otherwise we determine the possibly "winning side"
if (Math.Abs(pliesToQueenWHITE - pliesToQueenBLACK) < 3)
{
return ScoreC.SCORE_ZERO;
}
winnerSide = (pliesToQueenWHITE < pliesToQueenBLACK ? ColorC.WHITE : ColorC.BLACK);
pliesToQueenWinner = (winnerSide == ColorC.WHITE) ? pliesToQueenWHITE : pliesToQueenBLACK;
loserSide = Utils.flip_C(winnerSide);
// Step 3. Can the losing side possibly create a new passed pawn and thus prevent the loss?
b = candidates = pos.pieces_PTC(PieceTypeC.PAWN, loserSide);
while (b != 0)
{
s = Utils.pop_lsb(ref b);
// Compute plies from queening
queeningSquare = Utils.relative_square(loserSide, Utils.make_square(Utils.file_of(s), RankC.RANK_8));
movesToGo = Utils.rank_distance(s, queeningSquare)
- ((Utils.relative_rank_CS(loserSide, s) == RankC.RANK_2) ? 1 : 0);
pliesToGo = 2 * movesToGo - ((loserSide == pos.sideToMove) ? 1 : 0);
// Check if (without even considering any obstacles) we're too far away or doubled
if ((pliesToQueenWinner + 3 <= pliesToGo)
|| ((Utils.forward_bb(loserSide, s) & pos.pieces_PTC(PieceTypeC.PAWN, loserSide)) != 0))
{
Utils.xor_bit(ref candidates, s);
}
}
// If any candidate is already a passed pawn it _may_ promote in time. We give up.
//.........这里部分代码省略.........
示例6: Endgame_KRPPKRP
/// K, rook and two pawns vs K, rook and one pawn. There is only a single
/// pattern: If the stronger side has no passed pawns and the defending king
/// is actively placed, the position is drawish.
internal static ScaleFactor Endgame_KRPPKRP(Color strongerSide, Position pos)
{
Color weakerSide = strongerSide ^ 1;
Debug.Assert(pos.non_pawn_material(strongerSide) == Constants.RookValueMidgame);
Debug.Assert(pos.piece_count(strongerSide, PieceTypeC.PAWN) == 2);
Debug.Assert(pos.non_pawn_material(weakerSide) == Constants.RookValueMidgame);
Debug.Assert(pos.piece_count(weakerSide, PieceTypeC.PAWN) == 1);
Square wpsq1 = pos.pieceList[strongerSide][PieceTypeC.PAWN][0];
Square wpsq2 = pos.pieceList[strongerSide][PieceTypeC.PAWN][1];
Square bksq = pos.king_square(weakerSide);
// Does the stronger side have a passed pawn?
if (pos.pawn_is_passed(strongerSide, wpsq1)
|| pos.pawn_is_passed(strongerSide, wpsq2))
return ScaleFactorC.SCALE_FACTOR_NONE;
Rank r = Math.Max(Utils.relative_rank_CS(strongerSide, wpsq1), Utils.relative_rank_CS(strongerSide, wpsq2));
if (Utils.file_distance(bksq, wpsq1) <= 1
&& Utils.file_distance(bksq, wpsq2) <= 1
&& Utils.relative_rank_CS(strongerSide, bksq) > r)
{
switch (r)
{
case RankC.RANK_2: return (10);
case RankC.RANK_3: return (10);
case RankC.RANK_4: return (15);
case RankC.RANK_5: return (20);
case RankC.RANK_6: return (40);
default: Debug.Assert(false); break;
}
}
return ScaleFactorC.SCALE_FACTOR_NONE;
}
示例7: Endgame_KRKP
/// KR vs KP. This is a somewhat tricky endgame to evaluate precisely without
/// a bitbase. The function below returns drawish scores when the pawn is
/// far advanced with support of the king, while the attacking king is far
/// away.
internal static Value Endgame_KRKP(Color strongerSide, Position pos)
{
Color weakerSide = strongerSide ^ 1;
Debug.Assert(pos.non_pawn_material(strongerSide) == Constants.RookValueMidgame);
Debug.Assert(pos.piece_count(strongerSide, PieceTypeC.PAWN) == 0);
Debug.Assert(pos.non_pawn_material(weakerSide) == 0);
Debug.Assert(pos.piece_count(weakerSide, PieceTypeC.PAWN) == 1);
Square wksq, wrsq, bksq, bpsq;
int tempo = (pos.sideToMove == strongerSide ? 1 : 0);
wksq = pos.king_square(strongerSide);
wrsq = pos.pieceList[strongerSide][PieceTypeC.ROOK][0];
bksq = pos.king_square(weakerSide);
bpsq = pos.pieceList[weakerSide][PieceTypeC.PAWN][0];
if (strongerSide == ColorC.BLACK)
{
wksq = Utils.flip_S(wksq);
wrsq = Utils.flip_S(wrsq);
bksq = Utils.flip_S(bksq);
bpsq = Utils.flip_S(bpsq);
}
Square queeningSq = Utils.make_square(Utils.file_of(bpsq), RankC.RANK_1);
Value result;
// If the stronger side's king is in front of the pawn, it's a win
if (wksq < bpsq && Utils.file_of(wksq) == Utils.file_of(bpsq))
result = Constants.RookValueEndgame - (Utils.square_distance(wksq, bpsq));
// If the weaker side's king is too far from the pawn and the rook,
// it's a win
else if (Utils.square_distance(bksq, bpsq) - (tempo ^ 1) >= 3
&& Utils.square_distance(bksq, wrsq) >= 3)
result = Constants.RookValueEndgame - (Utils.square_distance(wksq, bpsq));
// If the pawn is far advanced and supported by the defending king,
// the position is drawish
else if (Utils.rank_of(bksq) <= RankC.RANK_3
&& Utils.square_distance(bksq, bpsq) == 1
&& Utils.rank_of(wksq) >= RankC.RANK_4
&& Utils.square_distance(wksq, bpsq) - tempo > 2)
result = (80 - Utils.square_distance(wksq, bpsq) * 8);
else
result = (200)
- (Utils.square_distance(wksq, bpsq + SquareC.DELTA_S) * 8)
+ (Utils.square_distance(bksq, bpsq + SquareC.DELTA_S) * 8)
+ (Utils.square_distance(bpsq, queeningSq) * 8);
return strongerSide == pos.sideToMove ? result : -result;
}
示例8: Endgame_KRKB
/// KR vs KB. This is very simple, and always returns drawish scores. The
/// score is slightly bigger when the defending king is close to the edge.
internal static Value Endgame_KRKB(Color strongerSide, Position pos)
{
Color weakerSide = strongerSide ^ 1;
Debug.Assert(pos.non_pawn_material(strongerSide) == Constants.RookValueMidgame);
Debug.Assert(pos.piece_count(strongerSide, PieceTypeC.PAWN) == 0);
Debug.Assert(pos.non_pawn_material(weakerSide) == Constants.BishopValueMidgame);
Debug.Assert(pos.piece_count(weakerSide, PieceTypeC.PAWN) == 0);
Debug.Assert(pos.piece_count(weakerSide, PieceTypeC.BISHOP) == 1);
Value result = (MateTable[pos.king_square(weakerSide)]);
return strongerSide == pos.sideToMove ? result : -result;
}
示例9: Endgame_KQKR
/// KQ vs KR. This is almost identical to KX vs K: We give the attacking
/// king a bonus for having the kings close together, and for forcing the
/// defending king towards the edge. If we also take care to avoid null move
/// for the defending side in the search, this is usually sufficient to be
/// able to win KQ vs KR.
internal static Value Endgame_KQKR(Color strongerSide, Position pos)
{
Color weakerSide = strongerSide ^ 1;
Debug.Assert(pos.non_pawn_material(strongerSide) == Constants.QueenValueMidgame);
Debug.Assert(pos.piece_count(strongerSide, PieceTypeC.PAWN) == 0);
Debug.Assert(pos.non_pawn_material(weakerSide) == Constants.RookValueMidgame);
Debug.Assert(pos.piece_count(weakerSide, PieceTypeC.PAWN) == 0);
Square winnerKSq = pos.king_square(strongerSide);
Square loserKSq = pos.king_square(weakerSide);
Value result = Constants.QueenValueEndgame
- Constants.RookValueEndgame
+ MateTable[loserKSq]
+ DistanceBonus[Utils.square_distance(winnerKSq, loserKSq)];
return strongerSide == pos.sideToMove ? result : -result;
}
示例10: Endgame_KBBKN
internal static Value Endgame_KBBKN(Color strongerSide, Position pos)
{
Color weakerSide = strongerSide ^ 1;
Debug.Assert(pos.piece_count(strongerSide, PieceTypeC.BISHOP) == 2);
Debug.Assert(pos.non_pawn_material(strongerSide) == 2 * Constants.BishopValueMidgame);
Debug.Assert(pos.piece_count(weakerSide, PieceTypeC.KNIGHT) == 1);
Debug.Assert(pos.non_pawn_material(weakerSide) == Constants.KnightValueMidgame);
Debug.Assert(pos.pieces_PT(PieceTypeC.PAWN) == 0);
Value result = Constants.BishopValueEndgame;
Square wksq = pos.king_square(strongerSide);
Square bksq = pos.king_square(weakerSide);
Square nsq = pos.pieceList[weakerSide][PieceTypeC.KNIGHT][0];
// Bonus for attacking king close to defending king
result += (DistanceBonus[Utils.square_distance(wksq, bksq)]);
// Bonus for driving the defending king and knight apart
result += (Utils.square_distance(bksq, nsq) * 32);
// Bonus for restricting the knight's mobility
result += ((8 - Bitcount.popcount_1s_Max15(Position.attacks_from_KNIGHT(nsq))) * 8);
return strongerSide == pos.sideToMove ? result : -result;
}
示例11: generate_evasion
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: generate_legal
internal static void generate_legal(Position pos, MoveStack[] ms, ref int mpos)
{
/// generate<LEGAL> generates all the legal moves in the given position
var pinned = pos.pinned_pieces();
Square ksq = pos.king_square(pos.sideToMove);
if (pos.in_check())
{
generate_evasion(pos, ms, ref mpos);
}
else
{
generate_non_evasion(pos, ms, ref mpos);
}
var last = mpos;
var cur = 0;
while (cur != last)
{
var curMove = ms[cur].move;
//if (!pos.pl_move_is_legal(ms[cur].move, pinned))
if ((pinned != 0 || Utils.from_sq(curMove) == ksq || Utils.type_of_move(curMove) == MoveTypeC.ENPASSANT) && !pos.pl_move_is_legal(curMove, pinned))
{
ms[cur].move = ms[--last].move;
}
else
{
cur++;
}
}
mpos = last;
}
示例13: generate_all
private static void generate_all(
GenType type,
Position pos,
MoveStack[] mlist,
ref int mpos,
int us,
ulong target,
CheckInfo ci)
{
var Checks = type == GenType.QUIET_CHECKS;
generate_pawn_moves(us, type, pos, mlist, ref mpos, target, ci);
generate_moves(PieceTypeC.KNIGHT, Checks, pos, mlist, ref mpos, us, target, ci);
generate_moves(PieceTypeC.BISHOP, Checks, pos, mlist, ref mpos, us, target, ci);
generate_moves(PieceTypeC.ROOK, Checks, pos, mlist, ref mpos, us, target, ci);
generate_moves(PieceTypeC.QUEEN, Checks, pos, mlist, ref mpos, us, target, ci);
if (!Checks && type != GenType.EVASIONS)
{
Square from = pos.king_square(us);
Bitboard b = Position.attacks_from_KING(from) & target;
// SERIALIZE(b);
while (b != 0)
{
#if X64
Bitboard bb = b;
b &= (b - 1);
mlist[mpos++].move = ((Utils.BSFTable[((bb & (0xffffffffffffffff - bb + 1)) * DeBruijn_64) >> 58]) | (from << 6));
#else
mlist[mpos++].move = Utils.make_move(from, Utils.pop_lsb(ref b));
#endif
}
}
if (type != GenType.CAPTURES && type != GenType.EVASIONS && pos.can_castle_C(us) != 0)
{
generate_castle(CastlingSideC.KING_SIDE, Checks, pos, mlist, ref mpos, us);
generate_castle(CastlingSideC.QUEEN_SIDE, Checks, pos, mlist, ref mpos, us);
}
}
示例14: allows
// 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;
}
示例15: check_is_dangerous
// 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;
}