osl¶
Note
miniosl is designed to be used from Python. C++ interface is supposed to be used in very exceptional cases.
Basic datatype¶
see basic-type.h
-
enum class osl::Player¶
Values:
-
enumerator BLACK = 0¶
the first player
-
enumerator WHITE = -1¶
the second player
-
enumerator BLACK = 0¶
-
constexpr Player osl::alt(Player player)¶
return the opponent player of
player
assert(alt(osl::BLACK) == osl::WHITE); assert(alt(osl::WHITE) == osl::BLACK);
-
constexpr int osl::idx(Player player)¶
return index of
player
assert(alt(osl::BLACK) == 0); assert(alt(osl::WHITE) == 1);
-
constexpr int osl::sign(Player player)¶
return sign of
player
for minimax searchassert(alt(osl::BLACK) == 1); assert(alt(osl::WHITE) == -1);
-
enum class osl::Ptype¶
駒の種類の4ビット表現
Values:
-
enumerator Ptype_EMPTY = 0¶
-
enumerator Ptype_EDGE = 1¶
-
enumerator PPAWN = 2¶
-
enumerator PLANCE = 3¶
-
enumerator PKNIGHT = 4¶
-
enumerator PSILVER = 5¶
-
enumerator PBISHOP = 6¶
-
enumerator PROOK = 7¶
-
enumerator KING = 8¶
-
enumerator GOLD = 9¶
-
enumerator PAWN = 10¶
-
enumerator LANCE = 11¶
-
enumerator KNIGHT = 12¶
-
enumerator SILVER = 13¶
-
enumerator BISHOP = 14¶
-
enumerator ROOK = 15¶
-
enumerator Ptype_EMPTY = 0¶
-
constexpr bool osl::is_basic(Ptype ptype)¶
ptypeが基本型 (promoteしていない)
assert(is_basic(osl::ROOK)); assert(! is_basic(osl::PROOK));
-
constexpr Ptype osl::unpromote(Ptype ptype)¶
promote前の型.
promoteしていない型の時はそのまま
assert(unpromote(osl::PROOK) == osl::ROOK); assert(unpromote(osl::ROOK) == osl::ROOK);
-
enum class osl::Direction¶
Direction.
steps achievable by a legal move only, i.e., no DDL or DDR. still, subtraction (sq - to_offset(UUL)) yields inverse step.
Values:
-
enumerator UL = 0¶
-
enumerator U = 1¶
-
enumerator UR = 2¶
-
enumerator L = 3¶
-
enumerator R = 4¶
-
enumerator DL = 5¶
-
enumerator D = 6¶
-
enumerator DR = 7¶
-
enumerator UUL = 8¶
-
enumerator UUR = 9¶
-
enumerator Long_UL = 10¶
-
enumerator Long_U = 11¶
-
enumerator Long_UR = 12¶
-
enumerator Long_L = 13¶
-
enumerator Long_R = 14¶
-
enumerator Long_DL = 15¶
-
enumerator Long_D = 16¶
-
enumerator Long_DR = 17¶
-
enumerator UL = 0¶
-
constexpr bool osl::is_base8(Direction d)¶
test
d
is eight neighborsassert(is_base8(osl::U)); assert(! is_base8(osl::UUL)); assert(! is_base8(osl::LONG_U));
-
constexpr bool osl::is_basic(Direction d)¶
test
d
presents a direct move direction (not unblockable)assert(is_base8(osl::U)); assert(is_base8(osl::UUL)); assert(! is_base8(osl::LONG_U));
-
class Square¶
location in a board with valid ranges in between [1,1] and [9,9], and with some invalid ranges outside the board for sentinels and piece stand.
Public Functions
-
inline Square()¶
make a square for piece stand (color is not included)
osl::Square sq; assert(! sq.isOnBoard()); assert(! sq.isPieceStand());
-
inline Square(int x, int y)¶
make a square
osl::Square sq(2, 6); assert(sq.x() == 2); assert(sq.y() == 6); assert(sq.isOnBoard());
- Parameters:
x – [1, 9]
y – [1, 9]
-
inline int x() const¶
将棋としてのX座標を返す.
-
inline int y() const¶
将棋としてのY座標を返す.
-
inline bool isPieceStand() const¶
test this is for piece stand
-
inline bool isOnBoard() const¶
盤面上を表すかどうかの判定. 1<=x() && x()<=9 && 1<=y() && y()<=9 Squareの内部表現に依存する.
-
inline int index81() const¶
return index in [0,80]
-
inline Square()¶
-
class Move¶
圧縮していない moveの表現.
special moves
invalid: isNormal 以外の演算はできない
declare_win: isNormal 以外の演算はできない
pass: from, to, ptype, oldPtype はとれる.player()はとれない.
Pieceとpromotepをそろえる -> 変える. 下位から 2009/12/10から
to : 8 bit
from : 8 bit
capture ptype : 4 bit
reserved : 3 bit
promote? : 1 bit
ptype : 4 bit promote moveの場合はpromote後のもの
owner : signed
Public Functions
-
inline Move(Square from, Square to, Ptype ptype, Ptype capture_ptype, bool is_promote, Player player)¶
make a move on board
-
inline PtypeO ptypeO() const¶
移動後のPtype, i.e., 成る手だった場合成った後
-
inline PtypeO oldPtypeO() const¶
移動前のPtypeO, i.e., 成る手だった場合成る前
-
bool isValid() const¶
test of legality (not sufficient)
-
inline bool isSpecial() const¶
true if
Resigen
orDeclare_WIN
, false forPASS
or usual moves
-
bool is_ordinary_valid() const¶
isNormal() and no violation with the shogi rules
-
class Piece¶
state dependent piece.
State¶
see base-state.h and state.h
-
class BaseState¶
state without piece covers
Subclassed by osl::EffectState
Public Functions
-
inline Piece pieceAt(Square sq) const¶
- Parameters:
sq – は isOnboardを満たす Square の12 近傍(8近傍+桂馬の利き) ! sq.isOnBoard() の場合は
Piece_EDGE
を返す
-
inline void changeTurn()¶
手番を変更する
-
bool move_is_consistent(Move move) const¶
lightweight validation of ordinary moves, primary intended for game record parsing.
See also
EffectState::isAcceptable for piece reachability.
-
inline Piece pieceAt(Square sq) const¶
-
class EffectState : public osl::BaseState¶
Standard state exported as
minioslcc.State
利きを持つ局面
effects (EffectSummary) 利き
pieces_onboard (PieceMask) 盤上にある駒
Public Functions
-
inline PieceMask promotedPieces() const¶
return a set of piece IDs promoted
-
inline bool inCheck() const¶
手番の玉が王手状態
-
bool inCheckmate() const¶
手番の玉が詰み (負け)
-
bool inNoLegalMoves() const¶
another loss condition for turn
-
inline Square pieceReach(Direction black_dir, Piece piece) const¶
return the furthest square of piece p for direction d.
i.e., assert(is_base8(d) && is_long_piece_id(piece.id()));
- Parameters:
piece – must have long move
dir – must be consistent with p’s move
-
inline Square kingVisibilityOfPlayer(Player p, Direction d) const¶
return the furthest square visible p’s King for direction d
- Parameters:
p – 注目する玉のプレイヤ
d – piece からみた向き
-
inline PieceMask effectAt(Player P, Square sq) const¶
return a set of piece IDs of color
P
cover the squaresq
-
inline int countEffect(Player player, Square target, PieceMask pins) const¶
利きの数を数える.
targetが盤をはみ出してはいけない
- Parameters:
pins – この駒の利きは数えない
-
inline bool hasEffectAt(Player player, Square target) const¶
対象とするマスにあるプレイヤーの利きがあるかどうか.
- Parameters:
player – 攻撃側
target – 対象のマス
-
inline bool hasEffectByPiece(Piece attack, Square target) const¶
駒attack が target に利きを持つか
- Parameters:
target – 対象のマス
-
template<Ptype PTYPE>
inline bool hasLongEffectAt(Player P, Square to) const¶ あるマスにPTYPEの長い利きがあるかどうか.
-
template<Player P>
inline bool findCheckPiece(Piece &attack_piece) const¶ 王手駒を探す
- Parameters:
attack_piece – 一つの駒による王手の場合はattck_pieceにその駒を入れる 複数の駒による王手の場合はPiece::EMPTY()を入れる
- Template Parameters:
P(template) – 玉
- Returns:
王手かどうか
-
inline Piece findLongAttackAt(Player owner, int piece, Direction d) const¶
pieceのd方向から長い利きがある場合にその駒を返す。
- Parameters:
d – piece からみた向き
-
bool isLegal(Move move) const¶
test legal move
valide win declaration, or
move.is_ordinary_valid() + isAcceptable() + isSafeMove() + ! isPawnDropCheckmate()
not implemented
evasion in check
repetition of states
-
bool isAcceptable(Move move) const¶
moves accepted by makeMove(), similar but a bit different from isLegal()
allowed: pass, ordinary moves consistent with state (regardless of king safety)
not allowed: win declaration, resign,
-
void generateLegal(MoveVector&) const¶
ほぼ全ての合法手を生成する.
玉の素抜きや打歩詰の確認をする. ただし, 打歩詰め絡み以外では有利にはならない手 (Move::ignoredUnpromote)は生成しない.
-
void generateWithFullUnpromotions(MoveVector&) const¶
打歩詰め絡み以外では有利にはならない手も含め, 全ての合法手を生成す る(Move::ignoredUnpromoteも生成する).
玉の素抜きや打歩詰の確認 をする.
-
void generateCheck(MoveVector &moves) const¶
王手生成
-
void make_move(std::string csa_or_usi)¶
make a move given in string, to update the state
osl::EffectState state; state.make_move("+7776FU");
Game record¶
see also record.h
-
enum osl::GameResult¶
status at the endgame
Values:
-
enumerator BlackWin¶
-
enumerator WhiteWin¶
-
enumerator Draw¶
-
enumerator InGame¶
-
enumerator BlackWin¶
-
constexpr GameResult osl::win_result(Player P)¶
test win for
P
-
constexpr bool osl::has_winner(GameResult r)¶
test whether
BlackWin
orWhiteWin
-
constexpr GameResult osl::flip(GameResult original)¶
return a new status inverting the winner
-
struct MiniRecord¶
A game record.
Public Functions
-
inline int move_size() const¶
numebr of moves
-
inline bool has_winner() const¶
test game is completed
-
inline int repeat_count(int id = 0) const¶
return number of occurrence of the specified state.
- Parameters:
id – = index if positive otherwise rollback from current (the last item), i.e., 0 for current
-
inline int previous_repeat_index(int id = 0) const¶
state (not move) index of repeating, only meaningful if has_repeat_state()
- Parameters:
id – = index if positive otherwise rollback from current (the last item), i.e., 0 for current
-
inline int consecutive_in_check(int id = 0) const¶
number of consecutive in-check states
- Parameters:
id – = index if positive otherwise rollback from current (the last item), i.e., 0 for current
-
void replay(EffectState &state, int idx)¶
set
state
asidx
-th state
Public Members
-
EffectState initial_state¶
initial state
-
std::vector<HashStatus> history¶
history status of moves.size()+1 to detect repetition
-
Move final_move = Move::PASS(BLACK)¶
resign or DeclareWin if game has the winner, PASS in InGame or end by repetition
-
GameResult result = InGame¶
result of the game or
InGame
unless completed
-
inline int move_size() const¶
-
MiniRecord osl::csa::read_record(std::istream &is)¶
read record from csa file
-
inline EffectState osl::csa::read_board(const std::string &str)¶
read state from csa file
-
MiniRecord osl::usi::read_record(std::string line)¶
read usi record
-
EffectState osl::usi::to_state(const std::string &line)¶
read state in usi
-
std::u8string osl::to_ki2(Move move, const EffectState&, Square prev = Square())¶
return japanese representation of
move
-
Move osl::kanji::to_move(std::u8string, const EffectState&, Square last_to = Square())¶
read japanese representation of move
-
struct GameManager¶
run 1:1 game
Public Functions
-
GameResult make_move(Move move)¶
make a move
- Parameters:
move – a usual move
- Returns:
result indicating the game was completed by the move
-
osl::GameResult export_heuristic_feature_after(Move, nn_input_element *ptr) const¶
export features for a state after move
- Parameters:
ptr – must be zero-filled in advance
- Returns:
InGame (usual cases) or a definite result if identified
-
bool export_heuristic_feature_after(Move move, int reply, nn_input_element *ptr) const¶
export features for a state after move
- Parameters:
move – considering move
reply – the opponent’s reply (can be illegal)
ptr – must be zero-filled in advance
- Returns:
InGame (usual cases) or a definite result if identified
Public Members
-
MiniRecord record¶
record
-
HistoryTable table¶
table to detect repetition
-
EffectState state¶
current state
-
MoveVector legal_moves¶
legal moves in current state to detect game ends
-
GameResult make_move(Move move)¶
-
struct SubRecord¶
subset of MiniRecord assuming completed game with the standard initial state
Public Functions
-
void export_feature_labels(int idx, nn_input_element *input, int &move_label, int &value_label, nn_input_element *aux_label, MoveVector &legal_moves) const¶
export features and labels
-
void sample_feature_labels(nn_input_element *input, int &move_label, int &value_label, nn_input_element *aux_label, uint8_t *legalmove_buf = nullptr, int decay = default_decay, TID tid = TID_ZERO) const¶
randomly sample index and call export_feature_labels()
-
void sample_feature_labels_to(int offset, nn_input_element *input_buf, int32_t *policy_buf, float *value_buf, nn_input_element *aux_buf, nn_input_element *input2_buf, uint8_t *legalmove_buf, int decay = default_decay, TID tid = TID_ZERO) const¶
randomly sample index and export features to given pointers (must be zero-filled)
Public Members
-
GameResult result = InGame¶
result of the game or
InGame
if not yet initialized
Public Static Functions
-
static int weighted_sampling(int limit, int N = default_decay, TID tid = TID_ZERO)¶
sample int in range [0, limit-1], with progressive 1/2 weight for the opening moves, 2^{-decay} for initial position
-
void export_feature_labels(int idx, nn_input_element *input, int &move_label, int &value_label, nn_input_element *aux_label, MoveVector &legal_moves) const¶