def start():
    print("Welcome. Type 'help' for available commands")
    exit = False
    created = False
    loaded_map = False
    while exit is False:
        full_command = input('--> ').strip().split(' ')
        if full_command[0] == 'load_map':
            dungeon = Dungeon(full_command[1])
            if dungeon.map:
                loaded_map = True
        elif full_command[0] == 'show_map' and loaded_map:
            print(dungeon.print_map())
        elif full_command[0] == 'create_hero' and loaded_map:
            my_hero = Hero(
                full_command[1], int(full_command[2]), full_command[3])
            my_hero.weapon = Weapon('Ashbringer', 40, 0.8)
            created = True
        elif full_command[0] == 'spawn_hero' and loaded_map:
            if created is True:
                dungeon.spawn(full_command[1], my_hero)
                dungeon.spawn_npcs()
            else:
                print('No created characters.')
        elif full_command[0] == 'move' and loaded_map:
            if full_command[1] not in dungeon.ingame:
                print ('Player name is incorrect.')

            elif not dungeon.ingame[full_command[1]].is_alive():
                exit = True
                return 'Your character is dead. Game over.'
            else:
                if dungeon.unlocked:
                    exit = True
                    return 'Game Over. You have won.'
                print(dungeon.move_player(full_command[1], full_command[2]))
                to_move = dungeon.get_random_npc()
                if not dungeon.ingame[to_move].is_alive():
                    dungeon.ingame = {
                        i: dungeon.ingame[i] for i in dungeon.ingame if i != 0}
                    dungeon.npcs = {i: dungeon.npcs[i]
                                    for i in dungeon.npcs if i != 0}
                else:
                    print(dungeon.move_npc(to_move))
                print(dungeon.print_map())
        elif full_command[0] == 'heal':
            if created and loaded_map:
                my_hero.health = my_hero.max_health
            else:
                print('No created characters.')
        elif full_command[0] == 'known_as':
            if created and loaded_map:
                print (my_hero.known_as())
            else:
                print('No created characters.')
        elif full_command[0] == 'help':
            print(helper)
        elif full_command[0] == 'exit':
            exit = True
        else:
            print("Invalid command. Type 'help' for available commands.")
