0

I've been working REALLY hard on this code and there are a few bugs but I will post the others in different questions because I hear this is a really professional website and I hate when I make mistakes. Anyway, I've been working on this for a while but I want to make it so that when the player gains a certain amount of XP, to have them level up. I do not have the level battle's code yet, but how would I make it so that the player will automatically level up upon hitting a specific number of XP?

Here is my code:

/* Skyy Civil
C++ Legend Game
*/
#include <iostream>
#include <cstring>
#include <fstream>
using namespace std;
/* No files, struct instead.

constructing monsters and hero and damage
*/
struct Monster{
    int monsterCount;
    string monsterType;
    int monsterHealth;
    
};
// Hero
struct Hero{
    
    int XP;
    int coins;
    int level;
    int health;

};


struct Attacks{
    string attack1;
    string attack2;
    string attack3;
    string attack4;
    int damageAttack1;
    int damageAttack2;
    int damageAttack3;
    int damageAttack4;
};

/*
        The actual level starts here
*/


void LevelStart(){
    cout << "WORK IN PROGRESS";
}


/*
Game setup is where the program checks the level and sets it up for the player to begin the game
*/

void levelSetup(){
    struct Hero player;
    struct Attacks playerAttacks;
    struct Attacks monsterAttacks;
    struct Monster easyLevelMonster;
    // Player attacks and stats
    
    player.health = 150;
    playerAttacks.attack1 = "FireBall";
    playerAttacks.attack2 = "Fire Slash";
    playerAttacks.attack3 = "Fire Barrage";
    playerAttacks.attack4 = "Blue Flame Bomb";
    playerAttacks.damageAttack1 = 4;
    playerAttacks.damageAttack2 = 9;
    playerAttacks.damageAttack3 = 14;
    playerAttacks.damageAttack4 = 19;
    // Monster attacks and stats
    easyLevelMonster.monsterCount = 1;
    easyLevelMonster.monsterType = "monster";
    easyLevelMonster.monsterHealth = 100;
    monsterAttacks.attack1 = "bash";
    monsterAttacks.damageAttack1 = 10;
    if(player.health == 150 && easyLevelMonster.monsterCount == 1){
        LevelStart();
    }
}

/*
Game menu includes options for the player to choose to play the game
*/

void GameMenu(string username, string password){
    struct Attacks sword;
    struct Hero player;
    string gameMenuChoice;
    player.level = 1;
    player.XP = 0;
    
    cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
    cout << "~~~~~~~+~~~~~~~LEGEND!~~~~~~~+~~~~~~~\n\n\n\n";
    cout << username << "\n\n";
    cout << "Level: " << player.level << "\n\n\n\n\n\n";
    cout << "XP: " << player.XP << "\n\n\n\n";
    cout << "(a) Play Level\n";
    cout << "(b) Exit\n";
    cin >> gameMenuChoice;
    if(gameMenuChoice == "a"){
        levelSetup();
    }
    if(gameMenuChoice == "b"){
        cout << "Game EXITED\n";
    
    }
}


void Login(){
    int loginChoice;
    string username;
    string password;
    fstream login;
    login.open("login.dat", ios::in | ios::out);
    cout << "Username\n";
    cout << "\n > ";
    cin >> username;
    login << "Username: " << username << endl;
    cout << "\n\n\n Password\n";
    cout << "\n > ";
    cin >> password;
    login << "Pass: " << password << endl;
    login.close();
    cout << "loading...\n\n\n";
    cout << "(1) Start\n";
    cin >> loginChoice;
    if(loginChoice == 1){
        GameMenu(username, password);
    }
}

// StoryMode

void StoryMode(void){
    int storyModeChoice;
    cout << "Welcome to story mode!\n\n";
    cout << "(1) Login\n";
    cin >> storyModeChoice;
    if(storyModeChoice == 1){
        Login();
    }
}

// Credits
void Credits(){
    
    int creditsChoice;
    cout << "Creator: Skyy Civil\n\n\n\n";
    cout << "(1) Back to menu\n";
    cout << "(2)Continue to story mode\n";
    cin >> creditsChoice;
    if(creditsChoice == 1){
        void Menu();
    }
    if (creditsChoice == 2){
        StoryMode();
    }
}

// Main Menu

void Menu(){
    int menuChoice;
    cout << "\nLegend\n";
    cout << "(1)Story Mode\n";
    cout << "(2) Credits\n";
    cin >> menuChoice;
    if(menuChoice == 1){
        StoryMode();
    }
    if(menuChoice == 2){
        Credits();
    }
};

// ------------MAIN--------------- \\

int main(void){
    Menu();
}

Sorry if my code is really messy, I am an intermediate C++ programmer and my skills are just really sad.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Skyy Civil
  • 63
  • 8
  • 2
    Suggestion: Start with a really small program that just exercises the XP tracking and leveling up. Everything else will just be noise helping hide potential mistakes in the levelling up system. Consider making `Hero` more `class`-like. If you have a method something like `void AddXP(int newXP)`... And GaryLOL just posted the rest of my comment as an answer. Have an upvote, Gary. – user4581301 Dec 14 '20 at 18:37

1 Answers1

4

You should probably create a method in your hero struct to make it gain XP and check wether it has enough XP or not to level up.

// Hero
struct Hero{

    int XP;
    int coins;
    int level;
    int health;
    void gainXP(int);
};

void Hero::gainXP(int xp)
{
    this->XP += xp;
    if(this->XP >= 100) //Change 100 to the amount of XP it needs to level up.
    {
        this->XP -= 100;
        this->level++;
    }
}

Then just call the method when it gains XP.

Additional information:

  1. using namespace std; is considered a bad practice. (More info here)
  2. You're not using the <cstring> header.
  3. In the login function you are not reading in any moment from the std::fstream opened with std::ios::in so you should probably do it an std::ofstream and just open it with std::ios::out.
  4. In the levelSetup function there is no need to check player.health == 150 && easyLevelMonster.monsterCount == 1.
Gary Strivin'
  • 908
  • 7
  • 20
  • If you make an array of the XP required to reach each level you could generalize to something like `if (this->XP >= levelxp[this->level])` and get a more D&D-sh (note that I'm still playing second edition when the group can get together (and that's been rarely these days ( COVID))) experience requiring different amounts for each level. – user4581301 Dec 14 '20 at 20:19