Skip to content

Latest commit

 

History

History
executable file
·
91 lines (75 loc) · 3.31 KB

7ec8.md

File metadata and controls

executable file
·
91 lines (75 loc) · 3.31 KB

Back to questions

7ec8: Battling fighters

In this question you will simulate a battle between fighters using rules based on the Fighting Fantasy adventure game books.

A fighter has a name (e.g. "Joe"), a type (e.g. "Elf Lord"), and two further attributes: skill, an integer initialised to a value between 1 and 18 that remains fixed, and stamina, an integer initialised to a value between 1 and 24.

A battle between two fighters consists of a series of rounds. In a round, each fighter computes an attack score. This is obtained by rolling two dice, and adding the result to the fighter's skill. If both fighters compute the same attack score, the round results in a draw. Otherwise the fighter with the higher attack score hits the fighter with the lower attack score, whose stamina is reduced by two (or set to zero if it is currently set to one). The battle ends when one of the fighters' stamina becomes zero.

Your task is to design two classes: Fighter and GameEngine.

The Fighter class should contain fields to represent the attributes of a fighter, an appropriate constructor, as well as (at least) the following methods:

// Reduce the fighter's stamina accordingly
public void takeDamage(int damage);

// Return the number of damage points to be inflicted on opponent
public int calculateDamage();

// Calculate an attack score for the fighter using the procedure described above
public int calculateAttackScore();

// Determine whether fighter is still alive
public boolean isDead();

With the exception of calculateAttackScore, these methods should be trivial to implement.

The GameEngine should contain (at least) the following method:

// Simulate battle between two fighters, displaying how the battle
// progresses and who wins
public void simulateBattle(Fighter first, Fighter second) {

Finally, you should provide a Main class with a main method that creates two fighters and a game engine, and launches the simulated battle.

The output of your program should look something like this:

At start of battle, stats are:
Joe - Human Warrior - skill: 16; stamina: 12
Alex - Elf Lord - skill: 18; stamina: 6
------------------------------
Alex hits Joe, stats are:
Joe - Human Warrior - skill: 16; stamina: 10
Alex - Elf Lord - skill: 18; stamina: 6
------------------------------
Joe hits Alex, stats are:
Joe - Human Warrior - skill: 16; stamina: 10
Alex - Elf Lord - skill: 18; stamina: 4
------------------------------
Alex hits Joe, stats are:
Joe - Human Warrior - skill: 16; stamina: 8
Alex - Elf Lord - skill: 18; stamina: 4
------------------------------
Alex hits Joe, stats are:
Joe - Human Warrior - skill: 16; stamina: 6
Alex - Elf Lord - skill: 18; stamina: 4
------------------------------
Joe draws with Alex
------------------------------
Alex hits Joe, stats are:
Joe - Human Warrior - skill: 16; stamina: 4
Alex - Elf Lord - skill: 18; stamina: 4
------------------------------
Alex hits Joe, stats are:
Joe - Human Warrior - skill: 16; stamina: 2
Alex - Elf Lord - skill: 18; stamina: 4
------------------------------
Alex hits Joe, stats are:
Joe - Human Warrior - skill: 16; stamina: 0
Alex - Elf Lord - skill: 18; stamina: 4
------------------------------
End of battle, Alex - Elf Lord - skill: 18; stamina: 4 wins!