Fully implemented loading SGF files, although we can only look at the main sequence for now
This commit is contained in:
49
lib/goban.py
49
lib/goban.py
@ -1,17 +1,17 @@
|
||||
import gomill.sgf
|
||||
from gomill import sgf
|
||||
|
||||
|
||||
class Goban:
|
||||
"""Represents the go board. Handles stone placement, captures, etc"""
|
||||
|
||||
# enum values for the board array
|
||||
EMPTY=0
|
||||
WHITE=1
|
||||
BLACK=2
|
||||
SCORE_BLACK=3
|
||||
SCORE_WHITE=4
|
||||
SCORE_DAME=5
|
||||
SCORING=6
|
||||
EMPTY='.'
|
||||
WHITE='w'
|
||||
BLACK='b'
|
||||
SCORE_BLACK='B'
|
||||
SCORE_WHITE='W'
|
||||
SCORE_DAME='d'
|
||||
SCORING='s'
|
||||
|
||||
|
||||
def __init__(self, board_size=19, file_name=None):
|
||||
@ -20,12 +20,6 @@ class Goban:
|
||||
num_points = board_size * board_size
|
||||
self.board = [Goban.EMPTY] * num_points
|
||||
|
||||
self.file_name = file_name
|
||||
self.sgf_game = None
|
||||
|
||||
if self.file_name is not None:
|
||||
self.load_sgf(file_name)
|
||||
|
||||
self.def_draw_codes = self._make_default_draw_codes()
|
||||
|
||||
self.to_move = Goban.BLACK
|
||||
@ -38,22 +32,35 @@ class Goban:
|
||||
self.elapsed_time = 0
|
||||
self.winner = Goban.EMPTY
|
||||
|
||||
self.file_name = file_name
|
||||
self.sgf_game = None
|
||||
|
||||
def load_sgf(self):
|
||||
if self.file_name is not None:
|
||||
self.load_sgf(file_name)
|
||||
|
||||
|
||||
def load_sgf(self, file_name):
|
||||
try:
|
||||
with open(self.file_name, 'r') as fn:
|
||||
with open(file_name, 'r') as fn:
|
||||
self.sgf_game = sgf.Sgf_game.from_string(fn.read())
|
||||
except IOError:
|
||||
# fixme - this should be convertable into a dialog box... perhaps it should throw an exception of its own
|
||||
print 'There was a problem loading the SGF file.'
|
||||
|
||||
# Do initial layout
|
||||
root = self.sgf_game.get_root()
|
||||
if root.has_setup_stones():
|
||||
black, white, empty = root.get_setup_stones()
|
||||
for point in black:
|
||||
self.board[self._real_pos(point)] = Goban.BLACK
|
||||
for point in white:
|
||||
self.board[self._real_pos(point)] = Goban.WHITE
|
||||
|
||||
|
||||
for node in self.sgf_game.get_main_sequence():
|
||||
color, pos = node.get_move()
|
||||
if color == 'b':
|
||||
color = Goban.BLACK
|
||||
elif color == 'w':
|
||||
color = Goban.WHITE
|
||||
self.play_move(color, pos)
|
||||
if color is not None:
|
||||
self.play_move(color, pos)
|
||||
|
||||
|
||||
def set_hover(self, pos):
|
||||
|
Reference in New Issue
Block a user