Intro to Java week 3 / project

BATTLE SIM

Two fighters go in. The program says who comes out.

Fighter 1Warrior 130.0
Fighter 2Mage 128.2

Fighter 1 wins!

Battle Sim

Ask about two fighters, score each one, print the winner.

Download week 3 BattleSim.java is in the zip, already set up.
BattleSim.java

The problem

Each fighter gets a combat score and a gear score, each out of 100. Add them for a total out of 200. Higher total wins.

Combat score

Ask whether the fighter is a Warrior or a Mage, then ask for that class's three stats, each 1 to 100.

ClassAsks forFormula
1) Warriorstrength, armor, stamina(2 * strength + armor + stamina) / 4.0
2) Mageintellect, mana, focus(intellect + 2 * mana + focus) / 4.0

Gear score

Ask for the fighter's level and the game's max level (both whole numbers), and a gear multiplier between 0.5 and 1.0 (a decimal).

gear = level / maxLevel * 100 * gearMultiplier

Result

Print exactly one of these:

Fighter 1 wins!
Fighter 2 wins!
It's a draw!

Example

Typed input is highlighted.

BATTLE SIM
Enter two fighters and I'll tell you who wins.

Fighter 1:
  1) Warrior or 2) Mage? 1
  Strength? 80
  Armor? 60
  Stamina? 55
  combat score = 68.8
  Level? 34
  Max level? 50
  Gear multiplier? 0.9
  gear score = 61.2

Fighter 2:
  1) Warrior or 2) Mage? 2
  Intellect? 70
  Mana? 85
  Focus? 60
  combat score = 75.0
  Level? 28
  Max level? 50
  Gear multiplier? 0.95
  gear score = 53.2

Fighter 1 total = 130.0
Fighter 2 total = 128.2

Fighter 1 wins!

Hints

Every gear score comes out 0.0

level / maxLevel is two whole numbers, so Java does whole-number division. 34 divided by 50 is 0. Same trap as week 2. Make one side a decimal.

Printing one decimal place

Scores will look like 68.75000000001. Use printf with a %.1f placeholder:

System.out.printf("  combat score = %.1f\n", combat);

%.1f gets replaced by the number rounded to one decimal. \n moves to the next line, because printf does not add one automatically.

The totals look like they do not add up

Only the display rounds. Compare the real, unrounded totals when determining the winner. Printed numbers that look one decimal off are correct and expected.

Why 4.0 and not 4

Same reason as the gear score. If everything in the sum is a whole number, dividing by 4 throws away the decimal part. Dividing by 4.0 keeps it.

Input goes wrong after a number

If any text input is added, nextInt() reads the digits and stops, leaving the Enter key still waiting. The next nextLine() grabs that and comes back empty. Add one extra input.nextLine(); to swallow it.

Requirements

Stretch goals

Only once the above works.

  1. Class advantage. Warriors beat Mages in a tie.
  2. Rank the winner. 180 or more is Legendary, 150 is Elite, 100 is Seasoned, below that is Rookie.
  3. A third class. Add a Rogue with its own stats and formula. Design what makes a Rogue strong.
  4. Crit chance. Give the winner a 1-in-4 chance of a critical hit and print an extra line when it lands.