Statistics

Problem Statement for "Battle"

Problem Statement

You are employed at a game studio, and are responsible for the balance of warrior powers in their latest RPG release. You decide to write a quick and dirty simulator that will take the warrior stats for a mock battle and determine the outcome, so you can get a feel for the numbers.

You will simulate as follows: You will be given two warrior Strings. Each warrior String will first contain a name, followed by a number symbolizing the warriors health, followed by a number symbolizing the warrior's defenses, and finally followed by a number symbolizing the warrior's offensive power, looking like this:

<WarriorName> <Health> <Defense> <Offense>

Each second (as soon as the battle starts), each warrior will strike the other warrior simultaneously, and each will cause the other damage, represented as an integer. This damage is equal to the <Offense> of the attacker minus the <Defense> of the defender, if positive(if negative or zero then there is no damage). The new <Health> of the warrior being attacked will be decremented this damage. A warrior is defeated when the warrior's <Health> is less than or equal to 0.

Example: Let warrior1 be "Rynos 100 20 30" and warrior2 be "Unka-Tarkal 1000 7 50". In the first second, Rynos attacks the Unka-Tarkal doing 30-7 or 23 damage bringing the Unka-Tarkals health to 1000-23 or 977. Simultaneously, the Unka-Tarkal will retaliate doing 50-20 or 30 damage to Rynos, putting him at 70 health. You would repeat this until the 4th second, where Rynos is defeated by the Unka-Tarkal. You would return the warrior defeated followed by the number of seconds it took, like this: Rynos 4. Note that there is a limit to seconds: If a battle takes longer than 1000 seconds, the battle is called off and you will return "NONE 1000" to symbolize this. Also, if the two warriors take each other out simultaneously, return "NONE " followed by the number of seconds it took to reach a draw.

You will write a function firstDefeated that takes warrior1 and warrior2, and returns the result String as detailed above, telling you who was defeated first and how many seconds it took.

Definition

Class:
Battle
Method:
firstDefeated
Parameters:
String, String
Returns:
String
Method signature:
String firstDefeated(String warrior1, String warrior2)
(be sure your method is public)

Notes

  • If a warrior's attack is less than the defender's defense, that defender takes no damage.

Constraints

  • Both warrior1 and warrior2 will be of the form (quotes and angle brackets for clarity only): " "
  • The warriors will not have identical names.
  • No integers in the warrior Strings will contain extra leading zeros.
  • will only consist of lowercase and uppercase letters ('a'-'z','A'-'Z') and will be between 1 and 20 characters in length, inclusive.
  • will be an integer between 1 and 1000000, inclusive.
  • will be an integer between 0 and 1000, inclusive.
  • will be an integer between 0 and 1000, inclusive.

Examples

  1. "Rynos 100 20 30"

    "UnkaTarkal 1000 7 50"

    Returns: "Rynos 4"

    From the example above.

  2. "UnkaTarkalA 1000 7 50"

    "UnkaTarkalB 1000 7 50"

    Returns: "NONE 24"

    Matched against one another, the two UnkaTarkals defeat one another in 24 rounds, with no clear winner.

  3. "Murp 1000000 1000 0"

    "Rynos 100 20 30"

    Returns: "NONE 1000"

    Rynos and the Murp are at a stalemate, and neither will defeat one another in under 1000 turns.

  4. "AntA 1001 0 1"

    "AntB 1000 0 1"

    Returns: "AntB 1000"

    AntB is defeated in the 1000th second, just on the boundary of having the match called for time.

  5. "AntA 1001 0 1"

    "AntB 1002 0 1"

    Returns: "NONE 1000"

    Even though AntB will win in 1001 seconds, the match is called off at 1000 seconds.

  6. "A 501 0 2"

    "B 497 1 3"

    Returns: "A 167"

  7. "Billygoat 100000 100 900"

    "Gimli 1000000 901 1000"

    Returns: "Billygoat 112"

  8. "Wizard 100000 500 500"

    "Cow 1000 0 0"

    Returns: "Cow 2"

  9. "A 1000 0 2"

    "B 999 1 1"

    Returns: "B 999"

  10. "A 1000 0 2"

    "B 1000 1 1"

    Returns: "NONE 1000"

  11. "A 500 0 2"

    "B 500 1 1"

    Returns: "NONE 500"

  12. "A 501 0 3"

    "B 750 0 2"

    Returns: "B 250"

  13. "Gandalf 1000000 1 500"

    "Balrog 500000 0 1000"

    Returns: "Balrog 1000"

  14. "Beholder 70 1 1000"

    "Dihtillius 100000 100 3"

    Returns: "Beholder 35"

  15. "WillowispA 1 1000 13"

    "WillowispB 1 1000 13"

    Returns: "NONE 1000"

  16. "A 91 7 13"

    "B 91 13 7"

    Returns: "NONE 1000"

  17. "A 178 0 2"

    "B 88 0 4"

    Returns: "B 44"

  18. "A 100 100 100"

    "B 100 200 200"

    Returns: "A 1"

  19. "A 910 13 107"

    "B 910 12 107"

    Returns: "NONE 10"

  20. "Poker 259896 4 1000"

    "Issoc 100000 15 1000"

    Returns: "Issoc 102"

  21. "foo 10000 0 0"

    "fb 10000 0 0"

    Returns: "NONE 1000"

  22. "daa 1000 1 1"

    "dad 1000 1 1"

    Returns: "NONE 1000"

  23. "A 99 0 1"

    "B 97 0 1"

    Returns: "B 97"

  24. "MrPogo 50 40 10"

    "Boobies 50 40 10"

    Returns: "NONE 1000"

  25. "W 100000 500 500"

    "C 1000 0 0"

    Returns: "C 2"

  26. "a 1000 7 50"

    "b 1000 7 50"

    Returns: "NONE 24"

  27. "U 1000 7 50"

    "UA 1000 7 50"

    Returns: "NONE 24"

  28. "AntA 1001 0 1"

    "AntB 1000 0 1"

    Returns: "AntB 1000"

  29. "Rynos 100 20 30"

    "UnkaTarkal 1000 7 50"

    Returns: "Rynos 4"

  30. "Wizard 12343 345 453"

    "Wizarad 45623 131 233"

    Returns: "Wizarad 142"

  31. "A 1000 7 50"

    "B 1000 7 50"

    Returns: "NONE 24"

  32. "manA 10 2 10"

    "manB 10 2 10"

    Returns: "NONE 2"

  33. "a 11 0 1"

    "b 9 0 1"

    Returns: "b 9"

  34. "a 10 2 10"

    "b 10 2 10"

    Returns: "NONE 2"


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: