Evaluate my console RPG! :)

Hi all, i just wrote this RPG game test, it took me about 30 min or more :)

I also have a little question - i have it all running in loops and if/else statements as u can see below, but i assume u can get much cleaner and better code by using functions, right? reason that i dont use functions btw is that the subject is the next chapter in my book, just read about loops and thought i would wanna try and make something with them to make sure i understand it :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>

using namespace std;

int main()

{
    int strenght, spellPower, health, playerLevel, choose, playerXp;
    int evilStrenght, evilSpellPower, evilHealth, evilLevel;
    playerLevel = 1;
    playerXp = 0;
    cout << "choose a class" << endl;
    cout << "1. Warrior" << endl;
    cout << "2. Mage" << endl;
    int playerClass;
    cin >> playerClass;
    if (playerClass == 1)
    {
        strenght = 10;
        spellPower = 2;
        health = 125;
        cout << "you choose a Warrior! here is your attributes!" << endl;
        cout << "Strenght = " << strenght << endl << "Spell Power = " << spellPower << endl << "Health = " << health << endl;
    }
    else if (playerClass == 2)
    {
        strenght = 3;
        spellPower = 15;
        health = 90;
        cout << "you choose a Mage! here is your attributes!" << endl;
        cout << "Strenght = " << strenght << endl << "Spell Power = " << spellPower << endl << "Health = " << health << endl;
    }

    bool gameOver = false;
    while (!gameOver)
    {
        if (playerLevel < 5)
        if (health == 0)
        gameOver = true;
        cout << "Welcome to the starting zone! What do you wish to do?" << endl << "\n 1. Kill monsters!" << "\n 2. Shop" << endl;
        cin >> choose;
        if (choose == 1)
        {
            cout << "1. Fight lvl 1 goblin" << endl;
            cin >> choose;
            if (choose == 1)
            {
                cout << string( 100, '\n' );
                cout << "fighting lvl 1 goblin!! Prepare for combat!" << endl;
                evilHealth = 40;
                evilStrenght = 4;
                evilSpellPower = 0;


                while (health > 0)
                {
                if (evilHealth < 0)
                {
                    playerXp += 10;
                    cout << "You killed the goblin! you are awarded 10 experience points! you now have " << playerXp << " experience points!" << endl;
                    cout << "write any integer to continue" << endl;
                    cin >> choose;
                    cout << string( 100, '\n' );

                    break;
                }
                health -= evilStrenght * 2;
                cout << "the goblin strikes and deals " << evilStrenght * 2 << " damage to you!" << "\nYou have " << health << " health left!" << endl;
                cout << "Choose an action!" << "\n 1. Attack" << "\ 2. Cast fireball" << endl;
                cin >> choose;
                if (choose == 1)
                {
                    evilHealth -= strenght * 2;
                    cout << "you swing your weapon and deal " << strenght * 2 << " damage to the goblin!" << endl;
                    cout << "the goblin has " << evilHealth << " health left!" << endl;
                }
                else if (choose == 2)
                {
                    evilHealth -= spellPower * 2;
                    cout << "you cast a fiery fireball which deals " << spellPower * 2 << " damage to the goblin!" << endl;
                    cout << "the goblin has " << evilHealth << " health left!" << endl;
                }
                }

            }
        }
    }


    return 0;
}
strenght?

EDIT: I'd learn functions and OOP before making an RPG, that way it can get past the singular menu-style test you have. I promise you'll be pleasantly surprised with functions and classes.

EDIT EDIT: Come to think of it, since this is just a friendly test to try out your programming so far, then I'm gonna congratulate you: this is a great program from someone who's still learning the bare basics. When I was learning the basics I did a lot of stuff like this and the practice is infinitely helpful. Good luck with functions and classes!
Last edited on
Thank you mate, i am reading about functions as we speak :) i just thought that posting some of my test programs after each finished chapter in my book could prove helpfull for some feedback and stuff :)

Your points are highly appriciated :) have a good day
Good job, it looks like you figured out how to use loops and if-statements and have the primary basics of C++ figured out.

Look at your code; int strenght, spellPower, health, playerLevel, choose, playerXp; int evilStrenght, evilSpellPower, evilHealth, evilLevel; playerLevel = 1; playerXp = 0; int playerClass;

The above code snippet contains variables all linked to one thing; the Player. Thus, it would make sense to somehow link this together, right? You can make objects in C++ by using 'classes'. In your case, making a 'Player' class would make sense.

Making a class goes like this in C++:
1
2
class Player {
};


As you can see you start with the keyword 'class' followed by the name of the class ('Player'). Then the body of the class between '{' and '}', and finish with a ';'

You can add your variables to the class:

filename: Player.h
1
2
3
4
5
6
7
8
9
10
11
12
class Player {
public:

private:

int goodStrenght, evilStrenght;
int goodSpellPower, evilSpellPower;
int goodHealth, evilHealth;
int goodLevel, evilLevel;
int xp;
int class;
};


