king8Info.cc
Go to the documentation of this file.
/* king8Info.cc
*/
#include "osl/bits/king8Info.h"
#include "osl/numEffectState.h"
#include "osl/additionalEffect.h"
#include <bitset>
#include <iostream>
#ifndef MINIMAL
std::ostream& osl::checkmate::operator<<(std::ostream& os, King8Info info)
{
typedef std::bitset<8> bs_t;
os << bs_t(info.moveCandidate2()) << " "
<< bs_t(info.libertyCandidate()) << " "
<< bs_t(info.liberty()) << " "
<< bs_t(info.dropCandidate());
return os;
}
#endif
namespace osl
{
namespace
{
template<Player P> inline
bool
#ifdef __GNUC__
__attribute__ ((pure))
#endif
hasEnoughEffect(NumEffectState const& state,Square target,Square pos, const PieceMask& pinned,
const PieceMask& on_board_defense,
Direction dir)
{
assert(state.kingSquare(P)==target);
assert(pos.isOnBoard());
PieceMask pieceMask = state.effectSetAt(pos)&on_board_defense;
if(pieceMask.none()) return false;
PieceMask pieceMask1=pieceMask&~pinned;
if(pieceMask1.any()) return true;
pieceMask&=pinned;
assert(pieceMask.any());
do {
int num=pieceMask.takeOneBit();
Piece p=state.pieceOf(num);
assert(p.isOnBoardByOwner(P));
Square pos1=p.square();
assert(Board_Table.getShortOffset(Offset32(pos,target))
== pos-target);
Direction dir1=Board_Table.getShort8<P>(target,pos1);
if(dir1==dir) return true;
} while(pieceMask.any());
return false;
}
}
}
template<osl::Player P,osl::Direction Dir>
uint64_t osl::checkmate::
King8Info::hasEffectMask(NumEffectState const& state,Square target, PieceMask pinned,
PieceMask on_board_defense)
{
const Player altP=alt(P);
Square pos=target-DirectionPlayerTraits<Dir,P>::offset();
Piece p=state.pieceAt(pos);
if(p.isEdge())
return 0ull;
if(!state.hasEffectAt(P,pos)){
if(p.canMoveOn<altP>()){ // 自分の駒か空白
if(p.isEmpty())
return 0x1000000000000ull+(0x100010100ull<<static_cast<int>(Dir));
else
return 0x1000000000000ull+(0x10100ull<<static_cast<int>(Dir));
}
else // 相手の駒
return 0ull;
}
const bool has_enough_effect = hasEnoughEffect<altP>(state,target,pos,pinned,on_board_defense,Dir);
if(has_enough_effect){
if(p.canMoveOn<altP>()){
if(p.isEmpty())
return 0x10100010000ull<<static_cast<int>(Dir);
else
return 0x10000ull<<static_cast<int>(Dir);
}
else
return 0x10000000000ull<<static_cast<int>(Dir);
}
else{
if(p.isEmpty())
return 0x10101010001ull<<static_cast<int>(Dir);
else if(p.isOnBoardByOwner<P>())
return 0x10000ull<<static_cast<int>(Dir);
else
return 0x10001000000ull<<static_cast<int>(Dir);
}
}
template<osl::Player P>
const osl::checkmate::King8Info
#if (defined __GNUC__) && (! defined GPSONE) && (! defined GPSUSIONE)
__attribute__ ((noinline))
#endif
osl::checkmate::King8Info::make(NumEffectState const& state,Square target, PieceMask pinned)
{
PieceMask on_board_defense=state.piecesOnBoard(alt(P));
on_board_defense.reset(KingTraits<alt(P)>::index);
uint64_t canMoveMask=
hasEffectMask<P,UR>(state,target,pinned,on_board_defense)+
hasEffectMask<P,R>(state,target,pinned,on_board_defense)+
hasEffectMask<P,DR>(state,target,pinned,on_board_defense)+
hasEffectMask<P,U>(state,target,pinned,on_board_defense)+
hasEffectMask<P,D>(state,target,pinned,on_board_defense)+
hasEffectMask<P,UL>(state,target,pinned,on_board_defense)+
hasEffectMask<P,L>(state,target,pinned,on_board_defense)+
hasEffectMask<P,DL>(state,target,pinned,on_board_defense);
mask_t longMask=state.longEffectAt(target,P);
while(longMask.any()){
int num=longMask.takeOneBit()+PtypeFuns<LANCE>::indexNum*32;
Piece attacker=state.pieceOf(num);
Direction d=
Board_Table.getShort8<P>(target,attacker.square());
if((canMoveMask&(0x100<<d))!=0){
canMoveMask-=((0x100<<d)+0x1000000000000ull);
}
}
return King8Info(canMoveMask);
}
template <osl::Player P>
const osl::checkmate::King8Info
#if (defined __GNUC__) && (! defined GPSONE) && (! defined GPSUSIONE)
__attribute__ ((noinline))
#endif
osl::checkmate::
King8Info::make(NumEffectState const& state, Square target)
{
return make<P>(state,target,state.pin(alt(P)));
}
const osl::checkmate::King8Info osl::checkmate::
King8Info::make(Player attack, NumEffectState const& state)
{
const Square king=state.kingSquare(alt(attack));
if (attack == BLACK)
return make<BLACK>(state, king);
else
return make<WHITE>(state, king);
}
const osl::checkmate::King8Info osl::checkmate::
King8Info::makeWithPin(Player attack, NumEffectState const& state,
const PieceMask& pins)
{
const Square king=state.kingSquare(alt(attack));
if (attack == BLACK)
return make<BLACK>(state, king, pins);
else
return make<WHITE>(state, king, pins);
}
/* ------------------------------------------------------------------------- */
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
additionalEffect.h
osl::NumEffectState
利きを持つ局面
Definition numEffectState.h:34
osl::NumEffectState::effectSetAt
const NumBitmapEffect effectSetAt(Square sq) const
Definition numEffectState.h:258
osl::NumEffectState::hasEffectAt
bool hasEffectAt(Square target) const
対象とするマスにあるプレイヤーの利きがあるかどうか.
Definition numEffectState.h:324
osl::NumEffectState::pin
const PieceMask pin(Player king) const
Definition numEffectState.h:65
osl::NumEffectState::longEffectAt
const mask_t longEffectAt(Square target) const
Definition numEffectState.h:298
osl::NumEffectState::piecesOnBoard
const PieceMask & piecesOnBoard(Player p) const
Definition numEffectState.h:63
osl::PieceMask
駒番号のビットセット.
Definition pieceMask.h:21
osl::PieceMask::any
bool any() const
Definition pieceMask.h:57
osl::PieceMask::reset
void reset(int num)
Definition pieceMask.h:54
osl::Piece
駒.
Definition basic_type.h:788
osl::Piece::square
const Square square() const
Definition basic_type.h:832
osl::Piece::isEmpty
bool isEmpty() const
Definition basic_type.h:913
osl::Piece::isEdge
bool isEdge() const
Definition basic_type.h:919
osl::Piece::canMoveOn
bool canMoveOn() const
Player Pの駒が，thisの上に移動できるか?
Definition basic_type.h:980
osl::Piece::isOnBoardByOwner
bool isOnBoardByOwner() const
piece がプレイヤーPの持ち物でかつボード上にある駒の場合は true.
Definition basic_type.h:852
osl::SimpleState::pieceOf
const Piece pieceOf(int num) const
Definition simpleState.h:76
osl::SimpleState::kingSquare
Square kingSquare() const
Definition simpleState.h:94
osl::SimpleState::pieceAt
const Piece pieceAt(Square sq) const
Definition simpleState.h:167
osl::Square
Definition basic_type.h:532
osl::Square::isOnBoard
bool isOnBoard() const
盤面上を表すかどうかの判定． 1<=x() && x()<=9 && 1<=y() && y()<=9 Squareの内部表現に依存する．
Definition basic_type.h:583
osl::checkmate::King8Info
敵玉の8近傍の状態を表す.
Definition king8Info.h:29
osl::checkmate::King8Info::libertyCandidate
unsigned int libertyCandidate() const
16-23 bit 目を 0-7bitにshiftして返す
Definition king8Info.h:64
osl::checkmate::King8Info::liberty
unsigned int liberty() const
8-15 bit 目を 0-7bitにshiftして返す
Definition king8Info.h:54
osl::checkmate::King8Info::moveCandidate2
unsigned int moveCandidate2() const
24-31 bit 目を 0-7bitにshiftして返す
Definition king8Info.h:69
osl::checkmate::King8Info::makeWithPin
static const King8Info makeWithPin(Player attack, NumEffectState const &state, const PieceMask &pinned)
alt(attack) のking について計算.
Definition king8Info.cc:159
osl::checkmate::King8Info::King8Info
King8Info(uint64_t v)
Definition king8Info.h:32
osl::checkmate::King8Info::dropCandidate
unsigned int dropCandidate() const
0-7 bit 目を返す
Definition king8Info.h:49
osl::checkmate::King8Info::make
static const King8Info make(NumEffectState const &state, Square king, PieceMask pinned)
osl::checkmate::King8Info::hasEffectMask
static uint64_t hasEffectMask(NumEffectState const &state, Square target, PieceMask pinned, PieceMask on_board_defense)
alt(P)の玉にDirの方向で迫るcanMoveMaskを計算する.
Definition king8Info.cc:67
osl::container::PieceMask64::none
bool none() const
Definition pieceMask64.h:57
osl::container::PieceMask64::takeOneBit
int takeOneBit()
Definition pieceMask64.h:82
osl::misc::GeneralMask::any
bool any() const
Definition mask.h:298
osl::misc::GeneralMask::takeOneBit
int takeOneBit()
non-zeroのmaskのsetされているビットをLSBから探し，その番号を返す 副作用としてmaskの対応するビットをクリアする
Definition mask.h:265
king8Info.h
osl::checkmate::operator<<
std::ostream & operator<<(std::ostream &, King8Info)
Definition king8Info.cc:10
osl::misc::mask_t
GeneralMask< mask_int_t > mask_t
Definition mask.h:351
osl
Definition additionalEffect.h:6
osl::Board_Table
const BoardTable Board_Table
Definition tables.cc:95
osl::Direction
Direction
Definition basic_type.h:310
osl::Offset32
Offset32Base< 8, 9 > Offset32
Definition offset32.h:63
osl::Player
Player
Definition basic_type.h:8
osl::BLACK
@ BLACK
Definition basic_type.h:9
osl::__attribute__
const PtypeO PTYPEO_EDGE __attribute__((unused))
osl::alt
constexpr Player alt(Player player)
Definition basic_type.h:13
numEffectState.h
osl::DirectionPlayerTraits
Definition directionTraits.h:242
osl::KingTraits
Definition ptypeTraits.h:357
osl::PtypeFuns
Definition ptypeTraits.h:311