forked from harshilp24/Hacktoberfest_2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
138 lines (120 loc) · 4.66 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
namespace SpeedTypingTestConsole
{
class Program
{
private static double average { get; set; } = 0;
private static TimeSpan timeTaken { get; set; }
private static List<string> words = new List<string>();
private static string path = AppDomain.CurrentDomain.BaseDirectory;
private static Random random = new Random();
static void Main(string[] args) => MenuStart();
private static void MenuStart()
{
//Hook up ProcessExit to save average when exiting
AppDomain.CurrentDomain.ProcessExit += new EventHandler(SaveAverage);
//All of this is just so that you can add custom words to this but
//if the file isn't found it will create it with the basic words
try { LoadWordsFile(); }
catch(FileNotFoundException)
{
CreateWordsFile();
LoadWordsFile();
}
//Load average spelling time, ignore if average.txt doesn't exist
try { LoadAverage(); }
catch(FileNotFoundException){}
Console.Clear();
Console.WriteLine("PRESS ANY KEY TO START THE TEST");
Console.WriteLine("AVERAGE TIME TAKEN: {0} SECONDS", average);
Console.ReadKey();
StartTypingTest();
}
private static void NextTestMenu()
{
//Writing over previous to minimize flickering
Console.SetCursorPosition(0,0);
Console.WriteLine("TIME TAKEN ON LAST WORD: {0} SECONDS", Math.Round(timeTaken.TotalSeconds,2));
Console.WriteLine("AVERAGE TIME TAKEN: {0} SECONDS", average);
Console.WriteLine("PRESS SPACE FOR NEXT TEST");
Console.WriteLine("PRESS ENTER TO EXIT");
switch(Console.ReadKey().Key)
{
case ConsoleKey.Enter:
break;
case ConsoleKey.Spacebar:
StartTypingTest();
break;
default:
NextTestMenu();
break;
}
}
private static void StartTypingTest()
{
var timeOfStart = DateTime.Now.ToLocalTime();
char input = ' ';
var typed = new StringBuilder();
var chosenWord = words[random.Next(0, words.Count)];
Console.Clear();
foreach(var letter in chosenWord)
{
DrawTypingText(chosenWord, typed.ToString());
//Takes letters and deletes them from the console
while(letter != input)
{
input = Console.ReadKey().KeyChar;
DrawTypingText(chosenWord, typed.ToString());
}
typed.Append(letter);
}
DrawTypingText(chosenWord, typed.ToString());
timeTaken = DateTime.Now.ToLocalTime() - timeOfStart;
average = average == 0 ? Math.Round(timeTaken.TotalSeconds, 2) : Math.Round((average + timeTaken.TotalSeconds)/2, 2);
Console.Clear();
NextTestMenu();
}
private static void DrawTypingText(string chosenWord, string typed)
{
//Writing over previous to minimize flickering
Console.SetCursorPosition(0,0);
Console.WriteLine("TYPE: {0}", chosenWord);
Console.Write("TYPED: {0}", typed);
}
private static void LoadWordsFile()
{
using(var streamReader = new StreamReader(path+"words.txt"))
{
string line;
while((line = streamReader.ReadLine()) != null)
words.Add(line);
}
}
private static void CreateWordsFile()
{
string[] basicWords = {"unciform", "shaped", "like", "hook", "rare", "repeat", "fix", "shell", "hacktober", "side"};
using(var streamWriter = new StreamWriter(path+"words.txt"))
{
foreach(var word in basicWords)
streamWriter.WriteLine(word);
}
}
private static void LoadAverage()
{
using(var streamReader = new StreamReader(path+"average.txt"))
{
average = double.Parse(streamReader.ReadLine());
}
}
private static void SaveAverage(object sender, EventArgs e)
{
using(var streamWriter = new StreamWriter(path+"average.txt"))
{
streamWriter.WriteLine(average);
}
}
}
}