Step into the arena — but instead of swinging swords, you’re wielding functions. In this lab you’ll build a program that simulates a duel between a player and a monster, but the real battle is internal: crafting well-structured functions that encapsulate logic, enforce input validation, and manage the flow of combat.
You’ll begin by asking the user for a valid ranged-level (3 ≤ level ≤ 99), then compute maximum damage values for both the player and a level-40 monster. Next you’ll loop through exchange after exchange: the player takes a randomized portion of their max damage, the monster does the same, and you update health accordingly until one side falls. Along the way you’ll implement five well-defined functions: one to read and validate level input, one to compute base damage, one to compute actual damage, one to apply damage to health, and one to run the fight sequence. You’ll combine input handling, mathematical computation, randomization, and looping — all packaged in a clear, modular structure.
Why does this matter? Because real-world software isn’t just a block of code doing things: it’s a collection of focused units (functions, methods) each with a single responsibility. Modularizing code in this way makes it easier to read, test, and maintain. Moreover, simulation of processes (like a fight) teaches you to manage state over time, respond to randomness, and enforce rules in a dynamic system. By completing this lab, you’ll sharpen your ability to design and implement a small but fully-functional system composed of interlocking functions — exactly the kind of structure that underpins larger software projects.
The file referenced in this task can be downloaded here
Create a small program that uses the user defined functions described below to read the range level of a player (3 ≤ level ≤ 99), then simulate a battle between the user and a monster. To simulate the battle the program will:
Calculate the max ranged damage the player (based on the level entered) and monster (based on a monster level of 40) can do.
Take a random percent of the player’s max damage then perform that much damage to the monster’s health.
Take a random percent of the monster’s max damage then perform that much damage to the player’s health.
Continue from 2 until either the player or monster runs out of health.
To do this 5 functions must be written:
int getRangeLvl() - Reads a valid integer between 3 and 99 from the user. Once a valid integer is read it is returned from the function. The user should be prompted with the prompt "Range Level: " and if an invalid or out of range level is entered the error message "invalid level\n" should be output. The function should loop until a valid integer within range is read in.
int baseDamage(int level) - Calculates the base ranged damage a character can do using the formula:
Where level is the parameter passed to the function. If however rangeDamage is 0 then it should be changed to 1. Once the rangeDamage is calculated it should be returned from the function.
int actualDamage(int base) - Calculates the actual damage by multiplying the base passed as a parameter by a random value between 0 and 1. To generate a random value first download the file here which contains a function that generates random numbers between 0 and 1. The file downloaded will be called rng.cpp, place this in the same directory as the main.cpp for this lab. Then use the preprocessor directive #include “rng.cpp” to include the function in the file in main.cpp. The function can then be used by calling rng() which will return a random value between 0 and 1 which can be used to multiply the base by. Note: The values generated by rng() are only pseudo-random as the same values will always be generated as the same values in the same order. The value should then be rounded up. So the complete formula is:
int performDamage(int health, int damage) - Subtracts the damage from the health. If the health becomes negative then it is changed to 0. Once the health is updated it should be returned from the function.
void fight(int playerLvl, int monsterLvl, int playerHealth, int monsterHealth) - Performs the fight sequence between the player and the monster. To do this call baseDamage() once passing it the playerLvl passed as a parameter, and a second time passing it the monsterLvl passed as a parameter. Save each respective base damage (max hit) to a variable, then output the max hits in the form “\nPlayer max hit: P\n” and “Monster max hit: M\n\n” where P is the player’s max hit and M is the monster’s max hit. Then, in a loop that loops while the playerHealth and monsterHealth are both not 0:
Call actualDamage() once passing it the player’s max hit, and a second time passing it the monster’s max hit. Save each respective actual damage to a variable.
Call performDamage() once passing it the monsterHealth and the variable holding the player’s actual damage. Save the returned new health back into the monsterHealth to update it. Then output the damage done in the form “Player causes D damage to the monster's health: OLD_M_HEALTH => NEW_M_HEALTH\n” where D is the damage the player caused to the monster, OLD_M_HEALTH is the health of the monster before the damage was done, and NEW_M_HEALTH is the health of the monster after the damage was done.
Call performDamage() a second time passing it the playerHealth and the variable holding the monster’s actual damage. Save the returned new health back into the playerHealth to update it. Then output the damage done in the form “Monster causes D damage to the player's health: OLD_P_HEALTH => NEW_P_HEALTH\n” where D is the damage the monster caused to the player, OLD_P_HEALTH is the health of the player before the damage was done, and NEW_P_HEALTH is the health of the player after the damage was done.
You will need to:
Write the functions described above.
In main, create variables for the player and monster’s healths and set them initially to 100.
In main, call getRangeLvl() and save the range level returned into a variable.
In main, call fight() passing it the player’s entered level, a level of 40 for the monster, and both of the variables for the player and monster’s health.
You may only use the following libraries in your program:
iostream
cmath
rng.cpp
An example of an interaction with your program is shown below. When interacted with, your program must produce the exact same results.
lab 8 % ./a.out Range Level: 2 invalid level Range Level: 100 invalid level Range Level: error invalid level Range Level: 99 Player max hit: 20 Monster max hit: 8 Player causes 1 damage to the monster's health: 100 => 99 Monster causes 6 damage to the player's health: 100 => 94 Player causes 7 damage to the monster's health: 99 => 92 Monster causes 6 damage to the player's health: 94 => 88 Player causes 3 damage to the monster's health: 92 => 89 Monster causes 5 damage to the player's health: 88 => 83 Player causes 10 damage to the monster's health: 89 => 79 Monster causes 5 damage to the player's health: 83 => 78 Player causes 8 damage to the monster's health: 79 => 71 Monster causes 3 damage to the player's health: 78 => 75 Player causes 8 damage to the monster's health: 71 => 63 Monster causes 7 damage to the player's health: 75 => 68 Player causes 4 damage to the monster's health: 63 => 59 Monster causes 3 damage to the player's health: 68 => 65 Player causes 13 damage to the monster's health: 59 => 46 Monster causes 7 damage to the player's health: 65 => 58 Player causes 20 damage to the monster's health: 46 => 26 Monster causes 7 damage to the player's health: 58 => 51 Player causes 10 damage to the monster's health: 26 => 16 Monster causes 5 damage to the player's health: 51 => 46 Player causes 13 damage to the monster's health: 16 => 3 Monster causes 2 damage to the player's health: 46 => 44 Player causes 15 damage to the monster's health: 3 => 0 Monster causes 6 damage to the player's health: 44 => 38 The monster died! :) lab 8 %
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.
Save: Save your code as main.cpp. Do not ignore this step or save your file(s) with different names.
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.