proofNumberTable.h
Go to the documentation of this file.
/* proofNumberTable.h
*/
#ifndef OSL_CHECKMATE_PROOF_NUMBER_TABLE_H
#define OSL_CHECKMATE_PROOF_NUMBER_TABLE_H
#include "osl/checkmate/proofDisproof.h"
#include "osl/numEffectState.h"
#include "osl/bits/king8Info.h"
#include "osl/additionalEffect.h"
#include "osl/bits/boardTable.h"
namespace osl
{
namespace checkmate
{
class ProofNumberTable
{
public:
struct Liberty
{
uint8_t liberty;
bool has_effect;
explicit Liberty(uint8_t l=0, bool e=false) : liberty(l), has_effect(e)
{
}
};
private:
CArray2d<CArray<Liberty,DIRECTION_SIZE>,0x100u,PTYPE_SIZE> liberties;
CArray2d<uint8_t,0x10000u,8> drop_liberty;
CArray2d<uint8_t,0x100u,0x100u> pmajor_liberty;
CArray2d<uint8_t,0x100u,0x100u> promote_liberty;
CArray2d<uint8_t,0x100u,0x100u> other_move_liberty;
public:
void init();
const Liberty countLiberty(Ptype ptype, Direction d, unsigned int liberty_mask) const
{
assert((d != UUL) && (d != UUR));
assert(liberty_mask <= 0xff);
return liberties[liberty_mask][ptype][d];
}
const Liberty countLibertyShortNotKnight(Player player, Square to, Ptype ptype,
Square king, King8Info info) const
{
assert(to.isNeighboring8(king));
assert(ptype != KNIGHT);
const unsigned int liberty_mask = info.liberty();
const Direction d =
(player == BLACK)
? Board_Table.getShort8<BLACK>(to, king)
: Board_Table.getShort8<WHITE>(to, king);
return countLiberty(ptype, d, liberty_mask);
}
const Liberty countLibertyLong(Player player, Square to, Ptype ptype,
Square king, King8Info info) const
{
assert(! to.isNeighboring8(king));
const unsigned int liberty_mask = info.liberty();
const Offset32 offset32(king,to);
const Offset offset = Board_Table.getShortOffsetNotKnight(offset32);
if (offset.zero())
return Liberty(0, false);
if (to + offset + offset != king) // 2マス以上遠く
{
if (isMajor(ptype))
ptype = unpromote(ptype);
else if (ptype != LANCE)
return Liberty(0, false);
}
const Direction d =
(player == BLACK)
? Board_Table.getLongDirection<BLACK>(offset32)
: Board_Table.getLongDirection<WHITE>(offset32);
assert(isLong(d));
return countLiberty(ptype, d, liberty_mask);
}
int countLiberty(const NumEffectState& state, int liberty_count,
Move move, Square king, King8Info info) const
{
assert(liberty_count == misc::BitOp::countBit(info.liberty()));
const Player attack = move.player();
const Player defense = alt(attack);
const Square to = move.to();
const Ptype ptype = move.ptype();
if (ptype == KNIGHT)
return std::max(1,liberty_count + state.countEffect(defense, to));
const bool neighboring = to.isNeighboring8(king);
Liberty liberty = neighboring
? countLibertyShortNotKnight(attack, to, ptype, king, info)
: countLibertyLong(attack, to, ptype, king, info);
if (liberty.liberty == 0)
return std::max(liberty_count-1,1);
if (! neighboring && liberty.has_effect)
{
// TODO: 詰将棋と協調できるなら liberty.liberty <=
// liberty_count を保つように調整したい，が．
++liberty.liberty; // 合駒の分，もし両王手の場合は本来は不要
}
liberty.liberty += state.countEffect(defense, to);
if (move.isDrop())
{
if (neighboring)
{
if (state.countEffect(attack, to))
--liberty.liberty; // adjust king capture
}
assert(liberty.liberty);
return liberty.liberty;
}
// 移動: 銀のただすてなどは本当は利きをはずしたい
if (neighboring)
{
if (state.countEffect(attack, to) >= 2
|| effect_util::AdditionalEffect::hasEffect(state, to, attack))
--liberty.liberty; // adjust king capture
}
assert(liberty.liberty);
return liberty.liberty;
}
int countLiberty(const NumEffectState& state, Move move) const;
int
#ifdef __GNUC__
__attribute__ ((pure))
#endif
libertyAfterAllDrop(const NumEffectState& state) const;
int
#ifdef __GNUC__
__attribute__ ((pure))
#endif
libertyAfterAllDrop(const NumEffectState& state, Player attack,
King8Info info) const;
int
#ifdef __GNUC__
__attribute__ ((pure))
#endif
libertyAfterAllMove(const NumEffectState& state) const;
int
#ifdef __GNUC__
__attribute__ ((pure))
#endif
libertyAfterAllMove(const NumEffectState& state, Player attack,
King8Info info, Square king) const;
int
#ifdef __GNUC__
__attribute__ ((pure))
#endif
libertyAfterAllCheck(const NumEffectState& state) const;
int
#ifdef __GNUC__
__attribute__ ((pure))
#endif
disproofAfterAllCheck(const NumEffectState&, Player, King8Info) const;
const ProofDisproof
#ifdef __GNUC__
__attribute__ ((pure))
#endif
attackEstimation(const NumEffectState& state) const;
const ProofDisproof
#ifdef __GNUC__
__attribute__ ((pure))
#endif
attackEstimation(const NumEffectState& state,
Player attack,
King8Info info, Square king) const;
};
// tables.ccに入れればconstにできる
extern ProofNumberTable Proof_Number_Table;
class EdgeTable
{
CArray2d<uint64_t, 2, Square::SIZE> edge_mask;
public:
void init();
const King8Info
#ifdef __GNUC__
__attribute__ ((pure))
#endif
resetEdgeFromLiberty(Player king_player, Square king, King8Info info) const
{
uint64_t ret = info.uint64Value();
ret &= edge_mask[king_player][king.index()];
const uint64_t count = misc::BitOp::countBit((ret>>8)&0xffull);
ret |= count << 48;
return King8Info(ret);
}
};
extern EdgeTable Edge_Table;
}
}
#endif /* _CHECKMATE_IMMEDIATE_CHECKMATE_TABLE_H */
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End:
additionalEffect.h
boardTable.h
osl::Move
圧縮していない moveの表現 .
Definition basic_type.h:1052
osl::Move::ptype
Ptype ptype() const
Definition basic_type.h:1155
osl::Move::player
Player player() const
Definition basic_type.h:1195
osl::Move::isDrop
bool isDrop() const
Definition basic_type.h:1150
osl::Move::to
const Square to() const
Definition basic_type.h:1132
osl::NumEffectState
利きを持つ局面
Definition numEffectState.h:34
osl::NumEffectState::countEffect
int countEffect(Player player, Square target) const
利きの数を数える.
Definition numEffectState.h:266
osl::Offset
座標の差分
Definition basic_type.h:430
osl::Offset::zero
bool zero() const
Definition basic_type.h:502
osl::Square
Definition basic_type.h:532
osl::Square::index
unsigned int index() const
Definition basic_type.h:572
osl::Square::isNeighboring8
bool isNeighboring8(Square to) const
Definition basic_type.cc:202
osl::checkmate::EdgeTable
Definition proofNumberTable.h:202
osl::checkmate::EdgeTable::init
void init()
Definition proofNumberTable.cc:373
osl::checkmate::EdgeTable::resetEdgeFromLiberty
const King8Info resetEdgeFromLiberty(Player king_player, Square king, King8Info info) const
liberty から盤の淵(xかyが1か9)を取り除く.
Definition proofNumberTable.h:211
osl::checkmate::EdgeTable::edge_mask
CArray2d< uint64_t, 2, Square::SIZE > edge_mask
Definition proofNumberTable.h:203
osl::checkmate::King8Info
敵玉の8近傍の状態を表す.
Definition king8Info.h:29
osl::checkmate::King8Info::uint64Value
uint64_t uint64Value() const
Definition king8Info.h:46
osl::checkmate::King8Info::liberty
unsigned int liberty() const
8-15 bit 目を 0-7bitにshiftして返す
Definition king8Info.h:54
osl::checkmate::ProofDisproof
証明数(proof number)と反証数(disproof number).
Definition proofDisproof.h:17
osl::checkmate::ProofNumberTable::libertyAfterAllDrop
int libertyAfterAllDrop(const NumEffectState &state) const
drop のみ
Definition proofNumberTable.cc:255
osl::checkmate::ProofNumberTable::attackEstimation
const ProofDisproof attackEstimation(const NumEffectState &state) const
全て
Definition proofNumberTable.cc:357
osl::checkmate::ProofNumberTable::pmajor_liberty
CArray2d< uint8_t, 0x100u, 0x100u > pmajor_liberty
龍や馬で王手をかけられる時のliberty: [liberty][move_mask]
Definition proofNumberTable.h:37
osl::checkmate::ProofNumberTable::init
void init()
Definition proofNumberTable.cc:84
osl::checkmate::ProofNumberTable::disproofAfterAllCheck
int disproofAfterAllCheck(const NumEffectState &, Player, King8Info) const
Definition proofNumberTable.cc:330
osl::checkmate::ProofNumberTable::drop_liberty
CArray2d< uint8_t, 0x10000u, 8 > drop_liberty
全ての有効drop -> 最小liberty.
Definition proofNumberTable.h:35
osl::checkmate::ProofNumberTable::promote_liberty
CArray2d< uint8_t, 0x100u, 0x100u > promote_liberty
王が1,2段目にいる時の移動王手によるliberty: [liberty][move_mask].
Definition proofNumberTable.h:41
osl::checkmate::ProofNumberTable::countLibertyLong
const Liberty countLibertyLong(Player player, Square to, Ptype ptype, Square king, King8Info info) const
Definition proofNumberTable.h:74
osl::checkmate::ProofNumberTable::libertyAfterAllMove
int libertyAfterAllMove(const NumEffectState &state) const
移動 のみ
Definition proofNumberTable.cc:321
osl::checkmate::ProofNumberTable::libertyAfterAllCheck
int libertyAfterAllCheck(const NumEffectState &state) const
全て
Definition proofNumberTable.cc:366
osl::checkmate::ProofNumberTable::liberties
CArray2d< CArray< Liberty, DIRECTION_SIZE >, 0x100u, PTYPE_SIZE > liberties
一つの王手 -> Liberty: long なdirection は1マスあけた王手を意味する
Definition proofNumberTable.h:31
osl::checkmate::ProofNumberTable::other_move_liberty
CArray2d< uint8_t, 0x100u, 0x100u > other_move_liberty
それ以外の移動liberty: [liberty][move_mask]
Definition proofNumberTable.h:43
osl::checkmate::ProofNumberTable::countLiberty
const Liberty countLiberty(Ptype ptype, Direction d, unsigned int liberty_mask) const
dir 方向からの王手をかけた時のlibertyの予想
Definition proofNumberTable.h:50
osl::checkmate::ProofNumberTable::countLiberty
int countLiberty(const NumEffectState &state, int liberty_count, Move move, Square king, King8Info info) const
move は王手である必要がある
Definition proofNumberTable.h:100
osl::checkmate::ProofNumberTable::countLibertyShortNotKnight
const Liberty countLibertyShortNotKnight(Player player, Square to, Ptype ptype, Square king, King8Info info) const
8近傍へのdropまたは取れない移動後のlibertyの予測値を返す.
Definition proofNumberTable.h:62
king8Info.h
ProofNumberTable
Definition proofNumberTable.cc:14
osl::checkmate
Definition king8Info.h:12
osl::checkmate::Edge_Table
EdgeTable Edge_Table
Definition proofNumberTable.cc:11
osl::checkmate::Proof_Number_Table
ProofNumberTable Proof_Number_Table
Definition proofNumberTable.cc:10
osl
Definition additionalEffect.h:6
osl::Ptype
Ptype
駒の種類を4ビットでコード化する
Definition basic_type.h:84
osl::KNIGHT
@ KNIGHT
Definition basic_type.h:97
osl::LANCE
@ LANCE
Definition basic_type.h:96
osl::CArray2d
CArray< CArray< T, Capacity2 >, Capacity1 > CArray2d
Definition container.h:95
osl::PTYPE_SIZE
const int PTYPE_SIZE
Definition basic_type.h:107
osl::unpromote
Ptype unpromote(Ptype ptype)
ptypeがpromote後の型の時に，promote前の型を返す． promoteしていない型の時はそのまま返す
Definition basic_type.h:157
osl::Board_Table
const BoardTable Board_Table
Definition tables.cc:95
osl::Direction
Direction
Definition basic_type.h:310
osl::UUR
@ UUR
Definition basic_type.h:323
osl::UUL
@ UUL
Definition basic_type.h:322
osl::Offset32
Offset32Base< 8, 9 > Offset32
Definition offset32.h:63
osl::Player
Player
Definition basic_type.h:8
osl::WHITE
@ WHITE
Definition basic_type.h:10
osl::BLACK
@ BLACK
Definition basic_type.h:9
osl::__attribute__
const PtypeO PTYPEO_EDGE __attribute__((unused))
osl::isMajor
bool isMajor(Ptype ptype)
Definition basic_type.h:185
osl::isLong
constexpr bool isLong(Direction d)
Definition basic_type.h:350
osl::alt
constexpr Player alt(Player player)
Definition basic_type.h:13
numEffectState.h
proofDisproof.h
osl::checkmate::ProofNumberTable::Liberty
Definition proofNumberTable.h:19
osl::checkmate::ProofNumberTable::Liberty::liberty
uint8_t liberty
玉の自由度の予測値.
Definition proofNumberTable.h:21
osl::checkmate::ProofNumberTable::Liberty::Liberty
Liberty(uint8_t l=0, bool e=false)
Definition proofNumberTable.h:24
osl::checkmate::ProofNumberTable::Liberty::has_effect
bool has_effect
false の場合は必ず空き王手
Definition proofNumberTable.h:23
osl::effect_util::AdditionalEffect::hasEffect
static bool hasEffect(const NumEffectState &, Square target, Player attack)
target に attack の追加利きが一つでもあるか． 相手の影利きが先にある場合は対象としない．
Definition additionalEffect.cc:5
osl::misc::BitOp::countBit
static int countBit(Integer mask)
Definition mask.h:160