def do_something(p, m, g): # print(p.__dict__) # print_map(m) # print(g.nodes()) goal_tile = None if p.CarriedRessources < p.CarryingCapacity: goal_tile = TileContent.Resource else: goal_tile = TileContent.House player_pos = (p.Position.to_tuple()[0] - m[0][0].x, p.Position.to_tuple()[1] - m[0][0].y) dest_pos = bfs(m, g, player_pos, goal_tile) if dest_pos is None: goal_tile = TileContent.Wall g = create_graph(m, True) dest_pos = bfs(m, g, player_pos, goal_tile) next_pos = nx.astar_path(g, player_pos, dest_pos)[1] # print(player_pos) # print(dest_pos) # print(next_pos) if next_pos == dest_pos: if (goal_tile == TileContent.Resource): return p.collect( Point(next_pos[0] + m[0][0].x, next_pos[1] + m[0][0].y)) elif (goal_tile == TileContent.Wall): return p.attack( Point(next_pos[0] + m[0][0].x, next_pos[1] + m[0][0].y)) return p.move(Point(next_pos[0] + m[0][0].x, next_pos[1] + m[0][0].y))
def test_link(): print("Testing single link") a = Node(Point(1, 1)) b = Node(Point(4, 5)) a.link(b) assert (len(a.neighbor_list()) == 1) assert (len(b.neighbor_list()) == 1) assert (a.neighbor_list()[0].loc() == b.loc()) assert (b.neighbor_list()[0].loc() == a.loc()) print("Testing multilink") c = Node(Point(3, 8)) d = Node(Point(-3, 9)) a.link(c) a.link(d) b.link(c) c.link(d) assert (len(a.neighbor_list()) == 3) a_neighbor_list_locs = [k.loc() for k in a.neighbor_list()] assert (b.loc() in a_neighbor_list_locs) assert (c.loc() in a_neighbor_list_locs) assert (d.loc() in a_neighbor_list_locs) assert (len(b.neighbor_list()) == 2) b_neighbor_list_locs = [k.loc() for k in b.neighbor_list()] assert (a.loc() in b_neighbor_list_locs) assert (c.loc() in b_neighbor_list_locs) assert (len(c.neighbor_list()) == 3) c_neighbor_list_locs = [k.loc() for k in c.neighbor_list()] assert (a.loc() in c_neighbor_list_locs) assert (b.loc() in c_neighbor_list_locs) assert (d.loc() in c_neighbor_list_locs) assert (len(d.neighbor_list()) == 2) d_neighbor_list_locs = [k.loc() for k in d.neighbor_list()] assert (a.loc() in d_neighbor_list_locs) assert (c.loc() in d_neighbor_list_locs)
def generate_segs(n, seed=None): if (seed is not None): np.random.seed(seed) rand = lambda: np.random.randint(0, 5 * bound) for i in range(n): q, w = rand(), rand() while q == w: w = rand() starting_segments.append( Segment(Point(q, rand(), 5, homogeneous=True), Point(w, rand(), 5, homogeneous=True)))
def test_closest_node(): """ Tests the closest_node function """ print("Testing default Point(0, 0) origin") for root in close_data: closest_node = structs.closest_node(root) assert (closest_node.loc() == Point(1, 2)) custom_origin = Point(-9, 7) print("Testing origin of %s" % str(custom_origin)) for root in close_data: closest_node = structs.closest_node(root, custom_origin) assert (closest_node.loc() == Point(-9, 8))
def test_make_forest(): """ Tests the make forest function """ print("Preliminary test") forest_lite = make_forest(forest_data_lite) forest_lite_actual_locs = [Point(1, 1), Point(4, 5), Point(8, 9)] assert (len(forest_lite) == 3) forest_lite_locs = [n.loc() for n in forest_lite] for loc in forest_lite_actual_locs: assert (loc in forest_lite_locs) print("Extensive test") forest = make_forest(forest_data)
def bot(): """ Main de votre bot. """ map_json = request.form["map"] # Player info encoded_map = map_json.encode() map_json = json.loads(encoded_map.decode('utf-8')) p = map_json["Player"] pos = p["Position"] x = pos["X"] y = pos["Y"] house = p["HouseLocation"] player = Player(p["Health"], p["MaxHealth"], Point(x, y), Point(house["X"], house["Y"]), p["Score"], p["CarriedResources"], p["CarryingCapacity"]) # print(player.__dict__) # print("MY POSITION", player.Position) # print(player.__dict__) # Map serialized_map = map_json["CustomSerializedMap"] deserialized_map = deserialize_map(serialized_map) graph = create_graph(deserialized_map, False) # print(deserialized_map) # print(player.__dict__) # print(player.Position) # print_map(deserialized_map) # print(graph.nodes()) otherPlayers = [] for player_dict in map_json["OtherPlayers"]: for player_name in player_dict.keys(): player_info = player_dict[player_name] if player_info == "notAPlayer": continue p_pos = player_info["Position"] # print("\n\n\n", p_pos) player_info = PlayerInfo(player_info["Health"], player_info["MaxHealth"], Point(p_pos["X"], p_pos["Y"])) otherPlayers.append({player_name: player_info}) return do_something(player, deserialized_map, graph)
def draw_result(segments, hot): ax_min, ax_max = 0 - eps / 2, bound + eps / 2 fig = plt.figure(figsize=(8,8)) ax = fig.add_subplot(111, aspect='equal') ax.set_xlim(ax_min, ax_max) ax.set_ylim(ax_min, ax_max) ax.set_xticks(np.arange(-eps / 2, bound, eps), minor=True) ax.set_yticks(np.arange(-eps / 2, bound, eps), minor=True) ax.grid(which='minor') ax.grid(which='major', alpha=0.0) for p in hot: ax.add_patch(patches.Rectangle((p.sw.x / p.sw.z, p.sw.y / p.sw.z), eps, eps, alpha=0.5, facecolor="red")) seg_col = [] seg_src = [] for p in pixelspassed.keys(): lis = pixelspassed[p] lis.sort(key=lambda smth: smth[0]) q = 0 while (q < len(lis) - 1): seg = Segment(Point(int(lis[q][1][0]), int(lis[q][1][1]), int(lis[q][1][2]), homogeneous=True), Point(int(lis[q + 1][1][0]), int(lis[q + 1][1][1]), int(lis[q + 1][1][2]), homogeneous=True)) seg_col.append('black') seg_src.append(seg) q += 1 seg_src = list(map(lambda seg: [(seg.start.x / seg.start.z, seg.start.y / seg.start.z), (seg.end.x / seg.end.z, seg.end.y / seg.end.z)], seg_src)) ax.add_collection(mc.LineCollection(seg_src, colors=seg_col)) return fig
def deserialize(data): if "x" in data and "y" in data: return Point(data["x"], data["y"]) elif "Name" in data: return Player(data["Health"], data["MaxHealth"], data["CarriedResources"], data["CarryingCapacity"], data["CollectingSpeed"], data["TotalResources"], data["AttackPower"], data["Defence"], data["Position"], data["HouseLocation"], data["CarriedItems"], data["Score"], data["Name"], data["UpgradeLevels"]) elif "CustomSerializedMap" in data: data["GameMap"] = GameMap(data["CustomSerializedMap"], data["xMin"], data["yMin"]) return data
def find_path(tiles, start, goal, player): """ Search for the minimal path from start to an adjacent cell of goal start and stop are absolute coordinates in the full map. """ def heuristic(cell, goal): return dist(cell, goal) - 1 pr_queue = [] #heappush(pr_queue, (0 + heuristic(start, goal), 0, start, start)) right = Point(start.X - 1, start.Y) if free(tiles, right, player): heappush(pr_queue, (0 + heuristic(right, goal), 0, right, right)) left = Point(start.X + 1, start.Y) if free(tiles, left, player): heappush(pr_queue, (0 + heuristic(left, goal), 0, left, left)) up = Point(start.X , start.Y - 1) if free(tiles, up, player): heappush(pr_queue, (0 + heuristic(up, goal), 0, up, up)) down = Point(start.X , start.Y + 1) if free(tiles, down, player): heappush(pr_queue, (0 + heuristic(down, goal), 0, down, down)) visited = {start} while pr_queue: _, cost, path, current = heappop(pr_queue) if current == goal: return path if current in visited: continue visited.add(current) right = Point(current.X - 1, current.Y) if free(tiles, right, player): heappush(pr_queue, (cost + heuristic(right, goal), cost + 1, path, right)) left = Point(current.X + 1, current.Y) if free(tiles, left, player): heappush(pr_queue, (cost + heuristic(left, goal), cost + 1, path, left)) up = Point(current.X , current.Y - 1) if free(tiles, up, player): heappush(pr_queue, (cost + heuristic(up, goal), cost + 1, path, up)) down = Point(current.X , current.Y + 1) if free(tiles, down, player): heappush(pr_queue, (cost + heuristic(down, goal), cost + 1, path, down)) return None
def __init__(self, tile_content, x, y): self.TileContent = tile_content self.Position = Point(x, y) pass
def test_dist(): assert_same(Point(1, 2).dist(Point(0, 0))**2, 5) assert_same(Point(-2, 2).dist(Point(0, 0))**2, 8) assert_same(Point(3, 4).dist(Point(0, 0))**2, 25) assert_same(Point(-3, 4).dist(Point(0, 0))**2, 25) assert_same(Point(5, 12).dist(Point(0, 0))**2, 13**2)
def assert_same(a, b, threshold=1e-10): assert (abs(a - b) < threshold) def test_dist(): assert_same(Point(1, 2).dist(Point(0, 0))**2, 5) assert_same(Point(-2, 2).dist(Point(0, 0))**2, 8) assert_same(Point(3, 4).dist(Point(0, 0))**2, 25) assert_same(Point(-3, 4).dist(Point(0, 0))**2, 25) assert_same(Point(5, 12).dist(Point(0, 0))**2, 13**2) close_point = [ Point(1, 2), Point(-3, 4), Point(2, -1), Point(2, 3), Point(5, -8), Point(-9, 0), Point(-3, 3), Point(9, 2), Point(-5, -9), Point(1, -8), Point(-9, 8), Point(4, -7), Point(-8, 0), Point(-4, 7), Point(-6, 2), Point(-2, 2),
def __init__(self, tileType, x, y): self.TileType = tileType self.Position = Point(x, y) pass