minioslcc

shogi utilities derived from osl. all members should be incorporated inside miniosl module

Basic datatype

class miniosl.Player

Members:

black : first player

white : second player

property name
miniosl.alt(color: osl::Player) osl::Player

alternative player color

>>> miniosl.alt(miniosl.black) == miniosl.white
True
miniosl.sign(color: osl::Player) int

+1 for black or -1 for white

class miniosl.Square

square (x, y) with onboard range in (1, 1) to (9, 9) and with some invalid ranges outside the board for sentinels and piece stand.

>>> sq = miniosl.Square(2, 6)
>>> sq.x()
2
>>> sq.y()
6
>>> sq.is_onboard()
True
>>> sq.to_xy()
(2, 6)
index81(self: minioslcc.Square) int

return index in range [0, 80]

is_onboard(self: minioslcc.Square) bool
is_piece_stand(self: minioslcc.Square) bool
is_promote_area(self: minioslcc.Square, color: osl::Player) bool

test this is in promote area for color

>>> miniosl.Square(1, 3).is_promote_area(miniosl.black)
True
>>> miniosl.Square(1, 3).is_promote_area(miniosl.white)
False
>>> miniosl.Square(5, 5).is_promote_area(miniosl.black)
False
rotate180(self: minioslcc.Square) minioslcc.Square

return a square after rotation.

>>> sq = miniosl.Square(2, 6)
>>> sq.rotate180().to_xy()
(8, 4)
to_csa(self: minioslcc.Square) str
to_usi(self: minioslcc.Square) str
to_xy(self: minioslcc.Square) Tuple[int, int]
x(self: minioslcc.Square) int

in [1,9]

y(self: minioslcc.Square) int

in [1,9]

class miniosl.Move

move in shogi.

containing the source and destination positions, moving piece, and captured one if any

>>> shogi = miniosl.UI()
>>> move = shogi.to_move('+7776FU')
>>> move.src() == miniosl.Square(7, 7)
True
>>> move.dst() == miniosl.Square(7, 6)
True
>>> move.ptype() == miniosl.pawn
True
>>> move.color() == miniosl.black
True
>>> move.is_drop()
False
>>> move.is_capture()
False
>>> move.to_usi()
'7g7f'
capture_ptype(self: minioslcc.Move) osl::Ptype
color(self: minioslcc.Move) osl::Player
static declare_win() minioslcc.Move

return win declaration

dst(self: minioslcc.Move) minioslcc.Square
is_capture(self: minioslcc.Move) bool
is_drop(self: minioslcc.Move) bool
is_normal(self: minioslcc.Move) bool
is_promotion(self: minioslcc.Move) bool
old_ptype(self: minioslcc.Move) osl::Ptype

piece type before move

policy_move_label(self: minioslcc.Move) int

move index for cross-entropy loss in training

ptype(self: minioslcc.Move) osl::Ptype

piece type after move

static resign() minioslcc.Move

return resign

rotate180(self: minioslcc.Move) minioslcc.Move
src(self: minioslcc.Move) minioslcc.Square
to_csa(self: minioslcc.Move) str
to_usi(self: minioslcc.Move) str
class miniosl.Ptype

piece type

Members:

pawn

lance

knight

silver

gold

bishop

rook

king

ppawn : promoted pawn

plance : promoted lance

pknight : promoted knight

psilver : promoted silver

pbishop : promoted bishop

prook : promoted rook

empty

edge

property name
class miniosl.Piece

a state of piece placed in a corresponding BaseState

color(self: minioslcc.Piece) osl::Player
equals(self: minioslcc.Piece, arg0: minioslcc.Piece) bool

equality w.r.t. PtypeO (i.e., ignoring piece id or location)

has(self: minioslcc.Piece, ptype: osl::Ptype, color: osl::Player) bool

test if piece has a specified piece type with color

id(self: minioslcc.Piece) int
is_piece(self: minioslcc.Piece) bool
ptype(self: minioslcc.Piece) osl::Ptype
square(self: minioslcc.Piece) minioslcc.Square

State

class miniosl.BaseState

parent of State. Please use State for usual cases.

>>> state = miniosl.State()
>>> state.turn() == miniosl.black
True
>>> state.piece_at(miniosl.Square(5, 9)).ptype() == miniosl.king
True
>>> state.king_square(miniosl.white) == miniosl.Square(5, 1)
True
>>> state.count_hand(miniosl.black, miniosl.pawn)
0
count_hand(self: minioslcc.BaseState, color: minioslcc.Player, ptype: minioslcc.Ptype) int
decode_move_label(self: minioslcc.BaseState, code: int) minioslcc.Move

interpret move index generated by Move.policy_move_label()

export_features(self: minioslcc.BaseState, moves: minioslcc.MoveVector) numpy.ndarray[numpy.float32]

