/usr/include/osl/checkmate/fixedDepthSearcher2.tcc is in libosl-dev 0.6.0-3.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | /* fixedDepthSearcher2.tcc
*/
#ifndef OSL_CHECKMATE_FIXED_DEPTH_SERCHER2_TCC
#define OSL_CHECKMATE_FIXED_DEPTH_SERCHER2_TCC
#include "osl/checkmate/fixedDepthSearcher2.h"
#include "osl/checkmate/immediateCheckmate.h"
#include "osl/checkmate/proofPieces.h"
#include "osl/checkmate/proofNumberTable.h"
#include "osl/state/numEffectState.h"
#include "osl/container/moveVector.h"
#include "osl/move_action/store.h"
#include "osl/move_action/count.h"
#include "osl/move_generator/addEffectWithEffect.h"
#include "osl/move_generator/addEffectWithEffect.tcc"
#include "osl/move_generator/escape_.h"
#include "osl/move_classifier/check_.h"
#include "osl/effect_util/effectUtil.h"
#include "osl/neighboring8.h"
#include "osl/stat/ratio.h"
#include <boost/foreach.hpp>
template <osl::Player P, bool SetPieces, bool HasGuide>
const osl::checkmate::ProofDisproof
osl::checkmate::FixedDepthSearcher2::
attackMayUnsafe(int depth, Move& best_move, PieceStand& proof_pieces)
{
NumEffectState* state= &states[depth];
assert(state->turn() == P);
const Square target_king
= state->template kingSquare<PlayerTraits<P>::opponent>();
if (state->hasEffectAt<P>(target_king))
return ProofDisproof::NoEscape();
return attack<P,SetPieces,HasGuide>(depth, best_move, proof_pieces);
}
template <osl::Player P, bool SetPieces, bool HasGuide>
const osl::checkmate::ProofDisproof
osl::checkmate::FixedDepthSearcher2::
attack(int depth, Move& best_move, PieceStand& proof_pieces)
{
NumEffectState* state= &states[depth];
assert(state->turn() == P);
assert ((! HasGuide)
|| (state->isAlmostValidMove(best_move)
&& move_classifier::
Check<P>::isMember(*state, best_move.ptype(), best_move.from(),
best_move.to())));
addCount();
const Square target_king
= state->template kingSquare<PlayerTraits<P>::opponent>();
assert(! state->hasEffectAt<P>(target_king));
const King8Info info(state->Iking8Info(alt(P)));
if ((! state->inCheck())
&& ImmediateCheckmate::hasCheckmateMove<P>(*state, info, target_king,
best_move))
{
if (SetPieces)
{
proof_pieces = PieceStand();
if (best_move.isDrop())
proof_pieces.add(best_move.ptype());
}
return ProofDisproof::Checkmate();
}
if (depth <= 0)
{
return Proof_Number_Table.attackEstimation(*state, P, info, target_king);
}
ProofDisproof pdp;
int minProof = ProofDisproof::PROOF_MAX;
int sumDisproof=0;
if (HasGuide)
{
states[depth-1].copyFrom(states[depth]);
states[depth-1].makeMove(best_move);
pdp=defense<P,SetPieces>(best_move,depth-1,proof_pieces);
if (pdp.isCheckmateSuccess())
{
if (SetPieces)
proof_pieces = ProofPieces::attack(proof_pieces, best_move, PieceStand(P,*state));
return ProofDisproof::Checkmate();
}
minProof = pdp.proof();
sumDisproof += pdp.disproof();
}
const Square targetKing
= state->template kingSquare<PlayerTraits<P>::opponent>();
MoveVector moves;
bool has_pawn_checkmate=false;
move_generator::GenerateAddEffectWithEffect::generate<true>
(P,*state,targetKing,moves,has_pawn_checkmate);
if (moves.size()==0){
if(has_pawn_checkmate)
return ProofDisproof::PawnCheckmate();
else
return ProofDisproof::NoCheckmate();
}
if(has_pawn_checkmate)
minProof=std::min(minProof,(int)ProofDisproof::PAWN_CHECK_MATE_PROOF);
BOOST_FOREACH(Move move, moves) {
if (HasGuide && move == best_move)
continue;
states[depth-1].copyFrom(states[depth]);
states[depth-1].makeMove(move);
pdp=defense<P,SetPieces>(move,depth-1,proof_pieces);
int proof=pdp.proof();
if (proof<minProof){
if (proof==0){
best_move=move;
if (SetPieces)
{
proof_pieces = ProofPieces::attack(proof_pieces, best_move, PieceStand(P,*state));
}
return ProofDisproof::Checkmate();
}
minProof=proof;
}
sumDisproof+=pdp.disproof();
}
// depth >= 3 では PawnCheckmateの際にunpromoteを試す必要あり
return ProofDisproof(minProof,sumDisproof);
}
template <osl::Player P, bool SetPieces>
inline
const osl::checkmate::ProofDisproof
osl::checkmate::FixedDepthSearcher2::
defenseEstimation(int depth,Move last_move, PieceStand& proof_pieces,
Piece attacker_piece, Square target_position) const
{
const NumEffectState* state= &states[depth];
assert(state->turn() == alt(P));
const Player Opponent = PlayerTraits<P>::opponent;
int count=King8Info(state->Iking8Info(Opponent)).libertyCount();
// multiple checkなので,pawn dropではない
if (attacker_piece.isEmpty())
{
if (count>0){
return ProofDisproof(count,1);
}
return ProofDisproof::NoEscape();
}
const Square attack_from=attacker_piece.square();
count += state->countEffect(alt(P), attack_from);
if (Neighboring8::isNeighboring8(attack_from, target_position))
--count;
const EffectContent effect
= Ptype_Table.getEffect(attacker_piece.ptypeO(),
attack_from, target_position);
if (! effect.hasUnblockableEffect())
{
// this is better approximation than naive enumeration of blocking moves,
// for counting of disproof number in df-pn,
++count;
}
if (count==0){
if (last_move.isValid() && last_move.isDrop() && last_move.ptype()==PAWN)
return ProofDisproof::PawnCheckmate();
if (SetPieces)
{
proof_pieces = ProofPieces::leaf(*state, P, PieceStand(P,*state));
}
return ProofDisproof::NoEscape();
}
return ProofDisproof(count, 1);
}
template <osl::Player Defense>
void osl::checkmate::FixedDepthSearcher2::
generateBlockingWhenLiberty0(int depth,Piece defense_king, Square attack_from,
MoveVector& moves) const
{
const NumEffectState* state= &states[depth];
assert(state->kingPiece(Defense) == defense_king);
using namespace move_generator;
using namespace move_action;
MoveVector all_moves;
{
Store store(all_moves);
Escape<Store>::
generateBlockingKing<Defense,false>(*state, defense_king, attack_from,store);
}
BOOST_FOREACH(Move move, all_moves)
{
if (move.isDrop())
{
if (! state->hasEffectAt<Defense>(move.to()))
continue;
}
else
{
// move
if (! Neighboring8::isNeighboring8(move.from(), defense_king.square()))
{
if (! state->hasMultipleEffectAt(Defense, move.to()))
continue;
}
}
moves.push_back(move);
}
}
template <osl::Player Defense> inline
int osl::checkmate::FixedDepthSearcher2::
blockEstimation(Square /*attack_from*/, Square /*defense_king*/) const
{
// 利きのあるマスを数えようと思ったら効果がなかった
return 1;
}
template <osl::Player P, bool SetPieces>
const osl::checkmate::ProofDisproof
osl::checkmate::FixedDepthSearcher2::
defense(Move last_move, int depth, PieceStand& proof_pieces)
{
NumEffectState* state= &states[depth];
assert(state->turn() == alt(P));
addCount();
const Player Defense = PlayerTraits<P>::opponent;
const Square attackerKing
= state->template kingSquare<P>();
/**
* 直前の攻め方の手が自殺手
*/
if (attackerKing.isOnBoard() && state->hasEffectAt<Defense>(attackerKing))
return ProofDisproof::NoCheckmate();
const Piece target_king
= state->template kingPiece<Defense>();
const Square target_position = target_king.square();
assert(state->hasEffectAt<P>(target_position));
Piece attacker_piece;
state->template findCheckPiece<Defense>(attacker_piece);
if (depth <= 0)
{
return defenseEstimation<P, SetPieces>
(depth,last_move, proof_pieces, attacker_piece, target_position);
}
assert(depth > 0);
MoveVector moves;
bool blockable_check = false;
int nonblock_moves;
{
using namespace move_generator;
using namespace move_action;
if (attacker_piece.isEmpty()) {
{
move_action::Store store(moves);
Escape<Store>::template
generateEscape<Defense,KING>(*state,target_king,store);
}
nonblock_moves = moves.size();
}
else {
const Square attack_from=attacker_piece.square();
{
move_action::Store store(moves);
Escape<Store>::
generateCaptureKing<Defense>(*state, target_king, attack_from,store);
}
const int num_captures = moves.size();
{
move_action::Store store(moves);
Escape<Store>::template
generateEscape<Defense,KING>(*state, target_king, store);
}
nonblock_moves = moves.size();
blockable_check =
! effect_util::UnblockableCheck::isMember(alt(P), *state);
if ((depth <= 1) && num_captures && (nonblock_moves > 2))
{
if (nonblock_moves > 3)
{
const int block_estimate = blockable_check
? blockEstimation<Defense>(attack_from, target_position)
: 0;
return ProofDisproof(nonblock_moves + block_estimate, 1);
}
}
if (moves.empty())
generateBlockingWhenLiberty0<Defense>(depth,target_king, attack_from, moves);
}
}
if (moves.empty()) {
if (last_move.isValid() && last_move.isDrop() && last_move.ptype()==PAWN)
return ProofDisproof::PawnCheckmate();
if (SetPieces)
{
proof_pieces = ProofPieces::leaf(*state, P, PieceStand(P,*state));
}
return ProofDisproof::NoEscape();
}
const bool cut_candidate = (depth <= 1)
&& (nonblock_moves - (state->hasPieceOnStand<GOLD>(P)
|| state->hasPieceOnStand<SILVER>(P)) > 4);
if (cut_candidate)
return ProofDisproof(nonblock_moves, 1);
if (SetPieces)
proof_pieces = PieceStand();
PieceStand child_proof;
ProofDisproof pdp;
int minDisproof = ProofDisproof::DISPROOF_MAX;
int sumProof = 0;
size_t i=0;
start_examine:
for (;i<moves.size();i++){
states[depth-1].copyFrom(states[depth]);
states[depth-1].makeMove(moves[i]);
pdp=attack<P,SetPieces,false>(depth-1, moves[i], child_proof);
const int disproof=pdp.disproof();
if (disproof<minDisproof){
if (disproof==0)
{
return pdp; // maybe PawnCheckmate
}
minDisproof=disproof;
}
sumProof+=pdp.proof();
if (sumProof == 0)
{
if (SetPieces)
proof_pieces = proof_pieces.max(child_proof);
}
else
{
if (i+1 < moves.size())
{
minDisproof = 1;
if ((int)i < nonblock_moves)
sumProof += nonblock_moves - (i+1);
if (blockable_check)
++sumProof;
}
return ProofDisproof(sumProof,minDisproof);
}
}
if ((sumProof == 0) && blockable_check
&& ((int)moves.size() == nonblock_moves))
{
using namespace move_generator;
using namespace move_action;
const Square attack_from=attacker_piece.square();
{
move_action::Store store(moves);
Escape<Store>::
generateBlockingKing<Defense,false>(*state, target_king, attack_from,store);
}
if ((int)moves.size() > nonblock_moves)
goto start_examine;
}
if (SetPieces && (sumProof == 0) && blockable_check)
{
ProofPiecesUtil::addMonopolizedPieces(*state, P, PieceStand(P,*state), proof_pieces);
}
// depth >= 2 では unprmote も試す必要あり
return ProofDisproof(sumProof,minDisproof);
}
template <osl::Player P>
const osl::checkmate::ProofDisproof
osl::checkmate::FixedDepthSearcher2::
hasEscapeByMove(Move next_move, int depth, Move& check_move,
PieceStand& proof_pieces)
{
proof_pieces = PieceStand();
ProofDisproof pdp;
states[depth-1].copyFrom(states[depth]);
states[depth-1].makeMove(next_move);
pdp=attackMayUnsafe<P,true,false>(depth-1, check_move, proof_pieces);
return pdp;
}
template <osl::Player P>
const osl::checkmate::ProofDisproof
osl::checkmate::FixedDepthSearcher2::
hasEscapeByMove(Move next_move, int depth)
{
PieceStand proof_pieces;
ProofDisproof pdp;
states[depth-1].copyFrom(states[depth]);
states[depth-1].makeMove(next_move);
pdp=attack<P,false,false>(depth-1, next_move, proof_pieces);
return pdp;
}
template <osl::Player P>
const osl::checkmate::ProofDisproof
osl::checkmate::FixedDepthSearcher2::
hasCheckmateWithGuide(int depth, Move& guide, PieceStand& proof_pieces)
{
NumEffectState* state= &states[depth];
assert(guide.isNormal());
if (! guide.isDrop())
{
const Piece p=state->pieceOnBoard(guide.to());
if (!p.isPtype<KING>())
guide=guide.newCapture(p);
}
if (state->template isAlmostValidMove<false>(guide)
&& move_classifier::Check<P>
::isMember(*state, guide.ptype(), guide.from(), guide.to()))
return attack<P,true,true>(depth, guide, proof_pieces);
return attack<P,true,false>(depth, guide, proof_pieces);
}
#endif /* OSL_CHECKMATE_FIXED_DEPTH_SERCHER2_TCC */
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
|