I'm a new C# programmer who started yesterday.
I tried to program my own simple guessing game. The user gets a theme and has to guess a secret word in 2 minutes.
I already coded the action that happen when the time is 0, but I failed at coding a timer like this. Basically I need a timer that can set my bool outofTime
which triggers the end to true
.
This is my code, so you can take a look at the things I tried to describe.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace EigenesProjekt
{
class Program
{
private static void Main(string[] args)
{
string userName = ("");
string userAge = ("");
bool confirm = true;
string trueFalse = ("");
Console.BackgroundColor = ConsoleColor.White;
Console.Clear();
Console.ForegroundColor = ConsoleColor.DarkBlue;
Console.WriteLine("Enter your username: ");
userName = Console.ReadLine();
Console.Clear();
Console.WriteLine("Enter your age: ");
userAge = Console.ReadLine();
Console.Clear();
Console.WriteLine("Hello " + userName + ", you're " + userAge + " years old. Is this TRUE or FALSE?");
trueFalse = Console.ReadLine();
MainGame(userName, userAge);
switch (trueFalse)
{
case "TRUE":
confirm = true;
break;
case "FALSE":
confirm = false;
break;
default:
Console.WriteLine("Invalid confirmation. Restart the application and use TRUE or FALSE");
Console.ReadLine();
break;
}
if (confirm)
{
Console.WriteLine("Okay, thanks for confirming your personal information");
Console.ReadLine();
}
else
{
Console.WriteLine("In this case please restart the application and try again");
Console.ReadLine();
}
}
public static void MainGame(string userName, string userAge)
{
string category = "";
string guess = "";
string rightAnswer = "";
bool outofTime = false;
Console.Clear();
Console.ForegroundColor = ConsoleColor.Black;
Console.WriteLine(" username: " + userName + " age: " + userAge);
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.DarkBlue;
Console.WriteLine("Your guessing category is " + category + ". You have 2 Minutes to get the right answer.");
Console.WriteLine("Main thread: starting a timer");
while (guess != rightAnswer)
{
if (outofTime == false && guess != rightAnswer)
{
Console.WriteLine("Wrong answer. Try again");
guess = Console.ReadLine();
}
else if (outofTime == true)
{
Console.WriteLine("You are out of time and lost the game. Try again by restarting the application");
Console.ReadLine();
}
}
while (guess == rightAnswer)
{
if (guess == rightAnswer && outofTime == false)
{
Console.WriteLine("You won the Game! The secret word was: " + rightAnswer);
Console.ReadLine();
}
}
}
}
}