14 Replies - 1306 Views - Last Post: 09 September 2011 - 10:43 AM Rate Topic: -----

#1 T0xificati0n   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 03-July 11

Make a console quiz

Posted 08 September 2011 - 01:50 AM

Hello everyone! :)

I want to build a trivia game in a console. I have the questions and answers stored in a .xml file. When the console displays the questions I want it to select a random question, and after the user has entered the right answer, there must be a small time delay (about 5seconds is fine), between the questions, and a message should display, like "you got the answer right!". And if the user gets it right they must be added + 1 to their score, and if they type for example: .s it must display their current score.

If anyone can offer me a sample code of this or a tutorial/website, or anything that will help, it will really be appreciated!!

Thanks and enjoy your day! :)

Is This A Good Question/Topic? 0
  • +

Replies To: Make a console quiz

#2 ragingben   User is offline

  • D.I.C Addict
  • member icon

Reputation: 177
  • View blog
  • Posts: 646
  • Joined: 07-October 08

Re: Make a console quiz

Posted 08 September 2011 - 03:27 AM

Look into the class Random and the Thread.Sleep() method, best place to start is MSDN.

Can you load the XML ok?
Was This Post Helpful? 0
  • +
  • -

#3 T0xificati0n   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 03-July 11

Re: Make a console quiz

Posted 08 September 2011 - 03:32 AM

Yah I can load them, but I don't know how to load them randomly.
Was This Post Helpful? 0
  • +
  • -

#4 ragingben   User is offline

  • D.I.C Addict
  • member icon

Reputation: 177
  • View blog
  • Posts: 646
  • Joined: 07-October 08

Re: Make a console quiz

Posted 08 September 2011 - 03:43 AM

Thats why you should look at this little beauty. I presume you are loading each question and answer pair into an instance of a class you have created for questions? If you load the questions and answers into an array you can then use the random class to generate a random number and then use this as an index to select a question from the array of questions. If you haven't got a class for questions and don't want to make one you could load the questions into one array, and the answers into another and then use the index to look up the correct answer. But you defiently want to use Random to generate a random index for selecting a random question
Was This Post Helpful? 0
  • +
  • -

#5 Sergio Tapia   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Make a console quiz

Posted 08 September 2011 - 08:46 AM

Here's some pseudo code to get you on the right path.

1. Create a class to hold each question: Question.cs.

2. Load a List<Question> variable with all the questions in your XML file.

3. Loop N times and for each iteration, generate a random number from 0 to questionList.Count and save it to a List<int>. Make sure you don't have duplicate numbers!

4. Then it's just a matter of fetching Questions[i], using your generated numbers. :)

If you need any help with any specific part, let us know. We're happy to help.
Was This Post Helpful? 1
  • +
  • -

#6 T0xificati0n   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 03-July 11

Re: Make a console quiz

Posted 08 September 2011 - 08:53 AM

That sounds like a good idea, and thanks for the feedback:) I guess I should have mentioned that I am relatively new to coding with C#, if you would be so kind, will you walk this path with me and assist me as I learn more?

Thanks :)

That sounds like a good idea, and thanks for the feedback:) I guess I should have mentioned that I am relatively new to coding with C#, if you would be so kind, will you walk this path with me and assist me as I learn more?

Thanks :)
Was This Post Helpful? 0
  • +
  • -

#7 Sergio Tapia   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Make a console quiz

Posted 08 September 2011 - 08:53 AM

Which point are you having trouble with?
Was This Post Helpful? 0
  • +
  • -

#8 T0xificati0n   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 03-July 11

Re: Make a console quiz

Posted 08 September 2011 - 09:20 AM

A few things, but here is a code I?m using but it?s just using a txt file instead of a .xml file to read the questions and answers.


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Quiz
{
    class Program
    {
        private static string dataFile = @"C:\quiz2.txt";
        private static Dictionary<string, string> questionArray = new Dictionary<string, string>();
        private static int score = 0;

        static void Main(string[] args)
        {
                 
                  
            var originalLines = File.ReadAllLines ("C:\\quiz2.txt");
            var shuffledLines = originalLines.OrderBy(line => Guid.NewGuid()).ToArray();
            File.WriteAllLines("C:\\quiz2.txt", shuffledLines);

            LoadData();
            StartQuiz();

            Console.WriteLine("Your score is: {0}", score);
            Console.ReadLine();
        }

        private static void LoadData()
        {
            FileInfo file = new FileInfo(dataFile);
            if (file.Exists)
            {
                using (StreamReader reader = new StreamReader(file.FullName))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        string[] splitted = line.Split('=');
                        if (splitted.Length == 2)
                        {
                            questionArray.Add(splitted[0], splitted[1]);
                        }
                    }
                }
            }
        }

        private static void StartQuiz()
        {
            for (int i = 0; i < questionArray.Count; i++)
            {
                AskQuestion(i);
            }
        }

        private static void AskQuestion(int questionIndex)
        {
            string question = questionArray.Keys.ElementAt(questionIndex);
            string correctAnswer = questionArray.Values.ElementAt(questionIndex);
            Console.Write("{0} ", question);
            string userAnswer = Console.ReadLine();

            if (userAnswer != correctAnswer)
            {
                Console.WriteLine("\tWRONG ANSWER!");
                //AskQuestion(questionIndex);
            }
            else
            {
                score += 1;
                Console.WriteLine("Congratz that was the correct answer!!");         
            }
        }

        
    }
}



