In this lab, you’ll build a small, text-based duel where your choices matter and the program reacts in a predictable rhythm. Each turn, the player chooses between a quick stab or a heavier slash, and the monster responds in a perfect 1-for-1 rotation—sometimes blocking, sometimes launching a fireball. Your program will read the player’s choice, validate it, apply effects, and update health until one side drops to zero.
The purpose is to practice clean selection and input handling inside a simple game loop. By the end, you’ll be comfortable validating choices, branching behavior (player action vs. monster response), tracking state over time (health), and detecting end-conditions to announce a clear winner. These are the same patterns you’ll reuse in larger programs—menu-driven tools, simulations, and command handlers—where reliable input checks and well-structured decisions keep your code correct and easy to debug.
Create a small, text-based combat loop where the player chooses between two attacks, and then the monster responds by either blocking the attack or taking damage and attacking back. The player will have two attacks to choose from:
Attack | Damage |
---|---|
stab | 2 |
slash | 3 |
The program will read in which of the two attacks to perform, then the monster will repond:
The block negating all damage from the player for the turn, and the attack being a fireball which causes 2 damage.
The player and monster will both start with 10 health and keep doing damage to one another until either the player, monster, or both the player and monster have no health.
To keep reading an attack from the player then player/monster attack eachother a loop must be used (called the game loop). In each iteration of the game loop one get attack/player attack/monster block or attack turn will occur. The game loop will continue to run until either one or both the monster/player's health is 0.
In a loop type of your choosing create the following game loop:
Output the player's health followed the monster's health in the format (replacing X
and Y
with the player and monster's health respectively):
Player Health: X Monster Health: Y
Read which attack the player wants to perform. Use the prompt "1-Stab -or- 2-Slash: "
Validate the attack selection is not input failure and that it is a valid selection (1 for stab and 2 for slash). If there is input failure output the error "Error: Invalid selection"
and continue from step (2).
If the monster blocks (which it does the first iteration of the game loop) then output "The monster blocks your attack"
and go on to the next iteration of the game loop.
If the monster does not block output an appropriate message depending on the attack style the player chose, then subtract the damage for the respective attack styles from a variable holding the monster's health (which starts at 10). The messages for each attack style are:
Attack | Message |
---|---|
stab | "You stab the monster causing 2 damage" |
slash | "You slash the monster causing 3 damage" |
If the monster does not block output "The monster shoots a fire ball at you causing 2 damage"
and subtract 2 damage from a variable holding the player's health (which starts at 10).
The game loop should continue until either the player, monster, or both the player and monster run out of health (0 or less health).
After the game loop has completed and the game is over one of three messages should be displayed depending on who won the fight:
Scenario | Message |
---|---|
Monster 0 health | "You killed the monster" |
Player 0 health | "The monster killed you" |
Monster/player 0 health | "The player and monster killed eachother" |
Player Health: 10 Monster Health: 10 1-Stab -or- 2-Slash: 0 Error: Invalid selection 1-Stab -or- 2-Slash: error Error: Invalid selection 1-Stab -or- 2-Slash: 3 Error: Invalid selection 1-Stab -or- 2-Slash: 1 The monster blocks your attack Player Health: 10 Monster Health: 10 1-Stab -or- 2-Slash: 2 You slash the monster causing 3 damage The monster shoots a fire ball at you causing 2 damage Player Health: 8 Monster Health: 7 1-Stab -or- 2-Slash: 2 The monster blocks your attack Player Health: 8 Monster Health: 7 1-Stab -or- 2-Slash: 1 You stab the monster causing 2 damage The monster shoots a fire ball at you causing 2 damage Player Health: 6 Monster Health: 5 1-Stab -or- 2-Slash: 2 The monster blocks your attack Player Health: 6 Monster Health: 5 1-Stab -or- 2-Slash: 2 You slash the monster causing 3 damage The monster shoots a fire ball at you causing 2 damage Player Health: 4 Monster Health: 2 1-Stab -or- 2-Slash: 2 The monster blocks your attack Player Health: 4 Monster Health: 2 1-Stab -or- 2-Slash: 2 You slash the monster causing 3 damage The monster shoots a fire ball at you causing 2 damage You killed the monster
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.