Monster Battle
OpenAIPhone screenMedium
Two teams of monsters fight one battle in turns, and you return a log of everything that happens, in order.
A team is an object with a string "name" and a list "monsters". A monster is an object with a string "name" and integers "health" and "attack". The order of the monsters list is the order the team fights in.
The log is a list of strings in exactly these formats:
- First:
Battle begins: <team A name> vs <team B name> - For every attack:
<attacker> attacks <target> for <damage> damage. <target> has <hp> HP remaining.or, when the target is eliminated,<attacker> attacks <target> for <damage> damage. <target> is eliminated! - Last:
Battle ends: <winning team name> wins!
Part 1: Basic battle
Team A attacks first and the teams then take turns, one attack each, until one team has no living monster left.
On an attack, the first living monster of the attacking team (in list order) hits the first living monster of the defending team. The target loses attack health. The attacker takes no damage. A monster with 0 or less health is eliminated and never acts or gets hit again. The turn passes to the other team after every attack, including one that eliminates a monster.
Implement battle(team_a, team_b) returning the log.
- Example 1
- team_a
{"name":"Heroes","monsters":[{"name":"Dragon","health":100,"attack":25},{"name":"Griffin","health":80,"attack":20}]}- team_b
{"name":"Monsters","monsters":[{"name":"Goblin","health":30,"attack":10},{"name":"Orc","health":50,"attack":15},{"name":"Troll","health":70,"attack":12}]}- Output
["Battle begins: Heroes vs Monsters","Dragon attacks Goblin for 25 damage. Goblin has 5 HP remaining.","Goblin attacks Dragon for 10 damage. Dragon has 90 HP remaining.","Dragon attacks Goblin for 25 damage. Goblin is eliminated!","Orc attacks Dragon for 15 damage. Dragon has 75 HP remaining.","Dragon attacks Orc for 25 damage. Orc has 25 HP remaining.","Orc attacks Dragon for 15 damage. Dragon has 60 HP remaining.","Dragon attacks Orc for 25 damage. Orc is eliminated!","Troll attacks Dragon for 12 damage. Dragon has 48 HP remaining.","Dragon attacks Troll for 25 damage. Troll has 45 HP remaining.","Troll attacks Dragon for 12 damage. Dragon has 36 HP remaining.","Dragon attacks Troll for 25 damage. Troll has 20 HP remaining.","Troll attacks Dragon for 12 damage. Dragon has 24 HP remaining.","Dragon attacks Troll for 25 damage. Troll is eliminated!","Battle ends: Heroes wins!"]
Why: Dragon hits Goblin twice: 30 HP minus 25 leaves 5, then the second hit eliminates it. Each team attacks in turn and the first living monster of each side is always the one involved, so the fight follows the list order until Monsters has nobody left.
Constraints
1 <= len(team_a["monsters"]) <= 101 <= len(team_b["monsters"]) <= 101 <= monster["health"] <= 10001 <= monster["attack"] <= 100- Monster names are unique across both teams