Skip to content

Instantly share code, notes, and snippets.

@kristofferremback
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kristofferremback/922ae7131b3d67d3e920 to your computer and use it in GitHub Desktop.
Save kristofferremback/922ae7131b3d67d3e920 to your computer and use it in GitHub Desktop.
// Declaring the integers used for positioning.
protected static int origingalRow;
protected static int originalColoumn;
protected static void WriteAt(string s, int x, int y)
{
// If an exception (error) is thrown insde the catch,
// the program will, in fact, not crash and whatever code inside catch will be run.
try
{
// Sets the position to write on and then writes there.
Console.SetCursorPosition(originalColoumn + x, origingalRow + y);
Console.Write(s);
}
catch (ArgumentOutOfRangeException e)
{
// Clear the Console and write the exception message.
Console.Clear();
Console.WriteLine(e.Message);
}
}
// For the fun of it, let's create a Timer.
protected static Timer timer;
static void Main(string[] args)
{
// Clear screen, then save the top and left coordinates,
// which should be top of the screen at the moment
Console.Clear();
originalColoumn = Console.CursorLeft;
origingalRow = Console.CursorTop;
// Instantiate timer with an interval of 0.2 seconds.
timer = new Timer(200);
// Everytime that interval's been reached, fire off this event.
timer.Elapsed += advancedTimer_Elapsed;
// Enable the timer to start printing stuff.
timer.Enabled = true;
// This one waits for user input, as long as none is given, the progam will go on forever.
// (I.E. a key is not pressed)
Console.ReadKey();
// Will just flash by as the program will be closing.
Console.WriteLine("Terminating the application...");
}
// Create a list of all the letters in the English alphabet
protected static List<string> englishAlphabet = new List<string>()
{
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k","l","m", "n", "o","p","q","r","s","t","u", "v", "x", "y", "z"
};
private static void advancedTimer_Elapsed(object sender, ElapsedEventArgs e)
{
// Instantiate a Random object. Only one, though!
Random random = new Random();
// A letter somewhere from the English alphabet.
string letter = englishAlphabet[random.Next(0, englishAlphabet.Count)];
// Get the coordinates
int x = random.Next(0, 5);
int y = random.Next(0, 5);
// Print the letter at x and y.
WriteAt(letter, x, y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment