Ejemplo n.º 1
0
class TestUnitTurns(unittest.TestCase):
    def setUp(self):
        # The test map is given below:
        #
        #                 1111
        #       01234567890123
        #     0 ##############
        #     1 #G.#E#.###.#.#
        #     2 #..#.#..E#####
        #     3 ##...#E#.#.G.#
        #     4 ####...G.#####
        #     5 ##############
        self.cavern_1 = Cavern([
            '##############', '#G.#E#.###.#.#', '#..#.#..E#####',
            '##...#E#.#.G.#', '####...G.#####', '##############'
        ])

        # The test map is given below:
        #
        #       012345
        #     0 ######
        #     1 #...G#
        #     2 #....#
        #     3 #....#
        #     4 #G..E#
        #     5 ######
        self.cavern_2 = Cavern(
            ['######', '#...G#', '#....#', '#....#', '#G..E#', '######'])

        self.cavern_custom = Cavern([
            '##############', '#G.#E#.###.#.#', '#..#.#..E#####',
            '##...#E#.#...#', '####...G.#####', '##############'
        ])

    def test_full_round(self):
        self.cavern_1.step(1)

        # Check all the units moved correctly
        self.assertIsNone(self.cavern_1.unit_at(Point(1, 1)))
        self.assertTrue(type(self.cavern_1.unit_at(Point(2, 1))) == Goblin)
        self.assertIsNone(self.cavern_1.unit_at(Point(4, 1)))
        self.assertTrue(type(self.cavern_1.unit_at(Point(4, 2))) == Elf)
        self.assertIsNone(self.cavern_1.unit_at(Point(8, 2)))
        self.assertTrue(type(self.cavern_1.unit_at(Point(8, 3))) == Elf)
        self.assertIsNone(self.cavern_1.unit_at(Point(6, 3)))
        self.assertTrue(type(self.cavern_1.unit_at(Point(6, 4))) == Elf)
        self.assertTrue(type(self.cavern_1.unit_at(Point(11, 3))) == Goblin)
        self.assertTrue(type(self.cavern_1.unit_at(Point(7, 4))) == Goblin)

        # Now check that all the units apart from the ones at (6, 4) and (7, 4) have 200 HP
        self.assertTrue(self.cavern_1.unit_at(Point(2, 1)).hp == 200)
        self.assertTrue(self.cavern_1.unit_at(Point(4, 2)).hp == 200)
        self.assertTrue(self.cavern_1.unit_at(Point(8, 3)).hp == 200)
        self.assertTrue(self.cavern_1.unit_at(Point(6, 4)).hp == 197)
        self.assertTrue(self.cavern_1.unit_at(Point(11, 3)).hp == 200)
        self.assertTrue(self.cavern_1.unit_at(Point(7, 4)).hp == 197)

    def test_full_round_equal_distance(self):
        self.cavern_2.step(1)

        # Check all the units moved correctly
        self.assertIsNone(self.cavern_2.unit_at(Point(4, 1)))
        self.assertTrue(type(self.cavern_2.unit_at(Point(4, 2))) == Goblin)
        self.assertIsNone(self.cavern_2.unit_at(Point(1, 4)))
        self.assertTrue(type(self.cavern_2.unit_at(Point(2, 4))) == Goblin)
        self.assertIsNone(self.cavern_2.unit_at(Point(4, 4)))
        self.assertTrue(type(self.cavern_2.unit_at(Point(4, 3))) == Elf)

        # Now check that all the units (apart from the first Goblin) have 200 HP
        self.assertTrue(self.cavern_2.unit_at(Point(4, 2)).hp == 197)
        self.assertTrue(self.cavern_2.unit_at(Point(2, 4)).hp == 200)
        self.assertTrue(self.cavern_2.unit_at(Point(4, 3)).hp == 200)

    def test_full_game_custom(self):
        rounds_played, hp, winning_team = self.cavern_custom.play()

        self.assertEqual(rounds_played, 68)
        self.assertEqual(hp, 300)
        self.assertEqual(winning_team, Elf)

    def test_examples(self):
        example_caverns = [
            {
                "starting_cavern": [
                    "#######", "#G..#E#", "#E#E.E#", "#G.##.#", "#...#E#",
                    "#...E.#", "#######"
                ],
                "rounds_played":
                37,
                "remaining_hit_points":
                982,
                "winning_team":
                Elf
            },
            {
                "starting_cavern": [
                    "#######", "#E..EG#", "#.#G.E#", "#E.##E#", "#G..#.#",
                    "#..E#.#", "#######"
                ],
                "rounds_played":
                46,
                "remaining_hit_points":
                859,
                "winning_team":
                Elf
            },
            {
                "starting_cavern": [
                    "#######", "#E.G#.#", "#.#G..#", "#G.#.G#", "#G..#.#",
                    "#...E.#", "#######"
                ],
                "rounds_played":
                35,
                "remaining_hit_points":
                793,
                "winning_team":
                Goblin
            },
            {
                "starting_cavern": [
                    "#######", "#.E...#", "#.#..G#", "#.###.#", "#E#G#G#",
                    "#...#G#", "#######"
                ],
                "rounds_played":
                54,
                "remaining_hit_points":
                536,
                "winning_team":
                Goblin
            },
            {
                "starting_cavern": [
                    "#########", "#G......#", "#.E.#...#", "#..##..G#",
                    "#...##..#", "#...#...#", "#.G...G.#", "#.....G.#",
                    "#########"
                ],
                "rounds_played":
                20,
                "remaining_hit_points":
                937,
                "winning_team":
                Goblin
            },
        ]

        for test_data in example_caverns:
            rounds_played, hp, winning_team = Cavern(
                test_data["starting_cavern"]).play()
            self.assertEqual(rounds_played, test_data["rounds_played"])
            self.assertEqual(hp, test_data["remaining_hit_points"])
            self.assertEqual(winning_team, test_data["winning_team"])
