Skip to content

Commit

Permalink
Added Final code.
Browse files Browse the repository at this point in the history
Added concise and final code.
  • Loading branch information
juleswhi committed Sep 23, 2023
1 parent ac6f63e commit abb6e86
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 4 deletions.
2 changes: 1 addition & 1 deletion ArrayCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ArrayCode
static bool AnsweredPrompt = false;
static ConsoleKeyInfo CurrentKey;

public static void Main(string[] args)
public static void Run(string[] args)
{
StartTimer();
Console.Clear();
Expand Down
4 changes: 2 additions & 2 deletions ArrayResize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ArrayResize
static ConsoleKeyInfo CurrentKey;


public static void Main(string[] args)
public static void Run(string[] args)
{
Console.Clear();

Expand All @@ -34,7 +34,7 @@ public static void Main(string[] args)
if(CharacterCount < TypedCharacters.Length)
{
TypedCharacters[CharacterCount] = CurrentKey.KeyChar;
ResizeArray();
// ResizeArray();
CharacterCount++;
}

Expand Down
59 changes: 59 additions & 0 deletions Concise.cs
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);
}
}
87 changes: 87 additions & 0 deletions FinalCode.cs
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);
}
}
2 changes: 1 addition & 1 deletion Timer_GCSE_Coursework.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down

0 comments on commit abb6e86

Please sign in to comment.