2

In my C# code (I am currently trying to learn it), I am trying to make a text - console based adventure game. The first 'Section' works, and I copy pasted it over to and changed the name of the variables etc. (Its a repetitive code), but it just wont work.

Here is the code:

using System;

namespace LearningC
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            lvl_1_start:                                                                                                                            //LEVEL ONE
            Console.WriteLine ("Welcome to AlexAdventureLand.\nPlease press the enter key to start.");
            Console.ReadKey ();
            Console.WriteLine ("You are a rabbit in a burrow. There is a badger outside. \nPress 1 to leave the burrow. \nPress 2 to wait.");
            int lvl_1 = Convert.ToInt32 (Console.ReadLine ());
            if (lvl_1 == 1) {
                Console.WriteLine ("The badger sees you and eats you. \nYou fail. \n\nPress enter to try again.");
                Console.ReadKey ();
                goto lvl_1_start;
            } else if (lvl_1 == 2) {
                Console.WriteLine ("The badger scurries away!");
                goto lvl_2_start;
            } else {
                Console.WriteLine ("You pressed the wrong button. Start again.");
                goto lvl_1_start;
            }
            lvl_2_start:                                                                                                                            //LEVEL TWO
            Console.WriteLine ("Now what do you want to do? \nPress 1 to leave burrow. \nPress 2 to wait.");
            Console.ReadKey ();
            int lvl_2 = Convert.ToInt32 (Console.Read ());
            if (lvl_2 == 1) {
                Console.WriteLine ("You leave the burrow and look around.");
                Console.ReadKey ();
                goto lvl_3_prt_a_start;
            } else if (lvl_2 == 2) {
                Console.WriteLine ("You wait a little longer. \nYou can hear footsteps around your burrow. You wonder whether it's your friend - troy - or the badger still.");
                goto lvl_3_prt_b_start;
            } else {
                Console.WriteLine ("You pressed the wrong button. Start again.");
                goto lvl_2_start;
            }
            lvl_3_prt_a_start:                                                                                                                      //LEVEL THREE PART A
            Console.WriteLine ("PlaceHolder");
            Console.ReadKey ();
            lvl_3_prt_b_start:
            Console.WriteLine ("PlaceHolder");
            Console.ReadKey ();

        }
    }
}

The problem occurs when running it. The first section works fine, and pressing '1', '2' or any other number works - but on section two, it always comes up with the else section, none of the if or if else clauses. Any quick help would be great. Thanks!

18166
  • 111
  • 1
  • 13
  • Please don't use goto! It's evil! – Fals Dec 05 '14 at 17:33
  • I'm glad you are trying to learn a new language. I would challenge you to not use the goto command ever again. That shouldn't be used. Second, divide you code into methods instead of a mega-method. – Doug Dawson Dec 05 '14 at 17:33
  • I think the problem might be that you're reading twice? In the first one you have Console.ReadKey(); and then you write again, in the next one you read key, and then read the line again. – AdamMc331 Dec 05 '14 at 17:34
  • 2
    @McAdam331 additionally, the second level read uses '.Read()' while the first level uses '.ReadLine()' – dodald Dec 05 '14 at 17:35
  • I think you hit it, @dodald. – Doug Dawson Dec 05 '14 at 17:35
  • @Fals - whats wrong with it? – 18166 Dec 05 '14 at 17:38
  • @DougDawson - Whats wrong with it? What will I use instead? – 18166 Dec 05 '14 at 17:41
  • @18166 Check out this post regarding the goto: http://stackoverflow.com/questions/46586/goto-still-considered-harmful The advice to not use goto extends to almost all high-level languages. – dodald Dec 05 '14 at 17:44
  • Your code is basically doing the same thing over and over again. Think about if you can express logic in code and game content as data so you don't have to repeat code for each encounter. – Brian Rasmussen Dec 05 '14 at 17:59

1 Answers1

2

The problem is that you're calling both ReadKey and Read. These each read a single character, so ReadKey gets your selection, and Read just gets your \r (first character of newline). ReadKey returns info about your keypress (which is ignored), and Read returns an int corresponding to the char value you entered (note that int and char are numeric types, and converting one of those ToInt32 is different from parsing a string!).

You should use code like your working code to read input:

int lvl_2 = Convert.ToInt32(Console.ReadLine()); // or, does the same thing:
int lvl_2 = int.Parse(Console.ReadLine());

(the Convert class supports conversions in a wide range of types; if all you need to do is parse a string to an int, int.Parse is the more common option and gives you more choices)

Also, you should avoid gotos in C#. They lend themselves to writing spaghetti code, and overall are a bad idea (except for very rare exceptions). I can see that they're already leading to your method being a God Method. Here's an example that should put you more on the right track:

static void Main(string[] args)
{
    Level1();
}
static void Level1()
{
    Console.WriteLine("Welcome to AlexAdventureLand.\nPlease press the enter key to start.");
    Console.ReadKey();
    Console.WriteLine("You are a rabbit in a burrow. There is a badger outside. \nPress 1 to leave the burrow. \nPress 2 to wait.");
    int selection = int.Parse(Console.ReadLine());
    if (selection == 1)
    {
        Console.WriteLine("The badger sees you and eats you. \nYou fail. \n\nPress enter to try again.");
        Console.ReadKey();
        Level1();
    }
    else if (selection == 2)
    {
        Console.WriteLine("The badger scurries away!");
        Level2();
    }
    else
    {
        Console.WriteLine("You pressed the wrong button. Start again.");
        Level1();
    }
}
static void Level2()
{
    Console.WriteLine("Now what do you want to do? \nPress 1 to leave burrow. \nPress 2 to wait.");
    int selection = int.Parse(Console.ReadLine());
    if (selection == 1)
    {
        Console.WriteLine("You leave the burrow and look around.");
        Console.ReadKey();
        Level3A();
    }
    else if (selection == 2)
    {
        Console.WriteLine("You wait a little longer. \nYou can hear footsteps around your burrow. You wonder whether it's your friend - troy - or the badger still.");
        Level3B();
    }
    else
    {
        Console.WriteLine("You pressed the wrong button. Start again.");
        Level2();
    }
}
static void Level3A()
{
    Console.WriteLine("PlaceHolder");
    Console.ReadKey();
}
static void Level3B()
{
    Console.WriteLine("PlaceHolder");
    Console.ReadKey();
}
Tim S.
  • 55,448
  • 7
  • 96
  • 122
  • `static` means that the method belongs to the class, not an instance. `void` means that the method doesn't return anything. You can look up these keywords for more detail. – Tim S. Dec 06 '14 at 20:01