loading...

C# Multiple Choice Quiz

kironroy profile image Kiron Roy Updated on ・2 min read

c sharp logo

C# Multiple Choice Repo
C# Multiple Choice Repl.it (little glitchy)

Below is some of the code


using QuizLibrary;
using System;


namespace CsharpQuizUI
{
    class Program
    {
        // main entry point of this console application

        static void Main(string[] args)
        {
            CsharpQuizQuestions();

            Console.ReadLine();
        }

        private static void CsharpQuizQuestions()
        {
            GraphicAtTheStartOfGame.StartTitle();

            // object initialize  from Class QuestionOne
            // Questions are in separate classes 

            QuestionOne questionOne = new QuestionOne();
            questionOne.Question();

            QuestionTwo questionTwo = new QuestionTwo();
            questionTwo.Question();
             ... 

            Console.Clear();
        }
    }
}

QuestionOne Class (all questions are structured like this)

using System;
using System.Collections.Generic;

namespace QuizLibrary
{
    public class QuestionOne
    {
        private static int index = 0;

        public void Question()
        {
            string question = " 1. When was C# invented?\n";
            Console.WriteLine(question);



            List<string> possibleAnswers = new List<string>()
            {
                " 1983",
                " 2002",
                " 2000",
                " 1995",
                " 2012"

            };

            Console.CursorVisible = false;
            while (true)
            {
                string questionAnswerItems = 
             QuestionScrollFunctionality(possibleAnswers);
                if (questionAnswerItems == " 2000")
                {
                    Console.Clear();

                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine(" \n Correct!\n");
                    Console.ResetColor();

                    QuestionTwo questionTwo = new QuestionTwo();
                    questionTwo.Question();
                }
                else if (questionAnswerItems != " 2000")
                {
                    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(question);
                    Console.ResetColor();


                }

            }

        }

        /// <summary>
        /// Creates scrolling effect
        /// </summary>
        /// <param name="items"></param>
        /// <returns></returns>
        public string QuestionScrollFunctionality(List<string> items)
        {
            for (int i = 0; i < items.Count; i++)
            {
                if (i == index)
                {
                    Console.BackgroundColor = ConsoleColor.DarkGreen;
                    Console.ForegroundColor = ConsoleColor.Yellow;

                    Console.WriteLine(items[i]);
                }
                else
                {
                    Console.WriteLine(items[i]);
                }
                Console.ResetColor();
            }

            ConsoleKeyInfo ckey = Console.ReadKey();

            if (ckey.Key == ConsoleKey.DownArrow)
            {
                if (index == items.Count - 1)
                {
                    //index = 0; //Remove the comment to return to the topmost item in the list
                }
                else { index++; }
            }
            else if (ckey.Key == ConsoleKey.UpArrow)
            {
                if (index <= 0)
                {
                    //index = menuItem.Count - 1; //Remove the comment to return to the item in the bottom of the list
                }
                else { index--; }
            }
            else if (ckey.Key == ConsoleKey.Enter)
            {
                return items[index];
            }
            else
            {
                return "";
            }

            Console.Clear();
            return "";
        }

    }
}

Posted on by:

Discussion

markdown guide
 

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