The class variables (also called data members or fields) are under 'private', which means that you cannot directly access them from the outside. In more simple terms, only a Player can change these using public functions (which do not exist yet, as you can see, there is nothing below 'public:').

Let's say you want to create a Player object called 'seerex'. The following code will do this:
Player seerex;

This variable 'Player seerex' already has goodSpellPower, evilSpellPower, goodHealth, etcetera (all set to '0'). To change any of the values you need to add methods (or member functions) to the Player class. Let's think of a few:

filename: Player.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Player {
public:
int addXP(int add);
int addGHealth(int damage);
int addBHealth(int damage);

private:

int goodStrenght, evilStrenght;
int goodSpellPower, evilSpellPower;
int goodHealth, evilHealth;
int goodLevel, evilLevel;
int xp;
int class;
};


I have added 3 member functions to the class: 'int addXP(int add);' , 'int addGHealth(int hp);', and 'int addBHealth(int hp);'.

A function is divided in 3 parts: a return type, a name, and parameters. For example 'int addXP(int add);' has return type 'int', name 'addXP', and one parameter 'int add'.

To implement the Player class we use a file 'Player.cpp' and include the class we made in the 'Player.h' file:

File: Player.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "Player.h"
int Player::addXP(int add) {
 xp = xp + add;
 return xp;
}

int Player::addGHealth(int hp) {
 goodHealth =+ hp;
 return goodHealth;

int Player::addBHealth(int hp) {
 return (badHealth=+ hp);
}

}


This code uses the class definition in 'Player.h'. Here you implement the functions you described in the header file; In the header file you say 'Look, I have a member function called 'addXP' that uses an int parameter 'add' and a return type of 'int'. In the CPP file 'Player.cpp' you actually implement the function; you program what should happen when calling that function. In member function you can use the class variables from the Player class; you can change xp, goodHealth, badHealth, etcetera, because the member functions are part of the Player class and can thus access the 'private' parts of the class.

The above code shows three member function implementations.

To wrap it up:

filename: Player.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Player {
public:
int addXP(int add);
int addGHealth(int damage);
int addBHealth(int damage);

private:

int goodStrenght, evilStrenght;
int goodSpellPower, evilSpellPower;
int goodHealth, evilHealth;
int goodLevel, evilLevel;
int xp;
int class;
};


File: Player.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "Player.h"
int Player::addXP(int add) {
 xp = xp + add;
 return xp;
}

int Player::addGHealth(int hp) {
 goodHealth =+ hp;
 return goodHealth;

int Player::addBHealth(int hp) {
 return (badHealth=+ hp);
}

}


File: game.cpp
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "Player.h"
using namespace std;

int main() {
 Player seerex;
 cout << "Seerex new XP: " << seerex.addXP(2) << "\n";
 int gHP = seerex.addGHealth(40);
 cout << "Seerex new Good Health: << gHP;
 seerex.addBHealth(50);
} 


In this line "cout << "Seerex new XP: " << seerex.addXP(2); you can see the 'return type' of a function at work. In the Player.cpp file you see that the function returns the current value of xp: return xp;. You can print it, save it to a variable (as seen in int gHP = seerex.addGHealth(40);), or do nothing with it (seerex.addBHealth(50);).

I hope this helps a bit :)
Last edited on
Your variables are well-named, which makes it easy to follow your program.
Consistent indentation (such as the block after while (health > 0)) would help readability further, though.
Last edited on
your program made me chuckle, it's good for us fellow beginners...
good luck improving.
Acr, did you read seerex's whole post? He said he hadn't learnt functions yet; why would you try to teach him classes?

Also, if you're just storing data you use a struct, not a class, in general.
Last edited on
thank u for ur post Acr, i will keep it so i can read it for future reference, but as Veltas states, i'm not at the point in book where i learn about these things, but eventually i will be. i'm reading the book slow in order to take lots and lots of notes and test some of the things i learn.

Thanks for taking ur time to write such a post though Acr, and i will indeed get to read it :)
Actually, Acr's post will be really useful when you do reach the chapter discussing classes. I think it's a good practice to go back to code you have written and improve it using new techniques.
Personally, I've found C++ classes to make my code easier to read, understand, and maintain. In fact, I would state that it perhaps makes learning to code in C++ easier because getting lost in 'the code' is less likely; Once you understand how classes work and how they are built up it's easier to find things because their location makes sense. As an example, the Player class of my code for Seerex holds all the code regarding a player and (eventually) all code dealing with the player's variables (adjusting, showing, etc). Perhaps the learning curve is a bit too steep for someone not knowing functions yet, but for me personally OOP simplified a lot of things.

And no, not all of us apply your rule of 'if you're just storing data you use a struct, not a class'..
Last edited on
Topic archived. No new replies allowed.