-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
516 lines (463 loc) · 17.4 KB
/
main.cpp
File metadata and controls
516 lines (463 loc) · 17.4 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <random>
#include <algorithm>
#include <thread>
#include <stdio.h>
#include <cctype>
#include <limits>
#include <set>
#include <array>
#include <map>
#include <functional>
#include <signal.h>
#include <unistd.h>
using namespace std::chrono;
#define INCORRECT_PENALTY 3000
#define REPEAT_CYCLE 4
/*
CLASSES
*/
class AddGenerator {
int add1, add2, add3, add4;
std::mt19937 rng;
std::uniform_int_distribution<> distr1, distr2;
public:
AddGenerator() = default;
AddGenerator(int a1, int a2, int a3, int a4) : add1{a1}, add2{a2}, add3{a3}, add4{a4},
rng{std::mt19937(std::random_device()())},
distr1{std::uniform_int_distribution<>(a1, a2)},
distr2{std::uniform_int_distribution<>(a3, a4)} {}
void printRange() {
std::cout << " ranges: (" << add1 << ", " << add2 << ") + " << "(" << add3 << ", " << add4 << ")\n";
}
std::vector<int> generate() {
int n1 = distr1(rng);
int n2 = distr2(rng);
return { n1, n2, n1 + n2 };
}
};
class SubGenerator {
int sub1, sub2, sub3, sub4, subdiff;
std::mt19937 rng;
std::uniform_int_distribution<> distr1, distr2;
public:
SubGenerator() = default;
SubGenerator(int s1, int s2, int s3, int s4, int sd) : sub1{s1}, sub2{s2}, sub3{s3}, sub4{s4}, subdiff{sd},
rng{std::mt19937(std::random_device()())} {
distr2 = std::uniform_int_distribution<>(s3, s4);
}
void printRange() {
std::cout << " ranges: (" << sub1 << ", " << sub2 << ") - " << "(" << sub3 << ", " << sub4 << ")\n";
}
std::vector<int> generate() {
int n2 = distr2(rng);
distr1 = std::uniform_int_distribution<>(std::max(sub1, n2+subdiff), sub2);
int n1 = distr1(rng);
return { n1, n2, n1 - n2 };
}
};
class MulGenerator {
int mul1, mul2, mul3, mul4;
std::mt19937 rng;
std::uniform_int_distribution<> distr1, distr2;
public:
MulGenerator() = default;
MulGenerator(int m1, int m2, int m3, int m4) : mul1{m1}, mul2{m2}, mul3{m3}, mul4{m4},
rng{std::mt19937(std::random_device()())},
distr1{std::uniform_int_distribution<>(m1, m2)},
distr2{std::uniform_int_distribution<>(m3, m4)} {}
void printRange() {
std::cout << " ranges: (" << mul1 << ", " << mul2 << ") X " << "(" << mul3 << ", " << mul4 << ")\n";
}
std::vector<int> generate() {
int n1 = distr1(rng);
int n2 = distr2(rng);
return { n1, n2, n1 * n2 };
}
};
class DivGenerator {
int div1, div2, div3, div4;
std::mt19937 rng;
std::uniform_int_distribution<> distr1, distr2;
public:
DivGenerator() = default;
DivGenerator(int d1, int d2, int d3, int d4) : div1{d1}, div2{d2}, div3{d3}, div4{d4},
rng{std::mt19937(std::random_device()())},
distr1{std::uniform_int_distribution<>(d1, d2)},
distr2{std::uniform_int_distribution<>(d3, d4)} {}
void printRange() {
std::cout << " ranges: (" << div1 << ", " << div2 << ") / " << "(" << div3 << ", " << div4 << ")\n";
}
std::vector<int> generate() {
int n1 = distr1(rng);
int n2 = distr2(rng);
return { n1 * n2, n2, n1 };
}
};
// Singleton class used throughout the program to manage questions and query histories
class Alphadd {
public:
int hash; // Used to determine filenames for data
std::vector<char> ops;
AddGenerator adder;
SubGenerator subber;
MulGenerator muller;
DivGenerator diver;
int time_length;
std::mt19937 rng;
std::uniform_int_distribution<> distr;
bool run_prog;
long long average;
int num_elems;
// Historical data store
// array = { difficulty, firstnum, rightnum, answer, operator(char) }
std::multiset<std::array<int,5>, std::greater<std::array<int,5>>> data;
// Maps firstnum, rightnum, operator to the difficulty
std::map<std::array<int, 3>, int> q_diff;
Alphadd() = default; // Default
Alphadd(int hash, std::vector<char> o, int a1, int a2, int a3, int a4, int s1, int s2, int s3,
int s4, int sd, int m1, int m2, int m3, int m4, int d1, int d2, int d3, int d4, int tlen) : hash{hash}, ops{o},
adder{AddGenerator(a1, a2, a3, a4)}, subber{SubGenerator(s1, s2, s3, s4, sd)},
muller{MulGenerator(m1, m2, m3, m4)}, diver{DivGenerator(d1, d2, d3, d4)}, time_length{tlen},
rng{std::mt19937(std::random_device()())}, distr{std::uniform_int_distribution<>(0, o.size()-1)},
run_prog{true}, average{0}, num_elems{0} { }
char getRandomOperator() {
return ops[distr(rng)];
}
};
/*
FUNCTIONS
*/
// Global variable for singleton class
Alphadd alpha;
// Introduction message
void intro() {
std::cout << "Welcome to Alphadd, an arithmetic training program.\n";
}
// Takes in the input of config.txt and applies it, does error checking as well
void processSettings() {
std::ifstream fin;
fin.open("config.txt");
int op1, op2, op3, op4;
fin >> op1 >> op2 >> op3 >> op4;
int a1, a2, a3, a4;
fin >> a1 >> a2 >> a3 >> a4;
int s1, s2, s3, s4, sdiff;
fin >> s1 >> s2 >> s3 >> s4 >> sdiff;
int m1, m2, m3, m4;
fin >> m1 >> m2 >> m3 >> m4;
int d1, d2, d3, d4;
fin >> d1 >> d2 >> d3 >> d4;
int tlen;
fin >> tlen;
fin.close();
// Input validation
if (!op1 && !op2 && !op3 && !op4) {
std::cout << "Please input at least one possible operation (add, sub, mul, div)\n";
exit(1);
} else if (s1 > s2 || s3 > s4) {
std::cout << "Wrong ranges for subtraction (must be small -> large)\n";
exit(1);
} else if (sdiff < 0) {
std::cout << "Difference for subtraction cannot be negative\n";
exit(1);
} else if (s2 < s3 + sdiff) {
std::cout << "Your second range + the difference is greater than the first range for subtraction\n";
exit(1);
} else if (s2 < s4) {
std::cout << "Your smaller range cannot have a larger number than your large range for subtraction\n";
exit(1);
} else if (s4 + sdiff > s2) {
std::cout << "The large value of your smaller range (RHS) for subtraction will never be reached because of sub difference.\n";
exit(1);
} else if (a1 > a2 || a3 > a4) {
std::cout << "Wrong ranges for addition (must be small -> large)\n";
exit(1);
} else if (m1 > m2 || m3 > m4) {
std::cout << "Wrong ranges for multiplication (must be small -> large)\n";
exit(1);
} else if (d1 > d2 || d3 > d4) {
std::cout << "Wrong ranges for division (must be small -> large)\n";
exit(1);
}
std::vector<int> nums = { op1, op2, op3, op4, a1, a2, a3, a4, s1, s2, s3, s4,
sdiff, m1, m2, m3, m4, d1, d2, d3, d4, tlen };
const int p = 31;
const int m = 1e9+9;
long long hash_val = 0;
long long p_pow = 1;
for (int i : nums) {
hash_val = (hash_val + (i + 1) * p_pow) % m;
p_pow = (p_pow * p) % m;
}
std::vector<char> ops;
if (op1) ops.push_back('+');
if (op2) ops.push_back('-');
if (op3) ops.push_back('*');
if (op4) ops.push_back('/');
alpha = Alphadd(hash_val, ops, a1, a2, a3, a4, s1, s2, s3, s4, sdiff, m1, m2, m3, m4, d1, d2, d3, d4, tlen);
}
// Obtains hashed file from .data folder and adds it into history of user
void processHistory() {
long long avg;
int nume;
int dif, q1, q2, q3, op;
std::string data_file = ".data/" + std::to_string(alpha.hash) + ".txt";
auto in = std::ifstream(data_file.c_str());
// Opens file and populates data and q_diff with the questions to use.
if (in.is_open()) {
in >> avg >> nume;
alpha.average = avg;
alpha.num_elems = nume;
while (in >> dif >> q1 >> q2 >> q3 >> op) {
alpha.data.insert({ dif, q1, q2, q3, op });
alpha.q_diff.insert({ {q1, q2, op}, dif });
}
in.close();
}
}
void writeData() {
std::string data_file = ".data/" + std::to_string(alpha.hash) + ".txt";
auto of = std::ofstream(data_file.c_str());
of << alpha.average << ' ' << alpha.num_elems << '\n';
for (auto it = alpha.data.begin(); it != alpha.data.end(); it++) {
auto ar = *it;
of << ar[0] << ' ' << ar[1] << ' ' << ar[2] << ' ' << ar[3] << ' ' << ar[4] << '\n';
}
}
void shutdown() {
std::cout << "Exiting alphadd...\n";
writeData();
exit(0);
}
// Outer loop to process settings / history
void configure() {
std::cout << "Configuring settings..." << std::endl;
processSettings();
std::cout << "... End configure\n";
std::cout << "Processing historical data..." << std::endl;
processHistory();
std::cout << "... End processing" << std::endl;
}
// Print settings and confirms if settings are correct
void confirm() {
std::cout << "\n====== SETTINGS ======\n\n";
std::cout << "Operations permitted:\n";
for (char c : alpha.ops) {
if (c == '+') {
std::cout << "\n- Addition\n";
alpha.adder.printRange();
} else if (c == '-') {
std::cout << "\n- Subtraction\n";
alpha.subber.printRange();
} else if ( c == '*') {
std::cout << "\n- Multiplication\n";
alpha.muller.printRange();
} else if (c == '/') {
std::cout << "\n- Division\n";
alpha.diver.printRange();
}
}
std::cout << std::endl;
std::cout << "To confirm these settings and start Alphadd, please press ENTER" << std:: endl;
char nl;
std::cin.get(nl);
}
int kbhit(int secs)
{
// timeout structure passed into select
struct timeval tv;
// fd_set passed into select
fd_set fds;
// Set up the timeout. here we can wait for 1 second
tv.tv_sec = secs;
tv.tv_usec = 0;
// Zero out the fd_set - make sure it's pristine
FD_ZERO(&fds);
// Set the FD that we want to read
FD_SET(STDIN_FILENO, &fds); //STDIN_FILENO is 0
// select takes the last file descriptor value + 1 in the fdset to check,
// the fdset for reads, writes, and errors. We are only passing in reads.
// the last parameter is the timeout. select will return if an FD is ready or
// the timeout has occurred
select(STDIN_FILENO+1, &fds, NULL, NULL, &tv);
// return 0 if STDIN is not ready to be read.
return FD_ISSET(STDIN_FILENO, &fds);
}
// Runs the main loop to generate questions and receive input
void run() {
alpha.run_prog = false;
time_point<steady_clock> start_time = steady_clock::now();
int ans = 0, inp = 0;
bool keep_run = true;
bool show_results = true;
bool input_taken = false;
int score = 0;
int ct = 1;
// Keep previous operator and question to not repeat questions
char prevop = '?';
std::vector<int> prevq;
while (duration_cast<seconds>(steady_clock::now() - start_time).count() < alpha.time_length && keep_run) {
char op;
std::vector<int> question;
int difficulty = 0;
bool repeated = false;
if (ct > REPEAT_CYCLE && ct % REPEAT_CYCLE == 0 && alpha.data.size()) {
// Grab the most 'difficult' number from data set
std::set<std::array<int,5>>::iterator it = alpha.data.begin();
difficulty = (*it)[0];
question = { (*it)[1], (*it)[2], (*it)[3] };
op = (char)(*it)[4];
repeated = true;
}
if (!repeated || (op == prevop && question == prevq)) {
// Random generate a set of numbers
op = alpha.getRandomOperator();
difficulty = 0;
if (op == '+') {
question = alpha.adder.generate();
} else if (op == '-') {
question = alpha.subber.generate();
} else if (op == '*') {
question = alpha.muller.generate();
} else if (op == '/') {
question = alpha.diver.generate();
}
auto it = alpha.q_diff.find({ question[0], question[1], (int)op });
if (it != alpha.q_diff.end()) {
difficulty = it->second;
}
}
if (question.size() != 3) {
std::cout << "ERROR: Incorrect number of elements in question!\n";
exit(1);
}
ans = question[2];
std::cout << "\n---- " << question[0] << ' ' << op << ' ' << question[1] << " ----\n\n";
std::cout << " ";
// Start clock to measure the time taken to solve the question
time_point<steady_clock> q_begin = steady_clock::now();
std::string tmp = "-1";
inp = -1;
// Wrong answer, add to database
int incorrect_ct = 0;
while (ans != inp) {
int time_left = alpha.time_length - duration_cast<seconds>(steady_clock::now() - start_time).count();
int has_input = kbhit(time_left);
if (has_input) {
std::cin >> tmp;
input_taken = true;
} else {
//alpha.run_prog = true;
keep_run = false;
break;
}
if (tmp == "r") {
alpha.run_prog = true;
keep_run = false;
show_results = false;
std::cout << "\n==== Restarting ====\n";
break;
} else if (tmp == "q") {
shutdown();
} else if (tmp == "t") {
std::cout << "\n " << alpha.time_length - duration_cast<seconds>(steady_clock::now() - start_time).count() << " seconds left\n\n ";
inp = -1;
continue;
} else if (tmp == "s") {
std::cout << "\n " << "Score: " << score << "\n\n ";
inp = -1;
continue;
}
inp = std::stoi(tmp);
if (ans != inp) {
printf("\a");
std::cout << "\n Incorrect \n\n ";
incorrect_ct++;
} else {
score++;
}
}
if (!keep_run) break;
int q_end = duration_cast<milliseconds>(steady_clock::now() - q_begin).count();
long long avg = alpha.num_elems > 0 ? alpha.average : INT_MAX;
int time_over = avg < q_end ? q_end : 0;
// If difficulty > 0, it means we took this element from the data array
// since we haven't set the difficulty yet unless we found it first
if (difficulty > 0) {
auto it = alpha.data.find({ difficulty, question[0], question[1], question[2], (int)op });
if (it == alpha.data.end()) {
std::cout << "\nCouldn't find data even though difficulty was set...\n";
exit(1);
}
// Lower penalty if fast, otherwise increase it to penalty.
difficulty = std::max((difficulty * 3) / 5, incorrect_ct * INCORRECT_PENALTY + time_over);
alpha.data.erase(it);
// Only insert if greater than average
if (difficulty > avg) {
alpha.data.insert({ difficulty, question[0], question[1], question[2], (int)op });
}
auto it2 = alpha.q_diff.find({ question[0], question[1], (int)op });
if (it2 == alpha.q_diff.end()) {
std::cout << "\nCouldn't find element in qdiff for some reason...\n";
exit(1);
}
if (difficulty > avg) {
it2->second = difficulty;
} else {
alpha.q_diff.erase(it2);
}
} else if (time_over + incorrect_ct * INCORRECT_PENALTY > avg) {
// New question, insert it with penalty.
difficulty = time_over + incorrect_ct * INCORRECT_PENALTY;
alpha.data.insert({ difficulty, question[0], question[1], question[2], (int)op });
alpha.q_diff.insert({ {question[0], question[1], (int)op}, difficulty });
}
// Calibrate scores inside data array
alpha.average = (alpha.average * alpha.num_elems + q_end) / (alpha.num_elems + 1);
alpha.num_elems++;
// Set previous values
prevop = op;
prevq = std::move(question);
ct++;
}
if (!keep_run && !show_results) return;
std::cin.clear();
if (input_taken) {
std::cin.ignore(std::numeric_limits<int>::max(), '\n');
}
std::cout << "\n==== Results ====\n\n";
std::cout << " Score: " << score << "\n\n";
std::cout << " Time: " << alpha.time_length << " seconds\n\n";
std::cout << "Please type q to quit. ENTER to restart." << std::endl;
char nl;
std::cin.get(nl);
if (nl != 'q') {
alpha.run_prog = true;
} else {
alpha.run_prog = false;
}
}
// Writes data when user presses CTRL-C
void sigint_handler(int sigint) {
shutdown();
exit(0);
}
/*
MAIN FUNCTION
*/
int main(int argv, char **argc) {
intro();
configure();
confirm();
signal(SIGINT, sigint_handler);
while(alpha.run_prog) {
run();
writeData();
}
shutdown();
return 0;
}