Question

I am programming a console "game" and I need to declare a "hp" of character in IF statements which depends on level of this character.

if ((char_level > 0) && (char_level < 4))
        {
            char_hp = 100;
        }
if ((level > 4) && (level < 6))
        {
            char_hp = 120;
        }
if ((level > 6) && (level < 8))
        {
            char_hp = 150;
        }
if ((level > 8) && (level < 10))
        {
            char_hp = 180;
        }

Then I need to use it later in code in a fight. After a successful fight character gets a new level and after that, program will get back to check these IF statements and if level is bigger than 4, character's hp will be increased to 120. But declaration of char_hp in IF statements does not change the value of hp in general and when the next fight comes after reaching level 4, character's hp is still like at the end of previous battle was. I am new in C# programming and I have tried everything but I can't solve it, if it is possible.

The same problem is with the "hp" of enemy that is randomly generated...then I need to use it in that fight

if((level>0) && (level<4))
        {
            random_enemy_hp = RND.Next(89, 111);
            goto enemy;
        }
if((level>4) && (level<6))
        {
            random_enemy_hp = RND.Next(109, 141);
            goto enemy;
        }
if ((level > 6) && (level < 8))
        {
            random_enemy_hp = RND.Next(149, 184);
            goto enemy;
        }
if ((level > 8) && (level < 10))
        {
            random_enemy_hp = RND.Next(189, 221);
            goto enemy;
        }

EDIT: I meant "saving values to variables" in IF statements, so I can use them later in code. This is how my code starts, then there are "Console.WriteLine()"-s, principe of a fight and statements shown above.

string name;
int char_hp = 100;
int level = 1;
int random_enemy_hp;
Random RND = new Random();
Was it helpful?

Solution

You're completely on the wrong track. You should be doing something like:

int[] charLevelHp = { 100, 100, 100, 100,
                      120, 120, 120,
                      150, 150,
                      180, 180 };

int charLevel = 1;
int charHp = charLevelHp[charLevel];

OTHER TIPS

I can't help but notice that you're comparing with char_level for your first couple if-statement, but you're comparing to level for your subsequent if-statements

if ((char_level > 0) && (char_level < 4))
    {
        char_hp = 100;
    }
if ((level > 4) && (level < 6))
    {
        char_hp = 120;
    }

I think you might have intended to use char_level for all of the conditions.

if ((char_level > 0) && (char_level < 4))
    {
        char_hp = 100;
    }
if ((char_level > 4) && (char_level< 6))
    {
        char_hp = 120;
    }

If that's the issue, it would be consistent with the kinds of errors you're seeing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top