Turn-Based Monster Battle Simulation
Problem statement
You are given two ordered monster teams, teamA and teamB. Each monster row is [name, health, attack], where health and attack are positive base-10 integers represented as strings.
Simulate their turn-based battle and return a detailed chronological event log.
Active monsters
The active monster of a team is its first monster whose health is greater than 0. A defeated monster leaves the battle, and the next living monster becomes active at the start of the next round.
Round rules
- Team A's active monster attacks Team B's active monster.
- If Team B's monster survives, it immediately counterattacks Team A's active monster.
- There is one explicit dying-counterattack exception: when both active monsters are the final remaining monsters on their teams, Team B's monster still completes its counterattack even if Team A's strike defeats it. This is the only way both teams can be eliminated in the same round.
An attack subtracts the attacker's attack value from the defender's health. Health is not clamped at zero. A monster is defeated when its health becomes less than or equal to 0.
Event log
Return a String[][] whose rows appear in exact chronological order:
- An attack row is
["ATTACK", attackerTeam, attackerName, defenderTeam, defenderName, damage, healthBefore, healthAfter]. - A defeat row is
["DEFEATED", team, monsterName]and appears immediately after the attack that caused it. - If the battle continues and a defeated active monster has a replacement, append
["ACTIVE", team, replacementName]at the round boundary. When both teams replace monsters, log Team A's replacement first. - The final row is
["RESULT", result], whereresultis exactly"Team A wins","Team B wins", or"Draw".
Use the exact team labels "Team A" and "Team B". The input arrays must not be modified.
Function
simulateMonsterBattle(teamA: String[][], teamB: String[][]) → String[][]Examples
Example 1
teamA = [["Ares","4","4"]]teamB = [["Basilisk","4","4"]]return = [["ATTACK","Team A","Ares","Team B","Basilisk","4","4","0"],["DEFEATED","Team B","Basilisk"],["ATTACK","Team B","Basilisk","Team A","Ares","4","4","0"],["DEFEATED","Team A","Ares"],["RESULT","Draw"]]Both teams begin with their final monster. Ares defeats Basilisk, so the dying-counterattack exception applies. Basilisk also reduces Ares to 0, and the result is a draw.
Example 2
teamA = [["Ares","8","5"]]teamB = [["Basilisk","6","2"]]return = [["ATTACK","Team A","Ares","Team B","Basilisk","5","6","1"],["ATTACK","Team B","Basilisk","Team A","Ares","2","8","6"],["ATTACK","Team A","Ares","Team B","Basilisk","5","1","-4"],["DEFEATED","Team B","Basilisk"],["ATTACK","Team B","Basilisk","Team A","Ares","2","6","4"],["RESULT","Team A wins"]]Basilisk survives the first strike and counterattacks. In the second round it is defeated, but because both monsters are their teams' final members, it makes one dying counterattack. Ares survives with health 4.
Example 3
teamA = [["Ares","4","2"],["Athena","6","3"]]teamB = [["Basilisk","8","5"],["Cyclops","4","1"]]return = [["ATTACK","Team A","Ares","Team B","Basilisk","2","8","6"],["ATTACK","Team B","Basilisk","Team A","Ares","5","4","-1"],["DEFEATED","Team A","Ares"],["ACTIVE","Team A","Athena"],["ATTACK","Team A","Athena","Team B","Basilisk","3","6","3"],["ATTACK","Team B","Basilisk","Team A","Athena","5","6","1"],["ATTACK","Team A","Athena","Team B","Basilisk","3","3","0"],["DEFEATED","Team B","Basilisk"],["ACTIVE","Team B","Cyclops"],["ATTACK","Team A","Athena","Team B","Cyclops","3","4","1"],["ATTACK","Team B","Cyclops","Team A","Athena","1","1","0"],["DEFEATED","Team A","Athena"],["RESULT","Team B wins"]]Ares is defeated in the first round, so Athena becomes active. Later Basilisk is replaced by Cyclops. Cyclops survives Athena's attack and defeats Team A's final monster.
Constraints
1 <= teamA.length, teamB.length <= 1000.- Every monster row contains exactly three strings:
[name, health, attack]. - Every
nameis non-empty and has at most50Unicode characters. healthandattackare canonical positive base-10 integers in the range[1, 10^9].- The sum of all initial health values across both teams is at most
200000.