• Stars
    star
    180
  • Rank 213,097 (Top 5 %)
  • Language
    Python
  • License
    GNU General Publi...
  • Created almost 10 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A pure Python shogi library with move generation and validation and handling of common formats.

python-shogi: a pure Python shogi library

Introduction

This is the module for shogi written in Pure Python. It's based on python-chess commit

This is the scholars mate in python-shogi:

>>> import shogi

>>> board = shogi.Board()

>>> board.push(shogi.Move.from_usi('7g7f'))

>>> board.push_usi('3c3d')
Move.from_usi('3c3d')
>>> board.push_usi('8h2b+')
Move.from_usi('8h2b+')
>>> board.push_usi('4a5b')
Move.from_usi('4a5b')
>>> board.push_usi('B*4b')
Move.from_usi('B*4b')
>>> board.push_usi('5a4a')
Move.from_usi('5a4a')
>>> board.push_usi('2b3a')
Move.from_usi('2b3a')
>>> board.is_checkmate()
True

Features

  • Supports Python 3.3+.

  • Supports standard shogi (hon shogi)

  • Legal move generator and move validation.

    >>> shogi.Move.from_usi("5i5a") in board.legal_moves
    False
  • Make and unmake moves.

    >>> last_move = board.pop() # Unmake last move
    >>> last_move
    Move.from_usi('2b3a')
    
    >>> board.push(last_move) # Restore
  • Show a simple ASCII board.

    >>> print(board)
     l  n  s  g  .  k +B  n  l
     .  r  .  .  g  B  .  .  .
     p  p  p  p  p  p  .  p  p
     .  .  .  .  .  .  p  .  .
     .  .  .  .  .  .  .  .  .
     .  .  P  .  .  .  .  .  .
     P  P  .  P  P  P  P  P  P
     .  .  .  .  .  .  .  R  .
     L  N  S  G  K  G  S  N  L
    <BLANKLINE>
     S*1
  • Show a KIF style board.

    >>> print(board.kif_str())
    後手の持駒:
      9 8 7 6 5 4 3 2 1
    +---------------------------+
    |v香v桂v銀v金v玉 馬v桂v香|
    |v飛 ・ ・v金  ・ ・ ・|
    |v歩v歩v歩v歩v歩v歩v歩v歩|
    | ・ ・ ・ ・ ・ ・v歩 ・ ・|
    | ・ ・ ・ ・ ・ ・ ・ ・ ・|
    | ・ ・  ・ ・ ・ ・ ・ ・|
    |       |
    | ・ ・ ・ ・ ・ ・ ・ |
    |         |
    +---------------------------+
    先手の持駒: 
  • Detects checkmates, stalemates.

    >>> board.is_stalemate()
    False
    >>> board.is_game_over()
    True
  • Detects repetitions. Has a half move clock.

    >>> board.is_fourfold_repetition()
    False
    >>> board.move_number
    8
  • Detects checks and attacks.

    >>> board.is_check()
    True
    >>> board.is_attacked_by(shogi.BLACK, shogi.A4)
    True
    >>> attackers = board.attackers(shogi.BLACK, shogi.H5)
    >>> attackers
    SquareSet(0b111000010000000000000000000000000000000000000000000000000000000000000000000000)
    >>> shogi.H2 in attackers
    True
    >>> print(attackers)
    . . . . . . . . .
    . . . . . . . . .
    . . . . . . . . .
    . . . . . . . . .
    . . . . . . . . .
    . . . . . . . . .
    . . . . . . . . .
    . . . . . . . 1 .
    . . . 1 1 1 . . .
  • Parses and creates USI representation of moves.

    >>> board = shogi.Board()
    >>> shogi.Move(shogi.E2, shogi.E4).usi()
    '2e4e'
  • Parses and creates SFENs

    >>> board.sfen()
    'lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL b - 1'
    >>> board.piece_at(shogi.I5)
    Piece.from_symbol('K')
  • Read KIFs.

    >>> import shogi.KIF
    
    >>> kif = shogi.KIF.Parser.parse_file('data/games/habu-fujii-2006.kif')[0]
    
    >>> kif['names'][shogi.BLACK]
    '羽生善治'
    >>> kif['names'][shogi.WHITE]
    '藤井猛'
    >>> kif['moves'] # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
    ['7g7f',
     '3c3d',
     ...,
     '9a9b',
     '7a7b+']
    >>> kif['win']
    'b'
  • Export to KIFs.

    >>> import shogi
    >>> import shogi.KIF
    
    >>> board = shogi.Board()
    >>> shogi.KIF.Exporter.kif_move_from('7g7f', board)
    '7六歩(77)'
    
    >>> sfen_summary = {'moves': ['7g7f', '3c3d'], 'sfen': 'lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL b - 1', 'names': ['羽生善治', '藤井猛'], 'win': 'w'}
    >>> shogi.KIF.Exporter.kif(sfen_summary)
    開始日時: \r
    終了日時: \r
    手合割平手\r
    先手羽生善治\r
    後手藤井猛\r
    手数----指手---------消費時間-- \r
    1六歩(77) \r
    2四歩(33) \r
    3 投了 \r
    まで2手で後手の勝ち\r
  • Communicate with a CSA protocol.

    Please see random_csa_tcp_match.

  • Parse professional shogi players' name

    >>> import shogi.Person
    >>> shogi.Person.Name.is_professional('羽生 善治 名人・棋聖・王位・王座')
    True

