Exemplo n.º 1
0
 def test_node_to_strings(self):
     start = Start()
     a = GNode("a")
     c = End()
     d = Key("key")
     e = Lock("e")
     start.add_child_s(a)
     start.add_child_s(d)
     d.add_lock_s(e)
     self.assertTrue(
         self.is_list_permutation_in_string(
             str(start), "Start(\'Start\', children={})", ["a", "key"]))
     c.add_parent_s([e, d])
     self.assertTrue(
         self.is_list_permutation_in_string(str(c),
                                            "End(\'End\', parents={})",
                                            ["e", "key"]))
     self.assertTrue(
         self.is_list_permutation_in_string(
             str(d),
             "Key(\'key\', parents=[\'Start\'], children={}, locks=[\'e\'])",
             ["e", "End"]))
     self.assertEqual(
         str(e),
         "Lock(\'e\', parents=[\'key\'], children=[\'End\'], keys=[\'key\'])"
     )
     self.assertEqual(str(a),
                      "GNode(\'a\', parents=[\'Start\'], children=[])")
Exemplo n.º 2
0
    def test_solver_linear_solvable(self):
        level = Level()
        level.upper_layer = np.array([[s, kR, lR, f]], dtype=object)

        # S--K--L--E
        start = Start()
        key = Key()
        lock = Lock()
        end = End()
        start.add_child_s([key, lock])
        key.add_lock_s(lock)
        lock.add_child_s(end)

        positions_map = {
            start: np.array([0, 0]),
            key: np.array([0, 1]),
            lock: np.array([0, 2]),
            end: np.array([0, 3]),
        }

        level.mission = start
        level.positions_map = positions_map
        does_level_follow_mission, solution = Solver.does_level_follow_mission(
            level)
        self.assertEqual(does_level_follow_mission, True)

        expected_steps = [(start, []), (key, rM), (lock, rM), (end, rM)]
        self.assert_steps_equal(solution.steps, expected_steps)
Exemplo n.º 3
0
    def test_solver_double_parent(self):
        level = Level()
        level.upper_layer = np.array([[s, lR, e, lR, f], [kR, w, kR, w, e]],
                                     dtype=object)

        # S--L--L--E
        #  \ |\ |
        #   \| \|
        #    K  K
        start = Start()
        key1 = Key("key1")
        lock1 = Lock("lock1")
        key2 = Key("key2")
        lock2 = Lock("lock2")
        end = End()
        start.add_child_s([key1, lock1])
        key1.add_lock_s(lock1)
        lock1.add_child_s([key2, lock2])
        key2.add_lock_s(lock2)
        lock2.add_child_s(end)

        positions_map = {
            start: np.array([0, 0]),
            key1: np.array([1, 0]),
            lock1: np.array([0, 1]),
            key2: np.array([1, 2]),
            lock2: np.array([0, 3]),
            end: np.array([0, 4])
        }

        level.mission = start
        level.positions_map = positions_map
        does_level_follow_mission, solution = Solver.does_level_follow_mission(
            level)
        self.assertEqual(does_level_follow_mission, True)
Exemplo n.º 4
0
    def test_solver_branch_trivial1(self):
        level = Level()
        level.upper_layer = np.array(
            [[s, kR, lR, e, e, f], [lB, kB, w, w, e, e]], dtype=object)

        # S--K--L--L--E
        #  \      /
        #   K-----
        start = Start()
        key1 = Key("key1")
        lock1 = Lock("lock1")
        key2 = Key("key2")
        lock2 = Lock("lock2")
        end = End()
        start.add_child_s([key1, key2])
        key1.add_child_s(lock1)
        key1.add_lock_s(lock1)
        lock1.add_child_s(lock2)
        key2.add_lock_s(lock2)
        lock2.add_child_s(end)

        positions_map = {
            start: np.array([0, 0]),
            key1: np.array([0, 1]),
            lock1: np.array([0, 2]),
            key2: np.array([1, 1]),
            lock2: np.array([1, 0]),
            end: np.array([0, 5]),
        }

        level.mission = start
        level.positions_map = positions_map
        does_level_follow_mission, solution = Solver.does_level_follow_mission(
            level)
        self.assertEqual(does_level_follow_mission, False)
