THINK FIRST·CODE LATER

← All labs

Arena: fighters that override behaviour

Problem

Write a superclass Fighter (protected name, health, power; attack(int round) returns power; takeDamage(int damage) never lets health go below 0; toString() → Ana[35]) and two subclasses:

  • Warrior(name, health, power, armor) overrides takeDamage: the armor absorbs part of each hit, but a hit always costs at least 1 (super.takeDamage(Math.max(1, damage - armor)));
  • Mage(name, health, power) overrides attack: every third round the damage is tripled (3 * super.attack(round)).

Arena.main reads two fighters (warrior NAME HEALTH POWER ARMOR, mage NAME HEALTH POWER or fighter NAME HEALTH POWER). Each round the first fighter strikes, then the second one if still alive; print both after every round. Stop when one falls or after 20 rounds (Draw after 20 rounds).

For the input

warrior Ana 40 6 2 mage Bo 30 5

the program prints

Round 1: Ana[37] Bo[24]
Round 2: Ana[34] Bo[18]
Round 3: Ana[21] Bo[12]
Round 4: Ana[18] Bo[6]
Round 5: Ana[18] Bo[0]
Ana wins in round 5

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l14-6-data-1.txt

warrior Ana 40 6 2 mage Bo 30 5
What your program must print for l14-6-data-1.txt
Round 1: Ana[37] Bo[24]
Round 2: Ana[34] Bo[18]
Round 3: Ana[21] Bo[12]
Round 4: Ana[18] Bo[6]
Round 5: Ana[18] Bo[0]
Ana wins in round 5

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l14-6-data-1.txt

Windows PowerShell does not understand <:

Get-Content l14-6-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.