class AttackTest(unittest.TestCase): """Test Monster Class attack function""" def setUp(self): print(self.shortDescription()) self.test_monster = Monster("The Boogie Monster") def test_ideal_attack(self): """test ideal attack, won't ko""" self.assertEqual(5, self.test_monster.attack(5)) def test_kod_attack(self): """test kod attack""" self.test_monster.attack(15) self.assertEqual("K.O.'d", self.test_monster.status) def test_incorrect_type_attack(self): """test if we prevent bad types""" with self.assertRaises(TypeError): self.test_monster.attack("some bad types") def test_negative_healt(self): """we should test for negative health catches""" pass
class test_attack(unittest.TestCase): def setUp(self): self.monsta = Monster() print(self.shortDescription()) #Tests if heal function works def test_attack_live(self): self.monsta.health = 6 expected = 1 actual = self.monsta.attack(5) self.assertEqual(expected, actual) #Heal function should not work if total > 10 def test_attack_die(self): self.monsta.health = 4 expected = -1 actual = self.monsta.attack(5) self.assertEqual(expected, actual) #Tests if invalid output was printed @patch ('sys.stdout', new_callable=StringIO) def test_attack_die_KO(self, mock_stdout): health = self.monsta.health(4) expected = "K.O.'d" actual = self.monsta.attack(6) self.assertNotEqual(mock_stdout.getValue(), text)
class AttackTest(unittest.TestCase): """Test Monster Class attack function""" def setUp(self): print(self.shortDescription()) self.test_monster = Monster("The Boogie Monster") def test_ideal_attack(self): """test ideal attack, won't ko""" self.assertEqual(5, self.test_monster.attack(5)) def test_kod_attack(self): """test kod attack""" self.test_monster.attack(15) self.assertEqual("K.O.'d", self.test_monster.status) def test_kod_attack_health(self): """test kod attack""" self.test_monster.attack(15) self.assertEqual(-5, self.test_monster.health) def test_incorrect_type_attack(self): """test if we prevent bad types""" with self.assertRaises(TypeError): self.test_monster.attack("some bad types") def test_negative_healt(self): """we should test for negative health catches""" pass
class AttackTest(unittest.TestCase): """Test the functionality of the Monster class attack function.""" def setUp(self): self.new_monster = Monster("Cookie") def tearDown(self): del self.new_monster def test_attack_stay_alive(self): """ Initialize health to 10. Call function with 7 and check that 3 is returned and status is unchanged. """ self.new_monster.health = 10 original_status = self.new_monster.status # Call function new_health = self.new_monster.attack(7) self.assertEqual(new_health, 3, "Expected 3 for updated health.") self.assertEqual(self.new_monster.status, original_status, "Status should have remained {}". format(original_status)) def test_attack_ko_ed(self): """ Initialize health to 10. Call function with 12 and check that -2 is returned and status is "K.O.'d". """ self.new_monster.health = 10 # Call function new_health = self.new_monster.attack(12) self.assertEqual(new_health, -2, "Expected 3 for updated health.") self.assertEqual(self.new_monster.status, "K.O.'d", "Status should be K.O.'d")
class test_attack(unittest.TestCase): def setUp(self): self.monsta = Monster() self.monsta.health = 5 #Tests if heal function works def test_attack_live(self): health = self.monsta.attack(5) self.assertEqual(health, 0, "Attack did not work") #Heal function should not work if total > 10 def test_attack_die(self): health = self.monsta.attack(6) self.assertEqual(health, -1, "Attack did not work") #Tests if invalid output was printed @patch('sys.stdout', new_callable=StringIO) def test_attack_die_KO(self, mock_stdout): health = self.monsta.attack(6) text = "That is not a valid input" self.assertNotEqual(mock_stdout.getValue(), text)
def spawn_monster(self): """Spawn a monster """ """PRE:/ POST: New object of class Monster. Add to the attribut """ monster = Monster(self) monster2 = Monster(self) monster2.rect.y = random.randint(100, 200) monster2.rect.x = 1300 monster2.velocity = 1.5 monster2.attack = 50 monster2.image = pygame.image.load('im/dragon.png') self.all_monsters.add(monster, monster2)
class test_heal(unittest.TestCase): def setUp(self): self.monsta = Monster() self.monsta.health = 5 #Tests if monster heals def test_valid_heal(self): health = self.monsta.heal(5) self.assertEqual(health, 10, "Heal did not work") #Monster should not heal if sum > 10 def test_invalid_heal(self): health = self.monsta.heal(6) self.assertNotEqual(health, 11, "Heal should not have worked") #Tests invalid output @patch('sys.stdout', new_callable=StringIO) def test_attack_die_KO(self, mock_stdout): health = self.monsta.attack(6) text = "Invalid heal amount" self.assertNotEqual(mock_stdout.getValue(), text)
class test_heal(unittest.TestCase): def setUp(self): self.monsta = Monster() self.monsta.health = 5 #Tests if monster heals def test_valid_heal(self): health = self.monsta.heal(5) self.assertEqual(health, 10, "Heal did not work") #Monster should not heal if sum > 10 def test_invalid_heal(self): health = self.monsta.heal(6) self.assertNotEqual(health, 11, "Heal should not have worked") #Tests invalid output @patch ('sys.stdout', new_callable=StringIO) def test_attack_die_KO(self, mock_stdout): health = self.monsta.attack(6) text = "Invalid heal amount" self.assertNotEqual(mock_stdout.getValue(), text)
from monster import Monster # Create monsters player_name = input("What is your monster called? ") you = Monster(player_name) bad_guy = Monster("Doombeast") # Describe the HP of both players you.describe() bad_guy.describe() # Continue while both players are alive while you.is_alive() and bad_guy.is_alive(): # Monster's turn if bad_guy.is_alive(): bad_guy.attack(you) you.describe() # Is the player now dead? if you.is_alive() == False: print("You lost :(") else: you.attack(bad_guy) # Describe the outcome bad_guy.describe() if bad_guy.is_alive() == False: print("You win!")
you.describe() you.describe_item() bad_guy.describe() bad_guy.describe_item() # Continue while both players are alive while you.is_alive() and bad_guy.is_alive(): # Would you like to attack or defend? action = input("Would you like to attack (a) or defend (d)?") if action == "d": you.defend() # Monster's turn if bad_guy.is_alive(): bad_guy.attack(you) you.describe() # Is the player now dead? if you.is_alive() == False: print("You lost :(") else: if action == "a": you.attack(bad_guy) elif action != "d": print("I don't know how to " + action) # Describe the outcome bad_guy.describe() if bad_guy.is_alive() == False:
print(json.dumps(response, indent=2)) answer = response["output"]["generic"][0]["text"] #print(actions[answer]) # Player picks up an item if answer == 'pick-up': print('successfully picked up item') player.add_inventory('key') print(player.__dict__) else: print('did not pick up') # Monster attacks player print('A monster jumped out and attack you.') if monster.attack(player) <= 0: print('Game over.') else: print(player.__dict__) # Delete session response = assistant.delete_session(assistant_id, session_id).get_result() # Intents: Looking around (describe environment), Interact with object (picking up, putting down/activating/using), attacking # For picking up actions think of an inventory system that keeps track of what you have picked up so far. # Maybe add entities to Watson and only allow users to progress through the story by interacting with them. # Questions: # How do we do settings? # We wanted the system to figure out the wording details: maybe have our NLP-model automatically upload intents to Watson to generate the wording. Want ways to have same meaning but with new flavor. # For the first timebox: Have simple structure by filling up conversation nodes.
def attack(self, target): """Attack the target.""" Monster.attack(self, Object.obj_dict[target])
class Room(object): def __init__(self, difficulty, player): self.hasMonster = False self.hasBoss = False self.hasChest = False self.isDone = False self.difficulty = difficulty self.settings = Settings() self.goal = self.settings.getGoal() self.player = player self.chest = Chest(self.player) self.monster = Monster() self.inspected = False self.handler = Stringhandler() self.boss = Endboss() self.endboss = self.boss.randomBoss(player) # Sets up a new Room and displays some "Intro Text" def newRoom(self): self.isDone = False string = "\n\n#######################################"+"\n\n"+\ self.handler.strRoom("intro",self)+"\n\n"+\ "#######################################"+"\n\n" for char in string: time.sleep(uniform(0.05, 0.01)) sys.stdout.write('\033[35m' + char) sys.stdout.flush() print Fore.WHITE # Decides whether the room gets a monster or a chest if int(self.player.level) <= int(self.goal): rnd = random.randint(0, 10) if rnd > 1: self.hasMonster = True else: self.hasChest = True # Setup monster if self.hasMonster: if self.difficulty == "easy": self.monster.setup(1, self.player.level) else: self.monster.setup(2, self.player.level) # Setup chest elif self.hasChest: if self.difficulty == "easy": self.chest.level = random.randint(1, 10) return self.handler.strActions("roomEmpty", self.player, self) else: self.chest.level = random.randint(1, 10) else: self.finish() else: self.hasBoss = True return self.boss.spawn(self.player) def getRoom(self, difficulty, player): return Room(difficulty, player) def getBoss(self): return self.boss # If the player has used "look around" the room should be set to inspected def inspectRoom(self): self.inspected = True # Kills the monster in this Room, required for the player to be able to continue -> Room set to "Done" def killMonster(self): self.monster.kill() self.player.lvlUp() self.finish() def attackMonster(self, player): room = self return self.monster.attack(room, player) # Opens the chest, required for the player to be able to continue -> Room set to "Done" def openChest(self): self.finish() return self.chest.open(self.player) def is_done(self): return self.isDone def finish(self): self.isDone = True if self.hasChest: self.chest.is_opened = True
def monster_fight(p): print("You encountered a Monster!") m = Monster(abs(int(rd.gauss(p.level * 100, p.level * 100 / 2))), abs(int(rd.gauss(p.level * 10, p.level * 10 / 2)))) m.printSelf() win = -1 while True: print() i = input( "Your move! Attack(1), Drink Potions(2), Try to Run(3), Check Stats(4): " ) if is_int(i): i = int(i) else: continue if i == 1: last_hp = m.remaining_hp p.attack(m) print("You attacked! Monster's hp fell from ", last_hp, " to ", m.remaining_hp) elif i == 2: t = False j = 0 while not t: print("You have ", p.potions, " potions.") if p.potions == 0: break j = input("How many do you want to drink (20hp per 1): ") if is_int(j): j = int(j) t = p.drink_potion(j) if j == 0: continue print("You drank ", j, " potions! Your new hp is ", p.remaining_hp) elif i == 3: dev = p.luck - p.level if dev < abs(int(rd.gauss(0, dev))): print("You were able to run away!") break else: print("You failed to run away!") elif i == 4: p.printSelf() continue else: continue if (m.remaining_hp <= 0): win = 1 break last_hp = p.remaining_hp m.attack(p) print("The monster attacked you! Your hp fell from ", last_hp, " to ", p.remaining_hp) if (p.remaining_hp <= 0): win = 0 break if win == 1: p.get_experience(m) del m elif win == 0: print("You Lost! The game is over!") exit()
class World: def run(self): self.player = Monster(name='Player') self._load_history(self.player) monster = random.choice(list(monsters.viewkeys())) self.monster = Monster(monsters[monster][0], monster, description=monsters[monster][1]) self.ui = UI(self.player) self.ui.monster = self.monster self.ui.welcome() a = 1 while a != 0: self._load_history(self.monster) a = self.run_loop() if a != 0: self.ui.update_display() self._save_history(self.player) self._save_history(self.monster) monster = random.choice(list(monsters.viewkeys())) self.monster = Monster(monsters[monster][0], monster, description=monsters[monster][1]) self.ui.monster = self.monster def run_loop(self): player_move = "Run" while player_move.lower() != ACTION_QUIT: if self.player.offbalance: moves = [MOVE_BASH, MOVE_DEFEND] else: moves = [MOVE_ATTACK, MOVE_BASH, MOVE_DEFEND] if self.monster.offbalance: monster_moves = [MOVE_BASH, MOVE_DEFEND] else: monster_moves = [MOVE_ATTACK, MOVE_BASH, MOVE_DEFEND] player_move = self.ui.player_move(moves) if player_move == ACTION_QUIT: break if player_move not in moves: self.ui.msg = "I don't understand your input, try again." self.ui.update_display() continue monster_move = self.monster.attack(monster_moves, self.player) self.ui.monster.move = monster_move self.ui.player.move = player_move if player_move + monster_move in action_table: outcome = action_table[player_move + monster_move] self.monster.take_damage(outcome[2]) self.monster.offbalance = outcome[3] self.player.take_damage(outcome[0]) self.player.offbalance = outcome[1] else: outcome = action_table[monster_move + player_move] self.monster.take_damage(outcome[0]) self.monster.offbalance = outcome[1] self.player.take_damage(outcome[2]) self.player.offbalance = outcome[3] # record the moves just made for the AI self.player.history.append(player_move) self.monster.history.append(monster_move) self.ui.update_display() if self.player.is_dead() and self.monster.is_dead(): self.ui.msg = "You have slain the foul " + self.monster.name + ", but it appears that, in its dying throes, the " + self.monster.name + "has also slain you." return 0 elif self.player.is_dead(): self.ui.msg = "Oh no! You seem to have perished!" return 0 elif self.monster.is_dead(): self.ui.msg = "You have slain the " + self.monster.name + "." return 1 return 0 def _load_history(self, monster): name = 'player' if monster.name.lower() != 'player': name = 'monster' try: f = open(name + '.moves', 'r+') monster.history = pickle.load(f) f.close() except Exception: pass def _save_history(self, monster): name = 'player' if monster.name.lower() != 'player': name = 'monster' try: f = open(name + '.moves', 'w+') print monster.name print len(monster.history) pickle.dump(monster.history, f) f.close() except Exception: pass
# Check for death. if enemy.is_dead(): #The player has won. game_end("You won!") elif action == "2": # Do a especial attack. player.special_attack(enemy) # Check for death. if enemy.is_dead(): #The player has won. game_end("You won!") elif action == "3": game_end("You just surrendered. Too bad! Waluigi time!") # Enemy's turn. enemy.attack(player) enemy_last_action = "\n\tThe " + enemy.get_name() + " attacked!\n" # Check for death. if player.is_dead(): # The player has lost. game_end("You lost. Try again.") turn_count += 1