Exemplo n.º 5
0
    def __init__(self, aesthetic):
        # Generate starting and ending nodes
        start = Start()
        end = End()
        self.start = start
        self.end = end

        self.lock_id = -1
        self.key_id = -1
        self.collect_id = -1
        self.room_id = -1

        node_to_grow = start
        tree_depth = np.random.randint(aesthetic.min_depth,
                                       aesthetic.max_depth)
        for _ in range(tree_depth):
            branch_count = np.random.choice(range(
                1,
                len(aesthetic.branch_probability) + 1),
                                            p=aesthetic.branch_probability)
            for _ in range(branch_count):
                f = self.grow_graph(node_to_grow, aesthetic)
            node_to_grow = f

        node_to_grow.add_child_s(end)

        if aesthetic.max_multi_lock_count > 0:
            for _ in range(np.random.randint(aesthetic.max_multi_lock_count)):
                self.add_multi_lock(lock_count=np.random.randint(
                    aesthetic.max_locks_per_multi_lock))

        self.insert_rooms(aesthetic)
        self.fill_rooms_with_collectables(aesthetic)
Exemplo n.º 6
0
    def test_sokoban_unsolvable(self):
        level = Level()
        level.upper_layer = np.array(
            [[s, kR, e, e, e, e, g, lR, f], [e, e, b, e, e, e, w, w, e]],
            dtype=object)

        start = Start()
        key = Key("key")
        block = SokobanKey("block")
        water = SokobanLock("water")
        lock = Lock("lock")
        end = End()
        start.add_child_s([key, block, water])
        block.add_lock_s(water)
        key.add_lock_s(lock)
        water.add_child_s(lock)
        lock.add_child_s(end)

        positions_map = {
            start: np.array([0, 0]),
            key: np.array([0, 1]),
            block: np.array([1, 2]),
            water: np.array([0, 6]),
            lock: np.array([0, 7]),
            end: np.array([0, 8]),
        }

        level.mission = start
        level.positions_map = positions_map
        does_level_follow_mission, solution = Solver.does_level_follow_mission(
            level)
        self.assertEqual(does_level_follow_mission, False)
Exemplo n.º 7
0
    def test_solver_linear_trivial(self):
        level = Level()
        level.upper_layer = np.array([[s, kR, lR, f], [e, e, e, e]],
                                     dtype=object)

        # S--K--L--E
        start = Start()
        key = Key("key")
        lock = Lock("lock")
        end = End()
        start.add_child_s(key)
        key.add_lock_s(lock)
        lock.add_child_s(end)

        positions_map = {
            start: np.array([0, 0]),
            key: np.array([0, 1]),
            lock: np.array([0, 2]),
            end: np.array([0, 3]),
        }

        level.mission = start
        level.positions_map = positions_map
        does_level_follow_mission, solution = Solver.does_level_follow_mission(
            level)
        self.assertEqual(does_level_follow_mission, False)
    def test_mission_generator_rooms(self):
        start = Start()
        key = Key()
        lock = Lock()
        room = Room()
        end = End()
        start.add_child_s([room, lock])
        room.add_child_s(key)
        key.add_lock_s(lock)
        lock.add_child_s(end)

        level = Level()
        w = Tiles.wall
        e = Tiles.empty
        layer = np.array([[w, w, w, w, w, w, w, w], [w, e, e, w, e, e, e, w],
                          [w, e, e, w, e, e, e, w], [w, w, w, w, w, w, w, w],
                          [w, e, e, w, e, e, e, w], [w, e, e, w, e, e, e, w],
                          [w, e, e, w, e, e, e, w], [w, w, w, w, w, w, w, w]],
                         dtype=object)

        aesthetic_settings = AestheticSettings()
        was_successful = Generator.generate(
            level_type=Level,
            size=layer.shape,
            aesthetic_settings=aesthetic_settings,
            max_retry_count=10,
            pregenerated_level_layer=layer,
            pregenerated_mission_start_node=start)
        self.assertTrue(was_successful)
