Search code examples
c#visual-studioconsole-applicationtext-based

C# | Text-Based RPG | Combat System & Currency (Randomisation)


I'm currently making my first Text-Based RPG but I can't seem to find a way about making a Combat System, what I have so far:

System.Threading.Thread.Sleep(500); // Stops all code from running for 0.5 second
Console.WriteLine("What do you want to do?"); // Fight Screen
Console.WriteLine("Attack"); // Attacks enemy
Console.WriteLine("Bag"); // Opens your bag - able to use potions and spells (if added)
Console.WriteLine("Block"); // Will block all damage from the next attack but will take away your turn.

string fight = Console.ReadLine(); // If the user types something in the battle screen it will be converted to the "fight" variable used below

if (fight == "Attack") // Attack Code that uses the code above.
{
    enemyHealth[0] = enemyHealth[0] - AtkDmg; // Takes the attack damage from the rat's health giving the rat's health a new value.
    Console.WriteLine("You lunge your sword at the Giant Rat"); // Attack Message
    Console.WriteLine("Giant Rat's Health: " + enemyHealth[0] + " / 10");
    charHealth = charHealth - enemyAtkDmg[0];
    Console.WriteLine("The Giant Rat charges at you, headbutting you, knocking you back.");
    Console.WriteLine("Your Health: " + charHealth + " / 20");
}

if (enemyHealth[0] == 0)
{
    System.Threading.Thread.Sleep(1500);
    Console.WriteLine("You have killed the Giant Rat");
    Console.WriteLine("The Giant Rat Dropped 10 Gold");
    Console.WriteLine("*You pick up the gold and put it in your purse");
}

do{
    gold++;
} while (gold != 10);

Console.Write("You now have " + gold + " Gold Pieces.");

Back OT:

My current code isn't working how I'd really like it to, I'd like the damage to be randomised from the character's attack damage +1 or -1 so if the Attack Damage of the player is 4 he can do 3,4, or 5 damage (5 being a critical hit).

I would also like to know if anyone knows of a better way to randomise the money gained from killing a monster from 1-10 or something instead of having it set to 10 every time.

If you need any other resources that I may have, please just leave a comment and I will get you anything you need if it may help you find some answers.

If you find a way to improve my code or make it more tidy, please leave a comment as well.


Solution

  • For randomization you can use the Random class. You can specify a min and a max value and get a random number based on that.

            Random randomNumber = new Random();
            int playerDamage = 5;
    
            var gold = randomNumber.Next( 1, 11 );//For gold
            var damage = randomNumber.Next( playerDamage - 1, playerDamage + 2 );//For damage
    

    The reason the max value seems 1 too high is because the max value itself is not included in the random but only up untill that point. So if the maximum possible value should be 10, the maximum value in the randomVar.Next should be 11.