def test_dungeon_build(): """method to build a dungeon for testing and dev.""" dungeon_name = "Test Dungeon" room_one_date =["Entry Room", "The first room in the test dungeon"] room_one = Room(room_one_date) roomtwo_data = ["second Room", "The second room of the test dungeon"] room_two = Room(roomtwo_data) exit1 = Exit("n", room_one, room_two) exit2 = Exit("s", room_two, room_one) room_one.add_exit(exit1) room_two.add_exit(exit2) test_dungeon = Dungeon(dungeon_name, room_one) test_dungeon.add_room(room_two) item_one_stats = ["Apple", "A red Fuji Apple", .2] item_one_actions = {"eat": "consume-You eat the apple", "examine": "It's a\ red apple. It looks really tasty.", "throw": "destroy-You hurl the \ apple agaisnt the wall. It smashes agaisnt it with a splat."} item_one_date = ["Apple", "A red Fuji Apple", 0.2,item_one_actions] item_one = item(item_one_date) item_two_stats = ["Spoon", "a metal spoon", .01] item_two_actions = {"examine": "examine-The spoon is made of some form of lite metal, perhaps tin.", "throw": "remove-You hurl the \ spoon agaisnt the wall. It clashes against the wall with a clatter."} item_two_data = ["Spoon", "a metal spoon", 0.01,item_two_actions] item_two = item(item_two_data) room_one.add_item(item_one) room_one.add_item(item_two) return test_dungeon
def moddungeon(request, did): d = Dungeon() d.load(did) monster_infos = [] for i in MonsterDB.objects.all(): mon = {} mon["id"] = "%d" % i.mid mon["name"] = i.name monster_infos.append(mon) ailist = [] for i in actmode.actionmodelist: ai = {} ai["id"] = "%d" % i.id ai["name"] = i.name ailist.append(ai) skill_list = [] for i in skills.skilllist: sk = {} sk["id"] = "%d" % i.id sk["name"] = i.name skill_list.append(sk) response = render_to_response('dungeonmod.tpl', {"did":did, "name":d.name, "minfo":monster_infos, "ailist":ailist, "sk":skill_list, "data":d.data}, context_instance=RequestContext(request)) if 'text/html' in response['Content-Type']: response.content = short(response.content) return response
def test_validate_map_with_more_than_one_gates(self): with self.assertRaises(AssertionError): Dungeon.from_string('''S.##.....T #G##..###. #.###E###E #.E...###. ###T#####G''')
def test_validate_map_with_no_starting_point_should_raise_error(self): with self.assertRaises(AssertionError): Dungeon.from_string('''T.##.....T #T##..###. #.###E###E #.E...###. ###T#####G''')
def setUp(self): self.filename = 'dungeon.txt' self.file_to_read = open(self.filename, 'w') field = ["S.##......", "#.##..###.", "#.###.###.", "#.....###.", "###.#####S"] self.file_to_read.write("\n".join(field)) self.file_to_read.close() self.dungeon = Dungeon("dungeon.txt")
def __init__(self, town_window): self.dungeon = Dungeon() self.town_controller = town_window.town_controller self.town = self.town_controller.town self.next_party() self.next_monster() self.last_attack = None
def setUp(self): self.dungeon = Dungeon("level1.txt") self.hero = Hero(name="Bron", title="Dragonslayer", health=100, mana=100, mana_regeneration_rate=2)
class Game: def __init__(self): self.screen = create_screen() self.tmp_dungeon = Dungeon(80, 20) self.tmp_dungeon.generate() self.dungeons = [self.tmp_dungeon] self.player = Player(*self.dungeons[0].up_stair) def play(self): self.screen.get_key() self.screen.draw_dungeon(self.dungeons[self.player.floor - 1], self.player) self.screen.push_message('Welcome to Necronomicon, my first roguelike...') while True: # Main game loop char = self.screen.get_key() if char == 'q': # Quit game quit = self.screen.ask_message('Do you really want to quit?') if quit.lower() in ['y', 'yes', 'yea']: break elif char == '?': # Show help self.screen.push_message('This is a fantastic game... :)') elif char in ['h', 'j', 'k', 'l', 'y', 'u', 'b', 'n', '.']: # Movement success = self.player.move(char, self.dungeons[self.player.floor - 1]) if success: self.screen.draw_dungeon(self.dungeons[self.player.floor - 1], self.player) self.screen.update_info(self.player) else: self.screen.push_message('You can\'t go on this way') elif char == '>': if self.dungeons[self.player.floor - 1].cell(self.player.x, self.player.y).char == '>': self.dungeons.append(Dungeon(80, 20)) self.player.floor += 1 self.dungeons[self.player.floor - 1].generate() self.player.moves += 1 self.screen.update_info(self.player) self.player.x, self.player.y = self.dungeons[self.player.floor - 1].up_stair self.screen.draw_dungeon(self.dungeons[self.player.floor - 1], self.player) self.screen.push_message('Welcome to the %d floor' % self.player.floor) else: self.screen.push_message('There isn\'t down stairs here') elif char == '<': if self.dungeons[self.player.floor - 1].cell(self.player.x, self.player.y).char == '<': if self.player.floor == 1: self.screen.push_message('You can\'t escape from the dungeon') else: self.player.floor -= 1 self.player.moves += 1 self.player.x, self.player.y = self.dungeons[self.player.floor - 1].down_stair self.screen.push_message('You returned at the %d dungeon' % self.player.floor) self.screen.update_info(self.player) self.screen.draw_dungeon(self.dungeons[self.player.floor - 1], self.player) else: self.screen.push_message('There isn\'up stairs here')
def test_print_map_function_should_print_map(self): load_map = Dungeon('level1.txt') printed_map = load_map.__str__() self.assertEqual(str(load_map), printed_map)
def test_move_hero_up_should_return_true_if_can_move_or_false_if_not(self): map_ = Dungeon('level1.txt') map_.level_map[0][0] = 'H' result = map_.move_hero('up') self.assertEqual(result, False)
class test_smtg_test(unittest.TestCase): """Testing the unit""" def setUp(self): self.filename = 'test_dng_map.txt' f = open(self.filename, "w") content_map = 'S.##......\n#.##..###.\n#.###.###.\n#.....###.\n###.#####S' f.write(content_map) f.close() self.new_map = Dungeon(self.filename) def test_load_map(self): expected = 'S.##......\n#.##..###.\n#.###.###.\n#.....###.\n###.#####S' self.assertEqual(expected, self.new_map.load_map()) def test_map_to_matrix(self): expected = [['S', '.', '#', '#', '.', '.', '.', '.', '.', '.'], ['#', '.', '#', '#', '.', '.', '#', '#', '#', '.'], ['#', '.', '#', '#', '#', '.', '#', '#', '#', '.'], ['#', '.', '.', '.', '.', '.', '#', '#', '#', '.'], ['#', '#', '#', '.', '#', '#', '#', '#', '#', 'S']] self.assertEqual(expected, self.new_map.map_to_matrix()) def tearDown(self): # fpath = os.getcwd() try: os.remove(self.filename) except OSError: print('No such file')
def test_move_hero_should_increase_hero_mana(self): map_ = Dungeon('level1.txt') map_.level_map[0][0] = 'H' result = map_.move_hero('right') self.assertEqual(result, True)
class TestDungeon(unittest.TestCase): def setUp(self): self.my_map = Dungeon("basic_dungeon.txt") #def test_map_dungeon(self): # self.my_map.print_map() def test_spawn_hero(self): good_hero = hero.Hero("Sebastian", 100, "The great") self.assertTrue(self.my_map.spawn("The Great", good_hero)) # self.my_map.print_map() bad_orc = orc.Orc("Mudagog", 100, 1.4) self.assertTrue(self.my_map.spawn("The Badass", bad_orc)) # self.my_map.print_map() def test_move_hero(self): good_hero = hero.Hero("Sebastian", 100, "The great") self.assertTrue(self.my_map.spawn("The Great", good_hero)) # self.my_map.print_map() bad_orc = orc.Orc("Mudagog", 100, 1.4) self.assertTrue(self.my_map.spawn("The Badass", bad_orc)) # self.my_map.print_map() self.assertFalse(self.my_map.move("The Great", "up")) self.assertFalse(self.my_map.move("The Great", "left")) self.assertTrue(self.my_map.move("The Badass", "up")) self.assertFalse(self.my_map.move("The Badass", "right")) self.my_map.print_map()
class Game(): def __init__(self): pygame.init() self.window_w = cons.TILE_D * cons.SCREEN_TW self.window_h = cons.TILE_D * cons.SCREEN_TH self.screensize = (self.window_w, self.window_h) self.screen = pygame.display.set_mode(self.screensize) self.running = True self.setup() def setup(self): dungeonsurface = pygame.Surface(cons.MAP_DIM) self.dungeon = Dungeon(dungeonsurface, cons.MAP_POS, 50, 50) statsurface = pygame.Surface(cons.STAT_DIM) self.statview = StatView(statsurface, cons.STAT_POS) logsurface = pygame.Surface(cons.LOG_DIM) self.logview = Log(logsurface, cons.LOG_POS) # Test player self.px = 25 self.py = 25 def handle_events(self): events = pygame.event.get() for event in events: # Quit the game. if event.type == pygame.QUIT: self.running = False break if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: self.running = False break # Toggle fullscreen. if event.type == pygame.KEYDOWN and event.key == pygame.K_f: if self.screen.get_flags() & pygame.FULLSCREEN: pygame.display.set_mode(self.screensize) else: pygame.display.set_mode(self.screensize, pygame.FULLSCREEN) # Move the player. if event.type == pygame.KEYDOWN and event.key == pygame.K_UP: self.py -= 1 if event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN: self.py += 1 if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT: self.px -= 1 if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT: self.px += 1 def draw(self): self.dungeon.draw(self.screen, self.px, self.py) self.statview.draw(self.screen) self.logview.draw(self.screen) def loop(self): while self.running: self.handle_events() self.draw() pygame.display.update() pygame.quit()
def setUp(self): self.hero = Hero("Bron", 100, "DragonSlayer") self.orc = Ork("BronOrk", 100, 1.1) self.axe = Weapon("Axe", 12.45, 0.2) self.sword = Weapon("Sword of Arthur", 13, 0.5) self.battle = Fight(self.hero, self.orc) self.dungeon = Dungeon("dungeon.txt")
def rat_in_looped_dungeon() -> Rat: """Return a rat object ready to explore a dungeon with 6 rooms where there are two paths from the start to the finish. """ r0 = Room('start', 0, 9, 9) d = Dungeon(r0) r1 = Room('one', 1, 9, 8) r2 = Room('two', 1, 9, 7) r3 = Room('three', 1, 9, 6) r4 = Room('four', 1, 9, 5) r5 = Room('five', 1, 9, 4) unconnected = Room('unconnected', 1, 8, 8) # r0 -> r1, r1 -> r2, r3; r2 -> r0, r3 -> r4, r4 -> r5 r0.add_neighbor(r1, Direction.DOWN) r1.add_neighbor(r2, Direction.NORTH) r1.add_neighbor(r3, Direction.SOUTH) r2.add_neighbor(r0, Direction.UP) r3.add_neighbor(r4, Direction.EAST) r4.add_neighbor(r5, Direction.SOUTH) d.add_room(r1) d.add_room(r2) d.add_room(r3) d.add_room(r4) d.add_room(r5) d.add_room(unconnected) rat = Rat(d, r0) return rat
def setUp(self): self.filename = "basic_dungeon.txt" self.filename2 = "basic_dungeon2.txt" self.string = "..##.....S\n" \ "#.##..###.\n" \ "#.###.###.\n" \ "#S....###.\n" \ "###.#####.\n" self.string2 = "..##....SS\n" \ "#.##..###.\n" \ "#S###.###.\n" \ "#S....###.\n" \ "###.#####.\n" with open(self.filename, 'w+') as file: file.write(self.string) with open(self.filename2, "w+") as file: file.write(self.string2) self.dung = Dungeon(self.filename) self.dung2 = Dungeon(self.filename2) self.l = self.string2.split('\n') self.l.pop() self.list = [] for line in self.l: self.list.append(list(line)) self.test_orc = Orc("Garosh", 100, 1) self.test_hero = Hero("Varyan", 100, "test") self.spawn_orc = self.dung.spawn("player2", self.test_orc) self.spawn_hero = self.dung.spawn("player1", self.test_hero) self.spawn_orc2 = self.dung.spawn("player3", self.test_orc)
def __init__(self): self.dungeon = Dungeon(4, 4) self.dungeon.generate() self.adventurer = Adventurer("") self.root = Tk() self.root.resizable(False, False) self.root.title("Dungeon Adventure") self.start_menu_init()
def connect_neighbors(dungeon: Dungeon, row: int, col: int) -> None: """Connects the room at the given row, col to all neighbors""" current_room = dungeon.find(str(row) + "," + str(col)) if row != 0: room = dungeon.find(str(row - 1) + "," + str(col)) current_room.add_neighbor(room, Direction.NORTH) if col != 0: room = dungeon.find(str(row) + "," + str(col - 1)) current_room.add_neighbor(room, Direction.WEST)
def check_paths_are_similar(dungeon: Dungeon, computed_path: List[str], expected_path: List[str]) -> None: """Checks if computed path is valid and has the right length.""" rooms_in_path = [dungeon.find(r) for r in computed_path] if not dungeon.valid_path(rooms_in_path): print("Computed path, " + str(computed_path) + ", is not connected.") assert dungeon.valid_path(rooms_in_path) if len(computed_path) > len(expected_path): print("Computed path, " + str(computed_path) + ", is longer than expected path, " + str(expected_path)) assert len(computed_path) == len(expected_path) # should neve fail
def setUp(self): self.test_m = [ '..##......', '#.##..###.', '#.###.###.', '#..S.S###.', '###.#####.' ] test_map_file = open('test_map.txt', 'w') test_map_file.write('\n'.join(self.test_m)) test_map_file.close() self.test_map = Dungeon('test_map.txt')
def test_trying_to_move_dead_hero(self): hero = Hero('Ivan', 'Hacker', 0, 0, 5) dungeon = Dungeon('level1.txt') dungeon.spawn(hero) expected = None result = dungeon.move_hero('up') self.assertEqual(expected, result)
def test_trying_to_move_hero_in_starting_position_down(self): hero = Hero('Ivan', 'Hacker', 20, 20, 5) dungeon = Dungeon('level1.txt') dungeon.spawn(hero) expected = False result = dungeon.move_hero('down') self.assertEqual(expected, result)
def test_map_after_spawn(self): map = Dungeon('levels/level1.txt') brom = Hero('Brom', 'Gunslinger', 200, 200, 200) map.spawn(brom) expected_result = [['H', '#', '#', '#', '#'], ['T', 'T', 'T', 'T', 'T'], ['E', '.', 'E', '.', 'E'], ['#', '#', '#', '#', 'G']] self.assertEqual(map.print_map(), expected_result)
def test_validate_map_with_unequal_lengths_of_map_rows(self): string = ('''S.##.....T #T##..###. #.###E### #.E...###. ###T#####G''') with self.assertRaises(AssertionError): Dungeon.from_string(string)
def setUp(self): file = open("test_file.txt", "w") content = ["S...S", "", "Spoon 4 0.2", "RustyFork 6 0.7"] file.write("\n".join(content)) file.close() self.dungeon = Dungeon("test_file.txt") self.hero = Hero("Don Quixote", 20, "The Common Sense") self.orc = Orc("Oplik", 20, 1.5)
def test_can_attack_reaches_obstacle(self): map = Dungeon("level2.txt", "treasures_file.json", "enemies.json") hero = Hero(name="Panda", title="Dragonslayer", health=100, mana=100, mana_regeneration_rate=2) map.spawn(hero) self.assertFalse(map.can_attack(0, 1, 3))
def test_spawns_hero_on_first_available_position(self): hero = Hero('Ivan', 'Hacker', 30, 40, 5) dungeon = Dungeon('level1.txt') expected = True result = dungeon.spawn(hero) self.assertEqual(expected, result) self.assertTrue(dungeon.map[0][0] == 'H')
def test_can_attack_finds_enemy(self): map = Dungeon("level2.txt", "treasures_file.json", "enemies.json") hero = Hero(name="Panda", title="Dragonslayer", health=100, mana=100, mana_regeneration_rate=2) map.spawn(hero) self.assertEqual((2, 0), map.can_attack(1, 0, 2))
def setUp(self): self.sniper = Weaphon("Gun", 85, 0.10) self.hammer = Weaphon("One-Hand", 30, 0) self.bron_orc = Orc("Pesho", 100, 1.50) self.bron_hero = Hero("Ivan", 100, "Magician") self.bron_orc.equip_weapon(self.hammer) self.bron_hero.equip_weapon(self.sniper) self.magura = Dungeon("/home/biser/TDD/simple_dungeon.txt")
def setUp(self): self.existing_file = str(uuid4) self.test_map = \ "S.##......\n#.##..###.\n#.###.###.\n#.....###.\n###.#####S" with open(self.existing_file, "w") as writeFile: writeFile.write(self.test_map) self.dungeon = Dungeon(self.existing_file) self.hero = Hero("Hero", 100, "Nick") self.orc = Orc("Orc", 100, 2)
def test_raises_exception_when_direction_is_not_string(self): exc = None dungeon = Dungeon('level1.txt') try: dungeon.move_hero(1) except Exception as err: exc = err self.assertIsNotNone(exc) self.assertEqual(str(exc), 'Wrong input - you must enter string!')
def test_raises_exception_when_direction_is_incorrect(self): exc = None dungeon = Dungeon('level1.txt') try: dungeon.move_hero('nagore') except Exception as err: exc = err self.assertIsNotNone(exc) self.assertEqual(str(exc), 'Incorrect value!')
def setup(self): dungeonsurface = pygame.Surface(cons.MAP_DIM) self.dungeon = Dungeon(dungeonsurface, cons.MAP_POS, 50, 50) statsurface = pygame.Surface(cons.STAT_DIM) self.statview = StatView(statsurface, cons.STAT_POS) logsurface = pygame.Surface(cons.LOG_DIM) self.logview = Log(logsurface, cons.LOG_POS) # Test player self.px = 25 self.py = 25
def setUp(self): self.start_map_content = 'S.##......\n#.##..###.\n#.###.###.\n#.....###.\n###.#####S' with open('basic_dungeon.txt', 'w') as map_file: map_file.write(self.start_map_content) self.map = Dungeon('basic_dungeon.txt') self.hero_player = Hero('Stoyan', 100, 'DeathBringer') self.hero_player.equip_weapon(Weapon('Sword', 20, 0.5)) self.orc_player = Orc('Broks', 100, 1.5) self.orc_player.equip_weapon(Weapon('Stick', 10, 0.1)) self.battle_map_content = 'S.#\n#S#' with open('battle_map.txt', 'w') as map_file: map_file.write(self.battle_map_content) self.battle_map = Dungeon('battle_map.txt') self.battle_map.spawn('human', self.hero_player) self.battle_map.spawn('orc', self.orc_player)
class test_smtg_test(unittest.TestCase): """Testing the unit""" def setUp(self): self.new_map = Dungeon(1) def test_load_map(self): expected = 'S.IP..........PPI###############\nI#########.#####################\n.....P.........................A\n.####.##.###.###################\nP####.##.###.#####.....#########\n.####.##.###.#####.###.##IIIIII#\nP####.I..###.#####.###.##P....I#\n.####P##P###.......###.##P######\nP####.##.############.........A.\nI####A...######################G' self.assertEqual(expected, self.new_map.load_map()) def test_map_to_matrix(self): expected = [['S', '.', 'I', 'P', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'P', 'P', 'I', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'], ['I', '#', '#', '#', '#', '#', '#', '#', '#', '#', '.', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'], ['.', '.', '.', '.', '.', 'P', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'A'], ['.', '#', '#', '#', '#', '.', '#', '#', '.', '#', '#', '#', '.', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#'], ['P', '#', '#', '#', '#', '.', '#', '#', '.', '#', '#', '#', '.', '#', '#', '#', '#', '#', '.', '.', '.', '.', '.', '#', '#', '#', '#', '#', '#', '#', '#', '#'], ['.', '#', '#', '#', '#', '.', '#', '#', '.', '#', '#', '#', '.', '#', '#', '#', '#', '#', '.', '#', '#', '#', '.', '#', '#', 'I', 'I', 'I', 'I', 'I', 'I', '#'], ['P', '#', '#', '#', '#', '.', 'I', '.', '.', '#', '#', '#', '.', '#', '#', '#', '#', '#', '.', '#', '#', '#', '.', '#', '#', 'P', '.', '.', '.', '.', 'I', '#'], ['.', '#', '#', '#', '#', 'P', '#', '#', 'P', '#', '#', '#', '.', '.', '.', '.', '.', '.', '.', '#', '#', '#', '.', '#', '#', 'P', '#', '#', '#', '#', '#', '#'], ['P', '#', '#', '#', '#', '.', '#', '#', '.', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '.', '.', '.', '.', '.', '.', '.', '.', '.', 'A', '.'], ['I', '#', '#', '#', '#', 'A', '.', '.', '.', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', 'G']] self.assertEqual(expected, self.new_map.map_to_matrix())
def main(): """ Quick game setup for testing purposes. """ win = pygcurse.PygcurseWindow(80, 30) win.font = pygame.font.Font(pygame.font.match_font("consolas"), 18) level1 = Dungeon.load_from_file("map/bigmap.txt") player = Player(1, 1) level1.add_player(player) msgbox = MessageBox() view = GameView( win, {ScrollingView(level1): (0, 0), HUDView(player): (700, 0), MessageBoxView(msgbox, 80, 5): (0, 460)} ) controller = Controller(level1, msgbox, view) win.autoupdate = False mainClock = pygame.time.Clock() running = True while running: for event in pygame.event.get(): if event.type == QUIT: running = False else: controller.process_event(event) controller.view.draw() win.blittowindow() mainClock.tick(30) pygame.quit() sys.exit()
def setUp(self): self.hero = Hero("Ahri", "Kingslayer", 100, 100, 2) self.enemy = Enemy(100, 100, 20) self.dungeon = Dungeon("level1.txt") self.weapon = Weapon("The Axe of Destiny", damage=20) self.spell = Spell( name="Fireball", damage=30, mana_cost=50, cast_range=1)
def run(self): running = True system('clear') print("Welcome to Dungeons & Pythons!") print("What are you waiting for? Create your character and start slaying...") self.initialize_game() print("Loading Level " + str(self.level) + '...') sleep(3) while running: system('clear') if not self.hero.is_alive(): print("\n\nGAME OVER!") sleep(3) self.run() print("Character: {0}".format(str(self.hero))) print("Health: {0}".format(str(self.hero.get_health()))) print("Weapon: {0}".format(str(self.hero.weapon))) print("Level: {0}".format(str(self.level))) print("\n") self.map.print_map() command = input("\nEnter direction <u, d, r, l>: ") self.parser.take_command(command) self.hero = self.map.hero if self.map.game_ended: system('clear') self.level += 1 self.map = Dungeon(self.level) if not self.map.map: print("YOU WON!!! Congratulations, Dungeon Master! ;)") self.map.spawn(self.hero) print("Loading Level " + str(self.level) + '...') sleep(3)
def __init__(self): self.screen = create_screen() self.tmp_dungeon = Dungeon(80, 20) self.tmp_dungeon.generate() self.dungeons = [self.tmp_dungeon] self.player = Player(*self.dungeons[0].up_stair)
def setUp(self): self.filename = 'test_dng_map.txt' f = open(self.filename, "w") content_map = 'S.##......\n#.##..###.\n#.###.###.\n#.....###.\n###.#####S' f.write(content_map) f.close() self.new_map = Dungeon(self.filename)
def initialize_game(self): name = input("Character Name> ") health = 100 nickname = input("Character Nickname> ") self.hero = Hero(name, health, nickname) self.level = 1 self.map = Dungeon() self.map.spawn(self.hero)
def test_move_left_and_fight(self): second_content = 'SS##......\n#.##..###.\n#.###.###.\n#.....###.\n###.#####.' second_filename = "basic_dungeon1.txt" second_file = open(second_filename,'w') second_file.write(second_content) second_file.close() second_map = Dungeon(second_filename) #---------------------------------------------- player1 = Hero("Bron", 10, "DragonSlayer") player2 = Orc('karakondjul', 100 , 1.5) second_map.spawn(player1.name, player1) second_map.spawn(player2.name, player2) self.assertTrue(second_map.move(player2.name, 'left')) os.remove(second_filename)
def setUp(self): map_dungeon = """S.##.....T #T##..###. #.###E###E #.E...###. ###T#####G""" self.dungeon = Dungeon.create_from_string(map_dungeon) self.hero = Hero("Bron", "Dragon", 100, 100, 2)
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()
class TestDungeon(unittest.TestCase): def setUp(self): self.filename = "maze.txt" self.contents = "S.##......\n#.##..###.\n#.###.###.\n#.....###.\n###.#####S" with open(self.filename, "w+") as f: f.write(self.contents) f.close() self.peshtera = Dungeon("maze.txt") self.orc = Orc("Ivancho", 100, 1.3) self.hero = Hero("Petarcho", 100, "Garbage") def tearDown(self): os.remove(self.filename) def test_dungeon_map(self): with open(self.filename, "r+") as f: actual = f.read() f.close() self.assertEqual(self.contents, actual) def test_print_map(self): output = self.peshtera.print_map() with open(self.filename, "r+") as f: actual = f.read() f.close() self.assertEqual(output, actual) def test_spawn_character(self): self.peshtera.spawn("Player One", self.orc) with open(self.filename, "r+") as f: actual = f.read() f.close() contents = "O.##......\n#.##..###.\n#.###.###.\n#.....###.\n###.#####S" self.assertEqual(actual, contents) def test_spawn_already_spawned_character(self): self.peshtera.spawnlist = {"Player One": self.orc} result = self.peshtera.spawn("Player One", self.orc) self.assertEqual("Character is already spawned.", result) def test_spawn_with_no_free_slots(self): self.filename = "maze.txt" self.contents = "..##......\n#.##..###.\n#.###.###.\n#.....###.\n###.#####." with open(self.filename, "w+") as f: f.write(self.contents) f.close() result = self.peshtera.spawn("One", self.orc) self.assertEqual("No free spawn slot.", result) def test_move_to_unknown_given_direction(self): self.peshtera.spawn("Orc", self.orc) result = self.peshtera.move("Orc", "test") self.assertEqual("Wrong direction given.", result)
def setUp(self): self.filename = "maze.txt" self.contents = "S.##......\n#.##..###.\n#.###.###.\n#.....###.\n###.#####S" with open(self.filename, "w+") as f: f.write(self.contents) f.close() self.peshtera = Dungeon("maze.txt") self.orc = Orc("Ivancho", 100, 1.3) self.hero = Hero("Petarcho", 100, "Garbage")
def setUp(self): self.content = 'S.##......\n#.##..###.\n#.###.###.\n#.....###.\n###.#####S' self.filename = "basic_dungeon.txt" self.file = open(self.filename,'w') self.file.write(self.content) self.file.close() ##------------------------- self.new_map = Dungeon(self.filename)
def setUp(self): self.new_map = [ ['S', '.', '#', '#', '.', '.', '.', '.', '.', 'T'], ['#', 'T', '#', '#', '.', '.', '#', '#', '#', '.'], ['#', '.', '#', '#', '#', 'E', '#', '#', '#', 'E'], ['#', '.', 'E', '.', '.', '.', '#', '#', '#', '.'], ['#', '#', '#', 'T', '#', '#', '#', '#', '#', 'G'] ] self.dungeon = Dungeon(self.new_map) self.hero = Hero("Floki", "Builder", 100, 100, 2)
def setUp(self): self.test_m = ['..##......', '#.##..###.', '#.###.###.', '#..S.S###.', '###.#####.'] test_map_file = open('test_map.txt', 'w') test_map_file.write('\n'.join(self.test_m)) test_map_file.close() self.test_map = Dungeon('test_map.txt')
def setUp(self): self.hero = Hero(name="Bron", title="Dragonslayer", health=100, mana=100, mana_regeneration_rate=2) self.weapon = Weapon(name="The Axe of Destiny", damage=20) self.spell = Spell(name="Fireball", damage=30, mana_cost=50, cast_range=2) self.d = Dungeon("level1.txt")
def setUp(self): self.dungeon_map = [ ['.', 'S', '#', '#', '.', '.', '.', '.', '.', 'T'], ['#', 'T', '#', '#', '.', '.', '#', '#', '#', '.'], ['#', '.', '#', '#', '#', 'E', '#', '#', '#', 'E'], ['#', '.', 'E', '.', '.', '.', '#', '#', '#', '.'], ['#', '#', '#', 'T', '#', '#', '#', '#', '#', 'G']] f = open('test_level.txt', 'w') for line in self.dungeon_map: f.write(''.join(line) + '\n') f.close() self.dungeon = Dungeon("test_level.txt")
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 self.grid = self.dungeon.convert_map_to_changeable_tiles() self.map_row = len(self.grid) self.map_col = len(self.grid[0]) return self.validator.generate_message()
def test_move_on_enemy(self): map2 = """S.##.....T #E##..###. #.###E###E #.E...###. ###T#####G""" dungeon2 = Dungeon.create_from_string(map2) dungeon2.spawn(self.hero) dungeon2.move("right") with self.assertRaises(Exception): dungeon2.move("down")