Exemplo n.º 9
0
    def test_solver_hazard_unsolvable(self):
        level = Level()
        level.upper_layer = np.array(
            [[s, e, Fb, W, W, W, e, fl, e, F, F, e, F, F, e, f]], dtype=object)

        # S--K--L--E
        start = Start()
        key1 = Key("flippers")
        lock1 = Lock("water")
        key2 = Key("fireboots")
        lock2 = Lock("fire1")
        lock3 = Lock("fire2")
        end = End()
        start.add_child_s([key1, lock1])
        key1.add_lock_s(lock1)
        lock1.add_child_s([key2, lock2])
        key2.add_lock_s([lock2, lock3])
        lock2.add_child_s(lock3)
        lock3.add_child_s(end)

        positions_map = {
            start: np.array([0, 0]),
            key1: np.array([0, 7]),
            lock1: np.array([0, 4]),
            key2: np.array([0, 2]),
            lock2: np.array([0, 10]),
            lock3: np.array([0, 13]),
            end: np.array([0, 15]),
        }

        level.mission = start
        level.positions_map = positions_map
        does_level_follow_mission, solution = Solver.does_level_follow_mission(
            level)
        self.assertEqual(does_level_follow_mission, False)
Exemplo n.º 10
0
    def _get_lock_water_fire_lock_graph():
        start = Start()
        key_red = Key("red")
        lock_red = Lock("red")
        flippers = Key("flippers")
        water1 = Lock("water1")
        water2 = Lock("water2")
        key_green = Key("green")
        lock_green = Lock("green")
        fire_boots = Key("fireboots")
        fire1 = Lock("fire1")
        fire2 = Lock("fire2")
        end = End()
        start.add_child_s([fire2, key_red, lock_red])
        key_red.add_lock_s(lock_red)
        lock_red.add_child_s([flippers, water1])
        flippers.add_lock_s([water1, water2])
        water1.add_child_s(water2)
        water2.add_child_s([fire_boots, fire1])
        fire_boots.add_lock_s([fire1, fire2])
        fire1.add_child_s(key_green)
        key_green.add_lock_s(lock_green)
        fire2.add_child_s(lock_green)
        lock_green.add_child_s(end)

        return Node.find_all_nodes(start, method="topological-sort")
Exemplo n.º 11
0
    def test_wrong_key_color(self):
        level = Level()
        level.upper_layer = np.array(
            [[s, e, lR, kR], [kR, e, w, w], [e, e, lR, f]], dtype=object)

        start = Start()
        key1 = Key("key1")
        lock1 = Lock("lock1")
        key2 = Key("key2")
        lock2 = Lock("lock2")
        end = End()
        start.add_child_s([key1, lock1, lock2])
        lock1.add_child_s(key2)
        lock2.add_child_s(end)
        key1.add_lock_s(lock1)
        key2.add_lock_s(lock2)

        positions_map = {
            start: np.array([0, 0]),
            key1: np.array([1, 0]),
            lock1: np.array([0, 2]),
            key2: np.array([0, 3]),
            lock2: np.array([2, 2]),
            end: np.array([2, 3]),
        }

        level.mission = start
        level.positions_map = positions_map
        does_level_follow_mission, solution = Solver.does_level_follow_mission(
            level)
        self.assertEqual(does_level_follow_mission, False)
Exemplo n.º 12
0
    def test_solver_collectables_unreachable_collectable(self):
        level = Level()
        level.upper_layer = np.array([[s, e, B, f], [c, e, w, c]],
                                     dtype=object)
        level.required_collectable_count = 2

        start = Start()
        c0 = Collectable("c0")
        c1 = Collectable("c1")
        barrier = CollectableBarrier("B", collectables=[c0, c1])
        end = End()

        start.add_child_s([c0, barrier])
        barrier.add_child_s([c1, end])

        positions_map = {
            start: np.array([0, 0]),
            c0: np.array([1, 0]),
            c1: np.array([1, 3]),
            barrier: np.array([0, 2]),
            end: np.array([0, 3]),
        }

        level.mission = start
        level.positions_map = positions_map
        does_level_follow_mission, solution = Solver.does_level_follow_mission(
            level)
        self.assertEqual(does_level_follow_mission, False)
