0

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();
10
  • 2
    goto in C#! It lives! Commented Nov 20, 2013 at 17:04
  • 1
    Whatever you want to do in C#, do not use "goto". Commented Nov 20, 2013 at 17:04
  • You're not declaring char_hp in any of the code you've shown us. Commented Nov 20, 2013 at 17:05
  • 2
    Show us where you declare char_hp Commented Nov 20, 2013 at 17:06
  • 1
    I remember when i first started programming on the TI83 graphing calculator. I used no functions or loops at all. only goto Commented Nov 20, 2013 at 17:39

2 Answers 2

1

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];
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.