C++ Lab: The Final Fight

Mon Nov 10 2025
Facebook share linkTwitter/X share linkLinkedIn share linkReddit share linkReddit share link

Introduction


Task

The file referenced in this task can be downloaded here

Create a small program that creates a data type called Character then uses that type to create a player and an array of monsters. The user should then be able to choose a monster from the array to fight, then fight the monster to the death. You are not required to write functions to do this, but some functions may be useful to write to make the code simpler (that you must come up with all on your own).

The Character data type will have 4 fields:

  1. name which is a string.

  2. hp which is an int.

  3. attack which is an int.

  4. defense which is an int.

Once defined a player can be defined of Character type with the following data making up the player:

  1. name which is Player 1.

  2. hp which is 100.

  3. attack which is 45.

  4. defense which is 15.

An array of monsters (of size 4 always for this lab) can also be defined of Character type to hold monsters read from monsters.txt.

And once all the data is stored for the player and read in from the file for the monsters, then the user can be asked for a monster to fight. When a valid monster is selected the monster and player can then fight by attacking one another using the formula:

damage=attackdefense;damage = attack - defense;

Using the player’s attack and monster’s defense when the player is attacking the monster, and the monster’s attack and player’s defense when the monster is attacking the player. This continues until either the player or monster dies.


Steps to Complete the Lab

All input files needed for this lab can be downloaded here.

You will need to:

  1. Create the Character data type.

  2. Create a player using the data above.

  3. Read the monsters from monsters.txt into an array of 4 Characters.

  4. Display the monsters (see example interaction for formatting).

  5. Get the selection the user wants of what monster to fight (must be a valid array index entered).

  6. Have the player and monster fight. Until either the player or monster die (have no health): display both the player and monster's healths and cause damage to the player’s health and monster’s health using the formula above. Once either the player or monster dies output Oh no, NAME died.\n if the player dies or NAME died.\n if the monster dies replacing NAME with the name of who died.


Libraries Permitted

You may only use the following libraries in your program:

  1. iostream

  2. fstream


Criteria for Success

See CodeGrade

Example Program Interaction

An example of an interaction with your program is shown below. When interacted with, your program must produce the exact same results.

Monsters: 0 - Goblin: Hitpoints: 5 Attack: 1 Defense: 1 1 - Guard: Hitpoints: 35 Attack: 18 Defense: 26 2 - Giant: Hitpoints: 50 Attack: 30 Defense: 31 3 - Scurrius: Hitpoints: 100 Attack: 60 Defense: 30 Choose a monster: -1 Invalid Monster Choose a monster: 4 Invalid Monster Choose a monster: error Invalid Monster Choose a monster: 0 Player 1 HP: 100 Goblin HP: 5 Player 1 causes 44 damage to Goblin Goblin causes 1 damage to Player 1 Goblin died.

See CodeGrade for more examples.

Note: The line breaks after the inputs in the example output formatting above are the ones placed into the terminal by the user hitting enter on their keyboard to input. CodeGrade does not enter values with a keyboard, but rather supplies input via Linux redirection to programs. So in CodeGrade's auto tests there will not be line breaks after the inputs.


Submission Instructions

  1. Save: Save your code as main.cpp. Do not ignore this step or save your file(s) with different names.

  2. Submit: Your program source code must be submitted via CodeGrade as a properly named .cpp file prior to the deadline to receive full credit. Any submissions after the deadline will be subject to the class’ late policy.