From a3ed5b24762c5c7a84ecb8a650ca616d3e51f619 Mon Sep 17 00:00:00 2001 From: yuiseki Date: Mon, 5 Oct 2020 15:43:32 +0900 Subject: [PATCH] add SVG.py --- README.rst | 9 ++ shogi/SVG.py | 360 ++++++++++++++++++++++++++++++++++++++++++++++ shogi/__init__.py | 6 + 3 files changed, 375 insertions(+) create mode 100644 shogi/SVG.py diff --git a/README.rst b/README.rst index ac059b1..7e1f8cd 100644 --- a/README.rst +++ b/README.rst @@ -105,6 +105,15 @@ Features +---------------------------+ 先手の持駒: 銀 +* Render board as SVG. + + .. code:: python + + >>> print(board.svg()) + + ... + + * Detects checkmates, stalemates. .. code:: python diff --git a/shogi/SVG.py b/shogi/SVG.py new file mode 100644 index 0000000..0ae69a3 --- /dev/null +++ b/shogi/SVG.py @@ -0,0 +1,360 @@ +# -*- coding: utf-8 -*- +# +# This file is part of the python-shogi library. +# +# Copyright (C) 2020- Yui Matsumura +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +def sfen_to_svg(sfen): + svg = """ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + > + + > + + > + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + + + + + + + + + + + + + + + + + + """ + + reftable = { + 'l': 'white-lance', + 'n': 'white-knight', + 's': 'white-silver', + 'g': 'white-gold', + 'k': 'white-king', + 'r': 'white-rook', + 'b': 'white-bishop', + 'p': 'white-pawn', + 'L': 'black-lance', + 'N': 'black-knight', + 'S': 'black-silver', + 'G': 'black-gold', + 'K': 'black-king', + 'R': 'black-rook', + 'B': 'black-bishop', + 'P': 'black-pawn' + } + + # render pieces on board + sfen_conf = sfen.split()[0].split('/') + board = [] + colnum = 1 + rownum = 1 + for col in sfen_conf: + colnum = 1 + for p in col: + if p.isdecimal(): + colnum = colnum+1 + else: + x = colnum * 20 + y = (rownum * 20) - 10 + ref = reftable[p] + t = '' + use = t.format(ref, x, y) + board.append(use) + colnum += 1 + rownum += 1 + + # render owned pieces + sfen_ownp = sfen.split()[2] + ownp_idx = 0 + first_lower = True + if not sfen_ownp == '-': + quantity = 1 + lastpiece = '' + for p in sfen_ownp: + if p.isdecimal(): + quantity = int(p) + continue + else: + lastpiece = p + x = 0 + y = 0 + if not p.islower(): + x = -20 + y = -90 - (ownp_idx*15) + ref = reftable[lastpiece] + t = '' + use = t.format(ref, x, y) + board.append(use) + if quantity > 1: + y = y + 15 + t = '{}' + text = t.format(x, y, quantity) + board.append(text) + else: + if first_lower: + ownp_idx = 0 + first_lower = False + x = -230 + y = -130 + (ownp_idx*15) + ref = reftable[lastpiece] + t = '' + use = t.format(ref, x, y) + board.append(use) + if quantity > 1: + x = abs(x)-5 + y = abs(y)-5 + t = '{}' + text = t.format(x, y, quantity) + board.append(text) + ownp_idx += 1 + quantity = 1 + svg = svg+'\n'.join(board)+'\n' + return svg \ No newline at end of file diff --git a/shogi/__init__.py b/shogi/__init__.py index 992964b..57d0687 100644 --- a/shogi/__init__.py +++ b/shogi/__init__.py @@ -28,6 +28,8 @@ from .Move import * from .Piece import * from .Consts import * +from .SVG import * + PIECE_TYPES_WITHOUT_KING = [ PAWN, LANCE, KNIGHT, SILVER, @@ -1124,6 +1126,10 @@ def sfen(self): return ''.join(sfen) + def svg(self): + svg = SVG.sfen_to_svg(self.sfen()) + return svg + def set_sfen(self, sfen): ''' Parses a SFEN and sets the position from it.