return np array of the standard set of features after moves are played

export_features_after_move(self: minioslcc.BaseState, moves: minioslcc.MoveVector, lookahead: minioslcc.Move) Tuple[numpy.ndarray[numpy.float32], minioslcc.GameResult]

return pair of (1) np array of the standard set of features after moves and lookahead are played and (2) GameResult indicating game termination by the move

hash_code(self: minioslcc.BaseState) Tuple[int, int]

64bit int for board and 32bit for (black) hand pieces

king_square(self: minioslcc.BaseState, color: minioslcc.Player) minioslcc.Square
piece(self: minioslcc.BaseState, internal_id: int) minioslcc.Piece
piece_at(self: minioslcc.BaseState, square: minioslcc.Square) minioslcc.Piece

a piece at given square

rotate180(self: minioslcc.BaseState) minioslcc.BaseState

make a rotated state

>>> s = miniosl.State()
>>> print(s.to_csa(), end='')
P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
P2 * -HI *  *  *  *  * -KA * 
P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
P4 *  *  *  *  *  *  *  *  * 
P5 *  *  *  *  *  *  *  *  * 
P6 *  *  *  *  *  *  *  *  * 
P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
P8 * +KA *  *  *  *  * +HI * 
P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
+
>>> _ = s.make_move('+7776FU')
>>> print(s.rotate180().to_csa(), end='')
P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
P2 * -HI *  *  *  *  * -KA * 
P3-FU-FU-FU-FU-FU-FU * -FU-FU
P4 *  *  *  *  *  * -FU *  * 
P5 *  *  *  *  *  *  *  *  * 
P6 *  *  *  *  *  *  *  *  * 
P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
P8 * +KA *  *  *  *  * +HI * 
P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
+
to_csa(self: minioslcc.BaseState) str
to_np_44ch(self: minioslcc.BaseState) numpy.ndarray[numpy.float32]

a simple set of state features including board and hands

to_png(*, filename='', **kwargs) Drawing | None

make a picture of state as svg, save to file if given filename

parameters are the same as state_to_svg() except for filename

to_svg(id: int = 0, *, decorate: bool = False, plane: ndarray | None = None, plane_color: str = 'orange', last_move_ja: str = '', last_to: Square | None = None, move_number: int = 0, repeat_distance: int = 0, repeat_count: int = 0, flip_if_white: bool = False) Drawing

make a picture of state as svg

Parameters:
  • state – state,

  • id – id for svg if given,

  • decorate – highlight king location and piece covers for each color,

  • plane – 9x9 numpy array to make a mark on squares,

  • last_move_ja – last move in japanese,

  • last_to – the destination square of the last move,

  • move_number – ply in a game record,

  • repeat_distance – distance to the latest same position,

  • repeat_count – number of the occurrence of this state,

  • flip_if_whiterotate180() if white to move

to_usi(self: minioslcc.BaseState) str
turn(self: minioslcc.BaseState) minioslcc.Player

player to move

class miniosl.State

Bases: BaseState

shogi state = board position + pieces in hand (mochigoma)

count_cover(self: minioslcc.State, color: minioslcc.Player, sq: minioslcc.Square) int

the number of pieces reachable to given square

>>> s = miniosl.State()
>>> s.count_cover(miniosl.black, miniosl.Square(1, 7))
2
>>> s.count_cover(miniosl.black, miniosl.Square(5, 7))
0
>>> s.count_cover(miniosl.black, miniosl.Square(9, 7))
3
genmove(self: minioslcc.State) minioslcc.MoveVector

genmove suitable for most cases (some moves are excluded)

>>> s = miniosl.State()
>>> len(s.genmove())
30
genmove_check(self: minioslcc.State) minioslcc.MoveVector

generate check moves only

genmove_full(self: minioslcc.State) minioslcc.MoveVector

genenerate full moves, including skip-promotion of pawn, bishop, and rook.

in_check(self: minioslcc.State) bool
in_checkmate(self: minioslcc.State) bool
in_no_legalmoves(self: minioslcc.State) bool
make_move(*args, **kwargs)

Overloaded function.

  1. make_move(self: minioslcc.State, move_string: str) -> None

take usa or csa representation

  1. make_move(self: minioslcc.State, move: minioslcc.Move) -> None

make_move_pass(self: minioslcc.State) None
pieces_cover(self: minioslcc.State, color: minioslcc.Player, square: minioslcc.Square) int

the bitset of piece-ids reachable to given square

read_japanese_move(self: minioslcc.State, move: str, last_to: minioslcc.Square = <Square '0_'>) minioslcc.Move

parse and return move

reset(self: minioslcc.State, src: minioslcc.State) None

re-initialize self copying src

to_move(self: minioslcc.State, move_string: str) minioslcc.Move

parse and return move

