more comments in pcf.c
[imago.git] / imago_pack / output.py
index ba43ebd..366d0d9 100644 (file)
@@ -1,5 +1,6 @@
 """Imago output module."""
 
 """Imago output module."""
 
+import copy
 import sys
 
 COORDS = 'abcdefghijklmnopqrs'
 import sys
 
 COORDS = 'abcdefghijklmnopqrs'
@@ -18,11 +19,19 @@ class Board:
                 line.append(self.stones[k])
                 k += 1
             lines.append(" ".join(line))
                 line.append(self.stones[k])
                 k += 1
             lines.append(" ".join(line))
+        lines.append("")
         return ("\n".join(lines))
 
     def asSGFsetPos(self):
         """Returns SGF (set position) representation of the position."""
 
         return ("\n".join(lines))
 
     def asSGFsetPos(self):
         """Returns SGF (set position) representation of the position."""
 
+        #TODO version numbering
+        sgf = "(;FF[4]GM[1]SZ[" + str(self.size) + "]AP[Imago:0.1.0]\n"
+        sgf += self.SGFpos()
+        sgf += ")"
+        return sgf
+
+    def SGFpos(self):
         black = []
         white = []
 
         black = []
         white = []
 
@@ -30,25 +39,76 @@ class Board:
             for j in range(self.size):
                 stone = self.stones[i * self.size + j]
                 if stone == 'B':
             for j in range(self.size):
                 stone = self.stones[i * self.size + j]
                 if stone == 'B':
-                    black.append((i, j))
+                    black.append(Move('B', i, j))
                 elif stone == 'W':
                 elif stone == 'W':
-                    white.append((i, j))
-
+                    white.append(Move('W', i, j))
         sgf = ""
         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 += ")"
+        if len(black) > 0:
+            sgf += "AB" + ''.join('[' + m.sgf_coords() + ']'
+                              for m in black) + "\n"
+        if len(white) > 0:
+            sgf += "AW" + ''.join('[' + m.sgf_coords() + ']' 
+                              for m in white) + "\n"
         return sgf
 
         return sgf
 
+    def addMove(self, m):
+        self.stones[(m.y * self.size) + m.x] = m.color
+
+    def getMoveCandidates(self, board):
+        candidates = []
+        for i in range(self.size):
+            for j in range(self.size):
+                if (self.stones[self.size * i + j] == "."):
+                    if (board.stones[self.size * i + j] == "W"):
+                        candidates.append(Move("W", i, j))
+                    elif (board.stones[self.size * i + j] == "B"):
+                        candidates.append(Move("B", i, j))
+        return candidates
+
+class Move:
+    def __init__(self, color, y, x, comment=None):
+        self.color = color
+        self.x = x
+        self.y = y
+        self.comment = comment
+
+    def sgf_coords(self):
+        return COORDS[self.x] + COORDS[self.y]
+
 class Game:
 class Game:
-    def __init__(self, size):
-        self.board =  (size * size) * "."
+    def __init__(self, size, board=None, debug=True):
+        self.init_board = board or Board(size, (size * size) * ".")
+        self.board = copy.deepcopy(self.init_board)
         self.moves = []
         self.moves = []
+        self.size = size
+        self.debug = debug
+        self.debug_comment = ""
+
+    def addMove(self, board):
+        candidates = self.board.getMoveCandidates(board)
+        if self.debug:
+            comment = str(board)
+            comment += "Candidates: " + str(len(candidates))
+            
+        if not candidates:
+            self.debug_comment += "No candidates."
+            return
+
+        move = candidates[0]
+        move.comment = comment
+        self.moves.append(move)
+        self.board.addMove(move)
+
+    def asSGF(self):
+        sgf = "(;FF[4]GM[1]SZ[" + str(self.size) + "]AP[Imago:0.1.0]\n"
+        sgf += self.init_board.SGFpos()
+        for m in self.moves:
+            if m:
+                sgf += ";" + m.color + "[" + m.sgf_coords() + "]"
+                if m.comment:
+                    sgf += "C[" + m.comment + "]"
+                sgf += "\n"
+        sgf += ")"
+        return sgf
 
 
-    def add(self, board):
-        self