Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

初期局面と手合割(rsshogi.initial_positions)

rsshogi.initial_positions モジュールは、平手、空盤面、および 13 種類の駒落ち初期局面の SFEN 定数と、日本語の手合割名と SFEN 文字列を相互変換する辞書や関数を提供する。

from rsshogi.initial_positions import (
    HANDICAP_TO_SFEN,
    SFEN_TO_HANDICAP,
    InitialPosition,
    handicap_to_sfen,
    sfen_to_handicap,
)

InitialPosition

rsshogi.initial_positions.InitialPosition

標準の enum.Enum を継承した列挙型であり、各メンバの .value に初期局面の SFEN 文字列を保持する。 駒落ち局面(HANDICAP_*)はいずれも上手(後手、w)の手番から始まる。

定数一覧

列挙メンバ手合割名SFEN 文字列 (member.value)
InitialPosition.STANDARD平手"lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL b - 1"
InitialPosition.EMPTY(空盤面)"9/9/9/9/9/9/9/9/9 b - 1"
InitialPosition.HANDICAP_LANCE香落ち"lnsgkgsn1/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1"
InitialPosition.HANDICAP_RIGHT_LANCE右香落ち"1nsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1"
InitialPosition.HANDICAP_BISHOP角落ち"lnsgkgsnl/1r7/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1"
InitialPosition.HANDICAP_ROOK飛車落ち"lnsgkgsnl/7b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1"
InitialPosition.HANDICAP_ROOK_LANCE飛香落ち"lnsgkgsn1/7b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1"
InitialPosition.HANDICAP_2_PIECES二枚落ち"lnsgkgsnl/9/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1"
InitialPosition.HANDICAP_3_PIECES三枚落ち"lnsgkgsn1/9/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1"
InitialPosition.HANDICAP_4_PIECES四枚落ち"1nsgkgsn1/9/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1"
InitialPosition.HANDICAP_5_PIECES五枚落ち"2sgkgsn1/9/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1"
InitialPosition.HANDICAP_LEFT_5_PIECES左五枚落ち"1nsgkgs2/9/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1"
InitialPosition.HANDICAP_6_PIECES六枚落ち"2sgkgs2/9/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1"
InitialPosition.HANDICAP_8_PIECES八枚落ち"3gkg3/9/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1"
InitialPosition.HANDICAP_10_PIECES十枚落ち"4k4/9/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL w - 1"
from rsshogi.core import Board
from rsshogi.initial_positions import InitialPosition
from rsshogi.types import Color

# 二枚落ちの初期局面で Board を初期化
board = Board(sfen=InitialPosition.HANDICAP_2_PIECES.value)
assert board.turn == Color.WHITE

手合割名と SFEN の相互変換

InitialPosition.EMPTY を除く 14 種類の標準手合割("平手" から "十枚落ち" まで)について、読み取り専用マッピング(MappingProxyType)と変換関数が用意されている。

定数 / 関数型 / シグネチャ説明
HANDICAP_TO_SFENMapping[str, str]日本語の手合割名(全 14 項目)をキー、SFEN 文字列を値とする読み取り専用辞書である。
SFEN_TO_HANDICAPMapping[str, str]SFEN 文字列をキー、日本語の手合割名を値とする読み取り専用辞書である。
handicap_to_sfenhandicap_to_sfen(handicap: str) -> str手合割名に対応する SFEN 文字列を返す。未登録の名前を渡すと KeyError を送出する。
sfen_to_handicapsfen_to_handicap(sfen: str) -> str | NoneSFEN 文字列に対応する手合割名を返す。該当しない場合は None を返す。
from rsshogi.initial_positions import (
    HANDICAP_TO_SFEN,
    SFEN_TO_HANDICAP,
    handicap_to_sfen,
    sfen_to_handicap,
)

sfen = handicap_to_sfen("飛香落ち")
assert sfen == HANDICAP_TO_SFEN["飛香落ち"]
assert sfen_to_handicap(sfen) == "飛香落ち"
assert SFEN_TO_HANDICAP[sfen] == "飛香落ち"
assert sfen_to_handicap("9/9/9/9/9/9/9/9/9 b - 1") is None

関連項目