Exemplo n.º 13
0
    def test_solver_difficult(self):
        level = Level()
        level.upper_layer = np.array(
            [[f, e, e, w, e, W, e, w], [e, w, lG, w, e, w, Fb, w],
             [e, w, F, w, W, w, e, w], [e, w, s, w, e, w, F, w],
             [e, w, kR, w, fl, w, e, w], [e, w, e, lR, e, w, kG, w]],
            dtype=object)

        start = Start()
        key_red = Key("red")
        lock_red = Lock("red")
        flippers = Key("flippers")
        water1 = Lock("water1")
        water2 = Lock("water2")
        key_green = Key("green")
        lock_green = Lock("green")
        fire_boots = Key("fireboots")
        fire1 = Lock("fire1")
        fire2 = Lock("fire2")
        end = End()
        start.add_child_s([fire2, key_red, lock_red])
        key_red.add_lock_s(lock_red)
        lock_red.add_child_s([flippers, water1])
        flippers.add_lock_s([water1, water2])
        water1.add_child_s(water2)
        water2.add_child_s([fire_boots, fire1])
        fire_boots.add_lock_s([fire1, fire2])
        fire1.add_child_s(key_green)
        key_green.add_lock_s(lock_green)
        fire2.add_child_s(lock_green)
        lock_green.add_child_s(end)

        # [ f, e, e, w, e, W, e, w],
        # [ e, w,lG, w, e, w,Fb, w],
        # [ e, w, F, w, W, w, e, w],
        # [ e, w, s, w, e, w, F, w],
        # [ e, w,kR, w,fl, w, e, w],
        # [ e, w, e,lR, e, w,kG, w]], dtype=object)
        positions_map = {
            start: np.array([3, 2]),
            key_red: np.array([4, 2]),
            lock_red: np.array([5, 3]),
            flippers: np.array([4, 4]),
            water1: np.array([2, 4]),
            water2: np.array([0, 5]),
            key_green: np.array([5, 6]),
            lock_green: np.array([1, 2]),
            fire_boots: np.array([1, 6]),
            fire1: np.array([3, 6]),
            fire2: np.array([2, 2]),
            end: np.array([0, 0])
        }

        level.mission = start
        level.positions_map = positions_map
        does_level_follow_mission, solution = Solver.does_level_follow_mission(
            level)
        self.assertEqual(does_level_follow_mission, True)
Exemplo n.º 14
0
    def _get_simple_graph():
        start = Start()
        key = Key()
        lock = Lock()
        end = End()
        start.add_child_s([key, lock])
        key.add_lock_s(lock)
        lock.add_child_s(end)

        return Node.find_all_nodes(start, method="topological-sort")
Exemplo n.º 15
0
    def test_solver_linear_solvable_multicolored_keys(self):
        level = Level()
        level.upper_layer = np.array([[kY, lG, kB, lR, s, kR, lB, kG, lY, f]],
                                     dtype=object)

        # S--K--L--K--L--K--L--K--L--E
        start = Start()
        key_red = Key("red")
        lock_red = Lock("red")
        key_blue = Key("blue")
        lock_blue = Lock("blue")
        key_green = Key("green")
        lock_green = Lock("green")
        key_yellow = Key("yellow")
        lock_yellow = Lock("yellow")
        end = End()

        start.add_child_s([lock_red, key_red, lock_blue])
        lock_red.add_child_s([lock_green, key_blue])
        lock_green.add_child_s(key_yellow)
        lock_blue.add_child_s([lock_yellow, key_green])
        lock_yellow.add_child_s(end)
        key_red.add_child_s(lock_red)
        key_red.add_lock_s(lock_red)
        key_blue.add_child_s(lock_blue)
        key_blue.add_lock_s(lock_blue)
        key_green.add_child_s(lock_green)
        key_green.add_lock_s(lock_green)
        key_yellow.add_child_s(lock_yellow)
        key_yellow.add_lock_s(lock_yellow)

        positions_map = {
            start: np.array([0, 4]),
            key_red: np.array([0, 5]),
            lock_red: np.array([0, 3]),
            key_blue: np.array([0, 2]),
            lock_blue: np.array([0, 6]),
            key_green: np.array([0, 7]),
            lock_green: np.array([0, 1]),
            key_yellow: np.array([0, 0]),
            lock_yellow: np.array([0, 8]),
            end: np.array([0, 9]),
        }

        level.mission = start
        level.positions_map = positions_map
        does_level_follow_mission, solution = Solver.does_level_follow_mission(
            level)
        self.assertEqual(does_level_follow_mission, True)

        expected_steps = [(start, []), (key_red, rM), (lock_red, 2 * lM),
                          (key_blue, lM), (lock_blue, 4 * rM), (key_green, rM),
                          (lock_green, 6 * lM), (key_yellow, lM),
                          (lock_yellow, 8 * rM), (end, rM)]
        self.assert_steps_equal(solution.steps, expected_steps)
