Question

I am a newbie programmer and I've been trying to deal with this problem for over 2 hours now. What I want the program to do is count seconds and if it gets past the second second (what?), to do something, for example to show "Game Over" or something like that. The problem is, the program doesn't do anything after these 2 seconds have passed. What might the problem be ?

Edit: Ok here is the whole info behind this. The user has to press a key corresponding to the character shown on the screen in 2 seconds. If the user doesn't press the key in 2 seconds or presses a wrong key, than the game has to be over, but it doesn't work as expected lol

Here is the whole code so far (yes I know goto sucks and I will change it with a loop later):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;
using System.Threading;
using System.Diagnostics;

class Game
{
    static void Main()
    {
    Start:
        Console.CursorVisible = false;
        Random rnd = new Random();
        int row = rnd.Next(1, 80);
        int col = rnd.Next(1, 25);
        int chars = rnd.Next(0, 62);
        string lettersAndChars = "";
        lettersAndChars = lettersAndChars.ToUpper();
        Console.SetCursorPosition(row, col);
        if (chars <= 10)
        {
            lettersAndChars += (char)(chars + '0');
        }
        else
        {
            lettersAndChars += (char)(chars + 'A');
        }
        lettersAndChars = lettersAndChars.ToUpper();
        Console.WriteLine(lettersAndChars);
        DateTime endTime = DateTime.Now.AddSeconds(2);
        var keyPress = Console.ReadKey();
        string keyPressString = keyPress.KeyChar.ToString();
        keyPressString = keyPressString.ToUpper();
        if (keyPressString == lettersAndChars && DateTime.Now < endTime)
        {
            Console.Clear();
            goto Start;
        }
        else if (keyPressString != lettersAndChars || DateTime.Now > endTime)
        {
            Console.Clear();
            Console.WriteLine("Game Over");
        }


    }
}
Was it helpful?

Solution

I wouldn't use Timer. You can do the following:

DateTime endTime = DateTime.Now.AddSeconds(2);
while (!Console.KeyAvailable && DateTime.Now < endTime)
    Thread.Sleep(1);
if (Console.KeyAvailable)
{
    var keyPress = Console.ReadKey();
    string keyPressString = keyPress.KeyChar.ToString();
    keyPressString = keyPressString.ToUpper();
    if (keyPressString == lettersAndChars)
    {
        Console.Clear();
        goto Start;
    }
}
Console.Clear();
Console.WriteLine("Game Over");

OTHER TIPS

@Ulugbek Umirov's answer worked! Thanks! Here is how it looks now (I also changed a lot of the code from before):

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Globalization;
    using System.Threading;
    using System.Diagnostics;

    class Game
    {
    static void Main()
    {
        bool isTrue = true;
        while (isTrue == true)
        {
        Console.BufferHeight = 25;
        Console.BufferWidth = 80;
        Console.CursorVisible = false;
        Random rnd = new Random();
        int row = rnd.Next(1, 80);
        int col = rnd.Next(1, 25);
        int numbers = rnd.Next(0,9);
        int chars = rnd.Next(0, 25);
        string lettersAndChars = "";
        Console.SetCursorPosition(row, col);
        int choose = rnd.Next(1,3);
        while (choose == 1)
        {        
            lettersAndChars += (int)(numbers); 
            break;
        }
        while (choose == 2)
        {
            lettersAndChars += (char)(chars + 'A');
            break;
        }
        lettersAndChars = lettersAndChars.ToUpper();
        Console.WriteLine(lettersAndChars);
        DateTime endTime = DateTime.Now.AddSeconds(2);      
        while (!Console.KeyAvailable && DateTime.Now < endTime)
            Thread.Sleep(1);
        if (Console.KeyAvailable)
        {
            var keyPress = Console.ReadKey();
            string keyPressString = keyPress.KeyChar.ToString();
            keyPressString = keyPressString.ToUpper();
            if (keyPressString == lettersAndChars)
            {
                Console.Clear();
                continue;
            }
        }

        Console.Clear();
        int leftOffSet = (Console.WindowWidth / 2) -3;
        int topOffSet = (Console.WindowHeight / 2) -2;
        Console.SetCursorPosition(leftOffSet, topOffSet);
        Console.WriteLine("Game Over");
        leftOffSet = (Console.WindowWidth / 2) - 25;
        topOffSet = (Console.WindowHeight / 2);
        Console.SetCursorPosition(leftOffSet, topOffSet);
        Console.WriteLine("Press \"R\" to start again or \"ESC\" to exit the game...");
        var resOrExit = Console.ReadKey();
        Console.Clear();
        if ((char)resOrExit.Key == 'R')
        {
            continue;
        }
        else if (resOrExit.Key == ConsoleKey.Escape)
        {
            Environment.Exit(0);
        }
        }

    }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top