Beispiel #2
0
class DungeonTest(unittest.TestCase):

    def setUp(self):
        self.mapfile = "testmap.txt"
        self.mapcontents = """ZZZZZZZZZZZZZZZZZZZ\nZ#...#.##.########Z
Z#.S.#.....N....K.Z\nZ#...#..#.....####Z
Z#...#..#.....N..#Z\nZ....#..#......#.#Z
Z#......#..N...#.#Z\nZ..............#C#Z
Z..###########....Z\nZZZZZZZZZZZZZZZZZZZ"""
        with open(self.mapfile, "w+") as f:
            f.write(self.mapcontents)

        self.dungeon = Dungeon("testmap.txt")
        self.hero = Hero("Arthas", 500, "Lich King")
        self.orc = Orc("Thrall", 500, 1.6)

    def tearDown(self):
        remove(self.mapfile)

    def test_init(self):
        self.assertEqual(self.dungeon.map, self.mapcontents)

    def test_load_map_with_valid_file(self):
        result = self.dungeon.load_map("testmap.txt")
        self.assertEqual(result, self.mapcontents)

    def test_load_map_with_invalid_file(self):
        result = self.dungeon.load_map("not_exists.txt")
        self.assertEqual(result, "")

    def test_map_with_empty_file(self):
        with open(self.mapfile, "w") as f:
            f.write("")

        result = self.dungeon.load_map("testmap.txt")
        self.assertEqual(result, "")

    def test_print_map_with_valid_file(self):
        result = self.dungeon.print_map()
        self.assertEqual(result, self.mapcontents)

    def test_print_map_with_empty_file(self):
        self.dungeon.map = ""
        result = self.dungeon.print_map()
        self.assertEqual(result, "No valid map loaded.")

    def test_print_map_after_battle(self):
        hero = Hero("Alan", 1, "Turing")
        hero.weapon = Weapon("Stick", 1, 0.1)
        self.dungeon.map = "ZSNZ"
        self.dungeon.spawn("1", hero)
        self.dungeon.spawn_npcs()

        self.dungeon.move_player("1", "right")
        self.assertEqual(self.dungeon.map, "Z.OZ")

    def test_valid_spawn(self):
        self.dungeon.spawn("Player 1", self.hero)
        contents = """ZZZZZZZZZZZZZZZZZZZ\nZ#...#.##.########Z
Z#.H.#.....N....K.Z\nZ#...#..#.....####Z
Z#...#..#.....N..#Z\nZ....#..#......#.#Z
Z#......#..N...#.#Z\nZ..............#C#Z
Z..###########....Z\nZZZZZZZZZZZZZZZZZZZ"""
        self.assertEqual(self.dungeon.map, contents)

    def test_spawn_when_char_is_ingame(self):
        self.dungeon.spawn("Player 1", self.hero)
        result = self.dungeon.spawn("Player 1", self.hero)
        self.assertEqual(result, "Character is already spawned.")

    def test_spawn_when_no_free_slots(self):
        self.dungeon.spawn("Player 1", self.hero)
        self.dungeon.spawn("Player 2", self.orc)
        testchar = Hero("Arthas", 200, "Lich")
        result = self.dungeon.spawn("Player 3", testchar)
        self.assertEqual(result, "No free spawn slot.")

    def test_map_conversion(self):
        actual = [["Z", "Z", "Z", "Z", "Z", "Z", "Z", "Z", "Z", "Z", "Z", "Z",
                   "Z", "Z", "Z", "Z", "Z", "Z", "Z"],
                  ["Z", "#", ".", ".", ".", "#", ".", "#", "#", ".", "#", "#",
                   "#", "#", "#", "#", "#", "#", "Z"],
                  ["Z", "#", ".", "S", ".", "#", ".", ".", ".", ".", ".", "N",
                   ".", ".", ".", ".", "K", ".", "Z"],
                  ["Z", "#", ".", ".", ".", "#", ".", ".", "#", ".", ".", ".",
                   ".", ".", "#", "#", "#", "#", "Z"],
                  ["Z", "#", ".", ".", ".", "#", ".", ".", "#", ".", ".", ".",
                   ".", ".", "N", ".", ".", "#", "Z"],
                  ["Z", ".", ".", ".", ".", "#", ".", ".", "#", ".", ".", ".",
                  ".", ".", ".", "#", ".", "#", "Z"],
                  ["Z", "#", ".", ".", ".", ".", ".", ".", "#", ".", ".", "N",
                   ".", ".", ".", "#", ".", "#", "Z"],
                  ["Z", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".",
                   ".", ".", ".", "#", "C", "#", "Z"],
                  ["Z", ".", ".", "#", "#", "#", "#", "#", "#", "#", "#", "#",
                   "#", "#", ".", ".", ".", ".", "Z"],
                  ["Z", "Z", "Z", "Z", "Z", "Z", "Z", "Z", "Z", "Z", "Z", "Z",
                   "Z", "Z", "Z", "Z", "Z", "Z", "Z"]]
        result = self.dungeon.convert_map_to_changeable_tiles()
        self.assertEqual(result, actual)

    def test_map_revert(self):
        test = self.dungeon.convert_map_to_changeable_tiles()
        result = self.dungeon.revert_map_to_string_state(test)
        self.assertEqual(result, self.dungeon.map)

    def test_locate_position(self):
        result = self.dungeon.get_position_in_map("S")
        self.assertEqual(result, [2, 3])

    def test_destination_coordinates_down(self):
        result = self.dungeon.get_destination_coordinates([4, 4], "down")
        self.assertEqual(result, [5, 4])

    def test_destination_coordinates_up(self):
        result = self.dungeon.get_destination_coordinates([4, 4], "up")
        self.assertEqual(result, [3, 4])

    def test_destination_coordinates_left(self):
        result = self.dungeon.get_destination_coordinates([4, 4], "left")
        self.assertEqual(result, [4, 3])

    def test_destination_coordinates_right(self):
        result = self.dungeon.get_destination_coordinates([4, 4], "right")
        self.assertEqual(result, [4, 5])

    def test_check_move_with_wrong_direction(self):
        result = self.dungeon.check_move(".", "test")
        self.assertEqual(result, False)

    def test_check_move_with_out_of_bounds_err(self):
        result = self.dungeon.check_move("Z", "up")
        self.assertEqual(result, False)

    def test_check_move_into_wall(self):
        result = self.dungeon.check_move("#", "left")
        self.assertEqual(result, False)

    def test_valid_move_into_free_space(self):
        self.dungeon.spawn("1", self.hero)
        self.dungeon.move_player("1", "right")
        self.assertEqual(self.hero.location, [2, 4])

    def test_invalid_hero_move(self):
        self.dungeon.map = "ZZZ\nZSZ\nZZZ"
        self.dungeon.spawn("Char", self.hero)
        result = self.dungeon.move_player("Char", "left")
        self.assertEqual(result, "Try again.")

    def test_move_npc_into_border(self):
        self.dungeon.map = "ZZZ\nZNZ\nZZZ"
        self.dungeon.spawn_npcs()
        result = self.dungeon.move_npc("NPC1")
        self.assertEqual(result, None)

    def test_move_npc_into_key(self):
        self.dungeon.map = "KKK\nKNK\nKKK"
        self.dungeon.spawn_npcs()
        result = self.dungeon.move_npc("NPC1")
        self.assertEqual(result, None)

    def test_move_npc_into_chest(self):
        self.dungeon.map = "CCC\nCNC\nCCC"
        self.dungeon.spawn_npcs()
        result = self.dungeon.move_npc("NPC1")
        self.assertEqual(result, None)

    def test_move_npc_into_wall(self):
        self.dungeon.map = "###\n#N#\n###"
        self.dungeon.spawn_npcs()
        result = self.dungeon.move_npc("NPC1")
        self.assertEqual(result, None)

    def test_move_npc_into_orc(self):
        self.dungeon.map = "OOO\nONO\nOOO"
        self.dungeon.spawn_npcs()
        result = self.dungeon.move_npc("NPC1")
        self.assertEqual(result, None)

    def test_move_hero_into_battle(self):
        mod = "ZZZZ\nZSNZ\nZZZZ"
        self.dungeon.map = mod
        self.dungeon.spawn("1", self.hero)
        self.dungeon.spawn_npcs()
        self.hero.weapon = Weapon("Frostmourne", 40, 0.9)
        result = self.dungeon.move_player("1", "right")
        self.assertIn(result, ["1 wins!", "NPC1 wins!"])

    def test_move_npc_into_battle(self):
        mod = "ZZSZ\nZSNS\nZZSZ"
        self.dungeon.map = mod
        self.dungeon.spawn("1", self.hero)
        self.dungeon.spawn("2", Hero("Tirion", 1, "Fondring"))
        self.dungeon.spawn("3", Hero("Lich", 1, "King"))
        self.dungeon.spawn("4", Hero("Bat", 1, "Svetlio"))
        self.dungeon.spawn_npcs()

        result = self.dungeon.move_npc("NPC1")
        self.assertIn(result, ["1 wins!", "NPC1 wins!"])

    def test_spawn_npcs(self):
        mod = "ZZZZ\nZN.Z\nZ.NZ\nZZZZ"
        self.dungeon.map = mod
        self.dungeon.spawn_npcs()
        actual = "ZZZZ\nZO.Z\nZ.OZ\nZZZZ"
        self.assertEqual(self.dungeon.map, actual)

    def test_obtain_key(self):
        mod = "ZZZZ\nZS.Z\nZK.Z\nZZZZ"
        self.dungeon.map = mod
        self.dungeon.spawn("1", self.hero)
        result = self.dungeon.move_player("1", "down")
        self.assertEqual(result, "Key Obtained.")

    def test_unlock_chest_when_key_is_obtained(self):
        mod = "ZZZZ\nZSKZ\nZ.CZ\nZZZZ"
        self.dungeon.map = mod
        self.dungeon.spawn("1", self.hero)
        self.dungeon.move_player("1", "right")
        result = self.dungeon.move_player("1", "down")
        self.assertEqual(result, "Chest Unlocked.")

    def test_unlock_chest_when_key_not_present(self):
        mod = "ZZZZ\nZS.Z\nZC.Z\nZZZZ"
        self.dungeon.map = mod
        self.dungeon.spawn("1", self.hero)
        result = self.dungeon.move_player("1", "down")
        self.assertEqual(result, "You must get the key first.")

    def test_get_random_npc(self):
        mod = "ZZZZ\nZNNZ\nZCNZ\nZZZZ"
        self.dungeon.map = mod
        self.dungeon.spawn_npcs()
        result = self.dungeon.get_random_npc()
        self.assertIn(result, self.dungeon.npcs)

    def test_move_npc_to_empty_spot(self):
        mod = "ZZZZZZZ\nZ.....Z\nZ..N..Z\nZ.....Z\nZZZZZZZ"
        self.dungeon.map = mod
        self.dungeon.spawn_npcs()
        self.dungeon.move_npc(self.dungeon.get_random_npc())
        self.assertIn(self.dungeon.map, [
            "ZZZZZZZ\nZ..O..Z\nZ.....Z\nZ.....Z\nZZZZZZZ",
            "ZZZZZZZ\nZ.....Z\nZ.O...Z\nZ.....Z\nZZZZZZZ",
            "ZZZZZZZ\nZ.....Z\nZ...O.Z\nZ.....Z\nZZZZZZZ",
            "ZZZZZZZ\nZ.....Z\nZ.....Z\nZ..O..Z\nZZZZZZZ"])

    def test_move_to_invalid_spot(self):
        mod = "ZZZZZZZ\nZ..K..Z\nZ.#NC.Z\nZ..O..Z\nZZZZZZZ"
        self.dungeon.map = mod
        self.dungeon.spawn_npcs()
        self.dungeon.move_npc(self.dungeon.get_random_npc())
        self.assertEqual(self.dungeon.map, mod.replace("N", "O"))

    def test_get_invalid_indicator(self):
        self.invalid = Entity("Name", 20)
        self.assertEqual(self.dungeon.get_entity_indicator(self.invalid), "")
