c++ 代写 COMP 3023 Software Development with C++

• Production Cost • HP
• Attack (Modifier)

Soldier Production Cost: 20

HP: 20 Attack: 1

Archer Chariot

School of Information Technology and Mathematical Sciences

COMP 3023 Software Development with C++ Practical Week 9
Containers, Polymorphism, and Templates

This week’s practical exercise is assessed – please attend your practical class and show your supervisor. Have your solution ready before class. External students submit via learnonline.

Your task is to write a simple turn-based battle simulation; one regiment against another. You will be provided with some skeleton source code that you will need to complete.

A regiment consists of a variable number of units; up to a total cost of the available production points. Each unit has the following aspects:

Production Cost: 20
HP: 20
Attack: 2
Note: archer attacks every other turn

Production Cost: 60 HP: 30
Attack: 3

Create classes to represent the given unit types (Soldier, Archer, Chariot); your unit types must inherit from the given Unit class. A unit is considered dead when their HP reaches zero (or less).

Follow the TODOs in the given source code to implement the battle.

Each regiment takes turns attacking the other. In the attack function, the attacking regiment damages the defending regiment. You must use polymorphism and the template function of AttackStrategy to calculate the attack damage for each (alive) unit for the attacking regiment. The damage for each attacker must be applied to each alive unit in the defending regiment: the first attacker attacks the first defender, the second attacker attacks the second defender and, so on. If there are fewer defenders alive than attackers, the remaining attackers attack the first defender, second defender, and so on until either all attackers have attacked or the defenders are all dead. Defending units facing an archer get a break (no damage) every second turn as archers must reload.

The calculation for attack damage for each unit is: Attack * random number – where the random number is between 2 and 12. Note: the random number generation has been provided in the code.

You must specialise the template function at least once to achieve the desired attack damage calculations. When implementing the output operator, you must use appropriate manipulators.

See next page for example output and marking criteria.

C++ Practical Week 9 Page 1 of 2

Example Output

The battle begins!
Attacking Regiment:
Soldiers:  10 | Archers:   5 | Chariots:   8
Total Remaining HP: 460
Defending Regiment:
Soldiers:  11 | Archers:  11 | Chariots:   9
Total Remaining HP: 620

*** End of round 1 ***

Attacking Regiment:
Soldiers:   5 | Archers:
Total Remaining HP: 186
Defending Regiment:
Soldiers:   8 | Archers:
Total Remaining HP: 362

*** End of round 2 ***

Attacking Regiment:
Soldiers:   2 | Archers:
Total Remaining HP: 92
Defending Regiment:
Soldiers:   6 | Archers:
Total Remaining HP: 230

*** End of round 3 ***

Attacking Regiment:
Soldiers:   0 | Archers:
Total Remaining HP: 0
Defending Regiment:
Soldiers:   4 | Archers:
Total Remaining HP: 161
5 | Chariots:   7
7 | Chariots:   8
2 | Chariots:   6
7 | Chariots:   6
0 | Chariots:   0
5 | Chariots:   3

********** The battle has ended. **********
The defenders successfully defended their position. Press <RETURN> to close this window…

Marking Criteria

Marking Criterion Mark

Matching output

2

AttackStrategy class implementation

7

Unit Types Class Hierarchy

21

– Unit (abstract) class implementation

7

– Soldier class implementation

4

– Archer class implementation

6

– Chariot class implementation

4

Regiment class implementation

20

– Choice of container class & isDefeated()

3

– generateRandom() implementation

5

– attack() implementation

6

– Stream output operator<<

6

Style

-10 (-2 per violation)

Total

50

C++ Practical Week 9

Page 2 of 2