Ejemplo n.º 2
0
    def test_examples(self):
        example_caverns = [
            {
                "starting_cavern": [
                    "#######", "#.G...#", "#...EG#", "#.#.#G#", "#..G#E#",
                    "#.....#", "#######"
                ],
                "elf_attack":
                15,
                "rounds_played":
                29,
                "remaining_hit_points":
                172,
                "winning_team":
                Elf
            },
            {
                "starting_cavern": [
                    "#######", "#E..EG#", "#.#G.E#", "#E.##E#", "#G..#.#",
                    "#..E#.#", "#######"
                ],
                "elf_attack":
                4,
                "rounds_played":
                33,
                "remaining_hit_points":
                948,
                "winning_team":
                Elf
            },
            {
                "starting_cavern": [
                    "#######", "#E.G#.#", "#.#G..#", "#G.#.G#", "#G..#.#",
                    "#...E.#", "#######"
                ],
                "elf_attack":
                15,
                "rounds_played":
                37,
                "remaining_hit_points":
                94,
                "winning_team":
                Elf
            },
            {
                "starting_cavern": [
                    "#######", "#.E...#", "#.#..G#", "#.###.#", "#E#G#G#",
                    "#...#G#", "#######"
                ],
                "elf_attack":
                12,
                "rounds_played":
                39,
                "remaining_hit_points":
                166,
                "winning_team":
                Elf
            },
            {
                "starting_cavern": [
                    "#########", "#G......#", "#.E.#...#", "#..##..G#",
                    "#...##..#", "#...#...#", "#.G...G.#", "#.....G.#",
                    "#########"
                ],
                "elf_attack":
                34,
                "rounds_played":
                30,
                "remaining_hit_points":
                38,
                "winning_team":
                Elf
            },
        ]

        for test_data in example_caverns:
            cavern = Cavern(test_data["starting_cavern"],
                            elf_attack=test_data["elf_attack"],
                            ignore_elf_death=False)
            try:
                turns, hp, winning_team = cavern.play()
            except UnitDiedException as ude:
                self.fail("An elf died unexpectedly ({elf})".format(
                    elf=ude.unit.debug_str()))

            self.assertEqual(turns, test_data["rounds_played"])
            self.assertEqual(hp, test_data["remaining_hit_points"])
            self.assertEqual(winning_team, test_data["winning_team"])