-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcribulator.cpp
More file actions
executable file
·2447 lines (2061 loc) · 107 KB
/
cribulator.cpp
File metadata and controls
executable file
·2447 lines (2061 loc) · 107 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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Cribulator: A Cribbage Tracker | Written by CodyNS, 2022 -> 2024
#include <iostream>
#include <iomanip>
#include <vector>
#include <fstream>
#include <ctime>
using namespace std;
// CONSTANTS (most important ones)
const string SAVE_FILE_NAME = "cribulator_player_save_data.txt"; // save file for current year
const string SAVE_FILE_LEGACY_NAME = "cribulator_legacy_player_data.txt"; // combined save data for all time before this year
const string BLANK_SAVE_FILE_INPUT_NAME = "test_(blank)_save_data.txt";
const string BLANK_SAVE_FILE_OUTPUT_NAME = "test_save_data_output.txt";
const string VAR_PREFIX = "### ";
const int SCREEN_WIDTH = 140;
const int SCREEN_WIDTH_NARROW = 45;
const int MAX_HEIGHT_VERTICAL_HISTO_BAR = 30;
struct Player {
string name;
string indtAdjstdName;
// These 4 only have > 0 values in the legacy data file. They're from times when I wasn't tracking more than just these.
// Combine them with the rest of this data in the legacy file to get the full legacy totals.
int wins_b4_cribulator = 0;
int cribSum_b4_cribulator = 0; // combined value (Molly + Johnny)
int numHands_b4_cribulator = 0; // combined value (Molly + Johnny)
int macroPts_b4_cribulator = 0;
vector<int> histoCutsForFirstCribAT {vector<int>(13,0)}; // in-struct vector initialization is a bit different...
vector<int> histoCutsForMyCribAT {vector<int>(13,0)}; // aka starter cards
vector<int> histoHandPtsAT {vector<int>(30,0)};
vector<int> histoCribPtsAT {vector<int>(30,0)};
vector<int> histoWinMarginsAT {vector<int>(120,0)};
int numFirstCribsAT = 0;
int numFirstCribsWonAT = 0;
int peggedPtsAt = 0;
vector<int> unusedPtsAT = {0,0,0,0}; // hand, crib, nibs, pegged
int macrogamePtsAT = 0;
int numSessionsPlayedAT = 0;
int numHandsAT = 0;
int numCribsAT = 0;
int gamesPlayedAT = 0;
int winsAT = 0;
int nobsAT = 0;
int FOKsAT = 0; // four-of-a-kinds
int flushesAT = 0;
int superFlushesAT = 0;
vector<int> macrogamePtsByMonth = {vector<int>(12,0)};
vector<int> gamesPlayedByMonth = {vector<int>(12,0)};
vector<int> winsByMonth = {vector<int>(12,0)};
vector<string> cutsForFirstCribToday;
vector<int> histoCutsForMyCribToday {vector<int>(13,0)};
vector<int> histoHandPtsToday {vector<int>(30,0)};
vector<int> histoCribPtsToday {vector<int>(30,0)};
vector<int> winMarginsToday;
int numFirstCribsToday = 0;
int numFirstCribsWonToday = 0;
int peggedPtsToday = 0;
vector<int> unusedPtsToday = {0,0,0,0};
int losingPositionToday = 0;
int macrogamePtsToday = 0;
int numHandsToday = 0;
int numCribsToday = 0;
int gamesPlayedToday = 0;
int winsToday = 0;
int nobsToday = 0;
int FOKsToday = 0;
int flushesToday = 0;
int superFlushesToday = 0;
int handPtsThisGame = 0;
int cribPtsThisGame = 0;
int numNibsThisGame = 0; // x2 to get pts
int peggedPtsThisGame = 0;
vector<int> unusedPtsThisGame = {0,0,0,0};
int numHandsThisGame = 0;
int numCribsThisGame = 0;
};
// CONSTANTS, continued...
const int HAND = 0; // these 4 used for excess point variable indeces
const int CRIB = 1;
const int NIBS = 2;
const int PEGGED = 3;
const bool USER_WANTS_TO_END_SESSION = false;
const bool DEALER_WINS_FROM_NIBS = true;
const bool CRIBBER_WINS = true;
const bool NON_CRIBBER_WINS = true;
const bool SOMEONE_HAS_WON = true;
const string INDENTATION = "\t\t\t"; // BLAH why aren't the following ones capitalized?
const vector<string> TOGGLE_PRESENTATION_STYLE_VARIATIONS = {"tog", "nar", "cen", "disp"};
const vector<string> CORRECTION_VARIATIONS = {"fix", "correct", "undo", "redo", "mistake", "oops", "woops"};
const vector<string> HISTO_VARIATIONS = {"histo", "hist", "histogram"};
const vector<string> WIN_VARIATIONS = {"w", "win"};
const vector<string> NOBS_VARIATIONS = {"n", "nobs", "knobs"};
const vector<string> FOK_VARIATIONS = {"fok", "four", "foak"};
const vector<string> FLUSH_VARIATIONS = {"f", "fl", "flush"};
const vector<string> SUPER_FLUSH_VARIATIONS = {"sf", "super", "superflush", "super flush"};
const vector<string> END_SESSION_VARIATIONS = {"end", "done", "exit", "fin", "quit"};
const vector<string> NUMBERS = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
// GLOBAL VARIABLES (gasp)
bool NARROW_PRESENTATION = true; // flag for toggling presentation style (enter: "toggle" during game)
bool usingBlankSaveData = false; // flips to true if blank save data file is found
bool userWantsToCorrectRound = false; // flag used to trigger the correction mechanism
bool userWantsToCorrect1stCribCut = false;
//bool backupWasJustRestored = false; // another flag part of the correction mechanism
int lastCutCardIndex = -1; // used as part of the correction mechanism. Needed because of where
// the backup snapshot is taken. We need to correct the crib cut data
// for the cribber using this (we -1 for this cut in the cribber's data)
string previousInvalidInput = "";
int previousInvalidInputFix = -1;
// These used to be defined in main() and passed in as parameters, but global scope simplifies the function calls so much.
// For a private, innocent game tracker like this, I just don't care: it's a worthy trade-off.
Player molly;
Player johnny;
vector<Player*> players = {&molly, &johnny};
Player mollyBackup;
Player johnnyBackup;
vector<Player*> playerBackups = {&mollyBackup, &johnnyBackup};
Player mollyLegacy;
Player johnnyLegacy;
vector<Player*> playersLegacy = {&mollyLegacy, &johnnyLegacy};
Player* cribber = NULL;
Player* nonCribber = NULL;
Player* cribberBackup = NULL;
Player* nonCribberBackup = NULL;
Player* playerWhoHadFirstCrib = NULL;
Player* winner = NULL;
time_t now = time(0);
tm *mytime = localtime(&now);
int year = mytime->tm_year + 1900;
int month = mytime->tm_mon; // 0 to 11
int day = mytime->tm_mday;
// ----- FUNCTION PROTOTYPES --------------------------------------------------
void initializeStuff();
bool userWantsToEndSession(string);
bool cutToSeeWhoGetsFirstCrib();
void assignCribTo(char);
void swapCribbers();
void incrementHandCounters();
bool cutTheStarterCard();
bool handleInputFlags(string, Player*);
Player* winHandler(string, int);
int sumOfHistogram(vector<int>);
void printEndOfHandStuff();
void updatePlayerDataAfterGame();
void printEndOfGameStuff();
void printPointsBreakdownForPlayerThisGame(Player*);
void resetThisGameDataForPlayers();
string thisMonthsName();
string abbrevForMonth(int);
void printEndOfSessionStuff();
string percentageString(int, int);
double combinedCribAverage(string);
void performEndOfSessionTasks();
int cardIndexOf(string);
string cardStringForIndex(int);
int pointValueOf(string);
vector<int> scaleHistoValuesToFitScreen(vector<int>, int, int, bool, bool);
void printHistogram(string);
double calcWeightedAvgWeird(double, int, int, int);
void printSummaryMessageAtBottomOfVsHisto(bool, bool);
string multString(int, string);
double computeMeanFromHisto(vector<int>);
int domainOfHisto(vector<int>);
int rangeOfHisto(vector<int>);
vector<int> combinedHistoFrom(vector<vector<int>>);
bool search(string, string);
bool search(string, vector<string>);
Player* determineLoser();
void printCentered(int, string, int);
void printCenteredManual(int, string, int, int, int);
vector<int> parseCommaSeparatedValuesFromString(string);
void loadDataForPlayerFromFile(Player*, fstream*);
void loadSaveDataFromFiles();
void saveATDataToFile();
string getlineMine();
void backupVariables();
void restoreBackupOfVariables();
void undoLastDrawForWhoGets1stCrib();
bool userWantsToCorrect1stCribDraw();
int mystoi(string);
bool handleStatsRequests(string);
// ----- MAIN -----------------------------------------------------------------
int main() {
bool isFirstGame = true;
string userInput;
initializeStuff();
if (NARROW_PRESENTATION) printCenteredManual(3, "* * * MOLLY & JOHNNY'S CRIBULATOR!!! * * *", 3, 45, 44);
else printCentered(3, "* * * * MOLLY & JOHNNY'S CRIBULATOR!!! * * * *", 1);
do { // game loop ------------------------------
if (!userWantsToCorrect1stCribCut)
if ( !isFirstGame) cout << "\nNew game!\n\n";
if (cutToSeeWhoGetsFirstCrib() == USER_WANTS_TO_END_SESSION)
break;
playerWhoHadFirstCrib = cribber;
cribber->numFirstCribsToday++;
cribber->numFirstCribsAT++;
if (userWantsToCorrectRound)
restoreBackupOfVariables();
do { // hand loop -----------------------
if (cutTheStarterCard() == DEALER_WINS_FROM_NIBS) {
winner = winHandler("nibs win", 0);
if (winner != NULL) break; // we have a winner
}
if (userWantsToCorrectRound) {
if (userWantsToCorrect1stCribDraw()) {
cribber->numFirstCribsToday--;
cribber->numFirstCribsAT--;
break;
}
restoreBackupOfVariables();
continue;
}
backupVariables();
//backupWasJustRestored = false; <-- was using this to be efficient, but it was getting tricky so fck it
incrementHandCounters();
do {
cout << nonCribber->indtAdjstdName << "'s hand: ";
userInput = getlineMine();
} while(handleStatsRequests(userInput) );
if (handleInputFlags(userInput, nonCribber) == SOMEONE_HAS_WON) {
if (!search(userInput, NUMBERS))
winner = winHandler("someone won via pegging", 0);
else
winner = winHandler("nonCribber wins via hand", pointValueOf(userInput));
if (winner != NULL) break;
}
if (userWantsToCorrectRound) {
restoreBackupOfVariables();
continue;
}
int ptValue = pointValueOf(userInput);
nonCribber->histoHandPtsAT[ptValue]++;
nonCribber->histoHandPtsToday[ptValue]++;
nonCribber->handPtsThisGame += ptValue;
do {
cout << cribber->indtAdjstdName << "'s hand: ";
userInput = getlineMine();
} while(handleStatsRequests(userInput) );
if (handleInputFlags(userInput, cribber) == CRIBBER_WINS) {
winner = winHandler("cribber wins via hand", pointValueOf(userInput));
if (winner != NULL) break;
}
if (userWantsToCorrectRound) {
restoreBackupOfVariables();
continue;
}
ptValue = pointValueOf(userInput);
cribber->histoHandPtsAT[ptValue]++;
cribber->histoHandPtsToday[ptValue]++;
cribber->handPtsThisGame += ptValue;
do {
cout << cribber->indtAdjstdName << "'s crib: ";
userInput = getlineMine();
} while(handleStatsRequests(userInput) );
if (handleInputFlags(userInput, cribber) == CRIBBER_WINS) {
winner = winHandler("cribber wins via crib", pointValueOf(userInput));
if (winner != NULL) break;
}
if (userWantsToCorrectRound) {
restoreBackupOfVariables();
continue;
}
ptValue = pointValueOf(userInput);
cribber->histoCribPtsAT[ptValue]++;
cribber->histoCribPtsToday[ptValue]++;
cribber->cribPtsThisGame += ptValue;
printEndOfHandStuff();
swapCribbers();
cout << "\n\n" << cribber->name << "'s crib\n";
} while(!search(userInput, END_SESSION_VARIATIONS)); // hand loop
if (!userWantsToCorrect1stCribCut) { // skip if user is correcting the draw to see who gets game's 1st crib
updatePlayerDataAfterGame();
printEndOfGameStuff();
resetThisGameDataForPlayers();
}
isFirstGame = false;
} while(!search(userInput, END_SESSION_VARIATIONS) ); // game loop
performEndOfSessionTasks();
return 0;
}
// ----- FUNCTION DEFINITIONS ------------------------------------------------
void initializeStuff() {
cout << setprecision(2) << fixed; // 2 decimal places for output BLAH: should this be global or in main?
loadSaveDataFromFiles(); // load the players' all-time data
}
bool userWantsToEndSession(string input) {
if (search(input, END_SESSION_VARIATIONS))
return true;
return false;
}
bool cutToSeeWhoGetsFirstCrib() {
// returns false if user indicates they want to quit playing; else true
string mollyCut;
string johnnyCut;
int mollyCardIndex;
int johnnyCardIndex;
do {
cout << " Molly draws a ";
mollyCut = getlineMine();
} while(handleStatsRequests(mollyCut) );
if (userWantsToEndSession(mollyCut))
return false; // quit for today
if (userWantsToCorrectRound)
return true; // will have been flipped in getlineMine() via the user's input if true
mollyCardIndex = cardIndexOf(mollyCut);
do {
cout << "Johnny draws a ";
johnnyCut = getlineMine();
} while(handleStatsRequests(johnnyCut) );
if (userWantsToEndSession(johnnyCut))
return false; // quit for today
if (userWantsToCorrectRound) {
userWantsToCorrectRound = false; // if correction input is entered here, assume user just wants to correct the cut input
return cutToSeeWhoGetsFirstCrib(); // just start again if user wants to correct at this step
}
johnnyCardIndex = cardIndexOf(johnnyCut);
molly.histoCutsForFirstCribAT[mollyCardIndex]++;
molly.cutsForFirstCribToday.push_back(cardStringForIndex(mollyCardIndex));
johnny.histoCutsForFirstCribAT[johnnyCardIndex]++;
johnny.cutsForFirstCribToday.push_back(cardStringForIndex(johnnyCardIndex));
if (mollyCardIndex < johnnyCardIndex)
assignCribTo('M');
else
assignCribTo('J');
cout << endl << cribber->name << " gets first crib!\n";
return true; // yes, we cut and are going to play a game
}
void assignCribTo(char cribberInitial) {
// BLAH these are using hard-coded values... at least use constants at the top of the program
if (cribberInitial == 'm' || cribberInitial == 'M') {
cribber = &molly;
nonCribber = &johnny;
}
else if (cribberInitial == 'j' || cribberInitial == 'J') {
cribber = &johnny;
nonCribber = &molly;
}
else {
cout << "\n\nIncorrect cribberInitial passed to \"assignCribTo\" function: \'"
<< cribberInitial << "\'\nprogram closing..." << endl << endl;
exit(0);
}
}
void swapCribbers() {
Player* temp = cribber;
cribber = nonCribber;
nonCribber = temp;
}
void incrementHandCounters() {
// note: this will track unused hands and cribs, because of where it gets called
cribber->numHandsThisGame++;
cribber->numHandsToday++;
cribber->numHandsAT++;
cribber->numCribsThisGame++;
cribber->numCribsToday++;
cribber->numCribsAT++;
nonCribber->numHandsThisGame++;
nonCribber->numHandsToday++;
nonCribber->numHandsAT++;
}
bool cutTheStarterCard() {
// returns true if the cut results in the cribber winning; false otherwise
// BLAH: refactor the if statement by using a to_upper() function (make one)
string theCut; // aka the "starter card" that is cut
int cutCardIndex;
do {
cout << " the cut: ";
theCut = getlineMine();
} while(handleStatsRequests(theCut) );
if (userWantsToCorrectRound)
return false;
cutCardIndex = cardIndexOf(theCut);
cribber->histoCutsForMyCribToday[cutCardIndex]++;
cribber->histoCutsForMyCribAT[cutCardIndex]++;
lastCutCardIndex = cutCardIndex;
if ( search(theCut, "j") || search(theCut, "J") ||
search(theCut, "w") || search(theCut, "W") ) { // nibs
cribber->numNibsThisGame++;
if (search(theCut, "w") || search(theCut, "W")) // nibs for the win!
return true;
}
return false;
}
bool handleInputFlags(string userInput, Player* player) {
// returns true if the flags indicate a win; false otherwise
// Be careful about the order you check these, especially the ones with single letter variations...
if (search(userInput, FOK_VARIATIONS) ){
player->FOKsToday++;
player->FOKsAT++;
} else if (search(userInput, SUPER_FLUSH_VARIATIONS) ){
player->superFlushesToday++;
player->superFlushesAT++;
} else if (search(userInput, FLUSH_VARIATIONS) && !search(userInput, FOK_VARIATIONS) ){ // don't let 'fok' also count as a flush
player->flushesToday++;
player->flushesAT++;
}
if (search(userInput, NOBS_VARIATIONS) ){
player->nobsToday++;
player->nobsAT++;
}
if (search(userInput, WIN_VARIATIONS) )
return true; // someone has won
return false; // no win indicated
}
Player* winHandler(string winCondition, int ptsInWinningInput) {
// Calculates all the necessary crap when user indicates a win
// returns a pointer to the winning player
// returns a NULL pointer in the event user wants to make a correction to the hand
string userInput; // just going to use this for most of the input and reuse it,
// so I'll have mind the order in which I calculate stuff...
if (search(winCondition, "nibs win") ){
if (NARROW_PRESENTATION)
printCenteredManual(2, "~ ~ ~ " + cribber->name + " wins! ~ ~ ~", 3, SCREEN_WIDTH_NARROW,
string("~ ~ ~ " + cribber->name + " wins! ~ ~ ~").length() );
else
printCentered(1, "~ ~ ~ " + cribber->name + " wins! ~ ~ ~", 3);
cout << "How many points were needed? ";
userInput = getlineMine(); if (userWantsToCorrectRound) return NULL; // user wants to make a correction; no winner rn
cribber->unusedPtsThisGame[NIBS] += (2 - mystoi(userInput) );
cribber->unusedPtsToday[NIBS] += cribber->unusedPtsThisGame[NIBS];
cribber->unusedPtsAT[NIBS] += cribber->unusedPtsThisGame[NIBS];
// calc winner's pegged points for this game
cribber->peggedPtsThisGame = 121 - mystoi(userInput)
- cribber->handPtsThisGame
- cribber->cribPtsThisGame
- ((cribber->numNibsThisGame - 1) * 2); // -1 because we've already incr. numNibsThisGame in cut function
cribber->peggedPtsToday += cribber->peggedPtsThisGame;
cribber->peggedPtsAt += cribber->peggedPtsThisGame;
// record loser's position
cout << " " << nonCribber->indtAdjstdName << "'s pos: "; // nonCribber lost
userInput = getlineMine(); if (userWantsToCorrectRound) return NULL;
nonCribber->losingPositionToday += mystoi(userInput);
// use that to calc the winner's winning margin
cribber->winMarginsToday.push_back(121 - mystoi(userInput));
cribber->histoWinMarginsAT[121 - mystoi(userInput)]++;
// and now calc loser's pegged points for this game
nonCribber->peggedPtsThisGame = mystoi(userInput)
- nonCribber->handPtsThisGame
- nonCribber->cribPtsThisGame
- (nonCribber->numNibsThisGame * 2);
nonCribber->peggedPtsToday += nonCribber->peggedPtsThisGame;
nonCribber->peggedPtsAt += nonCribber->peggedPtsThisGame;
// record the other uncounted and excess points...
// Hmmm, have to make an important decision here:
// Do we count the unused hands and cribs toward their respective stats?
// > Yes, I think we do. That way we can calc. averages properly.
// We'll also keep track of the unused pts so we can subtract and calc
// the "strict points" for a hand when desired
cout << nonCribber->indtAdjstdName << "'s hand: ";
userInput = getlineMine(); if (userWantsToCorrectRound) return NULL;
handleInputFlags(userInput, nonCribber);
nonCribber->handPtsThisGame += mystoi(userInput);
nonCribber->histoHandPtsToday[mystoi(userInput)]++;
nonCribber->histoHandPtsAT[mystoi(userInput)]++;
nonCribber->unusedPtsThisGame[HAND] += mystoi(userInput);
nonCribber->unusedPtsToday[HAND] += mystoi(userInput);
nonCribber->unusedPtsAT[HAND] += mystoi(userInput);
cout << cribber->indtAdjstdName << "'s hand: ";
userInput = getlineMine(); if (userWantsToCorrectRound) return NULL;
handleInputFlags(userInput, cribber);
cribber->handPtsThisGame += mystoi(userInput);
cribber->histoHandPtsToday[mystoi(userInput)]++;
cribber->histoHandPtsAT[mystoi(userInput)]++;
cribber->unusedPtsThisGame[HAND] += mystoi(userInput);
cribber->unusedPtsToday[HAND] += mystoi(userInput);
cribber->unusedPtsAT[HAND] += mystoi(userInput);
cout << cribber->indtAdjstdName << "'s crib: ";
userInput = getlineMine(); if (userWantsToCorrectRound) return NULL;
handleInputFlags(userInput, cribber);
cribber->cribPtsThisGame += mystoi(userInput);
cribber->histoCribPtsToday[mystoi(userInput)]++;
cribber->histoCribPtsAT[mystoi(userInput)]++;
cribber->unusedPtsThisGame[CRIB] += mystoi(userInput);
cribber->unusedPtsToday[CRIB] += mystoi(userInput);
cribber->unusedPtsAT[CRIB] += mystoi(userInput);
return cribber;
}
else if (search(winCondition, "someone won via pegging") ){
// REFACTOR: there is SO much duplication of the above, below; there has to be a better way of doing this
// can probably use *winner and *loser Player pointers to cut down on the code
cout << "Who won? ";
userInput = getlineMine(); if (userWantsToCorrectRound) return NULL;
cout << "How many points were needed? ";
string ptsNeeded;
ptsNeeded = getlineMine(); if (userWantsToCorrectRound) return NULL;
cout << "How many points would have been pegged if there were space? ";
string peggedHypothetically;
peggedHypothetically = getlineMine(); if (userWantsToCorrectRound) return NULL;
if (toupper(cribber->name[0]) == toupper(userInput[0]) ) { // cribber won
cribber->unusedPtsThisGame[PEGGED] += (mystoi(peggedHypothetically) - mystoi(ptsNeeded) );
cribber->unusedPtsToday[PEGGED] += cribber->unusedPtsThisGame[PEGGED];
cribber->unusedPtsAT[PEGGED] += cribber->unusedPtsThisGame[PEGGED];
// calc winner's pegged points for this game
cribber->peggedPtsThisGame = 121 - cribber->handPtsThisGame
- cribber->cribPtsThisGame
- (cribber->numNibsThisGame * 2)
+ cribber->unusedPtsThisGame[PEGGED]; // have to include these to be consistent with the other point variables (they all store the macrogame data, not strict game data)
cribber->peggedPtsToday += cribber->peggedPtsThisGame;
cribber->peggedPtsAt += cribber->peggedPtsThisGame;
// record loser's position
cout << " " << nonCribber->indtAdjstdName << "'s pos: "; // nonCribber lost
userInput = getlineMine(); if (userWantsToCorrectRound) return NULL;
nonCribber->losingPositionToday += mystoi(userInput);
// use that to calc the winner's winning margin
cribber->winMarginsToday.push_back(121 - mystoi(userInput));
cribber->histoWinMarginsAT[121 - mystoi(userInput)]++;
// and now calc loser's pegged points for this game
nonCribber->peggedPtsThisGame = mystoi(userInput)
- nonCribber->handPtsThisGame
- nonCribber->cribPtsThisGame
- (nonCribber->numNibsThisGame * 2);
nonCribber->peggedPtsToday += nonCribber->peggedPtsThisGame;
nonCribber->peggedPtsAt += nonCribber->peggedPtsThisGame;
// record the other uncounted and excess points...
cout << nonCribber->indtAdjstdName << "'s hand: ";
userInput = getlineMine(); if (userWantsToCorrectRound) return NULL;
handleInputFlags(userInput, nonCribber);
nonCribber->handPtsThisGame += mystoi(userInput);
nonCribber->histoHandPtsToday[mystoi(userInput)]++;
nonCribber->histoHandPtsAT[mystoi(userInput)]++;
nonCribber->unusedPtsThisGame[HAND] += mystoi(userInput);
nonCribber->unusedPtsToday[HAND] += mystoi(userInput);
nonCribber->unusedPtsAT[HAND] += mystoi(userInput);
cout << cribber->indtAdjstdName << "'s hand: ";
userInput = getlineMine(); if (userWantsToCorrectRound) return NULL;
handleInputFlags(userInput, cribber);
cribber->handPtsThisGame += mystoi(userInput);
cribber->histoHandPtsToday[mystoi(userInput)]++;
cribber->histoHandPtsAT[mystoi(userInput)]++;
cribber->unusedPtsThisGame[HAND] += mystoi(userInput);
cribber->unusedPtsToday[HAND] += mystoi(userInput);
cribber->unusedPtsAT[HAND] += mystoi(userInput);
cout << cribber->indtAdjstdName << "'s crib: ";
userInput = getlineMine(); if (userWantsToCorrectRound) return NULL;
handleInputFlags(userInput, cribber);
cribber->cribPtsThisGame += mystoi(userInput);
cribber->histoCribPtsToday[mystoi(userInput)]++;
cribber->histoCribPtsAT[mystoi(userInput)]++;
cribber->unusedPtsThisGame[CRIB] += mystoi(userInput);
cribber->unusedPtsToday[CRIB] += mystoi(userInput);
cribber->unusedPtsAT[CRIB] += mystoi(userInput);
return cribber;
}
else { // nonCribber won
nonCribber->unusedPtsThisGame[PEGGED] += (mystoi(peggedHypothetically) - mystoi(ptsNeeded) );
nonCribber->unusedPtsToday[PEGGED] += nonCribber->unusedPtsThisGame[PEGGED];
nonCribber->unusedPtsAT[PEGGED] += nonCribber->unusedPtsThisGame[PEGGED];
// calc winner's pegged points for this game
nonCribber->peggedPtsThisGame = 121 - nonCribber->handPtsThisGame
- nonCribber->cribPtsThisGame
- (nonCribber->numNibsThisGame * 2)
+ nonCribber->unusedPtsThisGame[PEGGED]; // (for same reason given above in 'if' case)
nonCribber->peggedPtsToday += nonCribber->peggedPtsThisGame;
nonCribber->peggedPtsAt += nonCribber->peggedPtsThisGame;
// record loser's position
cout << " " << cribber->indtAdjstdName << "'s pos: "; // cribber lost
userInput = getlineMine(); if (userWantsToCorrectRound) return NULL;
cribber->losingPositionToday += mystoi(userInput);
// use that to calc the winner's winning margin
nonCribber->winMarginsToday.push_back(121 - mystoi(userInput));
nonCribber->histoWinMarginsAT[121 - mystoi(userInput)]++;
// and now calc loser's pegged points for this game
cribber->peggedPtsThisGame = mystoi(userInput)
- cribber->handPtsThisGame
- cribber->cribPtsThisGame
- (cribber->numNibsThisGame * 2);
cribber->peggedPtsToday += cribber->peggedPtsThisGame;
cribber->peggedPtsAt += cribber->peggedPtsThisGame;
// record the other uncounted and excess points...
cout << nonCribber->indtAdjstdName << "'s hand: ";
userInput = getlineMine(); if (userWantsToCorrectRound) return NULL;
handleInputFlags(userInput, nonCribber);
nonCribber->handPtsThisGame += mystoi(userInput);
nonCribber->histoHandPtsToday[mystoi(userInput)]++;
nonCribber->histoHandPtsAT[mystoi(userInput)]++;
nonCribber->unusedPtsThisGame[HAND] += mystoi(userInput);
nonCribber->unusedPtsToday[HAND] += mystoi(userInput);
nonCribber->unusedPtsAT[HAND] += mystoi(userInput);
cout << cribber->indtAdjstdName << "'s hand: ";
userInput = getlineMine(); if (userWantsToCorrectRound) return NULL;
handleInputFlags(userInput, cribber);
cribber->handPtsThisGame += mystoi(userInput);
cribber->histoHandPtsToday[mystoi(userInput)]++;
cribber->histoHandPtsAT[mystoi(userInput)]++;
cribber->unusedPtsThisGame[HAND] += mystoi(userInput);
cribber->unusedPtsToday[HAND] += mystoi(userInput);
cribber->unusedPtsAT[HAND] += mystoi(userInput);
cout << cribber->indtAdjstdName << "'s crib: ";
userInput = getlineMine(); if (userWantsToCorrectRound) return NULL;
handleInputFlags(userInput, cribber);
cribber->cribPtsThisGame += mystoi(userInput);
cribber->histoCribPtsToday[mystoi(userInput)]++;
cribber->histoCribPtsAT[mystoi(userInput)]++;
cribber->unusedPtsThisGame[CRIB] += mystoi(userInput);
cribber->unusedPtsToday[CRIB] += mystoi(userInput);
cribber->unusedPtsAT[CRIB] += mystoi(userInput);
return nonCribber;
}
} // someone won via pegging
else if (search(winCondition, "nonCribber wins via hand") ){
if (NARROW_PRESENTATION)
printCenteredManual(2, "~ ~ ~ " + nonCribber->name + " wins! ~ ~ ~", 3, SCREEN_WIDTH_NARROW,
string("~ ~ ~ " + nonCribber->name + " wins! ~ ~ ~").length());
else
printCentered(1, "~ ~ ~ " + nonCribber->name + " wins! ~ ~ ~", 3);
cout << "How many points were needed? ";
userInput = getlineMine(); if (userWantsToCorrectRound) return NULL;
nonCribber->unusedPtsThisGame[HAND] += (ptsInWinningInput - mystoi(userInput) );
nonCribber->unusedPtsToday[HAND] += nonCribber->unusedPtsThisGame[HAND];
nonCribber->unusedPtsAT[HAND] += nonCribber->unusedPtsThisGame[HAND];
// calc winner's pegged points for this game
nonCribber->peggedPtsThisGame = 121 - mystoi(userInput)
- nonCribber->handPtsThisGame
- nonCribber->cribPtsThisGame
- (nonCribber->numNibsThisGame * 2);
nonCribber->peggedPtsToday += nonCribber->peggedPtsThisGame;
nonCribber->peggedPtsAt += nonCribber->peggedPtsThisGame;
// record loser's position
cout << " " << cribber->indtAdjstdName << "'s pos: "; // cribber lost
userInput = getlineMine(); if (userWantsToCorrectRound) return NULL;
cribber->losingPositionToday += mystoi(userInput);
// use that to calc the winner's winning margin
nonCribber->winMarginsToday.push_back(121 - mystoi(userInput));
nonCribber->histoWinMarginsAT[121 - mystoi(userInput)]++;
// and now calc loser's pegged points for this game
cribber->peggedPtsThisGame = mystoi(userInput)
- cribber->handPtsThisGame
- cribber->cribPtsThisGame
- (cribber->numNibsThisGame * 2);
cribber->peggedPtsToday += cribber->peggedPtsThisGame;
cribber->peggedPtsAt += cribber->peggedPtsThisGame;
// record the other uncounted and excess points...
nonCribber->handPtsThisGame += ptsInWinningInput;
nonCribber->histoHandPtsToday[ptsInWinningInput]++;
nonCribber->histoHandPtsAT[ptsInWinningInput]++;
cout << cribber->indtAdjstdName << "'s hand: ";
userInput = getlineMine(); if (userWantsToCorrectRound) return NULL;
handleInputFlags(userInput, cribber);
cribber->handPtsThisGame += mystoi(userInput);
cribber->histoHandPtsToday[mystoi(userInput)]++;
cribber->histoHandPtsAT[mystoi(userInput)]++;
cribber->unusedPtsThisGame[HAND] += mystoi(userInput);
cribber->unusedPtsToday[HAND] += mystoi(userInput);
cribber->unusedPtsAT[HAND] += mystoi(userInput);
cout << cribber->indtAdjstdName << "'s crib: ";
userInput = getlineMine(); if (userWantsToCorrectRound) return NULL;
handleInputFlags(userInput, cribber);
cribber->cribPtsThisGame += mystoi(userInput);
cribber->histoCribPtsToday[mystoi(userInput)]++;
cribber->histoCribPtsAT[mystoi(userInput)]++;
cribber->unusedPtsThisGame[CRIB] += mystoi(userInput);
cribber->unusedPtsToday[CRIB] += mystoi(userInput);
cribber->unusedPtsAT[CRIB] += mystoi(userInput);
return nonCribber;
}
else if (search(winCondition, "cribber wins via hand") ){
if (NARROW_PRESENTATION)
printCenteredManual(2, "~ ~ ~ " + cribber->name + " wins! ~ ~ ~", 3, SCREEN_WIDTH_NARROW,
string("~ ~ ~ " + cribber->name + " wins! ~ ~ ~").length());
else
printCentered(1, "~ ~ ~ " + cribber->name + " wins! ~ ~ ~", 3);
cout << "How many points were needed? ";
userInput = getlineMine(); if (userWantsToCorrectRound) return NULL;
cribber->unusedPtsThisGame[HAND] += (ptsInWinningInput - mystoi(userInput) );
cribber->unusedPtsToday[HAND] += cribber->unusedPtsThisGame[HAND];
cribber->unusedPtsAT[HAND] += cribber->unusedPtsThisGame[HAND];
// calc winner's pegged points for this game
cribber->peggedPtsThisGame = 121 - mystoi(userInput)
- cribber->handPtsThisGame
- cribber->cribPtsThisGame
- (cribber->numNibsThisGame * 2);
cribber->peggedPtsToday += cribber->peggedPtsThisGame;
cribber->peggedPtsAt += cribber->peggedPtsThisGame;
// record loser's position
cout << " " << nonCribber->indtAdjstdName << "'s pos: "; // nonCribber lost
userInput = getlineMine(); if (userWantsToCorrectRound) return NULL;
nonCribber->losingPositionToday += mystoi(userInput);
// use that to calc the winner's winning margin
cribber->winMarginsToday.push_back(121 - mystoi(userInput));
cribber->histoWinMarginsAT[121 - mystoi(userInput)]++;
// and now calc loser's pegged points for this game
nonCribber->peggedPtsThisGame = mystoi(userInput)
- nonCribber->handPtsThisGame
- nonCribber->cribPtsThisGame
- (nonCribber->numNibsThisGame * 2);
nonCribber->peggedPtsToday += nonCribber->peggedPtsThisGame;
nonCribber->peggedPtsAt += nonCribber->peggedPtsThisGame;
// record the other uncounted and excess points...
cribber->handPtsThisGame += ptsInWinningInput;
cribber->histoHandPtsToday[ptsInWinningInput]++;
cribber->histoHandPtsAT[ptsInWinningInput]++;
cout << cribber->indtAdjstdName << "'s crib: ";
userInput = getlineMine(); if (userWantsToCorrectRound) return NULL;
handleInputFlags(userInput, cribber);
cribber->cribPtsThisGame += mystoi(userInput);
cribber->histoCribPtsToday[mystoi(userInput)]++;
cribber->histoCribPtsAT[mystoi(userInput)]++;
cribber->unusedPtsThisGame[CRIB] += mystoi(userInput);
cribber->unusedPtsToday[CRIB] += mystoi(userInput);
cribber->unusedPtsAT[CRIB] += mystoi(userInput);
return cribber;
}
else if (search(winCondition, "cribber wins via crib") ){
if (NARROW_PRESENTATION)
printCenteredManual(2, "~ ~ ~ " + cribber->name + " wins! ~ ~ ~", 3, SCREEN_WIDTH_NARROW,
string("~ ~ ~ " + cribber->name + " wins! ~ ~ ~").length());
else
printCentered(1, "~ ~ ~ " + cribber->name + " wins! ~ ~ ~", 3);
cout << "How many points were needed? ";
userInput = getlineMine(); if (userWantsToCorrectRound) return NULL;
cribber->unusedPtsThisGame[CRIB] += (ptsInWinningInput - mystoi(userInput) );
cribber->unusedPtsToday[CRIB] += cribber->unusedPtsThisGame[CRIB];
cribber->unusedPtsAT[CRIB] += cribber->unusedPtsThisGame[CRIB];
// calc winner's pegged points for this game
cribber->peggedPtsThisGame = 121 - mystoi(userInput)
- cribber->handPtsThisGame
- cribber->cribPtsThisGame
- (cribber->numNibsThisGame * 2);
cribber->peggedPtsToday += cribber->peggedPtsThisGame;
cribber->peggedPtsAt += cribber->peggedPtsThisGame;
// record loser's position
cout << " " << nonCribber->indtAdjstdName << "'s pos: "; // nonCribber lost
userInput = getlineMine(); if (userWantsToCorrectRound) return NULL;
nonCribber->losingPositionToday += mystoi(userInput);
// use that to calc the winner's winning margin
cribber->winMarginsToday.push_back(121 - mystoi(userInput));
cribber->histoWinMarginsAT[121 - mystoi(userInput)]++; // check the indexing on this... hmmm.
// and now calc loser's pegged points for this game
nonCribber->peggedPtsThisGame = mystoi(userInput)
- nonCribber->handPtsThisGame
- nonCribber->cribPtsThisGame
- (nonCribber->numNibsThisGame * 2);
nonCribber->peggedPtsToday += nonCribber->peggedPtsThisGame;
nonCribber->peggedPtsAt += nonCribber->peggedPtsThisGame;
// record the other uncounted and excess points...
cribber->cribPtsThisGame += ptsInWinningInput;
cribber->histoCribPtsToday[ptsInWinningInput]++;
cribber->histoCribPtsAT[ptsInWinningInput]++;
return cribber;
}
else {
cout << "\n\n*** Error: 'else' statement reached in winHandler() ***\n";
cout << " program closing...\n\n\n";
exit(1);
}
}
int sumOfHistogram(vector<int> histo) {
int sum = 0;
for (int i = 0; i < histo.size(); i++)
sum += i * histo[i];
return sum;
}
void printEndOfHandStuff() {
// want it to be somethng like this for ex:
// 4.63 this game 5.46 today (33) 4.41 all-time (2034)
cout << "\n crib avg: ";
cout << ((players[0]->cribPtsThisGame + players[1]->cribPtsThisGame) * 1.0) /
(players[0]->numHandsThisGame * 1.0)
<< " this game "
<< combinedCribAverage("today") << " today (" << players[0]->numHandsToday << ") "
<< combinedCribAverage("year") << " year (" << players[0]->numHandsAT << ")";
}
void updatePlayerDataAfterGame() {
int macrogamePtsThisGame; // have to update this at game's end and not every hand because we need
// to have pegged points calculated first, and that only happens at game's end
for (Player* p : players) {
macrogamePtsThisGame = p->handPtsThisGame
+ p->cribPtsThisGame
+ (p->numNibsThisGame * 2)
+ p->peggedPtsThisGame;
p->macrogamePtsToday += macrogamePtsThisGame;
p->macrogamePtsAT += macrogamePtsThisGame;
p->macrogamePtsByMonth[month] += macrogamePtsThisGame;
p->gamesPlayedToday++;
p->gamesPlayedAT++;
p->gamesPlayedByMonth[month]++;
}
winner->winsToday++;
winner->winsAT++;
winner->winsByMonth[month]++;
if (winner->name == playerWhoHadFirstCrib->name) {
winner->numFirstCribsWonToday++;
winner->numFirstCribsWonAT++;
}
}
void printEndOfGameStuff() {
// calculate and display the stats you want to at the end of a game:
if (NARROW_PRESENTATION)
printCenteredManual(2, "Congratulations " + winner->name + "!", 1, SCREEN_WIDTH_NARROW,
string("Congratulations " + winner->name + "!").length());
else
printCentered(1, "Congratulations " + winner->name + "!", 1);
string winWord;
if (winner->winsToday == 1)
winWord = "1st";
else if (winner->winsToday == 2)
winWord = "2nd";
else if (winner->winsToday == 3)
winWord = "3rd";
else
winWord = "" + to_string(winner->winsToday) + "th";
if (NARROW_PRESENTATION)
printCenteredManual(0, "That's your " + winWord + " win today!", 4,
SCREEN_WIDTH_NARROW, string("That's your " + winWord + " win today!").length());
else
printCentered(0, "That's your " + winWord + " win today!", 3);
if (NARROW_PRESENTATION)
printCenteredManual(0, "Game " + to_string(players[0]->gamesPlayedToday) + " stats:", 2, SCREEN_WIDTH_NARROW,
string("Game " + to_string(players[0]->gamesPlayedToday) + " stats:").length());
else
printCentered(0, "Game " + to_string(players[0]->gamesPlayedToday) + " stats:", 2);
// points breakdown for the winner
printPointsBreakdownForPlayerThisGame(winner);
cout << endl;
// points breakdown for the loser
// will do it, but problem: loser's unused pts have been added to their 'ThisGame' variables already,
// ... so the numbers won't be correct for them as it stands... how to fix this?
// ^--- future self: not sure if this is still a problem. I might have fixed this...
printPointsBreakdownForPlayerThisGame(determineLoser());
cout << endl;
// xxx pts (Leader) to yyy pts (Trailer) in today's macro-game thus far...
// any average/combined stats of interest?
cout << endl;
}
void printPointsBreakdownForPlayerThisGame(Player* p) {
// BLAH: use printf() for this to simplify the formatting
cout << setprecision(0) << fixed;
string BD_INDENT;
if (NARROW_PRESENTATION)
BD_INDENT = " ";
else
BD_INDENT = " ";
int strictHandPts = p->handPtsThisGame - p->unusedPtsThisGame[HAND];
int strictCribPts = p->cribPtsThisGame - p->unusedPtsThisGame[CRIB];
int strictNibsPts = (p->numNibsThisGame * 2) - p->unusedPtsThisGame[NIBS];
int strictPeggedPts = p->peggedPtsThisGame - p->unusedPtsThisGame[PEGGED];
int finPos = strictHandPts + strictCribPts + strictNibsPts + strictPeggedPts;
double handPercent = (strictHandPts * 100.0) / (finPos * 1.0);
double cribPercent = (strictCribPts * 100.0) / (finPos * 1.0);
double nibsPercent = (strictNibsPts * 100.0) / (finPos * 1.0);
double peggedPercent = (strictPeggedPts * 100.0) / (finPos * 1.0);
if ( ! NARROW_PRESENTATION )
cout << " ";
cout << ((finPos > 99) ? "" : " ") << p->indtAdjstdName << "'s "
<< finPos << " pts:" << endl;
cout << BD_INDENT << strictHandPts << " hands"
<< ((handPercent > 9.5) ? string(5, ' ') : string(6, ' '))
<< handPercent << "%\n";
cout << BD_INDENT << ((strictCribPts >= 10) ? "" : " ")
<< strictCribPts << " cribs"
<< ((cribPercent > 9.5) ? string(5, ' ') : string(6, ' '))
<< cribPercent << "%\n";