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')