So I made a "game" in a c# console app project which is basically a hero who must kill a monster and both the hero and the monster have HP and a random amount of damage given and taken (as it should be). I have a couple tiny problems which don't make sense to me. There is a Regeneration Potion that I have added to the game which obviously adds a random amount of hp between 10-30 to the player.
//Regenerating or not
if (isRegen == false) //if it doesn't
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("You've failed to regenerate!");
Console.WriteLine("Enemy: " + monster.name);
Console.WriteLine("Enemy's hp: " + monster.hp);
GetDamage();
}
else //if it does
{
if (myHP + regenAmount > 100 || myHP == 100)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("You can't regenerate above 100 hp.");
Game();
}
else
{
if (potionCounter == 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("You are out of health potions. Cannot regenerate!");
Game();
}
potionCounter--;
myHP += regenAmount;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("You consumed a Health Potion!");
Console.WriteLine("Your hp was regenerated successfully! HP is raised by " + regenAmount);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Enemy: " + monster.name);
Game();
}
}
Now, as you can see in the code above, I have made an if statement that checks whether the sum of myHP and the regenAmount is higher than 100, a message that says "You can't regenerate above 100 hp" is displayed. Thing is, when I try this, sometimes it does display the message as presented here, but eventually it decides to display "You've failed to regenerate!" in here and well the game just goes on and the player gets hit. (which obviously shouldn't happen). There is even another similar problem with the potionCounter. As presented in the code:
if (potionCounter == 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("You are out of health potions. Cannot regenerate!");
Game();
}
Whenever the potion counter reaches 0, it should display a message that says that the user is out of potions. It happens and works, but similarly to the previous problem, it sometimes ignore the if statement and allows the user to either heal and continue on decrementing the potionCounter variable or fail to regenerate and get hit by the monster.
I know some of this code is kinda bad and well that's because im kinda new to programming but im trying my best to develop myself and learn as much as I can, which is why I decided to share it here.
Thank you for reading it and I hope you find the solution :)
Edit: The code for Game()
:
static void Game()
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(monster.name + "'s hp: " + monster.hp);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Your hp: " + myHP + "\n");
Console.ForegroundColor = ConsoleColor.White;
while(monster.hp > 0 && myHP > 0) //Running until the enemy or the player dies.
{
Console.Write("A - ATTACK (Give damage but always get hit)\nD - DEFEND (50% chance of not getting hit)\nR - REGENERATE (Regenerates HP, but if it fails you get hit) - ");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(potionCounter + " POTIONS LEFT\n");
string input = Console.ReadLine();
//ATTACK
if (input == "a" || input == "A")
{
Console.Clear();
Attack();
}
else if (input == "d" || input == "D")
{
Console.Clear();
Defend();
}
else if (input == "r" || input == "R")
{
Console.Clear();
Regenerate();
}
else
{
Console.Clear();
Game();
}
}
}