Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
199 lines (174 loc) · 6.25 KB
/
Copy pathmain.cpp
File metadata and controls
199 lines (174 loc) · 6.25 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <iostream>
#include <fstream>
#include <random>
#include <string>
#include <chrono>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <unistd.h>
#include <sys/types.h>
#ifdef _WIN32
#include <windows.h>
#endif
#ifdef __unix__
#include <pwd.h>
#include <filesystem>
#endif
using namespace std;
vector<string> words;
// For some reason, when using a random device as a seed for a generator on windows, it always gives the same value.
// So, I just give the time as a seed.
mt19937 generator(time(0));
uniform_int_distribution<mt19937::result_type> distribution;
// Grabs random words from the vector "words", and concatenates them together; delimited with a space;
string generateString(int wordCount){
string generated;
for (int i = 0; i < wordCount; ++i){
generated += words[distribution(generator)];
if (i != wordCount - 1){
generated += " ";
}
}
return generated;
}
int main(){
// Clears screen when first ran.
#ifdef _WIN32
system("cls");
#else
cout << "\x1B[2J";
cout << "\x1B[0;0H";
#endif
cout << "Enter the amount of words you want to type: (type \"quotes\" for quotes)\n";
string s = "bruh";
// Makes sure user input is valid.
while (s != "quotes" && (!(s.find_first_not_of("0123456789") == string::npos) || s == "0")){
#ifdef _WIN32
cout << "Please enter a positive non-zero integer.\n";
#else
cout << "\033[0;32mPlease enter a positive non-zero integer.\033[0m\n";
#endif
getline(cin, s, '\n');
}
ifstream wordsFile;
#ifdef _WIN32
char *path = getenv("appdata");
if (s == "quotes"){
strcat(path, "\\quotes.txt");
} else {
strcat(getenv("appdata"), "\\words.txt");
}
#else
struct passwd *pw = getpwuid(getuid());
char *path = pw->pw_dir;
if (s == "quotes"){
strcat(path, "/.local/share/quotes.txt");
} else {
strcat(path, "/.local/share/words.txt");
}
#endif
// Reads file into the vector "words".
wordsFile = ifstream(path);
if (!wordsFile){
if (s == "quotes"){
wordsFile = ifstream("quotes.txt");
} else {
wordsFile = ifstream("words.txt");
}
if (!wordsFile){
#ifdef _WIN32
cerr << "Error: quotes and/or words file is missing. Put \"words.txt\" and \"quotes.txt\" in \%appdata\%\\Roaming\\\n";
#else
cerr << "Error: quotes and/or words file is missing. Put \"words.txt\" and \"quotes.txt\" in ~/.local/share/\n";
#endif
return -1;
} else {
#ifdef _WIN32
// Moving the files in windows doesn't appear to work, but at the very least,
// there is no reason to move them in windows
if (s == "quotes"){
MoveFile("quotes.txt", path);
} else {
MoveFile("words.txt", path);
}
#else
if (s == "quotes"){
filesystem::rename("quotes.txt", path);
} else {
filesystem::rename("words.txt", path);
}
#endif
}
}
if (wordsFile.is_open()){
string line;
while (getline(wordsFile, line)){
words.push_back(line);
}
}
// Initializes random distribution, as before this the bounds are not decided.
distribution = uniform_int_distribution<mt19937::result_type>(0, words.size() - 1);
while (true){
// Grabs the next phrase, and loads it.
string phrase;
if (s == "quotes"){
phrase = words[distribution(generator)];
} else {
phrase = generateString(stoi(s));
}
// Clears screen, then displays text in green.
// (\033 is the escape character, 0;32 means green.)
#ifdef _WIN32
system("cls");
cout << phrase << "\n";
#else
cout << "\x1B[2J";
cout << "\x1B[0;0H";
cout << "\033[0;32m" << phrase << "\033[0m\n";
#endif
// Splits phrase delimited by space, and finds wordcount. (Will be used to rework new typing system)
vector<string> splitPhrase;
istringstream iss(phrase);
string word;
while (getline(iss, word, ' ')){
splitPhrase.push_back(word);
}
int wordCount = splitPhrase.size();
// Starts timer to calculate WPM.
auto startTime = chrono::system_clock::now();
string userInput;
getline(cin, userInput, '\n');
float timeTaken = (chrono::duration_cast<chrono::milliseconds>(chrono::system_clock::now() - startTime).count());
cout << "Time taken: " << timeTaken/1000 << "s\n";
cout << "Raw WPM: " << wordCount/(timeTaken/60000) << "wpm\n";
// Calculates the amount of mistyped characters.
// First, it checks if the size of the phrase typed and the actual phrase are the same, as if they aren't,
// checking each character would segfault due to an out of bounds exception.
// If it is, it adds the difference to the error count, and only check for differences up to the shorter length.
// If it isn't, it just checks each character with the same indicies.
int errors = 0;
if (userInput.size() != phrase.size()){
errors += abs((int)userInput.size() - (int)phrase.size());
for (int i = 0; i <= abs((int)userInput.size() - (int)phrase.size()); ++i){
if (phrase[i] != userInput[i]){
++errors;
}
}
} else {
for (int i = 0; i < (int)phrase.size(); ++i){
if (phrase[i] != userInput[i]){
++errors;
}
}
}
cout << "Mistyped characters: " << errors << "\n";
errors = 0;
// Waits for one second so that the user can actually read the output.
#ifdef _WIN32
Sleep(1000);
#else
usleep(1000000);
#endif
}
}