-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUsefulLogicFunctions.cpp
More file actions
116 lines (102 loc) · 2.77 KB
/
UsefulLogicFunctions.cpp
File metadata and controls
116 lines (102 loc) · 2.77 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
#include "UsefulLogicFunctions.h"
Probability::Probability(double prob = 0.0, Choice check = Choice::M) {
probability = prob;
guess = check;
}
double Probability::getProb() {
return probability;
}
Choice Probability::getChoice() {
return guess;
}
bool sortFun(Probability i, Probability j) {
return i.getProb() < j.getProb();
}
void calcProb(const vector<int>& choices, double& probM, double& probW, double& probB) {
int timesMSelected = 0;
int timesWSelected = 0;
int timesBSelected = 0;
for (unsigned int i = 0; i < choices.size(); ++i) {
switch (choices[i]) {
case 1:
++timesMSelected;
break;
case 2:
++timesWSelected;
break;
case 3:
++timesBSelected;
break;
}
}
probM = static_cast<double>(timesMSelected) / choices.size();
probW = static_cast<double>(timesWSelected) / choices.size();
probB = static_cast<double>(timesBSelected) / choices.size();
}
Choice makeGuess(const double& probM, const double& probW, const double& probB) {
vector<Probability> probs;
Probability ProM{ probM, Choice::M };
Probability ProW{ probW, Choice::W };
Probability ProB{ probB, Choice::B };
probs.push_back(ProM);
probs.push_back(ProW);
probs.push_back(ProB);
sort(probs.begin(), probs.end(), sortFun);
int selection = rand() % 100;
Probability select1{ 100 * probs[0].getProb(), probs[0].getChoice() };
Probability select2{ 100 * (probs[1].getProb() + probs[0].getProb()), probs[1].getChoice() };
Probability select3{ 100 * (probs[2].getProb() + probs[1].getProb() + probs[0].getProb()), probs[2].getChoice() };
if (selection == 0) {
return select3.getChoice();
}
if (selection <= select1.getProb()) {
return select1.getChoice();
}
if (selection <= select2.getProb()) {
return select2.getChoice();
}
if (selection <= select3.getProb()) {
return select3.getChoice();
}
}
int choiceToInt(Choice user) {
if (user == Choice::M) return 1;
if (user == Choice::W) return 2;
if (user == Choice::B) return 3;
}
Choice charToChoice(char input) {
if (input == 'M') return Choice::M;
if (input == 'W') return Choice::W;
if (input == 'B') return Choice::B;
}
bool compareChoices(Choice guess, Choice user) {
if (guess == user) return false;
else return true;
}
int trackScore(bool win, int score) {
if (win == true) {
return score + 10;
}
else {
return score;
}
}
double percentCorrect(bool win, double& timesCorrect, int& guesses) {
if (win == false) {
++timesCorrect;
}
double percent = 100.0 * (timesCorrect / guesses);
return percent;
}
string intToStr(int num) {
stringstream out;
out << num;
string fin = out.str();
return fin;
}
string dubToStr(double num) {
stringstream out;
out << num;
string fin = out.str();
return fin;
}