mod edit:
Use code tags when posting code!
:code:

--Curtis Rutland



But I want it to have a small time delay between each question, and during a question after 30seconds with no answer a message should pop up and say ?Time?s Up!? and go to the next question. Also how would one create a scoring system and write it to a text or xml file as the person gets an answer right?



The contents of the text file I am using is as follows:

Who is created Microsoft? - - - - + - - - - - =bill gates
What is the only fruit which has its seeds on the outside? _ _ _ _ _ _ _ _ _ _ =strawberry
Who is married to Angelina Jolie? - - - - + - - - - =brad pitt
Famous wizard from England - - - - - + - - - - - - =harry potter


Any tips?
Was This Post Helpful? 0
  • +
  • -

#9 Sergio Tapia   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Make a console quiz

Posted 08 September 2011 - 10:14 AM

I'm not at work, but for now:

Try formatting your strings in a different way.

If each line in your .txt file is a question you could have something like this.

Famous wizard with a scare on his head?|Harry Potter
Capital of the United States of America?|Washington, DC.
What is 2+2?|4



Then you could fetch every line in the .txt file, and use the String.Split method on each line using the | character as the delimeter.

I won't provide anymore code unless you ask for it, but try to give it some though and look into the String.Split method. :)
Was This Post Helpful? 0
  • +
  • -

#10 T0xificati0n   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 03-July 11

Re: Make a console quiz

Posted 08 September 2011 - 10:25 AM

Ok and what about the time delay between questions? And tips with that? :)
Was This Post Helpful? 0
  • +
  • -

#11 Sergio Tapia   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Make a console quiz

Posted 08 September 2011 - 11:15 AM

I would focus on this for now. Once you have this part nailed to a T, I'll help you out with the timer (wink wink) part.
Was This Post Helpful? 0
  • +
  • -

#12 T0xificati0n   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 03-July 11

Re: Make a console quiz

Posted 08 September 2011 - 12:44 PM

But won't it be same output as if I just change the ('=') from string[] splitted = line.Split('='); to ('|') to give it this code:

string[] splitted = line.Split('|');

Or do I have to use the String.slpit method?

This post has been edited by T0xificati0n: 08 September 2011 - 12:46 PM

Was This Post Helpful? 0
  • +
  • -

#13 Sergio Tapia   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1258
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Make a console quiz

Posted 08 September 2011 - 01:44 PM

In your code the line variable is a string, so you can call Split on it directly.
Was This Post Helpful? 0
  • +
  • -

#14 T0xificati0n   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 03-July 11

Re: Make a console quiz

Posted 08 September 2011 - 09:46 PM

But the code already has split to it..

String[] splitted = line.Split('|');


Was This Post Helpful? 0
  • +
  • -

#15 T0xificati0n   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 03-July 11

Re: Make a console quiz

Posted 09 September 2011 - 10:43 AM

Well I did some modifications with my trivia code.. Let me know what you think...

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Quiz
{
    class Program
    {
        private static string dataFile = @"C:\quiz2.txt";
        private static Dictionary<string, string> questionArray = new Dictionary<string, string>();
        private static int score = 0;
        private static int credit = 100;
         

        static void Main(string[] args)
        {
            var originalLines = File.ReadAllLines("C:\\quiz2.txt");
            var shuffledLines = originalLines.OrderBy(line => Guid.NewGuid()).ToArray();
            File.WriteAllLines("C:\\quiz2.txt", shuffledLines);
            
            LoadData();

            Console.WriteLine("Please enter a username.");
            string user;
                user = Console.ReadLine();
            Console.WriteLine("Hello " + user + " and welcome to TrivKey, the fast thinking game! Type .h to start.");

            string start = Console.ReadLine();

            if (start.StartsWith(".h"))
            {
                StartQuiz();
            }
        }

        private static void LoadData()
        {
            FileInfo file = new FileInfo(dataFile);
            if (file.Exists)
            {
                using (StreamReader reader = new StreamReader(file.FullName))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        string[] splitted = line.Split('|');
                        if (splitted.Length == 2)
                        {
                            questionArray.Add(splitted[0], splitted[1]);
                        }
                    }
                }
            }
        }

        private static void StartQuiz()
        {
            for (int i = 0; i < questionArray.Count; i++)
            {
                AskQuestion(i);
            }
        }

        private static void AskQuestion(int questionIndex)
        {
            string question = questionArray.Keys.ElementAt(questionIndex);
            string correctAnswer = questionArray.Values.ElementAt(questionIndex);
            Console.Write("{0} ", question);
            string userAnswer = Console.ReadLine();
            userAnswer = userAnswer.ToUpper();


            if (userAnswer != correctAnswer)
            {
                Console.WriteLine("Incorrect answer. Here is your next question...");
                credit -= 1;
            }
            else
            {
                score += 1;
                Console.WriteLine("Congratz that was the correct answer!!");

                credit -= 1;
            }

            string input = Console.ReadLine();
            if (input.StartsWith(".c"))
            {
                Console.WriteLine("You have {0} credits remaining", credit);
            }

            if (input.StartsWith(".s"))
            {
                Console.WriteLine("Your score is: {0}", score);
            }
        }

        
        }
    }

This post has been edited by Curtis Rutland: 09 September 2011 - 11:11 AM
Reason for edit:: use [CODE][/CODE] tags (forward slash, not backslash)

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1