1

So I am making a basic Zork console application games with one move. The doors are randomized each time (not finished yet) and the player has to guess which door to through with only one allowing passage. However to make the user type quicker i want to give the illusion of the walls closing in on them with a countdown timer and if they do no type anything before this reaches zero they are 'killed' however I can't figure out how to add a Countdown timer in but also have the console check for an input from the user regarding moving. Here is my current code and bare in mind it is not finished yet.

using System;
using System.Threading; 

namespace Prog4
{
    class program 
    {
        public static void Main(string[] args)
        {
           Random random = new Random();
           int north = random.Next(1,5);
           int south = random.Next(1,5);
           int east = random.Next(1,5);
           int west = random.Next(1,5);

           Console.WriteLine("You are in a room that is slowly closing in on you, it has only four exits." + Environment.NewLine +
                             "The four exits face: North, South, East and West. " + Environment.NewLine +
                             "Use n for North, s for South, e for East and w for West" + Environment.NewLine +
                             "Only one exit leads to outside, the rest lead to certain doooooooooooooom");
         }
    }
}
Krekkon
  • 1,349
  • 14
  • 35
Bacon
  • 23
  • 4
  • 3
    Play with my answers [here](http://stackoverflow.com/a/16973768/2330053) and [here](http://stackoverflow.com/a/16907575/2330053) for some ideas on how to do this. – Idle_Mind Oct 19 '13 at 13:25

2 Answers2

2

For starting please visit this thread: link After that you can see that.

using System;
using System.Timers;

namespace Prog4
{
    class program
    {
        private static int _countDown = 30; // Seconds
        private static bool waySelected = false;

        static void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            if (waySelected == false)
            {
                if (_countDown-- <= 0)
                    Console.WriteLine("You got crushed!");
                else
                {
                    Console.SetCursorPosition(0, 9);
                    Console.WriteLine(_countDown + " seconds until you are crushed");
                }
            }
        }

        static void Main(string[] args)
        {
            Timer aTimer = new Timer(1000);
            aTimer.Elapsed += OnTimedEvent;
            aTimer.Enabled = true;

            Random random = new Random();
            int north = random.Next(1, 5);
            int south = random.Next(1, 5);
            int east = random.Next(1, 5);
            int west = random.Next(1, 5);

            Console.WriteLine("You are in a room that is slowly closing in on you, it has only four exits.\n" +
                              "The four exits face: North, South, East and West. \n" +
                              "Press n for North, \n" +
                              "      s for South, \n" +
                              "      e for East, \n" +
                              "      w for West, \n" +
                              "Only one exit leads to outside, the rest lead to certain doooooooooooooom \n");

            Console.WriteLine("Press any key to continue...");

            ConsoleKeyInfo way = Console.ReadKey(true);
            waySelected = true;
            Console.Clear();

            switch (way.KeyChar)
            {
                case 'n':
                    Console.WriteLine("The Next room is looks like North ...");
                    break;
                case 's':
                    Console.WriteLine("The Next room is looks like South ...");
                    break;
                case 'e':
                    Console.WriteLine("The Next room is looks like East ...");
                    break;
                case 'w':
                    Console.WriteLine("The Next room is looks like West ...");
                    break;
                default:
                    Console.WriteLine("You choose wrong way, you got crushed!");
                    break;
            }

            Console.ReadKey(true);
        }
    }
}

It is not perfect but it works :)

Community
  • 1
  • 1
Krekkon
  • 1,349
  • 14
  • 35
  • What about if i wanted to make it so the number ticked down after each second? Thanks for the help btw :) – Bacon Oct 19 '13 at 16:53
  • Please read further in this article, Idle_Mind's ansver was enugh as I thnik. You can see the Time elapsed in the rigth upper corner. link (http://stackoverflow.com/questions/16970277/issue-with-multi-threading/16973768#16973768). That is what you need – Krekkon Oct 19 '13 at 16:57
  • This is my code and i believe i have implemented it correctly yet it will not display the code? Nor if i do Console.WriteLine(_countDown) will show a decreasing number. My current code: http://pastebin.com/YidDk527 – Bacon Oct 19 '13 at 23:12
  • That's awesome, thanks! Except it doesn't actually stop when the user inputs but it is better than nothing! Also would there be a way to make it ticks? with maybe Timer:{0} or something along those lines. – Bacon Oct 20 '13 at 00:05
  • I Updated, Ticks and stop in userinput. – Krekkon Oct 20 '13 at 00:06
  • That's great thank you mate! I can finally get on with the rest of it haha! – Bacon Oct 20 '13 at 00:08
  • Do it as you want, that is yours ;) Please write on me if you have some question. And please me as answerer and/or upvote ,) – Krekkon Oct 20 '13 at 00:10
  • If i could upvote i would but alas i am 4 rep short! – Bacon Oct 20 '13 at 00:12
  • Yeah i see now. No problem ;) – Krekkon Oct 20 '13 at 00:15
0

For the actual timer logic I would recommend using Rx. In Visual Studio add the Rx-Main NuGet package and then this will get you a function called every minute:

public static void UpdateTimer(long _)
{
    // do work to update the timer on the console
}

public static void Main()
{
    using (Observable.Interval(TimeSpan.FromSeconds(1)).Subscribe(UpdateTimer))
    {
        // code goes here that blocks while waiting for user input
        // when the using block is left, the UpdateTimer function will stop being called.
    }
}

As for what to put in the UpdateTimer function, that depends on how you want the timer to appear. I suggest following the instructions in the other answers for that.

Micah Zoltu
  • 6,764
  • 5
  • 44
  • 72