14817 - Battle Simulator   

Description

This year NTHU's I2P class is divided into two classes (Class A and Class B). To celebrate our first midterm the Domo  decided to host a tournament between the 2 classes.  Each class chooses one champion to represent them in a turn-based duel.

You are to simulate a turn-based battle between two characters.
Each character has three basic attributes:

  • HP (hit points) — health of the character.

  • Attack — determines how much damage they can deal.

  • Defense — reduces incoming damage.

Characters can receive buffs that modify these attributes before the battle starts.
Each buff can modify attack, defense, or HP using operations such as addition or multiplication.

You must implement a function composition system so that multiple buffs can be combined into a single function.

List of possible buffs are:

  • "atk_add val" : increases attack by val
  • "atk_mul val" : multiplies attack by val
  • "dfs_add val" : increases defense by val
  • "dfs_mul val" : multiplies defense by val

For example if you have the given buffs:

atk_add 5
atk_mul 2
atk_add 3
atk_mul 0.2

your new F function will be

F(x) = ((((x + 5) * 2) + 3) * 0.2)

Given the code:

class Character:
    def __init__(self, name, hp, attack, defense):
        self.name = name
        self.hp = hp
        self.attack = attack
        self.defense = defense
        self.buff_func = lambda atk, dfs, hp: (atk, dfs, hp)  # identity function

    def apply_buffs(self):
        """
        #TODO: 
        Apply the composed buff function to (attack, defense, hp).
        Returns the updated (attack, defense, hp).
        """
        pass
  
    def is_alive(self):
        #TODO: Return True if HP > 0.
        pass
  
    def take_damage(self, amount):
        #TODO: Reduce HP by damage
        pass
  
    def attack_target(self, other):
        #TODO: Attack another character
        pass
  
def compose_buffs(*funcs):
    """Compose multiple buffs into one function."""
    pass
  
def buff_factory(cmd, x):
    """Return a buff function that modifies (atk, dfs, hp)."""
    if cmd == "atk_add":
        pass
    elif cmd == "atk_mul":
        pass
    elif cmd == "dfs_add":
        pass
    elif cmd == "dfs_mul":
        pass
    else:
        pass
  
def battle(c1, c2):
    """
    #TODO
    Turn-based battle: c1 attacks first, alternate until one dies.
    returns the winner's name and hp
    """
    pass
  

if __name__ == "__main__":
    # Input format:
    # name1 hp1 atk1 dfs1
    # name2 hp2 atk2 dfs2
    # n
    # Then n lines:
    # target cmd value
    # (target is either name1 or name2)
    n1, h1, a1, d1 = input().split()
    n2, h2, a2, d2 = input().split()
    c1 = Character(n1, int(h1), int(a1), int(d1))
    c2 = Character(n2, int(h2), int(a2), int(d2))
  
    n = int(input())
    buffs_c1 = []
    buffs_c2 = []
    for _ in range(n):
        target, cmd, val = input().split()
        buff = buff_factory(cmd, float(val))
        if target == c1.name:
            buffs_c1.append(buff)
        elif target == c2.name:
            buffs_c2.append(buff)
  
    c1.buff_func = compose_buffs(*buffs_c1)
    c2.buff_func = compose_buffs(*buffs_c2)
  
    winner, hp_left = battle(c1, c2)
    print(f"{winner} wins with {hp_left:.1f} HP left.")

Simulate the Battle Between the 2 characters given the following rules:

  • The battle alternates turns:

    • The first character (c1) attacks first.

    • Then the second character (c2) attacks, and so on.

  • When a character’s HP becomes 0 or below, the battle ends.

  • Output the winner’s name and their remaining HP (1 decimal place).

  • Buffs will be applied EVERY round before attacking the other character (BUFF WILL STACK THROUGH THE ROUNDS)

example:
intial attack : 20
buff : attack_add 5
Round 1: attack = 20 + 5 =25
Round 2: attack = 25 + 5 = 30
Round 3: attack = 30 + 5 = 35
  • The damage taken for the character will be max(1, attackers.attack - receiver.defense) "APPLY BUFF FIRST BEFORE ATTACKING" 

 

Please refer to the following link to see the round by round break down explanation of the sample input if you are confused:
Link

Input

  • The first two line describe our 2 characters
    <name1> <hp1> <attack1> <defense1>
    <name2> <hp2> <attack2> <defense2>
  • Followed by an integer n
  • the next  n lines describes the buffs
    <target1> <cmd1> <val1>
    <target2> <cmd2> <val2>
    ...
    <targetn> <cmdn> <valn>


    target — the character’s name receiving the buff
    cmd — one of the buff commands (e.g., atk_add, hp_mul, etc.)
    val — a floating-point or integer value

 

Constraints

  • 1 ≤ hp, attack, defense ≤ 1000

  • 0 ≤ n ≤ 20

  • val can be integer or float (1 ≤ val ≤ 10)

  • Character names are unique and contain only letters.

Output

Output the winner's name and his/her hp with the following format

<winner_name> wins with <remaining_hp> HP left.

Sample Input  Download

Sample Output  Download

Tags




Discuss