1

I am new to C# and I am trying to build console quiz.

Here is my problem:

For every option removed I have to reduce one option (let's say the total points are 100).

If one option is removed I need to reduce the total points by 25 (i.e now the total points will be 75).

JSON data:

{
  "question": [
    {
      "level": "Easy",
      "cat": "sports",
      "description": "Who is the Highest run getter in 2019",
      "Option1": "Rohit Sharma",
      "Option2": "Virat Kohli",
      "Option3": "Kl Rahul",
      "Option4": "S Dhawan",
      "Answer":"1"
    }]
}

Program:

using System;
using System.Timers;
namespace CProgram
{
    class EasyQuestion
    {
        private string mLevel;
        private string mCat;
        private string mDescription;
        private string mOption1;
        private string mOption2;
        private string mOption3;
        private string mOption4;
        private string mAnswer;

        public string MDescription { get => mDescription;  }
        public string MOption1 { get => mOption1; }
        public string MOption2 { get => mOption2; }
        public string MOption3 { get => mOption3; }
        public string MOption4 { get => mOption4; }
        public string MAnswer { get => mAnswer; }
        public string MLevel { get => mLevel;  }
        public string MCat { get => mCat; }
        public static int sQcount=1;

        public  int sPlayerScore=0;
        public int mNoOfQuesAnswerd=0;

        static Timer questionTimer = new Timer(60000) ;


        private static void QuestionTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
             Console.WriteLine("Time up!");
             System.Console.WriteLine("Lets Move on to Next Question");
                questionTimer.Stop();
        }

        public EasyQuestion(string level,string cat,string description,string Option1,string Option2,string Option3,string Option4,string Answer)
        {
            this.mLevel=level;
            this.mCat=cat;
            this.mDescription=description;
            this.mOption1=Option1;
            this.mOption2=Option2;
            this.mOption3=Option3;
            this.mOption4=Option4;
            this.mAnswer=Answer;
        }

        public EasyQuestion()
        {

        }


        public void AskEasyQues(EasyQuestion easyQuestion)
        {

            System.Console.WriteLine("Here is Your:"+sQcount+" Question:");
            System.Console.WriteLine("***********************************");
            System.Console.WriteLine("Question is of The Category:"+easyQuestion.MCat);
            System.Console.WriteLine("***********************************");
            System.Console.WriteLine(easyQuestion.MDescription);
            System.Console.WriteLine("--------------------------------------");
            System.Console.WriteLine("1:"+easyQuestion.MOption1+"         "+"2:"+easyQuestion.MOption2);
            System.Console.WriteLine();
            System.Console.WriteLine("3:"+easyQuestion.MOption3+"         "+"4:"+easyQuestion.MOption4);
            System.Console.WriteLine();
            questionTimer.Elapsed += QuestionTimer_Elapsed;
            questionTimer.Enabled = true;
            questionTimer.Start();
            System.Console.WriteLine("Enter your Choice:");
            /*for (int a = 60; a >= 0; a--)
            {
                Console.Write("\rGenerating Preview in {0:00}", a);

                System.Threading.Thread.Sleep(1000);
            } */

            string ans=Console.ReadLine();

            if(ans==easyQuestion.MAnswer)
            {
                questionTimer.Stop();
                mNoOfQuesAnswerd++;
                System.Console.WriteLine();
                System.Console.WriteLine("------Well Played Champion!!!!!!-----");
                sPlayerScore=sPlayerScore+100;


            }
            else
            {
                System.Console.WriteLine();

               System.Console.WriteLine("------Wrong Choice Lets Move On--------");
            }
            System.Console.WriteLine();
            System.Console.WriteLine("Press any Key To Continue For Next Question");
            Console.ReadLine();
            System.Console.WriteLine();
            System.Console.WriteLine("----------------------------");
            sQcount=sQcount+1;
            Console.Clear();
        }
    }
}

I have a timer of 60 seconds and I have to remove an option every 15 seconds.

halfer
  • 19,824
  • 17
  • 99
  • 186