Exemplo n.º 16
0
    def test_topological_sort_bug(self):
        start = Start()
        key = Key("key")
        lock = Lock("lock")
        end = End()
        start.add_child_s(key)
        key.add_lock_s(lock)
        lock.add_child_s(end)

        nodes = Node.find_all_nodes(start, method="topological-sort")
        self.assertEqual(nodes, [start, key, lock, end])
Exemplo n.º 17
0
    def test_graph_with_collectables_and_locks(self):
        start = Start()
        key = Key("K0")
        lock = Lock("L0", key_s=key)
        c0 = Collectable("C0")
        c1 = Collectable("C1")
        b = CollectableBarrier("B", collectables=[c0, c1])
        end = End()
        start.add_child_s([key, lock, c0])
        lock.add_child_s([c1, b])
        b.add_child_s(end)

        layout = GraphVisualizer.get_node_layout(start)
        self.assert_no_overlapping_layout(layout)
Exemplo n.º 18
0
    def _get_water_lock_graph():
        start = Start()
        key_red = Key("red")
        lock_red = Lock("red")
        flippers = Key("flippers")
        water = Lock("water")
        end = End()

        start.add_child_s([flippers, water, lock_red])
        flippers.add_lock_s(water)
        water.add_child_s(key_red)
        key_red.add_lock_s(lock_red)
        lock_red.add_child_s(end)

        return Node.find_all_nodes(start, method="topological-sort")
Exemplo n.º 19
0
    def test_mutable_default_argument_bug(self):
        gnode1 = GNode("")
        gnode2 = GNode("")
        bad_node0 = GNode("BadNode0")
        gnode1.add_child_s(bad_node0)
        self.assertEqual(len(gnode2.child_s), 0)

        start1 = Start()
        start2 = Start()
        bad_node1 = GNode("BadNode1")
        start1.add_child_s(bad_node1)
        self.assertEqual(len(start2.child_s), 0)

        end1 = End()
        end2 = End()
        bad_node2 = GNode("BadNode2")
        end1.add_parent_s(bad_node2)
        self.assertEqual(len(end2.parent_s), 0)

        key1 = Key()
        key2 = Key()
        bad_node3 = GNode("BadNode3")
        key1.add_child_s(bad_node3)
        bad_node4 = GNode("BadNode4")
        key1.add_parent_s(bad_node4)
        self.assertEqual(len(key2.child_s), 0)
        self.assertEqual(len(key2.parent_s), 0)

        lock1 = Lock()
        lock2 = Lock()
        bad_node5 = GNode("BadNode5")
        lock1.add_child_s(bad_node5)
        bad_node6 = GNode("BadNode6")
        lock1.add_parent_s(bad_node6)
        self.assertEqual(len(lock2.child_s), 0)
        self.assertEqual(len(lock2.parent_s), 0)
    def test_works_with_branched_graphs(self):
        # return
        # S
        # |-----------
        # |    |     |
        # L1   K2    L2
        # |          |
        # E          K1
        start = Start()
        key1 = Key("1")
        key2 = Key("2")
        lock1 = Lock("1")
        lock2 = Lock("2")
        end = End()

        start.add_child_s([lock1, key2, lock2])
        lock1.add_child_s(end)
        lock2.add_child_s(key1)
        key1.add_child_s(lock1)
        key2.add_child_s(lock2)
        key1.add_lock_s(lock1)
        key2.add_lock_s(lock2)

        np.random.seed(4)

        level = Level()
        w = Tiles.wall
        e = Tiles.empty
        layer = np.array([[w, w, w, w, w, w, w, w], [w, e, e, e, e, e, e, w],
                          [w, e, e, e, e, e, e, w], [w, w, w, w, w, w, w, w],
                          [w, e, e, w, e, e, e, w], [w, e, e, w, e, e, e, w],
                          [w, e, e, w, e, e, e, w], [w, w, w, w, w, w, w, w]],
                         dtype=object)

        solution_node_order = Node.find_all_nodes(start,
                                                  method="topological-sort")

        aesthetic_settings = AestheticSettings()
        was_successful = Generator.generate(
            level=level,
            size=layer.shape,
            aesthetic_settings=aesthetic_settings,
            max_retry_count=10,
            pregenerated_level_layer=layer,
            pregenerated_solution_node_order=solution_node_order)
        self.assertTrue(was_successful)

        Log.print(level)
