Question
I have this C++ project where the purpose of it is to create a turn based rpg game using a class called character and a
I have this C++ project where the purpose of it is to create a turn based rpg game using a class called character and a header.
Each character will be represented by a Character C++ Class. Each character, both the player and the "computer" will have the following properties:
- Name, so the characters have some way to identify which one is which
- Strength, to represent how hard they can hit
- Health, to represent how much damage they can take before dying
- Defense, to reduce incoming damage
- Speed, to determine which character will attack first
Using the described character classes, each turn the characters take turns in combat. Combat follows the following rules:
- Each turn the characters will deal damage to each other
- The character with higher "speed" will deal damager first
- A character will deal damage based on how strong they are
- Characters can reduce incoming damage based on how tough they are
- damage taken = attacker's strength - defender's defense
- Characters are defeated when their health reaches 0
Main.cpp
#include "character.h"
using namespace std;
int main(){ // Initialize characters Character player("Barbarian", 5, 3, 20, 2); // Player Character Character computer("Dragon", 7, 4, 50, 1); // Computer Character while(player.isAlive() && computer.isAlive()){ // Combat // Who is faster? if(player.getSpeed() > computer.getSpeed()){ // Player attacks first computer.takeDamage(player.getStrength()); // health = health - (attacker's str - def); if(computer.isAlive()) break; player.takeDamage(computer.getStrength()); } else { // Computer attacks first } } // Evaluate winner // Print conclusion return 0; }
character.cpp
#include "character.h"
Character::Character(string n, int st, int d, int h, int sp){ name = n; strength = st; defense = d; health = h; speed = sp; }
bool Character::isAlive() const { return health > 0; }
int Character::getSpeed() const { return speed; }
character.h
#ifndef CHARACTER_H #define CHARACTER_H
#include
using namespace std;
class Character{ private: string name; int strength; int defense; int health; int speed; public: Character(string n, int st, int d, int h, int sp); bool isAlive() const; int getSpeed() const; };
#endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started