-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
169 lines (115 loc) · 2.84 KB
/
main.cpp
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
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
bool asktoreplay(int& card, bool& lastGuess, vector<int> deck) {
cout << "\nGAME OVER";
cout << "\nReplay? (y/n) ";
char replay;
cin >> replay;
if (replay == 'y') {
card = 0;
lastGuess;
deck = {1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8,
8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13};
return 1;
}
return 0;
}
int main() {
//initialize deck
vector<int> deck = {1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8,
8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13};
//declare here to avoid looping declarations
bool lastGuess = 1;
bool cardDeleted = 0;
string input;
int card = 0, lastCard = 0;
int higher = 0, lower = 0;
//start loop here
while (true) {
lastCard = card;
//get starting card
cout << "Card: ";
cin >> input;
if (input.length() == 1) {
card = (static_cast<int>(input[0]) - '0');
}
else {
card = stoi(input);
}
//account for K, j, etc.
if (card >= 1 && card <= 13) {
}
else if (card == 17 || card == 49) {
card = 1;
}
else if (card == 72 || card == 40 || card == 0) {
card = 10;
}
else if (card == 26 || card == 58) {
card = 11;
}
else if (card == 33 || card == 65) {
card = 12;
}
else if (card == 27 || card == 59) {
card = 13;
}
else {
cout << "Invalid";
return 0;
}
//check whether the previous guess was correct. Ask to replay if not
if (card > lastCard && lastGuess == 0 || card < lastCard && lastGuess == 1) {
if (asktoreplay(card, lastGuess, deck)) {
continue;
}
else {
return 0;
}
}
//iterate through deck, tally higher/lower cards, & delete pulled card
//this way the deck need only be read once
//+ reset variables
higher = 0;
lower = 0;
cardDeleted = 0;
for (int i = 0; i < deck.size(); i++) {
if (deck[i] > card) {
higher++;
}
else if (deck[i] < card) {
lower++;
}
else if (deck[i] == card && cardDeleted == 0) {
//delete card
deck.erase(deck.begin() + i);
cardDeleted = 1;
}
}
//find optimal guess & take it
if (higher > lower) {
cout << "\nHigher \n";
lastGuess = 1;
}
else if (lower > higher) {
cout << "\nLower \n";
lastGuess = 0;
}
else if (lower == higher) {
srand(time(NULL));
cout << "\nEqual odds\n";
if (rand() % 2 == 0) {
cout << "\nLower \n";
lastGuess = 0;
}
else {
cout << "\nHigher \n";
lastGuess = 1;
}
}
}
return 0;
}