samedi 3 décembre 2022

How do I use Random properly to generate number for text-based combat simulator game?

I'm trying to use Random to generate random number for a text-based combat simulator but it keeps generating a very large and unusual number. Here is the code: (Mind you, I am not a very experienced programmer. I worked with Visual Basic back in 2001 and I haven't really used anything else since then (other than a few webpages I made within last 10 years).

Here's my Visual studio screen:

internal class Program
    {
        static void Main(string[] args)
        { System.Console.WriteLine("Starting Simulator");
            Actor FoxTail = new Actor();
            FoxTail.HealthPoints = 15;
            FoxTail.Strength = 2;
            FoxTail.Defense = 12;
            FoxTail.Spirit = 12;
            FoxTail.Dexterity = 2;
            FoxTail.Agility = 3;
            FoxTail.Intelligence = 2;
            FoxTail.Level = 0;
            FoxTail.ExperiencePoints = 0;
            Actor GreyWolf = new Actor();
            GreyWolf.HealthPoints = 25;
            GreyWolf.Strength = 2;
            GreyWolf.Defense = 10;
            GreyWolf.Spirit = 8;
            GreyWolf.Dexterity = 5;
            GreyWolf.Agility = 7;
            GreyWolf.Intelligence = 2;
            GreyWolf.Level = 1;
            GreyWolf.ExperiencePoints = 5;
            int FoxTailAttack = new Randomizer.RandomIntegerGenerator(1).GeneratePositiveValue();
            if(FoxTailAttack >= GreyWolf.Defense)
            {
                int damage = new Randomizer.RandomIntegerGenerator(1).GeneratePositiveValue() + FoxTail.Strength;
                GreyWolf.HealthPoints = GreyWolf.HealthPoints - damage;
                Console.WriteLine("Grey Wolf is at " + GreyWolf.HealthPoints + " health!");

            }
            else
            { Console.WriteLine("Foxtail missed!");
            }
            Console.WriteLine("Press any key to continue...");
            System.Console.ReadLine();

I'm assuming the '1' in the parathesis after RandomIntegerGenerator is where my problem lies, but I just don't know how to define a range for the random number to generate. I get a weird -534011695 number that gets outputted in my runtime window. I'd like to get it to generate a number beween 1-25 if possible.

Any help is appreciated. I'm very new to all of this and i'm starting small. I did my first 'Hello world!' program only 2 days ago. lol Thanks in advanced.




Aucun commentaire:

Enregistrer un commentaire