-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added concise and final code.
- Loading branch information
Showing
5 changed files
with
150 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System; | ||
using System.Timers; | ||
|
||
public class Concise { | ||
|
||
static int SecondsToAnswer = 15; | ||
static List<char> TypedCharacters = new(); | ||
static bool HasAnsweredPrompt = false; | ||
static ConsoleKeyInfo CurrentKey; | ||
|
||
public static void Run(string[] args) { | ||
StartTimer(); | ||
|
||
Console.Clear(); | ||
Console.WriteLine("What is the largest country by land mass?"); | ||
|
||
while(!HasAnsweredPrompt) { | ||
CurrentKey = Console.ReadKey(true); | ||
|
||
if (CurrentKey.Key == ConsoleKey.Enter) GetAnswer(); | ||
else if (CurrentKey.Key == ConsoleKey.Escape) return; | ||
else if (CurrentKey.Key == ConsoleKey.Backspace) | ||
{ | ||
if (!TypedCharacters.Any()) continue; | ||
TypedCharacters.RemoveAt(TypedCharacters.Count - 1); | ||
} | ||
|
||
else TypedCharacters.Add(CurrentKey.KeyChar); | ||
|
||
Console.SetCursorPosition(0, 2); | ||
Console.Write("\r" + new string(' ', Console.WindowWidth - 1) + "\r"); | ||
Console.SetCursorPosition(0, 2); | ||
TypedCharacters.ForEach(x => Console.Write(x)); | ||
} | ||
} | ||
|
||
public static void StartTimer() | ||
{ | ||
System.Timers.Timer timer = new System.Timers.Timer(1000); | ||
timer.Elapsed += UpdateTimer; | ||
timer.Enabled = true; | ||
} | ||
|
||
public static void UpdateTimer(object source, ElapsedEventArgs e) { | ||
if (HasAnsweredPrompt) return; | ||
if (SecondsToAnswer-- <= 0) return; | ||
|
||
Console.SetCursorPosition(0, 1); | ||
Console.WriteLine($"{SecondsToAnswer} seconds left!"); | ||
Console.SetCursorPosition(TypedCharacters.Count, 2); | ||
} | ||
|
||
public static void GetAnswer() | ||
{ | ||
Console.Write("Your answer was: "); | ||
TypedCharacters.ForEach(x => Console.Write(x)); | ||
Environment.Exit(0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
| ||
using System; | ||
using System.Timers; | ||
|
||
public class Solution { | ||
|
||
// Create the timer | ||
static System.Timers.Timer timer; | ||
|
||
// Timer Length | ||
static int SecondsToAnswer = 15; | ||
|
||
// Array to store chars | ||
static char[] TypedCharacters = new char[0]; | ||
|
||
static bool HasAnsweredPrompt = false; | ||
static ConsoleKeyInfo CurrentKey; | ||
|
||
public static void Main(string[] args) { | ||
|
||
StartTimer(); | ||
|
||
Console.Clear(); | ||
Console.WriteLine("What is the largest country by land mass?"); | ||
|
||
while(!HasAnsweredPrompt) { | ||
CurrentKey = Console.ReadKey(true); | ||
|
||
if (CurrentKey.Key == ConsoleKey.Enter) GetAnswer(); | ||
|
||
else if (CurrentKey.Key == ConsoleKey.Escape) return; | ||
|
||
else if (CurrentKey.Key == ConsoleKey.Backspace) | ||
{ | ||
if (!TypedCharacters.Any()) continue; | ||
char[] temp = new char[TypedCharacters.Length - 1]; | ||
for(int i = 0; i < TypedCharacters.Length - 1; i++) | ||
temp[i] = TypedCharacters[i]; | ||
TypedCharacters = temp; | ||
} | ||
|
||
else | ||
{ | ||
char[] temp = new char[TypedCharacters.Length + 1]; | ||
for(int i = 0; i < TypedCharacters.Length; i++) | ||
temp[i] = TypedCharacters[i]; | ||
temp[^1] = CurrentKey.KeyChar; | ||
TypedCharacters = temp; | ||
} | ||
|
||
Console.SetCursorPosition(0, 2); | ||
Console.Write("\r" + new string(' ', Console.WindowWidth - 1) + "\r"); | ||
Console.SetCursorPosition(0, 2); | ||
|
||
for(int i = 0; i < TypedCharacters.Length; i++) | ||
Console.Write(TypedCharacters[i]); | ||
} | ||
} | ||
|
||
public static void StartTimer() | ||
{ | ||
timer = new System.Timers.Timer(1000); | ||
timer.Elapsed += UpdateTimer; | ||
timer.Enabled = true; | ||
} | ||
|
||
public static void UpdateTimer(object source, ElapsedEventArgs e) { | ||
if (HasAnsweredPrompt) timer.Enabled = false; | ||
if (SecondsToAnswer-- <= 0) return; | ||
|
||
Console.SetCursorPosition(0, 1); | ||
Console.WriteLine($"{SecondsToAnswer} seconds left!"); | ||
Console.SetCursorPosition(TypedCharacters.Length, 2); | ||
} | ||
|
||
public static void GetAnswer() | ||
{ | ||
HasAnsweredPrompt = true; | ||
Console.Clear(); | ||
Console.Write("Your answer was: "); | ||
for (int i = 0; i < TypedCharacters.Length; i++) | ||
Console.Write(TypedCharacters[i]); | ||
Console.WriteLine(); | ||
Console.WriteLine("You had " + SecondsToAnswer + " seconds left!"); | ||
Environment.Exit(0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters