Monster Battle

Asked at OpenAIPhone screen

Reported by the problem's creator, not verified

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!

Examples

  1. Example 1

    Input: {"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!"]

    Explanation: 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.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.