2 Replies - 1280 Views - Last Post: 16 September 2011 - 12:12 AM Rate Topic: -----

#1 T0xificati0n  Icon User is offline

  • New D.I.C Head

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

Console trivia game problems

Posted 11 September 2011 - 06:42 AM

Hi guys, I have created a trivia game in a console using C#. I created it so that if a user should type '.s' it shows their current score, or if the user types '.c' it shows how many credits they have left.

But the problem I am having is that the console reads these commands as answers, so here is what I want to do, make the console ignore all other text, and only accept the correct answer to the question, and once it reads the correct answer it then displays some text like: "Congratz the correct answer was " + userAnswer"

How do I do this? Here is my code?

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

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;
        private string user = Console.ReadLine();

        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.ToLower();
                        
            if (userAnswer != correctAnswer)
            {
                Console.WriteLine("Incorrect answer. Here is your next question...");
                credit -= 1;
            
            }
            else
            {
                score += 1;
                Console.WriteLine("Congratz the correct answer was " + userAnswer);
                credit -= 1;
            
            }

            string start = Console.ReadLine();

            if (start.StartsWith(".c"))
            {
                Console.WriteLine("You have {0}", credit + " credits remaining.");
            }


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

        
        }
    }




Also I am trying to add a small delay between each question(about 5 seconds) weather the user gets it right or not. Or if the user doesn?t answer correctly in 60 seconds a message should pop up and say something like "Time's up! You next question will appear shortly." And then after that 5 second delay display a new question.

If you guys will help me with this with some sample code or you can implement the code into mine, it would be very appreciated.

Thanks and enjoy your day! :)

This post has been edited by T0xificati0n: 11 September 2011 - 06:46 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Console trivia game problems

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Console trivia game problems

Posted 12 September 2011 - 03:04 PM

Use a switch statement with your userAnswer variable. check the status inputs first and use the default case to handle game input.
Was This Post Helpful? 0
  • +
  • -

#3 blue_elephant  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-September 11

Re: Console trivia game problems

Posted 16 September 2011 - 12:12 AM

I already done this before in my college days. I could only provide you the logic for this. To do this you could use a if else statement. The first if should check if what he typed is the command for showing the score or showing the credit. You just need to make sure that you need to validate what he type first before consider its an answer. For the time you just need to put a variable for it. That everytime the variable is equals to '0' it will stop the whole game and show the message box you wanted.

I created my game before using C# and also it can be played on LAN for multiplayer.

-Thanks

View PostT0xificati0n, on 11 September 2011 - 06:42 AM, said:

Hi guys, I have created a trivia game in a console using C#. I created it so that if a user should type '.s' it shows their current score, or if the user types '.c' it shows how many credits they have left.

But the problem I am having is that the console reads these commands as answers, so here is what I want to do, make the console ignore all other text, and only accept the correct answer to the question, and once it reads the correct answer it then displays some text like: "Congratz the correct answer was " + userAnswer"

How do I do this? Here is my code?

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

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;
        private string user = Console.ReadLine();

        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.ToLower();
                        
            if (userAnswer != correctAnswer)
            {
                Console.WriteLine("Incorrect answer. Here is your next question...");
                credit -= 1;
            
            }
            else
            {
                score += 1;
                Console.WriteLine("Congratz the correct answer was " + userAnswer);
                credit -= 1;
            
            }

            string start = Console.ReadLine();

            if (start.StartsWith(".c"))
            {
                Console.WriteLine("You have {0}", credit + " credits remaining.");
            }


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

        
        }
    }




Also I am trying to add a small delay between each question(about 5 seconds) weather the user gets it right or not. Or if the user doesn?t answer correctly in 60 seconds a message should pop up and say something like "Time's up! You next question will appear shortly." And then after that 5 second delay display a new question.

If you guys will help me with this with some sample code or you can implement the code into mine, it would be very appreciated.

Thanks and enjoy your day! :)

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1