Performance

python-shogi is not intended to be used by serious shogi engines where performance is critical. The goal is rather to create a simple and relatively highlevel library.

You can install the gmpy2 or gmpy modules in order to get a slight performance boost on basic operations like bit scans and population counts.

python-shogi will only ever import very basic general (non-shogi-related) operations from native libraries. All logic is pure Python. There will always be pure Python fallbacks.

Installing

  • With pip:

    pip install python-shogi
    

How to test

> make test

If you want to print lines from the standard output, execute nosetests like following.

> poetry run nosetests -s

How to release

poetry config repositories.testpypi https://test.pypi.org/legacy/
# poetry config pypi-token.testpypi "Test PyPI API Token"
make test-upload
# poetry config pypi-token.pypi "PyPI API Token"
make upload

ToDo

  • Support board.generate_attacks() and use it in board.is_attacked_by() and board.attacker_mask().
  • Remove rotated bitboards and support Shatranj-style direct lookup like recent python-chess.
  • Support %MATTA etc. in CSA TCP Protocol.
  • Support board.is_pinned() and board.pin().

More Repositories

1

kantan-ej-dictionary

English-Japanese dictionary
Ruby
60
star
2

gdbruby

gdbperl for Ruby
Ruby
46
star
3

timezone_finder

Look up timezone from lat / long offline. Ported from timezonefinder in PyPI.
Ruby
40
star
4

virtual-religion-web

Virtual Religion web page
HTML
35
star
5

gunyanza

A toy shogi AI
Python
23
star
6

api-server-standard

RESTful API server written in some programming languages.
Ruby
20
star
7

clone2ch

Clone of 2ch.
Scala
11
star
8

kinkin

An example of CI scripts which receives Webhook, build and notify the result into Slack
Ruby
10
star
9

snes-games

Games written in 65816 assembler for SNES (Super Famicom)
Assembly
7
star
10

python-qwiic-CCS811-BME280

For https://www.sparkfun.com/products/14348
Python
5
star
11

pytc

Python bindings for Tokyo Cabinet.
C
4
star
12

django-wikipedia-search

Wikipedia search with django, including a Python wikipedia parser.
Python
4
star
13

nicopealert

alert tool for niconico-pedia with PyQt.
Python
4
star
14

mumu

Template engine written in pure PHP
PHP
3
star
15

dotfiles

gunyarakun's dotfiles
Shell
2
star
16

japanese-bookkeeping-svg

Generate SVG files for Japanese style bookkeeping diagrams.
Ruby
2
star
17

p5-Digest-BLAKE2

BLAKE2 perl bindings
C
1
star
18

fake-s3

S3 for local tests
Python
1
star
19

cached-http-fetcher

Cache image optimizer
Python
1
star
20

cookie-clicker-generator

Cookie Clicker generator
1
star
21

blog.wktk.co.jp

Ruby
1
star
22

flutter_sensors_web

Flutter sensors plugin for web
1
star
23

mod_mobilega

lighttpd module embedding Google Analytics/Adsense for japanese mobile phone
C
1
star
24

mid2flmml

A converter from a standard MIDI file to MML which is playable on FlMML
Python
1
star
25

traditional-simplified-hanyu-japanese-kanji-mappings

A convertion table from/to traditional/simplified hanyu character and japanese kanji.
TeX
1
star
26

p5-Plack-Middleware-Image-Dummy

Dummy image responser for Plack like http://dummyimage.com/
Perl
1
star
27

nicotagmap

tag voronoi treemap of NicoNicoDouga
C
1
star
28

gunyara-mandarin-kit

Tools for learning Mandarin
JavaScript
1
star