simple sgf output
authorTomas Musil <tomik.musil@gmail.com>
Thu, 17 Oct 2013 21:57:41 +0000 (23:57 +0200)
committerTomas Musil <tomik.musil@gmail.com>
Thu, 17 Oct 2013 21:57:41 +0000 (23:57 +0200)
doc/modules.rst
imago_pack/imago.py
imago_pack/output.py

index 209dd33..4466cfc 100644 (file)
@@ -56,10 +56,10 @@ manual
 .. automodule:: manual
     :members:
 
-sgf
----
+output
+------
 
-.. automodule:: sgf
+.. automodule:: output
     :members:
 
 im_debug
index c31db64..e2c2876 100755 (executable)
@@ -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)
index 82d1e4f..ba43ebd 100644 (file)
@@ -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):