def generate_moves(board, use_pattern, check_selfatari): """ generate a list of policy moves on board for board.current_player. calls function from util folder for fallback """ if use_pattern: if board.last_move is not None: # checks if atari capture possible single_liberty = [] liberty_point = board._single_liberty( board.last_move, GoBoardUtil.opponent(board.current_player)) if liberty_point is not None and board.check_legal( liberty_point, board.current_player): single_liberty.append(liberty_point) #checks for selfatari single_liberty = GoBoardUtil.filter_moves( board, single_liberty, check_selfatari) if len(single_liberty) > 0: return single_liberty, "AtariCapture" #checks for atari defense defense_points = try_to_defend(board) defense_points = GoBoardUtil.filter_moves(board, defense_points, check_selfatari) if len(defense_points) > 0: return defense_points, "AtariDefense" policy_moves, type_of_move = GoBoardUtil.generate_all_policy_moves( board, use_pattern, check_selfatari) return policy_moves, type_of_move
def generate_all_policy_moves(board, pattern, check_selfatari): """ generate a list of policy moves on board for board.current_player. Use in UI only. For playing, use generate_move_with_filter which is more efficient """ if board.last_move != None: # AtariCapture atari_capture_move = GoBoardUtil_Go3.generate_atari_capture_move(board) if atari_capture_move: return [atari_capture_move], "AtariCapture" # AtariDefense atari_defense_moves = GoBoardUtil_Go3.generate_atari_defense_moves(board) if len(atari_defense_moves) > 0: return atari_defense_moves, "AtariDefense" # Pattern if pattern: pattern_moves = [] pattern_moves = GoBoardUtil.generate_pattern_moves(board) pattern_moves = GoBoardUtil.filter_moves(board, pattern_moves, check_selfatari) if len(pattern_moves) > 0: return pattern_moves, "Pattern" # Random return GoBoardUtil.generate_random_moves(board,True), "Random"
def generate_all_policy_moves(self, board,pattern,check_selfatari): """ generate a list of policy moves on board for board.current_player. Use in UI only. For playing, use generate_move_with_filter which is more efficient """ if pattern: atari_moves,msg = self.generate_atari_moves(board) atari_moves = GoBoardUtil.filter_moves(board, atari_moves, check_selfatari) if len(atari_moves) > 0: return atari_moves, msg pattern_moves = [] pattern_moves = GoBoardUtil.generate_pattern_moves(board) pattern_moves = GoBoardUtil.filter_moves(board, pattern_moves, check_selfatari) if len(pattern_moves) > 0: return pattern_moves, "Pattern" return GoBoardUtil.generate_random_moves(board,True), "Random"
def atari_def(self): player = self.board.current_player last_move = self.board.last_move if last_move is None: return False S, _, _ = self.board.find_S_and_E(player) moves = [] for stone in S: if self.board._liberty(stone, player) == 1: defense_p = self.board._single_liberty(stone, player) board = self.board.copy() board.move(defense_p, player) if board._liberty(defense_p, player) > 1: moves.append(defense_p) moves = GoBoardUtil.filter_moves(self.board, moves, True) if len(moves) < 1: return False self.respond('AtariDefense ' + GoBoardUtil.sorted_point_string(moves, self.board.NS)) return True
def atari_capture(self): #capture the last move if: last_move = self.board.last_move if last_move == None: return None opponent = GoBoardUtil.opponent(self.board.current_player) x, y = GoBoardUtil.point_to_coord(last_move, self.board.NS) if (self.board._liberty(last_move, opponent) == 1): capture_point = self.board._single_liberty(last_move, opponent) bcopy = self.board.copy() legal = bcopy.move(capture_point, self.board.current_player) filtered_moves = GoBoardUtil.filter_moves( self.board, [capture_point], self.go_engine.check_selfatari) if legal and len(filtered_moves): return filtered_moves #1 - It only has one liberty left #2 - The move is legal return None
def atari_defense(self): last_move = self.board.last_move if last_move == None: return None before_last_move = self.board.copy() before_last_move.undo_move() opponent = GoBoardUtil.opponent(self.board.current_player) S, E, S_eyes = self.board.find_S_and_E(self.board.current_player) run_moves = [] for i in S: if self.board._liberty(i, self.board.current_player ) == 1 and before_last_move._liberty( i, self.board.current_player) != 1: #If there is only one liberty, defend it #Check if it was only the last move run_away_point = self.board._single_liberty( i, self.board.current_player) bcopy = self.board.copy() bcopy.move(run_away_point, self.board.current_player) if bcopy._liberty(run_away_point, self.board.current_player) > 1: run_moves.append(run_away_point) #next check if capturing any of the opponents stones would increase the liberty sprime, ep, s_prime_eyes = self.board.find_S_and_E(opponent) for y in sprime: capture_point = self.board._single_liberty(y, opponent) bcopy = self.board.copy() bcopy.move(capture_point, self.board.current_player) if (bcopy._liberty(i, self.board.current_player) > 1): run_moves.append(capture_point) filtered_moves = GoBoardUtil.filter_moves( self.board, run_moves, self.go_engine.check_selfatari) if not len(filtered_moves): return None else: return run_moves
def policy_moves_cmd(self, args): """ Return list of policy moves for the current_player of the board """ #ATARI CAPTURE if self.board.last_move != None: moves = self.last_moves_empty() diagonal = self.board._diag_neighbors(self.board.last_move) capture_moves = list(set(moves) - set(diagonal)) capture_moves = GoBoardUtil.filter_moves( self.board, capture_moves, self.go_engine.check_selfatari) if (len(capture_moves) == 1): if (self.board._liberty( capture_moves[0], self.board._points_color(capture_moves[0])) == 1): policy_moves, type_of_move = capture_moves, 'AtariCapture' response = type_of_move + " " + GoBoardUtil.sorted_point_string( policy_moves, self.board.NS) self.respond(response) # self.respond("akjhfjkasdhfkjahsd") return # # ATARI DEFENCE # else: # defence_moves=[] # moves = self.board._neighbors(self.board.last_move) # # moves.extend(board._diag_neighbors2(board.last_move)) # for move in moves: # if(self.board._single_liberty(move,self.board.current_player)!=None): # # print(self.board._single_liberty(move,self.board.current_player)) # # print(self.board._point_to_coord(move)) # defence_moves.append(self.board._single_liberty(move,self.board.current_player)) # if(defence_moves != []): # defence_moves = GoBoardUtil.filter_moves(self.board, defence_moves, self.go_engine.check_selfatari) # policy_moves, type_of_move = defence_moves, 'AtariDefense' # response = type_of_move + " " + GoBoardUtil.sorted_point_string(policy_moves, self.board.NS) # self.respond(response) # return defence_moves = [] lm = self.board.last_move if lm != None: current_play = self.board.current_player opponent = GoBoardUtil.opponent(self.board.current_player) for elem in self.board._neighbors(lm): val = GoBoardUtil.color_to_int(self.board._points_color(elem)) if current_play == val: # print(self.board._neighbors(elem)) # if self.board._neighbors(val) != None: if (self.board._single_liberty( elem, GoBoardUtil.int_to_color(val)) != None): # val3 = GoBoardUtil.color_to_int(self.board._points_color(elem)) if self.board._liberty( self.board._single_liberty( elem, GoBoardUtil.int_to_color(val)), current_play) > 1: defence_moves.append( self.board._single_liberty( elem, GoBoardUtil.int_to_color(val))) ng = self.board._neighbors(lm) dg = self.board._diag_neighbors(self.board.last_move) all_ng = ng + dg # print(all_ng) count = 0 for elem in all_ng: val = GoBoardUtil.color_to_int(self.board._points_color(elem)) if opponent == val: # print(self.board._single_liberty(elem, GoBoardUtil.int_to_color(val))) if (self.board._single_liberty( elem, GoBoardUtil.int_to_color(val)) != None): # print(elem) for i in self.board._neighbors(elem): # print(i) val2 = GoBoardUtil.color_to_int( self.board._points_color(i)) if (val2 == opponent): # print(self.board._liberty(i, GoBoardUtil.int_to_color(val2))) if (self.board._liberty( i, GoBoardUtil.int_to_color(val2)) != None): count += 1 # print(count) if (count == 0): defence_moves.append( self.board._single_liberty( elem, GoBoardUtil.int_to_color(val))) count = 0 # print(self.board.co defence_moves) # for v in defence_moves: # print(v) defence_moves = GoBoardUtil.filter_moves( self.board, defence_moves, self.go_engine.check_selfatari) defence_moves = GoBoardUtil.sorted_point_string( defence_moves, self.board.NS) if len(defence_moves) > 0: # defence_moves = GoBoardUtil.filter_moves(self.board,defence_moves, self.go_engine.check_selfatari) self.respond("AtariDefense " + defence_moves) return policy_moves, type_of_move = GoBoardUtil.generate_all_policy_moves( self.board, self.go_engine.use_pattern, self.go_engine.check_selfatari) if len(policy_moves) == 0: self.respond("Pass") else: response = type_of_move + " " + GoBoardUtil.sorted_point_string( policy_moves, self.board.NS) self.respond(response) return