Exemplo n.º 21
0
    def get_man_graph():
        a = Start()
        b = GNode("b")
        c = GNode("c")
        d = GNode("d")
        e = GNode("e")
        f = End()
        g = GNode("g")
        h = GNode("h")
        i = GNode("i")

        a.add_child_s([b, c, d])
        b.add_child_s(e)
        c.add_child_s([f, g, h, i])
        h.add_child_s(i)

        return a, {a, b, c, d, e, f, g, h, i}
    def generate_mission_graph():
        a = Start()
        b = GNode([], [], "b")
        c = Key(name="c")
        d = GNode([], [], "d")
        e = End()
        f = Lock(name="f")
        g = GNode([], [], "g")

        a.add_child_s(b)
        a.add_child_s(c)
        a.add_child_s(d)
        b.add_child_s(e)
        c.add_child_s(f)
        c.add_child_s(g)
        f.add_key_s(c)
        c.add_lock_s(f)

        return a, {a, b, c, d, e, f, g}
Exemplo n.º 23
0
    def test_degenerate_loop_layout(self):
        level = Level()
        level.upper_layer = np.array(
            [[w, w, w, w, w, w, w, w, w], [w, s, w, e, e, e, e, f, w],
             [w, kR, w, w, w, w, lG, w, w], [w, kG, lR, e, e, e, e, e, w],
             [w, e, w, e, e, e, e, kR, w], [w, e, w, w, lR, w, e, w, w],
             [w, e, w, e, e, e, e, e, w], [w, w, w, w, w, w, w, w, w]],
            dtype=object)

        start = Start()
        key0 = Key("key0")
        lock0 = Lock("lock0")
        key1 = Key("key1")
        lock1 = Lock("lock1")
        key2 = Key("key2")
        lock2 = Lock("lock2")
        end = End()
        start.add_child_s([lock0, key0, key2])
        lock0.add_child_s([lock1, key1])
        lock1.add_child_s(lock2)
        lock2.add_child_s(end)
        key0.add_lock_s(lock0)
        key1.add_lock_s(lock1)
        key2.add_lock_s(lock2)

        positions_map = {
            start: np.array([1, 1]),
            key0: np.array([2, 1]),
            key2: np.array([3, 1]),
            lock0: np.array([3, 2]),
            key1: np.array([4, 7]),
            lock1: np.array([5, 4]),
            lock2: np.array([2, 6]),
            end: np.array([1, 7]),
        }

        level.mission = start
        level.positions_map = positions_map
        does_level_follow_mission, solution = Solver.does_level_follow_mission(
            level)
        self.assertEqual(does_level_follow_mission, False)
Exemplo n.º 24
0
    def test_node_seen_too_soon_incorrect_layout2(self):
        level = Level()
        level.upper_layer = np.array(
            [[s, e, lR, kB], [kR, e, w, w], [e, e, lB, f]], dtype=object)

        # S----
        # |   |
        # L1  K1
        # |----
        # |   |
        # L2  K2
        # |
        # F
        start = Start()
        key1 = Key("key1")
        lock1 = Lock("lock1")
        key2 = Key("key2")
        lock2 = Lock("lock2")
        end = End()
        start.add_child_s([key1, lock1])
        key1.add_lock_s(lock1)
        key2.add_lock_s(lock2)
        lock1.add_child_s([key2, lock2])
        lock2.add_child_s(end)

        positions_map = {
            start: np.array([0, 0]),
            key1: np.array([1, 0]),
            lock1: np.array([0, 2]),
            key2: np.array([0, 3]),
            lock2: np.array([2, 2]),
            end: np.array([2, 3]),
        }

        level.mission = start
        level.positions_map = positions_map
        does_level_follow_mission, solution = Solver.does_level_follow_mission(
            level)
        self.assertEqual(does_level_follow_mission, False)
