2

This is the example, please go easy on me as this is the first code I've really ever written. I'm not certain the formatting will translate entirely. This code was running fine until the last edit in which I changed the "rand" to "rand1" as if was giving me an error that "rand" was already used. Now when I run it, I get to "fight or hide" and as soon as I enter one of the options, the terminal closes. Yet, Visual Studio shows no errors.

namespace Text_Game
{
class Game
{
    static int playerHealth = 125;
    static int playerAttack = 10;
    static string playerName;
    static string playerChoice;

    static int enemyHealth = 100;
    static int enemyAttack = 5;


    static void Main(string[] args)
    {
        Welcome();
        FightLoop();
    }

    private static void Welcome()
    {
        Console.WriteLine("Hello Traveller, What is your name?");
        playerName = Console.ReadLine();

        Console.WriteLine("Well Hello, " + playerName);
        Console.WriteLine("You are alone in a dark dungeon. You can see a room behind you and a long hall in front of you.");
        Console.ForegroundColor = ConsoleColor.DarkRed;
        Console.WriteLine("You hear an enemy nearby...");
        Console.ForegroundColor = ConsoleColor.DarkCyan;
        Console.WriteLine("(fight) or (hide)?");
        Console.ForegroundColor = ConsoleColor.Gray;
        Console.ReadLine();
    }




    private static void FightLoop()
    {
        if (playerChoice == "fight")
        { 
            do
            {
               Console.WriteLine("You have " + playerHealth + "health left");
               Console.WriteLine("Enemy has " + enemyHealth + "health left");
               Console.ForegroundColor = ConsoleColor.DarkCyan;
               Console.WriteLine("(attack) or (defend)?");
               Console.ForegroundColor = ConsoleColor.Gray;
               playerChoice = Console.ReadLine(); Console.WriteLine();

                if (playerChoice == "attack")
                {
                  Random rand = new Random();
                  int attackdamage = (rand.Next(3, 5) * playerAttack);
                  enemyHealth = enemyHealth - attackdamage;

                  Console.ForegroundColor = ConsoleColor.Red;
                  Console.WriteLine("Your attack did {0} damage!", attackdamage);
                  Console.ForegroundColor = ConsoleColor.Gray;

                    //enemy attacks back
                  Console.ForegroundColor = ConsoleColor.Red;
                  Console.WriteLine("The enemy attacked you!");
                  Console.ForegroundColor = ConsoleColor.Gray;

                  Random rand1 = new Random();
                  int enemydamage = (rand.Next(2, 4) * enemyAttack);
                  playerHealth = playerHealth - enemydamage;

                  Console.ForegroundColor = ConsoleColor.Red;
                  Console.WriteLine("The enemy's attack did {0} damage!", enemydamage);
                  Console.ForegroundColor = ConsoleColor.Gray;


                }
                else if (playerChoice == "defend")
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("The enemy attacked you!");
                    Console.ForegroundColor = ConsoleColor.Gray;

                    Random rand = new Random();
                    int enemydamage = (rand.Next(2, 4) * enemyAttack);
                    playerHealth = playerHealth - enemydamage/2;

                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("The enemy's attack did {0} damage!", enemydamage/2);
                    Console.ForegroundColor = ConsoleColor.Gray;
                }
                else
                {
                    Console.WriteLine("Please choose (attack) or (defend).");
                }

            } while (playerHealth > 0 && enemyHealth > 0);


            if (enemyHealth <= 0)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("You have defeated the enemy!");
                Console.ForegroundColor = ConsoleColor.Magenta;
                Console.WriteLine("You were able to escape!");
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.WriteLine("Thanks for playing!");
            }
            if (playerHealth <= 0)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("The enemy has killed you!");
                Console.ForegroundColor = ConsoleColor.Gray;
            }


        }
        else if (playerChoice == "hide")
        {
            Console.WriteLine("You hid in the room behind you, but the enemy heard and is coming!");
            Console.WriteLine("You can see a wardrobe in the corner.");
            Console.ForegroundColor = ConsoleColor.DarkCyan;
            Console.WriteLine("(wardrobe) or (stay)?");
            Console.ForegroundColor = ConsoleColor.Gray;

            if (playerChoice == "wardrobe")
            {
                Console.WriteLine("You are now hidden.");
                Console.WriteLine("The enemy leaves through a secret door. You follow and find yourself outside.");
                Console.ForegroundColor = ConsoleColor.Magenta;
                Console.WriteLine("You have escaped!");
                Console.ForegroundColor = ConsoleColor.Blue;
                Console.WriteLine("Thanks for playing!");

            }
            else if (playerChoice == "stay")
            {
                do
                {
                    Console.WriteLine("You have " + playerHealth + "health left");
                    Console.WriteLine("Enemy has " + enemyHealth + "health left");
                    Console.ForegroundColor = ConsoleColor.DarkCyan;
                    Console.WriteLine("(attack) or (defend)?");
                    Console.ForegroundColor = ConsoleColor.Gray;
                    playerChoice = Console.ReadLine(); Console.WriteLine();

                    if (playerChoice == "attack")
                    {
                        Random rand = new Random();
                        int attackdamage = (rand.Next(3, 5) * playerAttack);
                        enemyHealth = enemyHealth - attackdamage;

                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("Your attack did {0} damage!", attackdamage);
                        Console.ForegroundColor = ConsoleColor.Gray;

                        //enemy attacks back
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("The enemy attacked you!");
                        Console.ForegroundColor = ConsoleColor.Gray;

                        Random rand1 = new Random();
                        int enemydamage = (rand.Next(2, 4) * enemyAttack);
                        playerHealth = playerHealth - enemydamage;

                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("The enemy's attack did {0} damage!", enemydamage);
                        Console.ForegroundColor = ConsoleColor.Gray;


                    }
                    else if (playerChoice == "defend")
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("The enemy attacked you!");
                        Console.ForegroundColor = ConsoleColor.Gray;

                        Random rand = new Random();
                        int enemydamage = (rand.Next(2, 4) * enemyAttack);
                        playerHealth = playerHealth - enemydamage / 2;

                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("The enemy's attack did {0} damage!", enemydamage / 2);
                        Console.ForegroundColor = ConsoleColor.Gray;
                    }
                    else
                    {
                        Console.WriteLine("Please choose (attack) or (defend).");
                    }

                } while (playerHealth > 0 && enemyHealth > 0);



                if (enemyHealth <= 0)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("You have defeated the enemy!");
                    Console.ForegroundColor = ConsoleColor.Magenta;
                    Console.WriteLine("You have escaped!");
                    Console.ForegroundColor = ConsoleColor.Blue;
                    Console.WriteLine("Thanks for playing!");
                }
                if (playerHealth <= 0)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("The enemy has killed you!");
                    Console.ForegroundColor = ConsoleColor.Gray;
                }
            }

        }
        else
        {
            Console.WriteLine("Choose (fight) or (hide).");
        }

    }
}

}

