Quickstart
Basics
Below is an example of creating a board, generating legal moves at the starting position, displaying them, and making a move.
>>> import cshogi
>>> board = cshogi.Board()
>>> for move in board.legal_moves:
... print(cshogi.move_to_usi(move))
1g1f
2g2f
3g3f
4g4f
5g5f
6g6f
7g7f
...
>>> board.push_usi('7g7f')
Features
Integration with IPython/Jupyter Notebook
>>> board
Playing/Undoing Moves
>>> move = board.push_usi('7g7f') # Play a move >>> board.pop() # Undo the move
Displaying the Board in Text Format
>>> board = Board('ln4skl/3r1g3/1p2pgnp1/p1ppsbp1p/5p3/2PPP1P1P/PPBSSG1P1/2R3GK1/LN5NL b P 43') >>> print(board)
' 9 8 7 6 5 4 3 2 1 P1-KY-KE * * * * -GI-OU-KY P2 * * * -HI * -KI * * * P3 * -FU * * -FU-KI-KE-FU * P4-FU * -FU-FU-GI-KA-FU * -FU P5 * * * * * -FU * * * P6 * * +FU+FU+FU * +FU * +FU P7+FU+FU+KA+GI+GI+KI * +FU * P8 * * +HI * * * +KI+OU * P9+KY+KE * * * * * +KE+KY P+00FU +
Check Determination, Game End Determination, Nyugyoku Declaration Judgment
>>> board.is_check() False >>> board.is_game_over() True >>> board.is_nyugyoku() False
Repetition Draw Determination
>>> board.is_draw() == REPETITION_DRAW # There's one or more identical positions False
Move Representation
Moves are handled as numbers. They can be converted to USI or CSA format with helper functions.
>>> move = list(board.legal_moves)[0] >>> move 66309 >>> move_to_usi(move) '1g1f' >>> move_to_csa(move) '1716FU'
Moves can be converted from USI or CSA format to numerical form.
>>> board.move_from_usi('7g7f') 73275 >>> board.move_from_csa('7776FU') 73275
Position Compression Format
Can read teacher positions generated by Apery and YaneuraOu.
>>> import numpy as np >>> hcpes = np.fromfile('teacher.hcpe', dtype=cshogi.HuffmanCodedPosAndEval) # Apery's teacher positions (HuffmanCodedPosAndEval) >>> board.set_hcp(hcpes[0]['hcp']) >>> psfens = np.fromfile('sfen.bin', dtype=cshogi.PackedSfenValue) # YaneuraOu's teacher positions (PackedSfenValue) >>> board.set_psfen(psfens[0]['sfen'])
Positions can be saved in Apery’s compression format.
>>> hcps = np.empty(1, dtype=cshogi.HuffmanCodedPos) >>> board.to_hcp(hcps) >>> hcps.tofile('hcp')
USI Engine Control
Can launch and control a USI engine.
>>> from cshogi.usi import Engine >>> engine = Engine('/content/LesserkaiSrc/Lesserkai/Lesserkai') >>> engine.isready() >>> engine.position(sfen='sfen 7nl/5kP2/3p2g1p/2p1gp3/p6sP/s1BGpN3/4nPSp1/1+r4R2/L1+p3K1L w GSNLPb6p 122') >>> engine.go()
Matches Between USI Engines
>>> from cshogi import cli >>> cli.main('/content/LesserkaiSrc/Lesserkai/Lesserkai', '/content/LesserkaiSrc/Lesserkai/Lesserkai')
Use
-cor--concurrencyto play several pairs at once. Each worker plays the two colors of an opening on the same hardware. Worker options override the named engine’s common options in command-line order. Supply one quoted option string per worker for each engine that needs overrides.python -m cshogi.cli yane dlshogi dlshogi --games 20 -c 2 \ --name2 base --name3 new --keep-process --pgn league.pgn \ --options2 "DNN_Model:base.onnx,UCT_Threads:3" \ --options3 "DNN_Model:new.onnx,UCT_Threads:3" \ --worker-options2 "UCT_Threads:2" "UCT_Threads:0,UCT_Threads2:2" \ --worker-options3 "UCT_Threads:2" "UCT_Threads:0,UCT_Threads2:2"
Here
[worker1]uses GPU 0 and[worker2]uses GPU 1. Budget CPU threads across workers, including both engines when pondering. For dlshogi builds with a TensorRT cache, initialize each model/GPU/batch-size combination once before starting workers that share the same cache file.In a Unix shell launcher, use
execto start the engine so Ctrl+C can terminate it directly.Head-to-head PGN records are appended in completion order. Their Round tags retain the scheduled game numbers. A three-engine league plays two games per pairing per opening, rounding an odd game count up for each pairing. Its Round tags are 1 and 2 in every mini-match.
The Python API accepts the same overrides, for example
cli.main(a, b, concurrency=2, worker_options=[None, [{'Threads': 2}, {'Threads': 4}]]). A falsy callback result stops new games. Games already started finish and contribute to the records and final totals. Parallel callbacks run serially on the reporting workers. Board display requires concurrency 1. Parallel debug output is queued so that a worker can keep reading engine responses while another worker prints standings or runs a callback. A debug label such as[worker1] g3 2identifies worker 1, scheduled game 3, and the second engine on the command line.Stopping a match with SPRT
Use
--sprt ELO0 ELO1to compare two hypotheses about the tested engine’s logistic Elo difference against its opponent. For example, this tests equal strength (H0) against a five-point improvement (H1), up to 40,000 games.python -m cshogi.cli new base --sprt 0 5 --games 40000 -c 4 \ --opening openings.sfen --pgn match.pgn
Each observation is the tested engine’s average score over two games from the same opening with colors reversed. Supply an even game limit and leave color swapping enabled. Use a large opening book. When a book repeats, deterministic engines playing under identical conditions can repeat the same pair of games without providing an independent observation.
The log-likelihood ratio (LLR) is updated in scheduled pair order. Crossing the lower bound accepts H0 and crossing the upper bound accepts H1. The type I and II error parameters,
--sprt-alphaand--sprt-beta, both default to 0.05. Reaching the game limit can leave the test undecided. After a decision, games already in progress finish and count toward the match totals and records. The reported SPRT counts and LLR stay fixed.In a three-engine league, specify the tested engine first and its opponent second. Here the test compares engine 3 against engine 2, and the limit applies to each pairing. A decision can stop the league before the first standings table, which requires all six games of a stage.
python -m cshogi.cli yane base new --sprt 0 5 --sprt-engines 3 2 \ --games 20000 -c 4 --opening openings.sfen --pgn league.pgn
In Python, pass
sprt=cli.SprtConfig(0, 5)tocli.main()orsprt=cli.SprtConfig(0, 5, engines=(3, 2))tocli.league(). The selected pairing’s result dictionary includes asprtentry withengines,result('H0','H1', orNone),llr,lower,upper,pairs, andpentanomial(counts in ascending score order). Head-to-head callbacks receive the same fields. Useprint_summary=Falseto suppress head-to-head SPRT output while retaining the result.