>>> s = miniosl.State()
>>> s.to_move('+2726FU') == s.to_move('2g2f')
True
to_np_cover(self: minioslcc.State) numpy.ndarray[numpy.int8]

squares covered by pieces as numpy array

to_np_state_feature(self: minioslcc.State, flipped: bool = False) numpy.ndarray[numpy.float32]

a subset of features without history information

to_png(*, filename='', **kwargs) Drawing | None

make a picture of state as svg, save to file if given filename

parameters are the same as state_to_svg() except for filename

to_svg(id: int = 0, *, decorate: bool = False, plane: ndarray | None = None, plane_color: str = 'orange', last_move_ja: str = '', last_to: Square | None = None, move_number: int = 0, repeat_distance: int = 0, repeat_count: int = 0, flip_if_white: bool = False) Drawing

make a picture of state as svg

Parameters:
  • state – state,

  • id – id for svg if given,

  • decorate – highlight king location and piece covers for each color,

  • plane – 9x9 numpy array to make a mark on squares,

  • last_move_ja – last move in japanese,

  • last_to – the destination square of the last move,

  • move_number – ply in a game record,

  • repeat_distance – distance to the latest same position,

  • repeat_count – number of the occurrence of this state,

  • flip_if_whiterotate180() if white to move

try_checkmate_1ply(self: minioslcc.State) minioslcc.Move

try to find a checkmate move

win_if_declare(self: minioslcc.State) bool
class miniosl.MiniRecord

a game record

consecutive_in_check(self: minioslcc.MiniRecord, id: int = 0) int
property final_move

resign or win declaration in Move

has_winner(self: minioslcc.MiniRecord) bool
property initial_state
move_size(self: minioslcc.MiniRecord) int

#moves played so far

property moves

list of Move s

pack_record(self: minioslcc.MiniRecord) List[int]

encode in uint64 array

previous_repeat_index(self: minioslcc.MiniRecord, id: int = 0) int
repeat_count(self: minioslcc.MiniRecord, id: int = 0) int
replay(n: int) State

return state after n-th move

Parameters:

n – number of moves from initial state or the last state if negative

>>> record = miniosl.usi_record('startpos moves 7g7f')
>>> s = record.replay(-1)
>>> s.piece_at(miniosl.Square(7, 6)).ptype() == miniosl.pawn
True
property result

GameResult

state_size(self: minioslcc.MiniRecord) int
to_apng(n: int = 0, start: int = 0, delay: int = 1000, decorate: bool = False) APNG

return animation of the specified range of record as apng

to_usi(self: minioslcc.MiniRecord) str

export to usi string

Game record

class miniosl.GameResult

Members:

BlackWin

WhiteWin

Draw

InGame

property name
class miniosl.RecordSet
from_npz(name: str = '', *, limit: int | None = None)

load miniosl.RecordSet from npz file

static from_usi_file(path: str) minioslcc.RecordSet

read usi lines

property records

list of MiniRecord s

save_npz(filename: str, name: str = '') None

save recordset to npz by np.savez_compressed

class miniosl.OpeningTree
class Node
black_advantage(self: minioslcc.OpeningTree.Node) float
count(self: minioslcc.OpeningTree.Node) int
property result_count

list of int for all GameResult

board_size(self: minioslcc.OpeningTree) int
export_all(self: minioslcc.OpeningTree) Tuple[List[int], List[int], List[int]]
static from_record_set(data: minioslcc.RecordSet, minimum_count: int) minioslcc.OpeningTree
static restore_from(board_code: List[int], stand_code: List[int], node_code: List[int]) minioslcc.OpeningTree

restore from data generated by export_all()

retrieve_children(state: BaseState) list[Node, Move]
save_npz(filename: str) ndarray

save OpeningTree to npz

size(self: minioslcc.OpeningTree) int
property table
miniosl.csa_board(state_string: str) osl::EffectState

parse and return State

miniosl.usi_board(state_string: str) osl::EffectState

parse and return State

miniosl.csa_record(record_string: str) minioslcc.MiniRecord

read str as a game record

miniosl.usi_record(record_string: str) minioslcc.MiniRecord

read str as a game record

miniosl.csa_file(path: str) minioslcc.MiniRecord

load a game record

miniosl.usi_file(path: str, line_id: int = 0) minioslcc.MiniRecord

load a game record

Details

class miniosl.Direction

direction.

>>> direction_set = miniosl.ptype_move_direction[int(miniosl.pawn)]
>>> bin(direction_set)
'0b10'
>>> (direction_set & (1 << int(miniosl.U))) != 0
True

Members:

UL : up left

U

UR

L

R

DL

D

DR

UUL : up up left for knight

UUR

Long_UL : UL for bishops

Long_U

Long_UR

Long_L

Long_R

Long_DL

Long_D

Long_DR

property name