Exemplo n.º 25
0
    def test_sokoban_solvable_lock_in_middle(self):
        level = Level()
        level.upper_layer = np.array(
            [[s, kR, b, e, lR, e, g, e, f], [e, e, e, e, w, e, w, w, e]],
            dtype=object)

        start = Start()
        key = Key("key")
        block = SokobanKey("block")
        water = SokobanLock("water")
        lock = Lock("lock")
        end = End()
        start.add_child_s([key, block, lock])
        block.add_lock_s([water])
        key.add_lock_s(lock)
        lock.add_child_s(water)
        water.add_child_s(end)

        positions_map = {
            start: np.array([0, 0]),
            key: np.array([0, 1]),
            block: np.array([0, 2]),
            water: np.array([0, 6]),
            lock: np.array([0, 4]),
            end: np.array([0, 8]),
        }

        level.mission = start
        level.positions_map = positions_map
        does_level_follow_mission, solution = Solver.does_level_follow_mission(
            level)
        self.assertEqual(does_level_follow_mission, True)

        expected_steps = [(start, []), (key, rM), (block, []),
                          (lock, dM + 2 * rM + uM + rM),
                          (water, lM + dM + 2 * lM + uM + 4 * rM),
                          (end, 3 * rM)]
        self.assert_steps_equal(solution.steps, expected_steps)
Exemplo n.º 26
0
    def test_two_keys_soon_unneeded(self):
        level = Level()
        level.upper_layer = np.array(
            [[s, kR, lR, e, e, f], [e, kR, lR, e, e, e]], dtype=object)

        # S----------
        # |    |    |
        # lR   kR   kB
        # |
        # lB
        # |
        # E
        start = Start()
        key1 = Key("key1")
        lock1 = Lock("lock1")
        key2 = Key("key2")
        lock2 = Lock("lock2")
        end = End()
        start.add_child_s([lock1, key1, key2])
        lock1.add_child_s(lock2)
        lock2.add_child_s(end)
        key1.add_lock_s(lock1)
        key2.add_lock_s(lock2)

        positions_map = {
            start: np.array([0, 0]),
            key1: np.array([1, 1]),
            lock1: np.array([0, 2]),
            key2: np.array([0, 1]),
            lock2: np.array([0, 3]),
            end: np.array([0, 5]),
        }

        level.mission = start
        level.positions_map = positions_map
        does_level_follow_mission, solution = Solver.does_level_follow_mission(
            level)
        self.assertEqual(does_level_follow_mission, False)
Exemplo n.º 27
0
    def test_solver_hazard_too_soon(self):
        level = Level()
        level.upper_layer = np.array(
            [[s, e, W, e, fl], [e, fl, w, w, w], [e, e, W, e, f]],
            dtype=object)

        # S--K--L--E
        start = Start()
        flippers1 = Key("flippers 1")
        water1 = Lock("water 1")
        flippers2 = Key("flippers 2")
        water2 = Lock("water 2")
        end = End()

        start.add_child_s([flippers1, water1, water2])
        flippers1.add_lock_s(water1)
        water1.add_child_s(flippers2)
        flippers2.add_lock_s(water2)
        water2.add_child_s(end)

        # [ s, e, W, e,fl],
        # [ e,fl, w, w, w],
        # [ e, e, W, e, f]], dtype=object)

        positions_map = {
            start: np.array([0, 0]),
            flippers1: np.array([1, 1]),
            water1: np.array([0, 2]),
            flippers2: np.array([0, 4]),
            water2: np.array([2, 2]),
            end: np.array([2, 4]),
        }

        level.mission = start
        level.positions_map = positions_map
        does_level_follow_mission, solution = Solver.does_level_follow_mission(
            level)
        self.assertEqual(does_level_follow_mission, False)
    def test_mission_generator(self):
        # S
        # |-----------
        # |    |     |
        # L1   K2    L2
        # |          |
        # E          K1
        start = Start()
        key1 = Key("1")
        key2 = Key("2")
        lock1 = Lock("1")
        lock2 = Lock("2")
        end = End()

        start.add_child_s([lock1, key2, lock2])
        lock1.add_child_s(end)
        lock2.add_child_s(key1)
        key1.add_lock_s(lock1)
        key2.add_lock_s(lock2)

        level = Level()
        w = Tiles.wall
        e = Tiles.empty
        layer = np.array([[w, w, w, w, w, w, w, w], [w, e, e, e, e, e, e, w],
                          [w, e, e, e, e, e, e, w], [w, w, w, w, w, w, w, w],
                          [w, e, e, w, e, e, e, w], [w, e, e, w, e, e, e, w],
                          [w, e, e, w, e, e, e, w], [w, w, w, w, w, w, w, w]],
                         dtype=object)

        aesthetic_settings = AestheticSettings()
        was_successful = Generator.generate(
            level_type=Level,
            size=layer.shape,
            aesthetic_settings=aesthetic_settings,
            max_retry_count=10,
            pregenerated_level_layer=layer,
            pregenerated_mission_start_node=start)
        self.assertTrue(was_successful)
