def __init__(self, fp): game_str = "" self.game_lines = fp.readlines() ## parse comment self.parse_comment() self.print_comment() self.remove_comment() self.delete_comment() ## look for the first (main) variation for i, line in enumerate(self.game_lines): res = self.reg_prop.search(line) if res: dbg_p("firstline:",line[:-1]) self.game_var.append([[self.var], 0]) self.start_end.append([i,res.start(), None]) self.prop_start.append(len(self.game_var)-1) #print "prop_start:*****", self.prop_start break ## find out the line the game play sequence started for m, line in enumerate(self.game_lines[i:]): result = self.reg_bw.search(line) if not result: game_str+=line[:-1] else: break ## check if game play sequence start on the line first property (; start # if m > i: # ## both not on the same line # self.var += 1 ## get game info self.parse_game_info(game_str) ### parse game play self.parse_game_var(m) self.parse_game_seq() ### print parsed game print "var:", self.var for i, line in enumerate(self.start_end): print "game_var:", self.game_var[i], print "\t\tstart_end:", line #print self.prop_start #print self.var_line ## free up space del self.game_lines
def add_me(self, pos, color): dbg_p("") dbg_p("add_me: pos =", pos, "; color =", color) assert (pos not in self.board) self.pos = pos self.color = color; self.invert_color = self.invert_color_dict[color] ## insert into board self.board.update([color+pos]) dbg_p(self.board) self.neighbour_checked = set() self.alive = False self.recursive_check() if self.captured_stone: captured = self.captured_stone self.captured_stone = set() dbg_p("captured:", captured) ## todo: remove captured stone from board for s in captured: self.board.remove(self.invert_color+s) dbg_p("board:", self.board) return captured
def my_liberty(self, pos): ## return the liberties of this stone have liberty=[] ## get the neighbour base on first coordinate of pos if pos[0]=='a': liberty.append('b'+pos[1]) elif pos[0]=='s': liberty.append('r'+pos[1]) else: ## a < pos[0] < s num=ord(pos[0]) liberty.extend([chr(num-1)+pos[1], chr(num+1)+pos[1]]) ## get the neighbour base on second coordinate of pos if pos[1]=='a': liberty.append(pos[0]+'b') elif pos[1]=='s': liberty.append(pos[0]+'r') else: ## a < pos[1] < s num=ord(pos[1]) liberty.extend([pos[0]+chr(num-1), pos[0]+chr(num+1)]) dbg_p("liberty:",liberty) return liberty
def recursive_check(self): for liberty in self.my_liberty(self.pos): dbg_p("liberty:", liberty) if self.invert_color+liberty not in self.board: ## this group have liberty, ignore dbg_p("recursive_check: pass ="******"recursive_check:", self.invert_color+liberty) #self.neighbour_checked.add(self.pos) self.temp.add(liberty) if self.captured_check(liberty): self.captured_stone.update(self.temp) ## reset variable self.temp = set() self.alive = False self.neighbour_checked = set()
def captured_check(self, pos): dbg_p("captured_check: pos =", pos) for liberty in self.my_liberty(pos): if liberty in self.temp or liberty == self.pos: dbg_p("liberty in temp/self.pos:", liberty, ", so pass") continue dbg_p(" == liberty = ", liberty) ## since some are shared liberty between a few stones, ## we want to skip whatever we have checked previously. ## if it has been checked, return False ## or if this group is alive, return False if not self.alive and liberty not in self.neighbour_checked: self.neighbour_checked.add(liberty) dbg_p(" == neighbour_checked =", self.neighbour_checked, ";",self.alive) else: dbg_p(" == neighbour_checked : else, alive =", self.alive) return False ## color here is inverted if self.invert_color+liberty in self.board: ## found connect stone of same color, expand the liberty ## and check if this group is being captured or not dbg_p("recursive capture check:", liberty) self.temp.add(liberty) self.captured_check(liberty) elif self.color+liberty not in self.board: ## this group has at least one liberty dbg_p(" == captured_check: alive, liberty @", liberty) #dbg_p("captured_check: board:", self.board) self.alive = True return False elif self.color+liberty in self.board: ## add the possible dead stone to temp self.temp.add(pos); dbg_p(" == temp =", self.temp) else: dbg_p(" *** why i am here *** ") ## continue recursive check for possible captured stone return True