| 1 |
/* checkMoveGenerator.tcc |
| 2 |
*/ |
| 3 |
#ifndef _CHECKMOVEGENERATOR_TCC |
| 4 |
#define _CHECKMOVEGENERATOR_TCC |
| 5 |
|
| 6 |
#include "checkMoveGenerator.h" |
| 7 |
#include "checkMoveList.h" |
| 8 |
#include "checkMoveListProvider.h" |
| 9 |
#include "checkAssert.h" |
| 10 |
#include "safeFilter.h" |
| 11 |
#include "checkFilter.h" |
| 12 |
#include "osl/checkmate/pawnCheckmateMoves.h" |
| 13 |
#include "osl/move_generator/escape_.h" |
| 14 |
#include "osl/move_generator/addEffectWithEffect.h" |
| 15 |
#include "osl/move_action/store.h" |
| 16 |
#include "osl/move_classifier/check_.h" |
| 17 |
#include "osl/move_classifier/moveAdaptor.h" |
| 18 |
#include "osl/move_classifier/pawnDropCheckmate.h" |
| 19 |
#include "osl/container/moveVector.h" |
| 20 |
#include <cassert> |
| 21 |
|
| 22 |
// TODO: 専用の move_action を作って高速化する |
| 23 |
|
| 24 |
template <osl::Player P> |
| 25 |
void osl::checkmate::CheckMoveGenerator<P>:: |
| 26 |
generateAttack(const NumEffectState& state, CheckMoveListProvider& src, |
| 27 |
CheckMoveList& out, bool& has_pawn_checkmate) |
| 28 |
{ |
| 29 |
has_pawn_checkmate = false; |
| 30 |
using namespace move_classifier; |
| 31 |
check_assert(out.empty()); |
| 32 |
const Square targetKing |
| 33 |
= state.template kingSquare<PlayerTraits<P>::opponent>(); |
| 34 |
const bool counter_check = state.inCheck(P); |
| 35 |
MoveVector moves; |
| 36 |
{ |
| 37 |
if (counter_check) |
| 38 |
{ |
| 39 |
#if 0 |
| 40 |
// 速いと思われるがコードサイズは1割増える |
| 41 |
{ |
| 42 |
move_action::Store store(moves); |
| 43 |
typedef move_action::CheckFilter<P,move_action::Store> check_filter_t; |
| 44 |
check_filter_t checkFilter(state,store); |
| 45 |
move_generator::Escape<P,check_filter_t>:: |
| 46 |
generateKingEscape(state, last_move, checkFilter); |
| 47 |
} |
| 48 |
#else |
| 49 |
MoveVector escape; |
| 50 |
move_generator::GenerateEscape<P>:: |
| 51 |
generateKingEscape(state, escape); |
| 52 |
for (size_t i=0; i<escape.size(); ++i) |
| 53 |
{ |
| 54 |
if (MoveAdaptor<Check<P> >::isMember(state, escape[i])) |
| 55 |
moves.push_back(escape[i]); |
| 56 |
} |
| 57 |
#endif |
| 58 |
} |
| 59 |
else |
| 60 |
{ |
| 61 |
move_action::Store store(moves); |
| 62 |
move_generator::AddEffectWithEffect<move_action::Store> |
| 63 |
::generate<P,true> |
| 64 |
(state,targetKing,store,has_pawn_checkmate); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
out.setSize(moves.size(), src); |
| 69 |
for (size_t i=0; i<moves.size(); ++i) |
| 70 |
{ |
| 71 |
CheckMove new_move(moves[i]); |
| 72 |
const Square from = moves[i].from(); |
| 73 |
if (! from.isPieceStand()) |
| 74 |
{ |
| 75 |
const Ptype ptype = moves[i].ptype(); |
| 76 |
const Square to = moves[i].to(); |
| 77 |
// 自殺手はfilter済み |
| 78 |
#ifdef PAWN_CHECKMATE_SENSITIVE |
| 79 |
if (PawnCheckmateMoves::effectiveOnlyIfPawnCheckmate<P>(ptype, from, to)) |
| 80 |
{ |
| 81 |
new_move.flags = MoveFlags::NoPromote; |
| 82 |
} |
| 83 |
#endif |
| 84 |
} |
| 85 |
#ifdef DELAY_SACRIFICE |
| 86 |
const Square to = moves[i].to(); |
| 87 |
const Ptype capturePtype = moves[i].capturePtype(); |
| 88 |
if (capturePtype == PTYPE_EMPTY) |
| 89 |
{ |
| 90 |
const int defense = state.countEffect(alt(P),to); |
| 91 |
int offense = state.countEffect(P,to); |
| 92 |
if (from.isPieceStand()) |
| 93 |
--offense; |
| 94 |
if (defense > offense) |
| 95 |
{ |
| 96 |
new_move.flags.set(MoveFlags::SacrificeAttack); |
| 97 |
} |
| 98 |
} |
| 99 |
#endif |
| 100 |
check_assert(std::find(moves.begin(), moves.begin()+=i, moves[i]) |
| 101 |
== (moves.begin()+=i)); // unique moves |
| 102 |
out[i] = new_move; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
namespace osl |
| 107 |
{ |
| 108 |
namespace checkmate |
| 109 |
{ |
| 110 |
Ptype getCheapestDrop(Player turn, const NumEffectState& state) |
| 111 |
{ |
| 112 |
if (state.hasPieceOnStand<PAWN>(turn)) |
| 113 |
return PAWN; |
| 114 |
if (state.hasPieceOnStand<LANCE>(turn)) |
| 115 |
return LANCE; |
| 116 |
if (state.hasPieceOnStand<KNIGHT>(turn)) |
| 117 |
return KNIGHT; |
| 118 |
if (state.hasPieceOnStand<SILVER>(turn)) |
| 119 |
return SILVER; |
| 120 |
if (state.hasPieceOnStand<GOLD>(turn)) |
| 121 |
return GOLD; |
| 122 |
if (state.hasPieceOnStand<BISHOP>(turn)) |
| 123 |
return BISHOP; |
| 124 |
if (state.hasPieceOnStand<ROOK>(turn)) |
| 125 |
return ROOK; |
| 126 |
return PTYPE_EMPTY; |
| 127 |
} |
| 128 |
Ptype getSecondCheapestDrop(Player turn, const NumEffectState& state, |
| 129 |
Ptype cheapest) |
| 130 |
{ |
| 131 |
if (cheapest == PAWN) |
| 132 |
{ |
| 133 |
if (state.hasPieceOnStand<LANCE>(turn)) |
| 134 |
return LANCE; |
| 135 |
if (state.hasPieceOnStand<KNIGHT>(turn)) |
| 136 |
return KNIGHT; |
| 137 |
if (state.hasPieceOnStand<SILVER>(turn)) |
| 138 |
return SILVER; |
| 139 |
if (state.hasPieceOnStand<GOLD>(turn)) |
| 140 |
return GOLD; |
| 141 |
if (state.hasPieceOnStand<BISHOP>(turn)) |
| 142 |
return BISHOP; |
| 143 |
if (state.hasPieceOnStand<ROOK>(turn)) |
| 144 |
return ROOK; |
| 145 |
} |
| 146 |
return PTYPE_EMPTY; |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
template <osl::Player P> |
| 152 |
unsigned int osl::checkmate::CheckMoveGenerator<P>:: |
| 153 |
generateEscape(const NumEffectState& state, CheckMoveListProvider& src, |
| 154 |
CheckMoveList& out) |
| 155 |
{ |
| 156 |
using namespace move_classifier; |
| 157 |
const Player Defense = PlayerTraits<P>::opponent; |
| 158 |
|
| 159 |
unsigned int simple_king_moves = 0; |
| 160 |
|
| 161 |
MoveVector moves; |
| 162 |
move_generator::GenerateEscape<Defense>:: |
| 163 |
generateKingEscape(state, moves); |
| 164 |
assert(out.empty()); |
| 165 |
out.setSize(moves.size(), src); |
| 166 |
#ifdef DELAY_INTERPOSE |
| 167 |
const Ptype cheapest = getCheapestDrop(alt(P), state); |
| 168 |
const Ptype secondCheapest = getSecondCheapestDrop(alt(P), state, cheapest); |
| 169 |
#endif |
| 170 |
size_t num_output = 0; |
| 171 |
for (size_t i=0; i<moves.size(); ++i) |
| 172 |
{ |
| 173 |
const Move m = moves[i]; |
| 174 |
CheckMove cm(m); |
| 175 |
const Square to = m.to(); |
| 176 |
const Square from = m.from(); |
| 177 |
const Ptype ptype = m.ptype(); |
| 178 |
if (from.isPieceStand()) |
| 179 |
{ |
| 180 |
if (PawnDropCheckmate<Defense>::isMember(state, ptype, from, to)) |
| 181 |
continue; // 受け方の打歩詰め王手 |
| 182 |
#ifdef DELAY_INTERPOSE |
| 183 |
if (((ptype != cheapest) |
| 184 |
&& (ptype != secondCheapest)) |
| 185 |
|| ((! state.hasEffectAt(alt(P), to)) // 自分の利きがない |
| 186 |
&& (! state.hasMultipleEffectAt(P, to)))) // 焦点でもない |
| 187 |
cm.flags = MoveFlags::BlockingBySacrifice; |
| 188 |
#endif |
| 189 |
} |
| 190 |
else |
| 191 |
{ |
| 192 |
assert(move_classifier::SafeMove<PlayerTraits<P>::opponent> |
| 193 |
::isMember(state, ptype, from, to)); |
| 194 |
#ifdef DELAY_INTERPOSE |
| 195 |
if ((ptype != KING) |
| 196 |
&& (m.capturePtype() == PTYPE_EMPTY) |
| 197 |
&& (! state.hasMultipleEffectAt(alt(P), to))) |
| 198 |
{ |
| 199 |
// 駒を動かす中合 |
| 200 |
cm.flags = MoveFlags::BlockingBySacrifice; |
| 201 |
} |
| 202 |
#endif |
| 203 |
if (ptype == KING && m.capturePtype() == PTYPE_EMPTY) |
| 204 |
++simple_king_moves; |
| 205 |
} |
| 206 |
out[num_output++] = cm; |
| 207 |
} |
| 208 |
out.shrinkSize(num_output); |
| 209 |
return simple_king_moves; |
| 210 |
} |
| 211 |
|
| 212 |
#endif /* _CHECKMOVEGENERATOR_TCC */ |
| 213 |
// ;;; Local Variables: |
| 214 |
// ;;; mode:c++ |
| 215 |
// ;;; c-basic-offset:2 |
| 216 |
// ;;; End: |