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

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 search

assert(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
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
constexpr bool osl::is_base8(Direction d)

test d is eight neighbors

assert(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 Square blackView(Player player) const

後手の場合は盤面を引っくり返す.

inline Square rotate180() const

make a new Square after rotation

inline bool isPromoteArea(Player player) const

test this is promote area for player

inline int index81() const

return index in [0,80]

Public Static Functions

static inline Square from_index81(int n)

make a square of index n in [0,80]

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 bool isNormal() const

Resign でも PASS でもない.

isValid()かどうかは分からない.

inline Move(Square from, Square to, Ptype ptype, Ptype capture_ptype, bool is_promote, Player player)

make a move on board

inline Move(Square to, Ptype ptype, Player player)

make a drop from piece stand

inline PtypeO ptypeO() const

移動後のPtype, i.e., 成る手だった場合成った後

inline PtypeO oldPtypeO() const

移動前のPtypeO, i.e., 成る手だった場合成る前

inline Ptype oldPtype() const

移動前のPtype, i.e., 成る手だった場合成る前

bool isValid() const

test of legality (not sufficient)

inline bool isSpecial() const

true if Resigen or Declare_WIN, false for PASS or usual moves

bool is_ordinary_valid() const

isNormal() and no violation with the shogi rules

inline Move unpromote() const

promote moveからunpromote moveを作る

inline Move promote() const

unpromote moveからpromote moveを作る

inline bool ignoreUnpromote(Player P) const

合法手ではあるが,打歩詰め絡み以外では有利にはならない手.

class Piece

state dependent piece.

Public Functions

inline Ptype ptype() const

type of a piece

inline PtypeO ptypeO() const

type and owner of a piece

inline int id() const

state dependent internal id of a piece

inline Square square() const

location of a piece

inline Piece promote() const

成る.

PROMOTE不可なpieceに適用不可

inline Piece unpromote() const

成りを戻す.

PROMOTE不可なpieceに適用可

inline bool isPromoted() const

promoteした駒かどうかをチェックする

inline Player owner() const

color of a piece

inline bool isOnBoard() const

test is on board

inline bool equalPtyeO(Piece other) const

equality w.r.t.

PtypeO (i.e., ignoring piece id or location)

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 Piece kingPiece(Player P) const

return piece of P’s King

inline const PieceMask &standMask(Player p) const

return a set of piece IDs on stand

inline bool pawnInFile(Player player, int x) const

return whether player’s pawn is on file x

inline Player turn() const

side to move

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 int countPiecesOnStand(Player pl, Ptype ptype) const

持駒の枚数を数える

BaseState rotate180() const

make a new rotated state

class EffectState : public osl::BaseState

Standard state exported as minioslcc.State

利きを持つ局面

  • effects (EffectSummary) 利き

  • pieces_onboard (PieceMask) 盤上にある駒

Public Functions

inline PieceMask piecesOnBoard(Player p) const

return a set of piece IDs on board

inline PieceMask promotedPieces() const

return a set of piece IDs promoted

inline PieceMask pin(Player king) const

return a set of piece IDs pinned

inline bool inCheck(Player P) const

Pの玉が王手状態

inline bool inCheck() const

手番の玉が王手状態

bool inCheckmate() const

手番の玉が詰み (負け)

bool inNoLegalMoves() const

another loss condition for turn

inline PieceMask effectedPieces(Player pl) const

pl からの利きが(1つ以上)ある駒一覧

Piece findThreatenedPiece(Player P) const

取られそうなPの駒で価値が最大のもの

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 square sq

inline int countEffect(Player player, Square target) const

利きの数を数える.

targetが盤をはみ出してはいけない

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

not implemented

  • evasion in check

  • repetition of states

bool isSafeMove(Move move) const

a part of legal move conditions

bool isCheck(Move move) const

classify move property

bool isPawnDropCheckmate(Move move) const

a part of illegal move conditions

bool isDirectCheck(Move move) const

classify move property

bool isOpenCheck(Move move) const

classify move property

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

王手生成

Move tryCheckmate1ply() const

1手詰めの手を見つけられれば生成

Move findThreatmate1ply() const

自玉の詰めろを見つけられれば生成

void makeMove(Move move)

make a move to update the state

Move to_move(std::string) const

interpret string representation of move in usi or csa

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");
void findEffect(Player P, Square target, PieceVector &out) const

target に利きのあるPieceをoutに格納する

Game record

see also record.h

enum osl::GameResult

status at the endgame

Values:

enumerator BlackWin
enumerator WhiteWin
enumerator Draw
enumerator InGame
constexpr GameResult osl::win_result(Player P)

test win for P

constexpr bool osl::has_winner(GameResult r)

test whether BlackWin or WhiteWin

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 as idx-th state

Public Members

EffectState initial_state

initial state

std::vector<Move> moves

moves

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

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

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

std::vector<Move> moves

moves

Move final_move

resign or DeclareWin if game has the winner

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