-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
499 lines (391 loc) · 9.01 KB
/
main.cpp
File metadata and controls
499 lines (391 loc) · 9.01 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
// main.cpp : Defines the entry point for the console application.
/*
written and tested on vs_2017, win7x64
may need stdafx depending on project settings
C++ code that follows some good conventions/practices
except separating classes/funcs into other files.
upwards of ~2.7x faster than v2.6
also utilizes STL
remove console color calls and windows.h to compile on linux
tested on eclipse CDT, ubuntu 18.04LTS
*/
#define MEASURE_TIME
///TODO:
// error handling
// lower coupling
// maybe dont slap inline everywhere
//#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <random>
//#ifdef MEASURE_TIME
//#include <chrono>
#include <windows.h>
//#include <cstdlib>
//#endif
using namespace std;
// lol globals
random_device rando;
mt19937 gen(rando());
vector<char> v_ops;
int total;
int group_total;
int last_total;
bool times_negative1 = false;
bool twenty_side = false;
bool F_DOUBLES = false;
bool F_EXPLODES = true;
//bool F_VERB = 0x4;
string last_statement = "a20+7";
const string DELIMITERS = "+-*/";
const string OPERATION_TYPES = "acdvtACDVT";
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
// console color alias
inline const auto ConColor(int c) {
SetConsoleTextAttribute(hConsole, c);
}
enum OPERATION {
ADV = 0, DISADV, NORMAL, CRIT, TRIPLE, NOOP
};
enum COLOR {
GRAY = 8, DARKBLUE = 9, GREEN = 10, CYAN = 11, RED = 12, MAGENTA = 13, YELLOW = 14, WHITE = 15
};
void print();
void printStats();
inline bool isDelim(char &c) {
return DELIMITERS.find(c) != string::npos;
}
inline bool isDice(string &c) {
return OPERATION_TYPES.find_first_of(c) != string::npos;
}
// generic base class for vector
class unit {
public:
unit(bool tf) : d(tf) {};
virtual string show() { return ""; };
virtual int calc() { return -9999; };
bool d;
};
class modifier : public unit {
public:
modifier(int i) : number(i), unit(false) {};
string show() override {
return "mod_t";
}
int calc() override {
return number;
}
private:
int number;
};
class dice : public unit {
public:
dice(string &input) : count(1), unit(true) {
size_t found = input.find_first_of(OPERATION_TYPES);
char c = input[found];
switch (c) {
// Advantage
case 'a':
case 'A':
op = ADV; break;
// nothing
case 'd':
case 'D':
op = NORMAL; break;
// disadVantage
case 'v':
case 'V':
op = DISADV; break;
// crit
case 'c':
case 'C':
op = CRIT; break;
//triple advantage, super speshiul case
case 't':
case 'T':
op = TRIPLE; break;
default: op = NORMAL;
}
if (input[0] != c)
count = stoi(input.substr(0, found));
sides = stoi(input.substr(found + 1));
}
inline int calc() override {
/// we always return t1
uniform_int_distribution<int> rnd(1, sides);
int t1 = 0, t2 = 0, t3 = 0;
switch (op) {
case ADV:
for (int i = 0; i < count; ++i) {
t1 += rnd(gen);
rnd.reset();
t2 += rnd(gen);
rnd.reset();
}
if (t1 > t2)
break;
t1 = t2;
break;
case NORMAL:
for (int i = 0; i < count; ++i) {
t1 += rnd(gen);
rnd.reset();
}
break;
case DISADV:
for (int i = 0; i < count; ++i) {
t1 += rnd(gen);
rnd.reset();
t2 += rnd(gen);
rnd.reset();
}
if (t1 < t2)
break;
t1 = t2;
break;
case CRIT:
if(F_DOUBLES) // double dice rolling
count *= 2;
else t1 = count*sides; // add max value
if (F_EXPLODES) {
if (sides == 1)
return -1;
int value(0);
for (int i = 0; i < count; ++i) {
value = rnd(gen);
if (value == sides)
--i;
t1 += value;
rnd.reset();
}
} // else no explosions
else {
for (int i = 0; i < count; ++i) {
t1 += rnd(gen);
rnd.reset();
}
}
break;
case TRIPLE:
t1 = 0;
t2 = 0;
t3 = 0;
for (int i = 0; i < count; ++i) {
t1 += rnd(gen);
rnd.reset();
t2 += rnd(gen);
rnd.reset();
t3 += rnd(gen);
rnd.reset();
}
if (t1 > t2 && t1 > t3)
break;
if (t2 > t1 && t2 > t3) {
t1 = t2;
break;
}
t1 = t3;
}
// returning t1, is it a 20?
if (sides == 20) {
twenty_side = true;
if (t1 == 1)
ConColor(RED);
else if (t1 == 20)
ConColor(GREEN);
cout << "--> " << t1;
ConColor(WHITE);
}
return t1;
}
string show() override {
string str = "sides, count: ";
str += to_string(count);
str += ", ";
str += to_string(sides);
return str;
}
private:
int count;
int sides;
OPERATION op;
};
int main()
{
//SetConsoleTextAttribute(hConsole, RED);
//string input = "1d6-1d2+76";
string input = "d8+4*5d6";
string::iterator isi;
vector<unique_ptr<unit>> v_units;
string atom = "";
print();
while (true) {
v_units.clear();
v_ops.clear();
atom.clear();
times_negative1 = false;
cout << "\n:";
cin >> input;
if (input == "clear") {
system("cls");
print();
continue;
}
cout << endl;
if (input == "stats") {
printStats();
continue;
}
else if (input == "doubles") {
F_DOUBLES = !F_DOUBLES;
continue;
}
else if (input == "explodes") {
F_EXPLODES = !F_EXPLODES;
continue;
}
else if (input == "last") {
ConColor(DARKBLUE);
cout << ":" << last_statement << endl;
ConColor(WHITE);
input = last_statement;
}
last_statement = input;
isi = input.begin();
v_ops.push_back('+'); // first
while (isi != input.end()) {
if (isDelim(*isi)) {
v_ops.push_back(*isi);
if (isDice(atom))
v_units.push_back(unique_ptr<dice>(new dice(atom)));
else
v_units.push_back(unique_ptr<modifier>(new modifier(stoi(atom))));
atom.clear();
}
else
atom += *isi;
++isi;
}
// put the last one on the vector
if (isDice(atom))
v_units.push_back(unique_ptr<dice>(new dice(atom)));
else
v_units.push_back(unique_ptr<modifier>(new modifier(stoi(atom))));
total = 0;
group_total = 0;
int value = 0;
int d_count = 0;
int twenty_mod = 0;
//#ifdef MEASURE_TIME
//auto start = chrono::steady_clock::now();
//#endif
vector<char>::iterator itr = v_ops.begin();
for (int i = 0; i < v_units.size(); ++i) {
if (v_units[i]->d) {
++d_count;
if (times_negative1)
group_total *= -1;
times_negative1 = false;
if (d_count > 1) {
if (twenty_side) {
if (twenty_mod > 0)
cout << '+' << twenty_mod;
else if (twenty_mod < 0)
cout << twenty_mod;
ConColor(CYAN);
cout << " ---> " << group_total << "\n\n";
ConColor(WHITE);
}
else
cout << "--| " << group_total << "\n\n";
switch (*itr) {
//case '*': value = v_units[i]->calc(); total += value * group_total; md_group = true; break;
//case '/': if (!group_total) group_total = 1; total /= group_total; break;
case '-': times_negative1 = true;
case '+':
default: total += group_total; break;
} // end.switch
}
twenty_side = false;
group_total = v_units[i]->calc();
++itr;
} //end.isdice
else {
value = v_units[i]->calc();
switch (*itr) {
case '*': cout << "--> " << group_total; group_total *= value; cout << '\n'; break;
case '/': if (!value) value = 1; cout << "--> " << group_total; group_total /= value; cout << '\n'; break;
case '-': value *= -1;
case '+': if (twenty_side) twenty_mod += value;
default: group_total += value; break;
} //end.switch
++itr;
} //end.isnotdice
}
//--------------------------------------------------------------------------------
if (d_count <= 1) {
if (twenty_side) {
if (twenty_mod > 0)
cout << '+' << twenty_mod;
else if (twenty_mod < 0)
cout << twenty_mod;
ConColor(CYAN);
cout << " ---> " << group_total << '\n';
ConColor(WHITE);
}
total = group_total;
}
else {
if (times_negative1)
group_total *= -1;
total += group_total;
if (twenty_side) {
if (twenty_mod > 0)
cout << '+' << twenty_mod;
else if (twenty_mod < 0)
cout << twenty_mod;
ConColor(CYAN);
cout << " ---> " << group_total << "\n\n";
ConColor(WHITE);
}
else
cout << "--| " << group_total << "\n\n";
}
if (!twenty_side) {
ConColor(MAGENTA);
cout << "\n-----> " << total << endl;
ConColor(WHITE);
}
last_total = total;
cout.flush();
// #ifdef MEASURE_TIME
//auto end = chrono::steady_clock::now();
//cout << "Elapsed time = " << chrono::duration_cast<chrono::microseconds>(end - start).count() << " [microsec]" << endl;
// #endif
}
return 0;
}
void print() {
ConColor(DARKBLUE);
cout << "Dice roller v3.0\nNow with STL!\nA is for Advantage\nD is for dice\nV is for DisadVantage\nC is for Crit\n\n";
ConColor(WHITE);
}
void printStats() {
ConColor(GRAY);
cout << "\nF_DOUBLES:\t" << (F_DOUBLES) << '\n'
<< "F_EXPLODES:\t" << (F_EXPLODES) << '\n'
<< "last statement:\t" << last_statement << '\n'
<< "last result:\t" << last_total << endl << endl;
ConColor(WHITE);
}
/*
break string into units
once broken down, units can either be: dice unit or modifier unit, classify them and push onto a vector
once classified, run sequentially to the end
*/
/*
recursive descent for regex
nah, cool but unnecessary
*/