0

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();

                }
            }
        }
    }
}
DerStarkeBaer
  • 669
  • 8
  • 28
  • Hello and welcome to SO! `I already coded the action that happen when the time is 0, but I failed at coding a timer like this`, where's the code for the timer you've tried, what isn't working? Have you [looked and read](https://learn.microsoft.com/en-us/dotnet/api/system.timers.timer?view=netcore-3.1) the actual document on `System.Timers.Timer` class to see how it works? If not, that would be a great start. If you've tried it and it's not working, please come back and [edit](https://stackoverflow.com/posts/63272663/edit) your question so we can further help you out. – Trevor Aug 05 '20 at 20:02
  • I tried some code, but it completely didn't worked out and so I deleted it. I'm going to take a look at this class document. Thank you :) – Adrian Buchholz Aug 05 '20 at 20:06
  • You're welcome and good luck! If you're still having a specific issue please come back and update your post and we can further help you. – Trevor Aug 05 '20 at 20:07
  • Why not just get the current time, add the amount of time you want to give them to complete the game, and then check the current time against that value inside your loop? It's not a super accurate timer, but it would be a good start. – Rufus L Aug 05 '20 at 20:12

2 Answers2

0

Its a really good start for a one day programmer. Actually the thing you are trying to do is a bit tricky. The Console.ReadLine() freeze the current Thread waiting for the user input, so you can't go deeper in your code. A guy created a utility class that do the job for you >>>there<<< and you can use it in your code like this :

Reader.ReadLine(1000); // for waiting 1 second max

instead of :

Console.ReadLine();

His code is throwing a TimeoutException if the user take more time to respond. You can handle the "timeout" like this:

try {
    var line = Reader.ReadLine(1000);
    Console.WriteLine("user typed : " + line);
}
catch(TimeoutException) {
    Console.WriteLine("user take too much time to answer");
}
Orkad
  • 630
  • 5
  • 15
0

One way to create a rudimentary timer is to capture the current time, add some number of minutes to it, and save this as the endTime. Then you can check if DateTime.Now > endTime, and if it is, then we've passed the end time.

For example:

Console.Write("Enter your first guess to start the clock: ");
string guess = Console.ReadLine();

// Set end time to 2 minutes from now
DateTime endTime = DateTime.Now.AddMinutes(2);

// Continue to get user input while their answer is incorrect and there's still time left
while (guess != rightAnswer && DateTime.Now < endTime)
{
    TimeSpan timeRemaining = endTime - DateTime.Now;

    Console.WriteLine("Incorrect guess. You have " + 
        timeRemaining.Minutes + " minutes and " +
        timeRemaining.Seconds + " seconds left...");

    Console.Write("Enter your guess: ");
    guess = Console.ReadLine();
}

if (guess == rightAnswer && DateTime.Now <= endTime)
{
    Console.WriteLine("You won the Game! The secret word was: " + rightAnswer);
}
else
{
    Console.WriteLine("You are out of time and lost the game.");
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43