Exemplo n.º 29
0
    def test_solver_collectables(self):
        level = Level()
        level.upper_layer = np.array([[s, e, B, f], [c, c, w, e]],
                                     dtype=object)
        level.required_collectable_count = 2

        start = Start()
        c0 = Collectable("c0")
        c1 = Collectable("c1")
        barrier = CollectableBarrier("B", collectables=[c0, c1])
        end = End()

        start.add_child_s([c0, c1, barrier])
        barrier.add_child_s(end)

        positions_map = {
            start: np.array([0, 0]),
            c0: np.array([1, 0]),
            c1: np.array([1, 1]),
            barrier: np.array([0, 2]),
            end: np.array([0, 3]),
        }

        level.mission = start
        level.positions_map = positions_map
        does_level_follow_mission, solution = Solver.does_level_follow_mission(
            level)
        self.assertEqual(does_level_follow_mission, True)

        if solution.steps[1][0] == c1:
            expected_steps = [(start, []), (c1, dM + rM), (c0, lM),
                              (barrier, rM + uM + rM), (end, rM)]
        else:
            expected_steps = [(start, []), (c0, dM), (c1, rM),
                              (barrier, uM + rM), (end, rM)]
        self.assert_steps_equal(solution.steps, expected_steps)
Exemplo n.º 30
0
    def test_node_create_params(self):
        n0 = GNode("n0")
        n2 = GNode("n2")
        n1 = GNode("n1", n0, n2)
        n3 = GNode("n3", [n0], [n2])
        start = Start(n0)
        end = End(n2)
        key1 = Key("key1", n0)
        lock1 = Lock("lock1", n2, end, key1)
        lock2 = Lock("lock1", n2, end)
        key2 = Key("key1", n0, lock2)

        self.assertEqual(start.parent_s, set())
        self.assertEqual(start.child_s, {n0})
        self.assertEqual(n0.parent_s, {start})
        self.assertEqual(n0.child_s, {n1, n3, key1, key2})
        self.assertEqual(n1.parent_s, {n0})
        self.assertEqual(n1.child_s, {n2})
        self.assertEqual(n2.parent_s, {n1, n3})
        self.assertEqual(n2.child_s, {end, lock1, lock2})
        self.assertEqual(n3.parent_s, {n0})
        self.assertEqual(n3.child_s, {n2})
        self.assertEqual(key1.parent_s, {n0})
        self.assertEqual(key1.child_s, {lock1})
        self.assertEqual(key1.lock_s, {lock1})
        self.assertEqual(lock1.parent_s, {n2, key1})
        self.assertEqual(lock1.child_s, {end})
        self.assertEqual(lock1.key_s, {key1})
        self.assertEqual(key2.parent_s, {n0})
        self.assertEqual(key2.child_s, {lock2})
        self.assertEqual(key2.lock_s, {lock2})
        self.assertEqual(lock2.parent_s, {n2, key2})
        self.assertEqual(lock2.child_s, {end})
        self.assertEqual(lock2.key_s, {key2})
        self.assertEqual(end.parent_s, {n2, lock1, lock2})
        self.assertEqual(end.child_s, set())