-1

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.

EpicKip
  • 4,015
  • 1
  • 20
  • 37
Anthony P
  • 31
  • 2
  • 9
  • If you're going to dislike, at least explain why.. – Anthony P Apr 06 '17 at 07:41
  • 1
    Have you done any research? Have you found the [Random class](https://msdn.microsoft.com/en-us/library/system.random(v=vs.110).aspx)? Have you tried to do anything with it? – PJvG Apr 06 '17 at 07:41
  • Also, this is obviously a duplicate of [How do I generate a random int number in C#?](http://stackoverflow.com/questions/2706500/how-do-i-generate-a-random-int-number-in-c/2706537#2706537). – PJvG Apr 06 '17 at 08:07
  • SO is for answering questions. To request any comments or critique you should post the code to http://codereview.stackexchange.com/ – enkryptor Apr 06 '17 at 11:05

1 Answers1

1

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.

EpicKip
  • 4,015
  • 1
  • 20
  • 37
  • 1
    @AnthonyP Look closely at my code, I use 1 random variable to create 2 random values. Just do this every time based on monster health/level/whatever :) – EpicKip Apr 06 '17 at 08:11