re: C# Multiple Choice Quiz VIEW POST

FULL DISCUSSION
 

Hi @kironroy ,

for me it's always great to see people start learning to code. With C# (at least in my opinion) you made a good choice.
Also, I like the idea of learning to code by writing little games. I got into programming when my father showed me how to develop a multiple choice quiz similar to yours. :-)
And showing your code "to the world of experienced developers" is a great move. Of course it can be tricky from time to time, because people on the internet are mean. A lot of them
will tell you what you did wrong, that you chose the wrong language, the wrong computer (of course everybody should develop on a Mac Book Pro) and so on.
But here on dev.to almost everybody is nice. :-) So it's a great place to show your code.

I checked out your repository and played with it. It's a fun game.
The first thing that I noticed when I checked your code, was that you created a class for every question. That is something that you will realize when you dive deeper into
object-orientation. If you think about it, all the questions have the same format. #

They consist of:

  • a question text
  • a set of possible answers
  • an information about which is the correct answer

And that is exactly what defines your Question class. The concrete values of the before-mentioned things should be created "from the outside" of your class.
When I played with the code, the class Question ended up looking something like this:

public class Question
{
    public string QuestionText { get; set; }
    public List<string> Answers { get; set; }
    public string CorrectAnswer { get; set; }

    private int index = 0;

    public void Show()
    {
        Console.WriteLine(QuestionText);

        Console.CursorVisible = false;
        while (true)
        {
            string questionAnswerItems = QuestionScrollFunctionality(Answers);
            if (questionAnswerItems == CorrectAnswer)
            {
                Console.Clear();

                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine(" \n Correct!\n");
                Console.ResetColor();
                break;
            }
            else if (questionAnswerItems != CorrectAnswer)
            {
                Console.Clear();

                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write(" \n Wrong answer");
                Console.Write(" \n Select another choice, then press enter\n\n");
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine(QuestionText);
                Console.ResetColor();
            }
        }
    }

    public string QuestionScrollFunctionality(List<string> items)
    {
        // ... almost nothing was changed here
    }
}

As you can see, there is no more concrete information about the question. We just assume, that the creator of the object (the code that calls new on the class) will
provide the desired information. But still most of the logic stays the same.

So, where does the information come from? For now, I just moved it into the Program.cs inside of the CsharpQuizQuestions method.

private static void CsharpQuizQuestions()
{
    var questions = new List<Question>
    {
        new Question
        {
            QuestionText = "1. When was C# invented?\n",
            Answers = new List<string>
            {
                " 1983",
                " 2002",
                " 2000",
                " 1995",
                " 2012"
            },
            CorrectAnswer = " 2000"
        },
        new Question()
        {
            QuestionText = "2. Yes or No?",
            Answers = new List<string>
            {
                " Yes",
                " No"
            }, 
            CorrectAnswer = " Yes"
        }
    };

    GraphicAtTheStartOfGame.StartTitle();

    foreach (var question in questions)
    {
        question.Show();
    }

    Console.Clear();
}

Here you can see, that you can easily add quetions by creating a new item in the list. No addtional files needed to add questions. After the creation we just use a
for loop to show them one after the other.

Of course there is always room for more optimization. But take one step at a time and keep learning.

One more thing: You can have nice syntax highlighting here on dev.to by adding "csharp" after your opening backticks of your code block. Look here for more details: github.com/adam-p/markdown-here/wi...

Happy Coding!

 

Amazing! Thank you for the detailed response. Yes, I knew the question classes were breaking DRY rules and couldn't figure out how to refactor.

I do work with Mac as well, but my friend encouraged me to learn C# and I really like the Visual Studio environment on Windows and never looked back.

Kind Regards,
Kiron Roy

code of conduct - report abuse