From eb3c787367efefbba1ddf077d3a589468b77d2d1 Mon Sep 17 00:00:00 2001 From: Tomas Musil Date: Thu, 17 Oct 2013 23:57:41 +0200 Subject: [PATCH] simple sgf output --- doc/modules.rst | 6 +++--- imago_pack/imago.py | 12 +++++++++--- imago_pack/output.py | 45 ++++++++++++++++++++++----------------------- 3 files changed, 34 insertions(+), 29 deletions(-) diff --git a/doc/modules.rst b/doc/modules.rst index 209dd33..4466cfc 100644 --- a/doc/modules.rst +++ b/doc/modules.rst @@ -56,10 +56,10 @@ manual .. automodule:: manual :members: -sgf ---- +output +------ -.. automodule:: sgf +.. automodule:: output :members: im_debug diff --git a/imago_pack/imago.py b/imago_pack/imago.py index c31db64..e2c2876 100755 --- a/imago_pack/imago.py +++ b/imago_pack/imago.py @@ -35,6 +35,8 @@ def argument_parser(): help="save images instead of displaying them") parser.add_argument('-c', '--cache', dest='l_cache', action='store_true', help="use cached lines") + parser.add_argument('-S', '--sgf', dest='sgf_output', action='store_true', + help="output in SGF") parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', help="report progress") return parser @@ -101,10 +103,14 @@ def main(): board = intrsc.board(image, lines, show_all, do_something) - #simple ASCII output: - print board + if len(args.files) == 1: + + if args.sgf_output: + print board.asSGFsetPos() + else: + print board - if len(args.files) > 1: + else: for f in args.files[1:]: try: image = Image.open(f) diff --git a/imago_pack/output.py b/imago_pack/output.py index 82d1e4f..ba43ebd 100644 --- a/imago_pack/output.py +++ b/imago_pack/output.py @@ -4,29 +4,6 @@ import sys COORDS = 'abcdefghijklmnopqrs' -def txt2sgf(board): - """Converts textual represantation to SGF (set position).""" - - board = sys.stdin.read().split('\n') - board = [line.split() for line in board] - - black = [] - white = [] - - for i in range(19): - for j in range(19): - if board[i][j] == 'B': - black.append((i, j)) - elif board[i][j] == 'W': - white.append((i, j)) - - sgf = "" - #TODO version numbering - sgf += "(;FF[4]GM[1]SZ[19]AP[Imago:0.1.0]" - sgf += "AB" + ''.join('[' + coords[j] + coords[i] + ']' for (i, j) in black) - sgf += "AW" + ''.join('[' + coords[j] + coords[i] + ']' for (i, j) in white) - sgf += ")" - class Board: def __init__(self, size, stones): self.stones = stones @@ -43,7 +20,29 @@ class Board: lines.append(" ".join(line)) return ("\n".join(lines)) + def asSGFsetPos(self): + """Returns SGF (set position) representation of the position.""" + + black = [] + white = [] + for i in range(self.size): + for j in range(self.size): + stone = self.stones[i * self.size + j] + if stone == 'B': + black.append((i, j)) + elif stone == 'W': + white.append((i, j)) + + sgf = "" + #TODO version numbering + sgf += "(;FF[4]GM[1]SZ[" + str(self.size) + "]AP[Imago:0.1.0]\n" + sgf += "AB" + ''.join('[' + COORDS[j] + COORDS[i] + ']' + for (i, j) in black) + "\n" + sgf += "AW" + ''.join('[' + COORDS[j] + COORDS[i] + ']' + for (i, j) in white) + "\n" + sgf += ")" + return sgf class Game: def __init__(self, size): -- 2.4.2