Home > database >  C# Trouble changing a method after it has been defined
C# Trouble changing a method after it has been defined

Time:12-03

I am very VERY new to all things coding, and I have run into a bit of trouble. I have this method that generates a random number between 1 - 6 (its supposed to simulate a dice roll), and what I want to do is through an input enable the user to change how many sides the dices has (The second parameter of the int nr in this case).

I want to be able to change the 7 to any number with a piece of code that is outside the method itself. I can not wrap my head around how I am supposed to accomplish this.

static int RullaTärning(Random slumpObjekt)
{
    int nr = slumpObjekt.Next(1, 7); 
    return nr;
}

Any and all help is appreciated, and if there is some piece of information missing that is needed to solve this, please let me know.

Thanks

I've tried changing the 7 to an integer variable "sides", but I don't know how to change this integer variable later on in the program. I have tried a load of random stuff without much thought, but I don't know how to accomplish this at all. I really am lost.

CodePudding user response:

You managed to realize that you need to somehow make the second param to Next method variable. Great! Let's do it:

static int RullaTärning(Random slumpObjekt)
{
    int sides = 6;
    int nr = slumpObjekt.Next(1, sides  1); 
    return nr;
}

now all you need to do is get the value from the user instead of hardcode it:

static int RullaTärning(Random slumpObjekt, int sides)
{
    int nr = slumpObjekt.Next(1, sides 1); 
    return nr;
}

If you do not want to force the user to choose a side-count (most dice would be 6-sided) you can work with a default:

static int RullaTärning(Random slumpObjekt, int sides = 6)
{
    int nr = slumpObjekt.Next(1, sides 1); 
    return nr;
}

// Can be used as
int roll = RullaTärning(rnd); // no second param => 6 will be used
// or
int damage = RullaTärning(rnd, 16);

I would always encourage to check your inputs:

static int RullaTärning(Random slumpObjekt, int sides = 6)
{
    // Assuming we had a requirement to allow no less than 6 sides.
    if( sides < 6 ) throw new ArgumentException("Dice need at least 6 sides.", nameof(sides));
    // Mind that I did not check `slumpObjekt` for null,
    // because a NullReferenceException will be thrown anyway
    // on the attempt to call `Next` on `null`.
    int nr = slumpObjekt.Next(1, sides 1); 
    return nr;
}

// because there is always "that guy" who will try
int nonsense = RullaTärning(rnd, -5);

... and a really pedantic fellow just made me mention clean code and "no magic numbers" ...

const int MINIMAL_ALLOWED_SIDES = 6; // Now we know why "6" and 
                                     // we only need to change 1 place in code 
                                     // if requirement of minimal allowed sides 
                                     // changes
static int RullaTärning(Random slumpObjekt, int sides = MINIMAL_ALLOWED_SIDES)
{
    if( sides < MINIMAL_ALLOWED_SIDES) throw new ArgumentException($"Dice need at least {MINIMAL_ALLOWED_SIDES} sides.", nameof(sides));
    int nr = slumpObjekt.Next(1, sides 1); 
    return nr;
}

CodePudding user response:

Pass another parameter to your method:

static int RullaTärning(Random slumpObjekt, int sides)
{
    int nr = slumpObjekt.Next(1, sides);
    return nr;
}

CodePudding user response:

Hope this works for you this is simple method. Please go through basics

static int RullaTärning(Random slumpObjekt, int numberOfFaces){
int nr = slumpObjekt.Next(1, numberOfFaces 1); 
return nr;
}
  • Related