Rtn
  • 13
  • 4
  • You're probably going to want to put all your options on a single line, then you can print out a bunch of \b backspace characters and overwrite them with the reduced options – Caius Jard Feb 05 '20 at 17:13
  • Now my total timer will be around 60 seconds at the begining there will be all four options after 15seconds if the player wont enter any option within first 15seconds then one option other than answer should be removed and it goes on like this any idea how to implement with this please help me with code snippets as I am new to C# – Rtn Feb 05 '20 at 17:16
  • You're also really going to struggle with doing this as a console app because while you're waiting for your respondent to type an answer your program is blocked, not executing... Do it as a windows GUI instead – Caius Jard Feb 05 '20 at 17:29

2 Answers2

0

Timers and consoles do not mix that well. Or really at all. In Console usually you go from one Blocking Input request to the next (ReadLine() and ReadKey()), with the odd phase of processing in between.

It is possible to poll input without blocking in a console, but that is a pretty advanced topic. And if you ever need to do that, chances are you should not have a console programm in the first place.

The rest is just clear+reprint or setting the cursor back and overwriting. Personally I prefer the clean+rewrite method for such a case.

Counting of the time can be done with DateTime.Now and .AddSeconds(). But I can only repeat, that with Windows Forms or another GUI this would be incredibly trivial. Would be just adding a timer and setting one button to hidden.

Christopher
  • 9,634
  • 2
  • 17
  • 31
  • Now my total timer will be around 60 seconds at the begining there will be all four options after 15seconds if the player wont enter any option within first 15seconds then one option other than answer should be removed and it goes on like this any idea how to implement with this please help me with code snippets as I am new to C# – Rtn Feb 05 '20 at 17:16
  • @Rtn If you are new to C#, stop trying to do that in console and make it in WindowsForms instead. This would be a uphill battle even for a experienced programmer. We know not to even start with console exactly because such things are unessesarily hard to add. – Christopher Feb 05 '20 at 17:20
  • @Rtn You can do it. But one of Douglas Adams sentences comes to mind: "The technology involved in making anything invisible is so infinitely complex that nine hundred and ninety-nine billion, nine hundred and ninety-nine million, nine hundred and ninety-nine thousand, nine hundred and ninety-nine times out of a trillion it is much simpler and more effective just to take the thing away and do without it." – Christopher Feb 05 '20 at 17:24
  • sir cant i do like this put all 4options into a list and call display method and after 15seconds remove a option from the list and call display method again if i can do like this i need help of code snippet to do this operation. – Rtn Feb 05 '20 at 17:28
  • You can do it but your person cannot answer the question!! Or, you can put the things and show them and wait for them to answer but then your program stops working until they answer, so they get as long as they want to look at it.. – Caius Jard Feb 05 '20 at 17:30
  • @Rtn `DateTime dueTime = DateTime.Now.AddSeconds(15);` followed by a `if(DateTime.Now >= dueTime)` to do the things, set a new dueTime, etc. – Christopher Feb 05 '20 at 17:55
  • @Christoper Sir can u provide with code snippet to do what u told – Rtn Feb 05 '20 at 18:53
0

Here; I wrote this for you to show you why you can't easily do what you're asking:

    static void Main(string[] args)
    {
        string[] answers = new[] { "answer one", "answer two", "answer three", "answer four" };
        Random r = new Random();

        int rightAnswer = 2;

        Console.Write("\r" + string.Join(", ", answers));

        for (int i = 1; i < 60; i++)
        {

            if (i % 15 == 0)
            {
                //randomly remove an answer that is not the right one
                int a = r.Next(answers.Length);
                while (a == rightAnswer || answers[a][0] == ' ') // dont remove the right answer! dont pick an answer that is already blanked
                    a = r.Next(answers.Length);

                answers[a] = new string(' ', answers[a].Length); //replace answer with spaces

                //return to the start of the line and overwrite 
                Console.Write("\r" + string.Join(", ", answers));
            }

            System.Threading.Thread.Sleep(100);

        }

        Console.Write("\nQuit");
    }

It "works" in that it will remove one option every 1.5 seconds (if you want 15, extend the sleep) but the question cannot be answered on the console. As soon as you put a ReadLine() in there to get the answer, the program will halt waiting at that point until the user puts the answer in. You can take this and work out some other super complicated way of getting the answer in, such as opening a listening port and having the user telnet into the program and submit their answer that way etc...

But truly; have a play and see what me and Chris are saying and then do it in a windows GUI

Caius Jard
  • 72,509
  • 5
  • 49
  • 80