Ask coding questions

← Back to all posts
Why does my game all of the sudden clear without a Console.Clear() method?
CDSWgrimly (0)

My game wants to clear the console and not work for some reason after I input my name

using System;
using System.Threading;
using System.Linq;

namespace CS1
{
    //Makes the character
    public class FighterClass
    {
        public string Name;
        public int AttackDamage;
        public float Health;
        public float Exp;
        public int Lvl;
        public int toLevel;
        public int Balance;
        public int magicPoints;

        public FighterClass(string _name, int _attackDamage, float _health, int _mp, float _exp, int _lvl, int _tolevel, int _balance)
        {
            Name = _name;
            AttackDamage = _attackDamage;
            Health = _health;
            Exp = _exp;
            Lvl = _lvl;
            toLevel = _tolevel;
            Balance = _balance;
            magicPoints = _mp;

        }

        
    }

    //Makes the enemy
    public class Enemy
    {
        public string Name;
        public int Damage;
        public int Health;
        public float expValue;
        public int coinValue;

        public Enemy(string _name, int _damage, int _health, float _exp, int _coinValue)
        {
            Name = _name;
            Damage = _damage;
            Health = _health;
            expValue = _exp;
            coinValue = _coinValue;
        }
    }



    class Program
    {
        static void Main(string[] args)
        {
          //Initializes and gives the new enemy stats
            Random rand = new Random();
            int[] dam = new int[] { 1, 5, 4, 6, 10, 25};
            string[] eName = new string[] {"Slime", "Golem", "Zombie", "Skeleton"};
            
            //Gives player stats (Potions + Name)
            int Potions = 10;
            string name = "";
            Console.WriteLine("Enter the name: ");
            name = Console.ReadLine();
            FighterClass Melee = new FighterClass(name, 10, 100, 20, 1, 1, 5, 0);

            //Fight starts
            for(int z = 0; z < 1000; z++) {
                int d = dam[rand.Next(1, 6)];
                string n = eName[rand.Next(1, 4)];
                //Shopkeep
                  if (rand.Next(1, 10) == 10){
                   Console.WriteLine("The Shopkeep has arrived! He will sell you 1 potion for 10 coins!");
                   Console.WriteLine("\nDo you want to buy potions? y/n");
                   string shop = Console.ReadLine();

                    if(shop == "y"){
                      
                      Console.WriteLine("How many potions?");
                      int potionAmount = Convert.ToInt32(Console.ReadLine());
                      int cost = potionAmount * 10;
                      if (cost >= Melee.Balance){
                        Console.WriteLine("You gained: " + potionAmount + " potions!");
                       Potions = Potions + potionAmount;
                        Melee.Balance = Melee.Balance - cost;
                      }
                      else{
                        Console.WriteLine("You cannot afford that many potions!");
                      }
                    }
                  }
                Enemy E = new Enemy(n, d, rand.Next(20, 50), 1, rand.Next(1, 10));
                Console.WriteLine("\nAn enemy approaches!");
                Console.WriteLine("Name: " + E.Name + "\nHealth: " + E.Health);

                //Actions the player can take
                for (int i = 0; i < 1000; i++)
                {
                  //Checks for Game Over
                  if(Melee.Health <= 0)
                  {
                    Console.Clear();
                    Console.WriteLine("-GAME OVER- \n\n type \"Continue\" to restart the fight or \"End\" to exit the game.");
                    string restart = Console.ReadLine();

                    if(restart == "Continue"){
                      z = 0;
                      E.Health = 100;
                      Melee.Health = 100;
                      Thread.Sleep(1000);
                      Console.Clear();
                      Console.WriteLine("Health: " + E.Health);
                    }
                    else{
                      System.Environment.Exit(0);
                    }
                  
                  

                    Console.WriteLine("Command List: \n1. attack\n2. heal");
                    Console.WriteLine("Command: ");
                    string command = Console.ReadLine();

                    //Attack command
                    if (command == "1")
                    {
                        Console.Clear();
                        Console.WriteLine("You dealt " + Melee.AttackDamage + " to " + E.Name);
                        E.Health = E.Health - Melee.AttackDamage;
                        Console.WriteLine("Health: " + E.Health);
                    }

                    //Heal command
                    if (command == "2")
                    {
                        if (Potions > 0)
                        {
                            Console.WriteLine("You have " + Potions + " potions.");
                            Console.WriteLine("Type \"y\" to confirm consumption of 1");
                            string option = Console.ReadLine();
                            if (option == "y")
                            {
                                Console.Clear();
                                Melee.Health += 50;
                                Potions -= 1;
                                Console.WriteLine("You gained 50 health! You now have " + Melee.Health + " health and " + Potions + " potions left.");
                            }
                        }
                        else
                        {
                            Console.WriteLine("You do not have any potions");
                        }
                    }

                    //Enemy is not defeated to restart the loop
                    if (E.Health > 0)
                    {
                        Thread.Sleep(1000);
                        Console.WriteLine("Enemy " + E.Name + " attacks!");
                        Console.WriteLine("Enemy " + E.Name + " dealt " + E.Damage + " to " + Melee.Name);
                        Melee.Health = Melee.Health - E.Damage;
                        Console.WriteLine("You now have " + Melee.Health + " health.");
                        Thread.Sleep(1000);
                    }

                    //Enemy is defeated, reward is given and loop starts again
                    if (E.Health == 0)
                    {
                        
                        Console.WriteLine(E.Name + " was slain!" + "\nYou gained " + E.expValue + " exp!");
                        Melee.Exp = Melee.Exp + E.expValue;
                        Console.WriteLine("Exp: " + Melee.Exp);
                        Console.WriteLine("It also dropped " + E.coinValue + " coins!");
                        Melee.Balance = Melee.Balance + E.coinValue;
                        i = 1000;
                        Thread.Sleep(1000);
                    }

                    //Level Up
                    if (Melee.Exp == Melee.toLevel)
                    {
                        Console.Clear();
                        Console.WriteLine("You leveled up!");
                        Melee.AttackDamage += 2;
                        Melee.Health += 1.25f;
                        Melee.toLevel += 2;
                        Melee.Exp = 0;
                        Melee.Lvl = Melee.Lvl + 1;
                        Console.WriteLine("\n Level: " + Melee.Lvl + "\n Attack Damage: " + Melee.AttackDamage + "\n Health:" + Melee.Health);
                        Console.WriteLine("\nPress Enter to continue.");
                        Console.ReadKey();
                    }
                }
                Thread.Sleep(1000);
                Console.Clear();
            }
        }
    }
 }
}
Answered by Coder100 (18089) [earned 5 cycles]
View Answer
Comments
hotnewtop
Coder100 (18089)

yes you added a console.clear

also, the other code won't ever run because it is inside an if statement that checks if the melee health is <= 0, which isn't true.

CDSWgrimly (0)

@Coder100 I guess I should've worded this differently. The code past line 100 worked before, now it wants to clear without a console.clear statement. The line you are mentioning is at the end of a loop where the enemy has been defeated. The clear is after the loop for a single fight has been completed

Coder100 (18089)

No, you put all the code in an if statement that won't run, look at line 105 @ConnorGrim

CDSWgrimly (0)

@Coder100 Ok, i just needed to move a curly bracket, and now it works

Coder100 (18089)

nice!
mark as correct answer please @ConnorGrim

Coder100 (18089)

so next time check for correct code placement

CDSWgrimly (0)

This is confusing because I never touched the code and then it started not working with me