Beispiel #3
0
class CommandLineInterface():

    def __init__(self):
        self.command_list = {
            "show_map": self.display_map,
            "create_hero": self.create_character,
            "known_as": self.known_as,
            "spawn_hero": self.spawn_character,
            "move": self.move_character,
            "heal": self.heal_character,
            "help": self.help_message,
            "exit": self.exit_game
        }
        self.exit = False
        self.is_created = False
        self.map_loaded = False
        self.validator = None
        self.input = None
        self.help = """show_map - Displays current dungeon map
create_hero <Name> <Health> <Nickname> - Creates hero with entered attributes
spawn_hero <Username> - Spawns hero with specified username into the dungeon
known_as - Displays name and nickname of hero
move <username> <direction> - Moves the hero in the specified direction
help - Displays this message
exit - Closes the program"""

    def start_game(self):
        print("Welcome. Type 'help' for available commands")
        while self.exit is False:
            command = self.get_input_command()
            if command[0] in self.command_list:
                execute = self.command_list[command[0]]
                output = execute()
                print(output) if output is not None else None
            else:
                print("Invalid command. Type 'help' for available commands.")

    def get_input_command(self):
        self.input = input("--> ").strip().split(" ")
        return self.input

    def load_map(self, map_path):
        self.validator = MapValidator(map_path)
        if not self.validator.validate_map():
            return self.validator.generate_message()
        else:
            self.dungeon = Dungeon(map_path)
            if self.dungeon.map:
                self.map_loaded = True
                return self.validator.generate_message()

    def display_map(self):
        return self.dungeon.print_map()

    def create_character(self):
        self.char = Hero(self.input[1], int(self.input[2]), self.input[3])
        self.char.weapon = Weapon("Ashbringer", 40, 0.8)
        self.is_created = True

    def known_as(self):
        return self.char.known_as()

    def spawn_character(self):
        if self.is_created is True:
            self.dungeon.spawn(self.input[1], self.char)
            self.dungeon.spawn_npcs()
        else:
            return "No created characters."

    def move_character(self):
        if self.input[1] not in self.dungeon.ingame:
            return "Player name is incorrect."

        elif not self.dungeon.ingame[self.input[1]].is_alive():
            self.exit = True
            return "Your character is dead. Game over."
        else:
            if self.dungeon.unlocked:
                self.exit = True
                return "Game Over. You have won."
            return self.dungeon.move_player(self.input[1], self.input[2])
            to_move = self.dungeon.get_random_npc()
            if not self.dungeon.ingame[to_move].is_alive():
                self.dungeon.ingame = {i: self.dungeon.ingame[i]
                                       for i in self.dungeon.ingame
                                       if i != 0}
                self.dungeon.npcs = {i: self.dungeon.npcs[i]
                                     for i in self.dungeon.npcs if i != 0}
            else:
                return self.dungeon.move_npc(to_move)
            return self.display_map()

    def heal_character(self):
        if self.is_created and self.map_loaded:
            self.char.health = self.char.max_health
        else:
            return "No created characters."

    def help_message(self):
        return self.help

    def exit_game(self):
        self.exit = True
        return "Goodbye!"