Is this a good level up system (RPG)?

I was thinking about how to make a level up system for a text based rpg when I came up with this solution:
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

#include <iostream>

using namespace std;

int myexp(0), mylvl(1)

void LevelUp(){}

int main()
{
//main code
}

void LevelUp()

{
if(myexp >= 0 && myexp < 45)
{
mylvl = 1;
} 
if(myexp >= 45 && myexp < 95)
{
mylvl = 2;
} 
if(myexp >= 95 && myexp < 145)
{
mylvl = 3 ;
} 
if(myexp >= 145 && myexp < 210)
{
mylvl = 4;
} 
if(myexp >= 210 && myexp < 285)
{
mylvl = 5;
} 
if(myexp >= 285 && myexp < 380)
{
mylvl = 6;
} 
if(myexp >= 380 && myexp < 495)
{
mylvl = 7;
} 
if(myexp >= 495 && myexp < 610)
{
mylvl = 8;
} 
if(myexp >= 610 && myexp < 745)
{
mylvl = 9;
} 
}


The program runs with my code, but i was wondering if this is the way somebody should code it. This is just a way to determine what level the player is after each battle and the exp gained is added up.
The only problem with this code is that it is long and inefficient. There should be some sort of algorithm to increase the amount per level without having to type out each separately, you just have to figure it out.
Hardcoding things (ie: embedding data in your actual code) is typically a bad way to go. Beginners tend to do it because it's the most intuitive at first.

But really, for something like this, I'd use a lookup table:

1
2
3
4
5
6
7
8
9
10
void LevelUp()
{
  static const int required_experience[] =
  {
    45, 95, 145, 210, 285, 380, 495, 610, 745, 99999999
  };

  while(myexp >= required_experience[mylvl])
    ++mylvl;
}



If you want to go a step further and completely remove all the hardcoding... you can store the required experience level in a file and load it at runtime.


EDIT: also note I assume 'mylvl' is zero based because I tend to make all things zero based unless I have a very good reason not to. In your case, I would make mylvl zero based and simply add 1 to it when you print it to the user (so level 1 would really be mylvl=0).

Zero based variables are easier to use with arrays and more consistent with how the language works in general.


EDIT 2: or maybe 1-based makes more sense here afterall, depending on how else mylvl is used in calculations. It's your call.

To make my above code 1-based, you can just add a 0 entry to the start of that lookup table:

1
2
3
4
5
  static const int required_experience[] =
  {
    0, 45, 95, 145, 210, 285, 380, 495, 610, 745, 99999999
//  ^
  };



EDIT 3:

Also, you really want to avoid having duplicate/redundant code. It makes things very difficult when you want to change stuff.

For example:

1
2
3
4
5
6
7
8
if(myexp >= 495 && myexp < 610)  // <- checking against 610 here
{
  mylvl = 8;
} 
if(myexp >= 610 && myexp < 745) // <- and also here
{
  mylvl = 9;
} 


The reason this is such a big problem is because it makes it easy to screw up and have bugs. Say you decide you want to change the level requirement to 650 experience instead... and you change that first 610 but forget about the second one. Now your program will act screwy and you'll have to spend time and effort figuring out why.

If you find yourself doing this, it means you are doing something wrong. So stop and rethink how you want to approach the problem. Raw data should exist once and only once. The only time you'd want to duplicate it is if you are trying to do some kind of anti-hacker protective measure.
Last edited on
Ok, i think i get it now:

1
2
3
4
5
6
7
8
9
10
11
void LevelUp()
{
  static const int required_experience[] =
  {
    0, 45, 95, 145, 210, 285, 380, 495, 610, 745, 99999999
  };
  //so whenever myexp reaches on of these integers, mylvl will go up by 1

  while(myexp >= required_experience[mylvl])
    ++mylvl;
}

Thanks :)
Topic archived. No new replies allowed.