def connect_lonely_people(player, board, distances, game_state): aims = [] moves = [] my_groups = [] for tile in board: if board[tile]["player"] == player: g = group(tile, board) if g not in my_groups: my_groups.append(g) for my_group in my_groups: libs = liberties(my_group[0], board) for l in libs: for n in board[l]["neighbours"]: for mg in my_groups: if n in mg: if mg != my_group: aims.append(l) if n in liberties(mg[0], board): if mg != my_group: aims.append(l) for i in aims: if board[i]["player"] == None and is_suicidal(player, i, board) == False: if is_ko == False or game_state["last_ko"] == False: good = False for n in board[i]["neighbours"]: a = is_alive(n, board) if a == False: good = True if good == True: moves.append(i) return moves
def seal_off_territory(player, board, distances, game_state): aims = [] moves = [] for tile in distances[2]: if board[tile]["player"] == player: for n in board[tile]["neighbours"]: if n in distances[2]: if board[n]["player"] is None: aims.append(n) for nn in board[n]["neighbours"]: if nn in distances[2] and nn != tile: if board[nn]["player"] is None: aims.append(nn) if n in distances[1]: good = True for nn in board[n]["neighbours"]: if board[nn]["player"] == player and nn in distances[1]: good = False if good == True: aims.append(n) for i in aims: if board[i]["player"] == None and is_suicidal(player, i, board) == False: if is_ko == False or game_state["last_ko"] == False: moves.append(i) return moves
def is_allowed(tile, board, player, game_state): answer = False if board[tile]["player"] == None: if board[tile]["player"] == None and is_suicidal(player, tile, board) == False: if check_ko(player, tile, board, game_state) == False: answer = True return answer
def invade_large_empty_space(player, board, distances, game_state): moves = [] for tile in board: if tile in distances[2]: good = False if board[tile]["player"] == None and is_suicidal( player, tile, board) == False: if is_ko == False or game_state["last_ko"] == False: good = True for n in board[tile]["neighbours"]: if board[n]["player"] is not None: good = False if n in distances[1]: for nn in board[n]["neighbours"]: if nn != tile and board[nn]["player"] is not None: good = False if good == True: moves.append(tile) return moves
def seal_the_edges(player, board, distances, game_state): aims = [] moves = [] for tile in distances[1]: if board[tile]["player"] == player: for n in board[tile]["neighbours"]: if n in distances[0]: if board[n]["player"] is None: aims.append(n) elif board[n]["player"] != player: for nn in board[n]["neighbours"]: if nn in distances[0]: for nnn in board[nn]["neighbours"]: if board[nnn][ "player"] is not None and board[nnn][ "player"] != player and nnn is not n: aims.append(nn) for i in aims: if board[i]["player"] == None and is_suicidal(player, i, board) == False: if is_ko == False or game_state["last_ko"] == False: moves.append(i) return moves
def play(player,board): #this is a territorial player my_groups=[] enemy_groups=[] my_territory=[] enemy_territory=[] loosely_mine=[] loosely_enemy=[] neutral_space=[] allowed_moves=[] stupid_moves=[] fight_points=[] distances=distances_from_edge(board) #work out what's what for tile in board: if board[tile]["player"]==player: g=group(tile,board) if g not in my_groups: my_groups.append(g) elif board[tile]["player"] is None: if is_ko(player, tile, board)==False or game_state["last_ko"]==False: if is_suicidal(player, tile, board)==False: allowed_moves.append(tile) sort_of_owner=loosely_territory(tile,board) if sort_of_owner==player: loosely_mine.append(tile) elif sort_of_owner=="Neutral": neutral_space.append(tile) elif sort_of_owner=="Nobody": fight_points.append(tile) else: loosely_enemy.append(tile) actual_owner=territory(tile,board) if actual_owner==player: my_territory.append(tile) elif actual_owner=="Neutral": pi=3.14 else: enemy_territory.append(tile) else: h=group(tile,board) if h not in enemy_groups: enemy_groups.append(h) #forget about stuff inside other people's eyes for enemy_group in enemy_groups: rep=enemy_group[0] libs=liberties(rep,board) is_deaded=False eyeness=is_eye_candidate(libs[0],board) if eyeness==None: break else: if eyeness["owner"]==player: enemy_groups.remove(enemy_group) for l in eyeness["group"]: if l in allowed_moves: stupid_moves.append(l) if l in fight_points: fight_points.remove(l) for my_group in my_groups: rep=my_group[0] libs=liberties(rep,board) is_deaded=False eyeness=is_eye_candidate(libs[0],board) if eyeness==None: break else: if eyeness["owner"]!=player: my_groups.remove(my_group) for l in eyeness["group"]: if l in allowed_moves: stupid_moves.append(l) if l in fight_points: fight_points.remove(l) #1: if possible, make a non-alive group of mine be an alive group #2: make a nearly_alive group of the enemy's be a deaded group #3: seal off some loosely territory into proper territory (multiple seal points??) #4: claim some new loosely territory close-ish to the edge (row 3 or 4) #We do these in reverse order so that more important things overwrite the move variable move="pass" #So without further ado: Number 4: break_new_ground=[] for i in allowed_moves: if i in distances[2] or (i in distances[3] and i not in distances[5]): if i in neutral_space: break_new_ground.append(i) #Cool. Now let's try Number 3, securing loose territory: fight_moves=[] for i in fight_points: if i in allowed_moves: fight_moves.append(i) #Before we do that, let's kill dangerous people, 2.5: saviours=[] for my_group in my_groups: a=is_in_atari(my_group,board) if a==True: n_g=neighbouring_groups(my_group[0],board) for nnn in n_g: b=is_in_atari(nnn,board) if b==True and nnn[0] in allowed_moves: saviours.append(liberties(nnn[0],board)[0]) feedback="I'm not doing anything" if len(fight_moves)>0: move=random.choice(fight_moves) feedback="I'm fighting" if len(break_new_ground)>0: move=random.choice(break_new_ground) feedback="I'm breaking new ground" if len(saviours)>0: move=random.choice(saviours) feedback="I'm saving people" #print(feedback) return move
def play(player, board, game_state): #find how far each tile is from the end of the board distances = greatest_distance(board) #patterns is the list of functiosn (declared below) that return true or false when given a move, according to certain criteria. These are in priority order. patterns = [ capture, save_from_probable_death, ladder, invade_large_empty_space, seal_off_territory, seal_the_edges, sneak ] #default move is pass, allowed moves stores all moves that aren't illegal and decisions is a dictionary mapping from pattern to movs that the patern wants to make move = "pass" allowed_moves = [] decisions = {} #friends is a list of tiles that are essential to the life of a group. f is the list of friend moves friends = find_friends(board) f = [] #loop through friends, if any of the moves are allowed then add them to f for i in friends: if is_allowed(i, board, player, game_state) == True: f.append(i) #if there are any moves in f, then pick a random f to return (because friends is the most important patern and is run across the entire board if len(f) > 0: return random.choice(f) #loop through the board, if the move is allowed (not occupied and not suicidal) and is not ko then pretend to play there for tile in board: if board[tile]["player"] == None and is_suicidal(player, tile, board) == False: if check_ko(player, tile, board, game_state) == False: board[tile]["player"] = player #check whether the group is in atari after the move a = is_in_atari(group(tile, board), board) b = False #loop through the board and if the move makes any captures then set b = True for n in board[tile]["neighbours"]: if board[n]["player"] is not None and board[n][ "player"] != player: l = liberties(n, board) if len(l) == 0: b = True #if we are not putting ourselves in atari or we make captures despite this then add the tile to allowed moves if a == False or b == True: allowed_moves.append(tile) #undo the hypothetical move board[tile]["player"] = None #loop through patterns, adding a new empty list for each pattern (these will store the moves that this pattern wants to make) for p in patterns: decisions[p] = [] #loop through all allowed moves and all patterns. for tile in allowed_moves: for pat in patterns: #call the pattern's function on the tile and if the pattern wants to play there, add it to it's corresponding decisions list verdict = pat(tile, player, board, distances, game_state) if verdict == True: decisions[pat].append(tile) #loop through patterns again and if there are any moves for that pattern then choose a random one of them to return for i in patterns: if len(decisions[i]) > 0: move = random.choice(decisions[i]) return move #this will return pass if none of the patterns decide on any moves return move