Click here to Skip to main content
15,903,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I'm working on a C# game in a console application. I tried to move my "commands" that you can use into separate class files, and now they produce a StackOverflow error. Rough.

I have 4 Files: Program.cs (Handles the Game loop and method input and processing), BasicCommands(Holds 5 Methods for commands), SpeechCommands(Same as Basic except specifically for speech strings) and Character (character constructor/object for the player character and NPCs).

namespace DigitalLove
{
    public class Program
    {
        public List<string> validWords = new List<string> { "look",
            "move",
            "check status",
            "hallway",
            "dance room",
            "bathroom",
            "punch bowl",
            "talk",
            "the scrawny boy",
            "the tall boy",
            "the girl",
            "take",
            "cup of punch",
            "the blond boy",
            "Rock",
            "Blues",
            "Forte",
            "Roll"
            };
        public List<string> TalkingResponse = new List<string>
        {
            "uh...hi.",
            "of course not.",
            "yes",
            "no",
            "just here to have a good time.",
            "looking for people",

        };
        public string[] Locations = { "Dance Room", "Bathroom", "Hallway", "Punch Bowl" };
        public string Location,
        response,
        PlayerName,
        HeldItem;
        public Character Rockman;
        public Character Blues;
        public Character Forte;
        public Character Roll;
        public Character Player;
        public bool HoldingItem;
        SpeechCommands Command = new SpeechCommands();
        BasicCommands Basic = new BasicCommands();
        static void Main(string[] args)
        {
            Program Game = new Program();
            Game.Game();
        }
        public void Game()
        {
            //Game title here
            Console.WriteLine("Press enter to begin!");
            Console.ReadKey();
            Console.Clear();
            Console.WriteLine("What is your name?");
            PlayerName = Console.ReadLine();
            while (PlayerName == "")
            {
                Console.WriteLine("Please enter a name!");
                PlayerName = Console.ReadLine();
            }
            Console.WriteLine("Thank you, " + PlayerName + ". Good luck!");
            Console.WriteLine("Press enter to continue. . .");
            Console.ReadKey();
            Console.Clear();
            Location = Locations[0];
            Rockman = new Character();
            Blues = new Character();
            Forte = new Character();
            Roll = new Character();
            Player = new Character();

            //Player definitons
            Player.CharacterName = PlayerName;
            Player.Location = Location;

            //Rockman definitons
            Rockman.CharacterName = "Rock";
            Rockman.Location = Locations[0];
            Rockman.MetTrigger = false;

            //Blues definitions
            Blues.CharacterName = "Blues";
            Blues.Location = Locations[0];
            Blues.MetTrigger = false;

            //Forte definitions
            Forte.CharacterName = "Forte";
            Forte.Location = Locations[1];
            Forte.MetTrigger = false;

            //Roll definitons
            Roll.CharacterName = "Roll";
            Roll.Location = Locations[3];
            Roll.MetTrigger = false;
            Console.WriteLine("You stand near the edge of the dance, shying away from everyone else who seems to be having a very good time." + "\n" +
                "You would love to join, but no one has approached you yet! You feel awkward trying to approach anyone, despite the numerous amount of people in the same position.");
            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.WriteLine("Command List: Look, Move, Check Status");
            Console.ResetColor();
            GetInput();
        }
        public string GetInput()
        {
            var Test = true;

            while (Test)
            {
                response = Console.ReadLine().ToLower();
                if (validWords.Contains(response))
                {
                    Test = false;
                    ProcessInput(response);
                }
                else
                {
                    Console.WriteLine("I'm sorry, I do not understand.");
                }
            }
            return response;
        }

        public void ProcessInput(string response)
        {
            switch (response)
            {
                case "look":
                Basic.LookCommand();
                break;
                case "move":
                Basic.MoveCommand();
                break;
                case "Move":
                Basic.MoveCommand();
                break;
                case "check status":
                Basic.CheckStatus();
                break;
                case "hallway":
                Basic.MoveCommand();
                break;
                case "dance room":
                Basic.MoveCommand();
                break;
                case "bathroom":
                Basic.MoveCommand();
                break;
                case "punch bowl":
                Basic.MoveCommand();
                break;
                case "talk":
                Basic.TalkCommand();
                break;
                case "the scrawny boy":
                Basic.TalkCommand();
                break;
                case "the tall boy":
                Basic.TalkCommand();
                break;
                case "the girl":
                Basic.TalkCommand();
                break;
                case "take":
                Basic.TakeCommand();
                break;
                case "cup of punch":
                Basic.TakeCommand();
                break;
                case "the blond boy":
                Basic.TalkCommand();
                break;
            }
        }
        public string GetTalkInput()
        {
            var Test = true;
            while (Test)
            {
                response = Console.ReadLine().ToLower();
                if (TalkingResponse.Contains(response))
                {
                    Test = false;
                    ProcessTalkInput(response);
                }
                else
                {
                    Console.WriteLine("I'm sorry, I do not understand.");
                }
            }
            return response;
        }
        public void ProcessTalkInput(string response)
        {
            switch (response)
            {
                case "uh...hi.":
                Basic.TalkCommand();
                break;
                case "of course not.":
                Basic.TalkCommand();
                break;
                case "yes":
                Basic.TalkCommand();
                break;
                case "no":
                Basic.TalkCommand();
                break;
            }
        }

    }
}

Where is the exception happening? Why is it happening? I don't know much about the stack overflow exception. The call stack in Visual Studio only says "external code".

What I have tried:

I've tried cutting back on how many times I call the methods in the other responses.
Posted
Updated 13-Apr-17 2:40am
Comments
Dave Kreskowiak 11-Apr-17 17:41pm    
You normally get a StoackOverflow when you have a method constantly calling itself without any way of bailing out of it or have a collection of methods that form a calling loop without any way of bailing out of it.

Quote:
Where is the exception happening? Why is it happening?

Use the debugger to locate where you get the error, you may discover that routine is calling itself endlessly.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Comments
Hub_Batch 11-Apr-17 19:08pm    
I thought using the debugger would be moot point as this happens as soon as the program starts, haha. Thank you! I'll try this out.
Patrice T 11-Apr-17 19:12pm    
Computers are fast ! many things can happen as soon as the program starts.
put a breakpoint in first line of program and run step by step to locate the problem.
Hub_Batch 11-Apr-17 19:18pm    
Ah, I found out where the exception is happening- it's happening on "SpeechCommands Command = new SpeechCommands();"- the call stack just repeats this over and over. How come?
Patrice T 11-Apr-17 19:24pm    
Did you found with the debugger ?
Use Improve question to update your question.
So that everyone can pay attention to this information.
Dave Kreskowiak 11-Apr-17 19:47pm    
We can't see the SpeechCommands class so it's impossible for anyone to say. You should be looking in the constructor for SpeechCommands to see what's in there. The debugger can Step Into that easily. When the code gets to that line yhour pointed out, hit F11 to step into the constructor.
I found out what was happening- my class files were being initialized indefinitely. Thanks for your help!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900