def find_furthest(world: World, list_vertex): my_head = world.get_self().get_head() heads = [] for s in world.snakes: head = world.snakes[s].get_head() if not my_head == head: heads.append(head) dist0 = my_head.dist(heads[0]) dist1 = my_head.dist(heads[1]) dist2 = my_head.dist(heads[2]) nearest_head = None for index in range(6): if index > dist0: nearest_head = heads[0] break if index > dist1: nearest_head = heads[1] break if index > dist2: nearest_head = heads[2] break if nearest_head is None: return None distances = [] for v in list_vertex: distances.append(v.dist(nearest_head)) max_dist = max(distances) if len(distances) > 0 and max_dist == distances[0]: return list_vertex[0] if len(distances) > 1 and max_dist == distances[1]: return list_vertex[1] if len(distances) > 2 and max_dist == distances[2]: return list_vertex[2] return None
def thrd_L(world: World, first_L): for act_one in first_L: move_one = act_one[0] for act_two in act_one[1]: move_two = act_two[0] head = world.get_self().get_head() head_pos = ((world.get_self().get_head() + move_one) + move_two) okays = [] next_head = [] next_head.append(head_pos + Vector2D(1, 0)) next_head.append(head_pos + Vector2D(0, 1)) next_head.append(head_pos + Vector2D(-1, 0)) next_head.append(head_pos + Vector2D(0, -1)) next_move = [] next_move.append(Vector2D(1, 0)) next_move.append(Vector2D(0, 1)) next_move.append(Vector2D(-1, 0)) next_move.append(Vector2D(0, -1)) next_head_ok = [True, True, True, True] h_number = 0 for h in next_head: accident = False for s in world.snakes: if h in world.snakes[s].get_body(): accident = True break if h in world.get_walls(): accident = True if (h == (head + move_one)): accident = True if accident: next_head_ok[h_number] = False h_number += 1 for i in range(4): if next_head_ok[i]: score = evaluate(world, next_head[i], (move_one + head), head_pos) okays.append([next_move[i], score]) act_two.append(okays)
def is_accident(world: World, i, j): vertex = Vector2D(i, j) accident = False for s in world.snakes: if vertex in world.snakes[s].get_body(): accident = True break if vertex in world.get_walls(): accident = True return accident
def get_action(world: World): head_pos = world.get_self().get_head() next_head = [] next_head.append(head_pos + Vector2D(1, 0)) next_head.append(head_pos + Vector2D(0, 1)) next_head.append(head_pos + Vector2D(-1, 0)) next_head.append(head_pos + Vector2D(0, -1)) next_head_ok = [True, True, True, True] h_number = 0 for h in next_head: accident = False for s in world.snakes: if h in world.snakes[s].get_body(): accident = True break if accident: print(h, 'snake') if h in world.get_walls(): accident = True print(h, 'wall') if accident: next_head_ok[h_number] = False h_number += 1 print(next_head) print(next_head_ok) min_dist = 1000 h_best = -1 for h in range(4): if not next_head_ok[h]: continue dist = next_head[h].dist(world.goal_position) if dist < min_dist: min_dist = dist h_best = h print(h_best) actions = ['d', 'r', 'u', 'l'] if h_best > 0: return actions[h_best] return actions[0]
def scnd_L(world: World, first_L): for act in first_L: okays = [] head_pos = (world.get_self().get_head() + act[0]) next_head = [] next_head.append(head_pos + Vector2D(1, 0)) next_head.append(head_pos + Vector2D(0, 1)) next_head.append(head_pos + Vector2D(-1, 0)) next_head.append(head_pos + Vector2D(0, -1)) next_move = [] next_move.append(Vector2D(1, 0)) next_move.append(Vector2D(0, 1)) next_move.append(Vector2D(-1, 0)) next_move.append(Vector2D(0, -1)) next_head_ok = [True, True, True, True] h_number = 0 for h in next_head: accident = False for s in world.snakes: if h in world.snakes[s].get_body(): accident = True break if h in world.get_walls(): accident = True if accident: next_head_ok[h_number] = False h_number += 1 for i in range(4): if next_head_ok[i]: #score = evaluate(world,next_head[i]) okays.append([next_move[i]]) act.append(okays)
def run(): parser = ArgumentParser() parser.add_argument("-n", "--name", dest="name", type=str, default='team_name' + str(random.randint(0, 10000)), help="Client Name", metavar="NAME") parser.add_argument("-c", "--client", dest="client_type", type=str, default='auto', help="greedy, random, hand, auto", metavar="ClientType") args = parser.parse_args() sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.settimeout(1) server_address = ('localhost', 20002) world = World() message_snd = MessageClientConnectRequest(args.name).build() while is_run: sock.sendto(message_snd, server_address) try: message_rcv = sock.recvfrom(4096) except: continue message = parse(message_rcv[0]) if message.type == 'MessageClientConnectResponse': print('my id is ' + str(message.id)) world.set_id(message.id, message.ground_config['goal_id']) break while is_run: try: r = sock.recvfrom(4096) except: continue message = parse(r[0]) if message.type == 'MessageClientDisconnect': break elif message.type == 'MessageClientWorld': world.update(message) world.print() if args.client_type == 'greedy' or (args.client_type == 'auto' and world.self_id == 1): action = c_greedy.get_action(world) elif args.client_type == 'random' or (args.client_type == 'auto' and world.self_id >= 2): action = c_random.get_action(world) elif args.client_type == 'best': action = c_best.get_action(world) elif args.client_type == 'your': action = c_your.get_action(world) elif args.client_type == 'hand': action = input('enter action (u or d or l or r:') sock.sendto(MessageClientAction(string_action=action).build(), server_address)
def first_L(world: World): head_pos = world.get_self().get_head() okays = [] next_head = [] next_head.append(head_pos + Vector2D(1, 0)) next_head.append(head_pos + Vector2D(0, 1)) next_head.append(head_pos + Vector2D(-1, 0)) next_head.append(head_pos + Vector2D(0, -1)) next_move = [] next_move.append(Vector2D(1, 0)) next_move.append(Vector2D(0, 1)) next_move.append(Vector2D(-1, 0)) next_move.append(Vector2D(0, -1)) next_head_ok = [True, True, True, True] h_number = 0 for h in next_head: accident = False for s in world.snakes: if h in world.snakes[s].get_body(): accident = True break if h in world.get_walls(): accident = True if accident: next_head_ok[h_number] = False h_number += 1 for i in range(4): if next_head_ok[i]: okays.append([next_move[i]]) return okays
def get_action(world: World): head_pos = world.get_self().get_head() ways = first_L(world) scnd_L(world, ways) thrd_L(world, ways) print(ways) print(world.cycle) act = find_max(world, ways) action = translator(act) return action
def possible_to_accident(world: World, i, j): vertex = Vector2D(i, j) my_head = world.get_self().get_head() possible = False for s in world.snakes: head = world.snakes[s].get_head() if not my_head == head: if vertex == head + Vector2D(1, 0): possible = True if vertex == head + Vector2D(0, 1): possible = True if vertex == head + Vector2D(-1, 0): possible = True if vertex == head + Vector2D(0, -1): possible = True return possible
def get_minimum_other_snakes(world: World): my_head = world.get_self().get_head() lengths = [] for s in world.snakes: head = world.snakes[s].get_head() if not my_head == head: cur_vertex = Vertex(head.i, head.j) # lengths.append(bfs_for_other_snakes(world, cur_vertex)) lengths.append(head.dist(world.goal_position)) if len(lengths) > 0: minimum = lengths[0] print("lengths : ", len(lengths)) for l in lengths: if l < minimum: minimum = l return minimum else: print("nashodkeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee") return 1000
def first_pos(world: World, bdy): head = world.get_self().get_head() bdy.append(head) if head == Vector2D(1, 3): bdy.append(Vector2D(1, 2)) bdy.append(Vector2D(1, 1)) elif head == Vector2D(1, 26): bdy.append(Vector2D(1, 27)) bdy.append(Vector2D(1, 28)) elif head == Vector2D(28, 3): bdy.append(Vector2D(28, 2)) bdy.append(Vector2D(28, 1)) elif head == Vector2D(28, 26): bdy.append(Vector2D(28, 27)) bdy.append(Vector2D(28, 28))
def get_action(world: World): head_pos = world.get_self().get_head() next_head = [] next_head.append(head_pos + Vector2D(1, 0)) next_head.append(head_pos + Vector2D(0, 1)) next_head.append(head_pos + Vector2D(-1, 0)) next_head.append(head_pos + Vector2D(0, -1)) min_dist = 1000 h_best = -1 for h in range(4): dist = next_head[h].dist(world.goal_position) if dist < min_dist: min_dist = dist h_best = h print(h_best) actions = ['d', 'r', 'u', 'l'] if h_best > 0: return actions[h_best] return actions[0]
def get_action(world: World): board = world.board opened = [[False for x in range(len(board[0]))] for y in range(len(board))] print(len(board), len(board[0])) head_pos = world.get_self().get_head() min_other = get_minimum_other_snakes(world) my_dist = head_pos.dist(world.goal_position) if my_dist - min_other < 1 or my_dist - min_other > 15 or my_dist > 20: # start cur_vertex = Vertex(head_pos.i, head_pos.j) queue.append(cur_vertex) print( "Saaaaaaaaaaaaaaaaaaaaaalaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaam" ) while not is_finished(world, cur_vertex): if len(queue) > 0: cur_vertex = queue.pop(0) opened[cur_vertex.row][cur_vertex.column] = True #print(cur_vertex.row, " , ", cur_vertex.column) open_vertex(world, cur_vertex, opened) if len(queue) == 0: break goal_pos = world.goal_position temp_vertex = Vertex(goal_pos.i, goal_pos.j) cur_vertex.path.append(temp_vertex) queue.clear() print("***************** finally :") if len(cur_vertex.path) > 1: answer_row = cur_vertex.path[1].row answer_column = cur_vertex.path[1].column answer = Vector2D(answer_row - head_pos.i, answer_column - head_pos.j) print(answer) if answer == Vector2D(1, 0): return 'd' elif answer == Vector2D(0, 1): return 'r' elif answer == Vector2D(-1, 0): return 'u' elif answer == Vector2D(0, -1): return 'l' print("****************** nashod ke") actions = [] if is_accident(world, head_pos.i + 1, head_pos.j) is False \ and action_to_lose(world, head_pos.i + 1, head_pos.j) is False: actions.append('d') if is_accident(world, head_pos.i - 1, head_pos.j) is False \ and action_to_lose(world, head_pos.i - 1, head_pos.j) is False: actions.append('u') if is_accident(world, head_pos.i, head_pos.j + 1) is False \ and action_to_lose(world, head_pos.i, head_pos.j + 1) is False: actions.append('r') if is_accident(world, head_pos.i, head_pos.j - 1) is False \ and action_to_lose(world, head_pos.i, head_pos.j - 1) is False: actions.append('l') list_vertex = [] for a in actions: if a == 'd': list_vertex.append(Vector2D(head_pos.i + 1, head_pos.j)) if a == 'u': list_vertex.append(Vector2D(head_pos.i - 1, head_pos.j)) if a == 'r': list_vertex.append(Vector2D(head_pos.i, head_pos.j + 1)) if a == 'l': list_vertex.append(Vector2D(head_pos.i, head_pos.j - 1)) better_answer = None if len(list_vertex) > 0: better_answer = find_furthest(world, list_vertex) if better_answer is not None: action = 'r' if better_answer == Vector2D(head_pos.i + 1, head_pos.j): action = 'd' if better_answer == Vector2D(head_pos.i - 1, head_pos.j): action = 'u' if better_answer == Vector2D(head_pos.i, head_pos.j + 1): action = 'r' if better_answer == Vector2D(head_pos.i, head_pos.j - 1): action = 'l' return action if len(actions) > 0: action = actions[random.randint(0, len(actions) - 1)] return action return 'r'