def add_stones(pos, black_stones_added, white_stones_added): working_board = np.copy(pos.board) go.place_stones(working_board, go.BLACK, black_stones_added) go.place_stones(working_board, go.WHITE, white_stones_added) new_position = Position(board=working_board, n=pos.n, komi=pos.komi, caps=pos.caps, ko=pos.ko, recent=pos.recent, to_play=pos.to_play) return new_position
def add_stones(pos, black_stones_added, white_stones_added): working_board = np.copy(pos.board) go.place_stones(working_board, go.BLACK, black_stones_added) go.place_stones(working_board, go.WHITE, white_stones_added) new_position = Position(board=working_board, n=pos.n, komi=pos.komi, caps=pos.caps, ko=pos.ko, recent=pos.recent, to_play=pos.to_play) return new_position
def get_board(self): if not self.header or not self.stones: return None pos = board.Board(komi=self.header_prop('KM')) pos.first_color = self.stones[0].color pos.to_move = pos.first_color pos.player1_name = str(self.header_prop('PB')) pos.player2_name = str(self.header_prop('PW')) pos.handicap = self.header_prop('HA') or 0 pos.result = self.header_prop('RE') pos.points = parse_result(pos.result) ab = self.header_prop("AB") aw = self.header_prop("AW") if ab: ms = re.findall(r'\[([^\[\]]+?)\]', ab) ss = list(map(lambda x: parse_move(x), ms)) go.place_stones(pos.stones, go.BLACK, ss) pos.build_groups() if aw: ms = re.findall(r'\[([^\[\]]+?)\]', aw) ss = list(map(lambda x: parse_move(x), ms)) go.place_stones(pos.stones, go.WHITE, ss) pos.build_groups() for stone in self.stones: pos.replay_move(stone.move, stone.color) return pos
def play_move(self, move, color=None, check_legal=True): if check_legal: ill = self.is_move_legal(move) if ill > 0: config.logger.debug('{}棋着法{} {},不合法!'.format(go.get_color_str(self.to_move), \ go.get_cmd_from_coor(move), go.get_legal_str(ill))) return ill, None if color is None: color = self.to_move self.recent.append( PlayerMove(move, self.to_move, self.stones.copy(), self.ko_hash)) if self.ko: self.hash = self.hash ^ dbhash.ZOBRIST[go.KO][dbhash.get_index( self.ko)] self.hash = self.hash ^ dbhash.ZOBRIST_BLACKTOMOVE if move is None or move == go.PASS or move == go.RESIGN: #vertex(20,19) coor(19, 0) self.step += 1 self.to_move = go.oppo_color(self.to_move) self.ko = None return 0, None potential_ko = is_eye(self.stones, move) opp_color = go.oppo_color(color) move_ind = dbhash.get_index(move) self.hash = self.hash ^ dbhash.ZOBRIST[go.EMPTY][move_ind] self.ko_hash = self.ko_hash ^ dbhash.ZOBRIST[go.EMPTY][move_ind] self.stones[move] = color self.hash = self.hash ^ dbhash.ZOBRIST[color][move_ind] self.ko_hash = self.ko_hash ^ dbhash.ZOBRIST[color][move_ind] captured_stones = self.add_stone(move, color) go.place_stones(self.stones, go.EMPTY, captured_stones) if captured_stones: for s in captured_stones: ind = dbhash.get_index(s) self.hash = self.hash ^ dbhash.ZOBRIST[opp_color][ind] self.hash = self.hash ^ dbhash.ZOBRIST[go.EMPTY][ind] self.ko_hash = self.ko_hash ^ dbhash.ZOBRIST[opp_color][ind] self.ko_hash = self.ko_hash ^ dbhash.ZOBRIST[go.EMPTY][ind] if len(captured_stones) == 1 and potential_ko == opp_color: new_ko = list(captured_stones)[0] else: new_ko = None if new_ko: self.hash = self.hash ^ dbhash.ZOBRIST[go.KO][dbhash.get_index( new_ko)] if self.to_move == go.BLACK: new_caps = (self.caps[0] + len(captured_stones), self.caps[1]) else: new_caps = (self.caps[0], self.caps[1] + len(captured_stones)) self.step += 1 self.caps = new_caps self.ko = new_ko self.to_move = go.oppo_color(self.to_move) return 0, captured_stones
def get_try_stones(self, move): if move == go.PASS: return self.stones.copy() pos = self.copy() captured_stones = pos.add_stone(move, pos.to_move) stones = pos.stones go.place_stones(stones, go.EMPTY, captured_stones) stones[move] = self.to_move return stones
def replay_move(self, move, color=None): if move is None or move == go.PASS or move == go.RESIGN: #vertex(20,19) coor(19, 0) self.play_move(go.PASS) return if color is None: color = self.to_move self.recent.append( PlayerMove(move, self.to_move, self.stones.copy(), self.ko_hash)) self.stones[move] = color captured_stones = self.add_stone(move, color) go.place_stones(self.stones, go.EMPTY, captured_stones) if self.to_move == go.BLACK: new_caps = (self.caps[0] + len(captured_stones), self.caps[1]) else: new_caps = (self.caps[0], self.caps[1] + len(captured_stones)) self.caps = new_caps self.step += 1 self.to_move = go.oppo_color(self.to_move)
def scorebw(self): 'Return score from B perspective. If W is winning, score is negative.' working_board = self.stones.copy() bs = [] ws = [] has_uk = False while go.EMPTY in working_board: bk = [] wk = [] uk = [] unassigned_spaces = np.where(working_board == go.EMPTY) c = unassigned_spaces[0][0] territory, borders = find_reached(working_board, c) border_colors = set(working_board[b] for b in borders) X_border = go.BLACK in border_colors O_border = go.WHITE in border_colors if X_border and not O_border: bk += territory elif O_border and not X_border: wk += territory else: uk += territory has_uk = True go.place_stones(working_board, go.BLACK, bk) go.place_stones(working_board, go.WHITE, wk) go.place_stones(working_board, go.UNKNOWN, uk) bs += bk ws += wk blacks = np.count_nonzero(working_board == go.BLACK) whites = np.count_nonzero(working_board == go.WHITE) return (blacks - whites - self.komi - self.handicap, bs, ws, has_uk)
def add_from_qp(self): while config.running: if self.data_size>6400: self.save() self.clear() self.qp.clear() left = random.randint(0,100) top = random.randint(0,160) width = random.randint(2400, 3200-left) height = random.randint(1200, 1800-top) self.qp.resize(left,top, width, height) self.qp.board = board.Board() #if random.randint(0,100)>50: #moves = random.choices(go.ALL_COORDS, k=random.randint(0,int(go.N*go.N*0.8))) #moves = [go.get_coor_from_vertex(move) for move in moves] for i in range(random.randint(0,int(go.N*go.N*0.8))): move = random.choice(go.ALL_COORDS) #move = select_random(self.qp.board, True) #self.qp.board.play_move(move) #self.qp.update() go.place_stones(self.qp.board.stones, random.randint(go.BLACK,go.WHITE), [move]) self.qp.update() #time.sleep(0.1) im = pag.screenshot(region=(0, 0, 1800, 1800)) im = im.resize((IMAGE_SIZE,IMAGE_SIZE), Image.ANTIALIAS) im = np.array(im).swapaxes(0, 2) while not self.event.is_set(): self.event.wait(0.1) self.event.clear() self.imgs.append(im) self.labels.append(self.qp.get_label()) self.data_size = len(self.labels) self.event.set() if self.data_size>640: self.save() self.clear()