1 """Imago output module."""
6 COORDS = 'abcdefghijklmnopqrs'
8 # TODO refactor method names
11 """Represents the state of the board."""
12 def __init__(self, size, stones):
17 """Retrun string representation of the board."""
20 for i in range(self.size):
22 for j in range(self.size):
23 line.append(self.stones[k])
25 lines.append(" ".join(line))
27 return ("\n".join(lines))
29 def asSGFsetPos(self):
30 """Returns SGF (set position) representation of the position."""
32 #TODO version numbering
33 sgf = "(;FF[4]GM[1]SZ[" + str(self.size) + "]AP[Imago:0.1.0]\n"
42 for i in range(self.size):
43 for j in range(self.size):
44 stone = self.stones[i * self.size + j]
46 black.append(Move('B', i, j))
48 white.append(Move('W', i, j))
51 sgf += "AB" + ''.join('[' + m.sgf_coords() + ']'
52 for m in black) + "\n"
54 sgf += "AW" + ''.join('[' + m.sgf_coords() + ']'
55 for m in white) + "\n"
59 """Add move to the board."""
60 self.stones[(m.y * self.size) + m.x] = m.color
62 def getMoveCandidates(self, board):
63 """Take the next board in game and return a list of moves that are
66 for i in range(self.size):
67 for j in range(self.size):
68 if (self.stones[self.size * i + j] == "."):
69 if (board.stones[self.size * i + j] == "W"):
70 candidates.append(Move("W", i, j))
71 elif (board.stones[self.size * i + j] == "B"):
72 candidates.append(Move("B", i, j))
76 """Repsresents a move."""
77 def __init__(self, color, y, x, comment=None):
81 self.comment = comment
84 """Return coordinates of the move in SGF."""
85 return COORDS[self.x] + COORDS[self.y]
88 """Represents a game."""
89 def __init__(self, size, board=None, debug=True):
90 self.init_board = board or Board(size, (size * size) * ".")
91 self.board = copy.deepcopy(self.init_board)
95 self.debug_comment = ""
97 def addMove(self, board):
98 """Add next move to the game."""
99 candidates = self.board.getMoveCandidates(board)
102 comment += "Candidates: " + str(len(candidates))
105 self.debug_comment += "No candidates."
109 move.comment = comment
110 self.moves.append(move)
111 self.board.addMove(move)
114 """Return the game representation as SGF string."""
115 sgf = "(;FF[4]GM[1]SZ[" + str(self.size) + "]AP[Imago:0.1.0]\n"
116 sgf += self.init_board.SGFpos()
119 sgf += ";" + m.color + "[" + m.sgf_coords() + "]"
121 sgf += "C[" + m.comment + "]"