2

So right now im making a game for learning purpose in which a player clicks a button and it takes away a random number from total score. The thing is it works but only once , cause when click it again it resets. So how do you make it that it would work multiple times? Sorry for being a bother.

 public void playerTwo(object sender, EventArgs e)
    {
        Random rnd = new Random();
        int attack = rnd.Next(1, 10);

        int playerOneScore = 1000;
        int playerOneScores;

        playerOneScores = playerOneScore - (1 * attack);


        playerOneHealth.Text = playerOneScores.ToString();
Dryg
  • 23
  • 2
  • 2
    Move the value outside of your method, if you have it inside it will get set every time the method gets called. – Rand Random Feb 26 '19 at 18:04
  • 1
    As an add-on to what Rand said, you may want to research the concept of "scope" in C# to understand why moving that line outside of the method lets it persist. – Michael Feb 26 '19 at 18:08
  • It's due to the scope of `playerOneScore`. Right now, it gets reset every time the execution hits the `int playerOneScore = 1000;` line. Try storing that outside the function. – Vlad Feb 26 '19 at 18:08
  • Why do you multiply `attach` by one before subtracting? – juharr Feb 26 '19 at 18:25

2 Answers2

2

Change your code to the following.

int playerOneScore = 1000;
public void playerTwo(object sender, EventArgs e)
{
    Random rnd = new Random();
    int attack = rnd.Next(1, 10);

    //int playerOneScore = 1000; <-- don't set playerOneScore inside the method, as this line gets executed with every time calling this method so it again and again gets set to 1000
    //int playerOneScores; <-- don't get the meaning of this one, so I removed it and I am changing the playerOneScore field

    playerOneScore = playerOneScore - (1 * attack);
    playerOneHealth.Text = playerOneScore.ToString();
}

Additionally to this you should consider moving the Random variable outside the method aswell, since creating a Random object to fast will give you the same result over and over again.

You can read why this happens here: Random number generator only generating one random number

So, I would suggest changing it to this:

int playerOneScore = 1000;
Random rnd = new Random();
public void playerTwo(object sender, EventArgs e)
{
    //Random rnd = new Random(); <-- don't create Random here as it will likey produce the same random number over and over again
    int attack = rnd.Next(1, 10);

    //int playerOneScore = 1000; <-- don't set playerOneScore inside the method, as this line gets executed with every time calling this method so it again and again gets set to 1000
    //int playerOneScores; <-- don't get the meaning of this one, so I removed it and I am changing the playerOneScore field

    playerOneScore = playerOneScore - (1 * attack);
    playerOneHealth.Text = playerOneScore.ToString();
}

See it in action here: https://dotnetfiddle.net/djr94j

(I had to change it to static fields and methods, thats not necessary for you)

Rand Random
  • 7,300
  • 10
  • 40
  • 88
  • 1
    @Dryg - out of curiosity why did you write `1 * attack`, as multiplying the attack value by `1` will always give the same result so its pointless in my opinion?!? – Rand Random Feb 26 '19 at 18:25
  • Yeah i know it's there just so i could change it to some other "attack" multiplier. – Dryg Feb 26 '19 at 18:52
-1

Move your declaration of variable outside the function (to the top)

int playerOneScore = 1000;
int playerOneScores;


public void playerTwo(object sender, EventArgs e)
{
    Random rnd = new Random();
    int attack = rnd.Next(1, 10);

    playerOneScores = playerOneScore - (1 * attack);

    playerOneHealth.Text = playerOneScores.ToString();
Prabin Yovan
  • 164
  • 1
  • 3
  • 14
  • Since this doesn't update `playerOneScore` you'll always subtract from 1000 and basically still have the same problem. Only one variable is really needed to keep track of the current score. – juharr Feb 26 '19 at 18:28