Josh Henry
  • 23
  • 3
  • 2
    When `FightLoop` gets entered, the value of `playerChoice` is going to be null. That will make it enter the final `else` block, which just consists of a single `Console.WriteLine`. As such, there's nothing to stop the program from printing that line and exiting `FightLoop` and, consequently, finishing execution. – Abion47 Feb 26 '19 at 19:38
  • 1
    Put a Console.ReadLine(); statement so that it waits for a Enter to close the console. The program ends – Charles May Feb 26 '19 at 19:38
  • 1
    Just as a side note `FightLoop` is **way** too big. If you break it into separate methods it won't be as painful to debug in the future. – Broots Waymb Feb 26 '19 at 19:40
  • 2
    There is no wrong time to learn the basics of using the debugger. I would say this is a rather good time. Really, once you understand basic debugging techniques (like single-stepping, for example), you won't believe you ever tried to write/test/troubleshoot a program without being able to use the debugger: [Learn to debug using Visual Studio](https://learn.microsoft.com/en-us/visualstudio/debugger/getting-started-with-the-debugger), [Navigating through Code with the Debugger](https://learn.microsoft.com/en-gb/visualstudio/debugger/navigating-through-code-with-the-debugger) –  Feb 26 '19 at 19:45
  • 1
    I don't see that you set variable playerChoice. – Senad Meškin Feb 26 '19 at 19:52

2 Answers2

1

The problem is on your Welcome() method. You ask the player to choose fight or hide, but you don't save the answer in the playerChoice variable. So when you check for playerChoice on your FightLoop, it ends on the else, running the line Console.WriteLine("Choose (fight) or (hide).");. But because you are not waiting for any key press, the program finishes and closes. To fix, you have to:

Read the player choice on your Welcome method, change the line Console.ReadLine(); to playerChoice = Console.ReadLine();

Wait for any keypress on the else part of your fightloop, adding Console.ReadLine(); after Console.WriteLine("Choose (fight) or (hide).");

Stormhashe
  • 704
  • 6
  • 16
1

When execution of FightLoop method begins playerChoice variable has no value. so execution goes to block 3 as shown below and then exits method.

private static void FightLoop()
{
    if (playerChoice == "fight")
    {
         //block 1
    }
    else if (playerChoice == "hide")
    {
         //block 2
    }
    else
    {
        //block 3
        Console.WriteLine("Choose (fight) or (hide).");
    }
}

You need to change the last line of Welcome method so it saves the read string into the playerChoice variable.

private static void Welcome()
{
    Console.WriteLine("Hello Traveller, What is your name?");
    playerName = Console.ReadLine();

    Console.WriteLine("Well Hello, " + playerName);
    Console.WriteLine("You are alone in a dark dungeon. You can see a room behind you and a long hall in front of you.");
    Console.ForegroundColor = ConsoleColor.DarkRed;
    Console.WriteLine("You hear an enemy nearby...");
    Console.ForegroundColor = ConsoleColor.DarkCyan;
    Console.WriteLine("(fight) or (hide)?");
    Console.ForegroundColor = ConsoleColor.Gray;
    playerChoice = Console.ReadLine();
}

Since you don't use rand1 variable anywhere, you can delete the following line.

Random rand1 = new Random();

Also, a good practice would be to use methods for "fight" and "hide":

private static void FightLoop()
{
    if (playerChoice == "fight")
    {
         fight();
    }
    else if (playerChoice == "hide")
    {
         hide();
    }
    else
    {
        Console.WriteLine("Choose (fight) or (hide).");
    }
}
elgaspar
  • 458
  • 3
  • 11