Skip to content

Instantly share code, notes, and snippets.

@bpmct
Created February 12, 2020 01:33
Show Gist options
  • Save bpmct/2e27dde34afe6e9ce332e7588f159b9d to your computer and use it in GitHub Desktop.
Save bpmct/2e27dde34afe6e9ce332e7588f159b9d to your computer and use it in GitHub Desktop.
woaskdl;s
using System;
using System.Timers;
namespace _3questions_clone
{
class Program
{
static bool bTimeOut = false;
static Timer timeOutTimer;
//Variable to hold question #
static int numQuestion;
static int questionsIndex;
//String with answers
static string[] answers = new string[3] { "black", "42", "What do you mean? African or European swallow?" };
static void Main(string[] args)
{
//global variables
string startGame = "";
//for collecting answers
string userAnswer;
//for playing again
string pAgain = "";
//index based on questons
//Answers
// starting game
start:
//prompts which question they want
do
{
Console.Write("Choose your question (1-3): ");
startGame = Console.ReadLine();
} while (!int.TryParse(startGame, out numQuestion) || (numQuestion < 1 || numQuestion > 3));
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("You have 5 seconds to answer the following question");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine();
//create & start timer for 5 seconds
timeOutTimer = new Timer(5000);
timeOutTimer.Start();
//add the TimesUp function that runs when time runs out
timeOutTimer.Elapsed += new ElapsedEventHandler(TimesUp);
//Reset bTimeOut
bTimeOut = false;
//depending on the number will display which question
Console.ForegroundColor = ConsoleColor.Yellow;
//ask the question depending on the input
if (numQuestion == 1)
{
Console.WriteLine("What is your favorite color?");
}
else if (numQuestion == 2)
{
Console.WriteLine("What is the answer to life, the universe and everything?");
}
else
{
Console.WriteLine("What is the airspeed velocity of an unladen swallow?");
}
//Subtract 1 to get the answers & questions array
questionsIndex = numQuestion - 1;
userAnswer = Console.ReadLine();
//check for the correct answer
if (userAnswer == answers[questionsIndex])
{
Console.WriteLine("Well done!");
}
//Display an error if it is wrong & not timed out yet.
else if (!bTimeOut)
{
Console.WriteLine($"The answer is: {answers[questionsIndex]}");
}
timeOutTimer.Stop();
//if they player wants to play again
do
{
// prompt if they want to play again
Console.Write("Do you want to play again? ");
pAgain = Console.ReadLine();
if (pAgain.ToLower() == "yes")
{
goto start;
}
else if (pAgain.ToLower() == "no")
{
break;
}
} while (true);
}
static void TimesUp(object source, ElapsedEventArgs e)
{
Console.WriteLine("Times Up!");
Console.WriteLine($"The correct answer is: {answers[questionsIndex]}");
Console.Write("Please press enter. ");
// set the bTimeOut flag to quit the game
bTimeOut = true;
// stop the timeOutTimer
timeOutTimer.Stop();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment