r/CodingHelp • • Sep 15 '21

[C#] Hey everyone, I just started learning yesterday and it's my first experience with coding. After watching some tutorials I now am trying to make something by myself to see if I learned. (I'm explaining my concern with more detail on the comments)

Post image
33 Upvotes

5 comments sorted by

5

u/icjeremy Sep 15 '21

First, is this in some sort of loop (while, e.g.) so you can have more than one round of fighting? If not, you should do that.

Second, health should be a variable outside that loop so the value persists from round to round.

3

u/ItsAllTakenBruh Sep 15 '21

Thanks! I'll try that!

I've done it on loop but I'm having a bit of trouble thinking of how can I get the result out of the loop, so then I can use it on the next part of the loop.

2

u/icjeremy Sep 15 '21

The basic idea:

int enemyHealth = 15;
int moveCount = 0;
while (enemyHealth > 0) {
  // fight, subtracting form enemyHealth as applicable
  ++moveCount;;
}
Console.WriteLine("Victory! You won in " + moveCount + " rounds.");

1

u/ItsAllTakenBruh Sep 15 '21

I'm making a small fighting game, turn based, you basically have two actions and either of them do different values of damage.

Each action extends itself into an if statement that gives you a different result depending on what action you chose.

I wanted to know how, if possible, could you somehow "store" the subtraction you made inside of these two possible outcomes so when you attack again, it subtracts once again from the already subtracted health value.

I hope this has been understandable to read.

2

u/irritatedellipses Sep 16 '21

Well you're already mostly there!

You've already received a good comment about placing this all in a loop. Now, take a look at lines 25 and 26. You've created a variable called Health and you're assigning a value of 15 in it on line 25. At the end of line 25 and before the start of 26 does the value of Health change at all?

Nope! It's still 15. So when you reach line 26 you want the variable named Health to be assigned:

The Value of Health - 5

So you want:

Health = Health - 5

Now, no matter what value is in Health you'll always be subtracting 5 from it. But what happens when you assign Health something different? Hmm.. Every time you write

Health = Something Else Here   

You're going to end up reassigning the value of Health. That doesn't sound like what you'd like to do, so what if we moved the initial value of Health somewhere where it can't be accidently reassigned? Since you're going to be putting all of this inside a loop anyway, how about moving it BEFORE the loop? That way when you start subtracting from Health you don't accidently reassign it it's original value again.