-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlackjack_game.cpp
260 lines (230 loc) · 7.67 KB
/
Blackjack_game.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
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
#include <iostream>
#include <cstdint>
#include <vector>
#include <unordered_set>
class Carte {
uint16_t nr{};
std::string stema;
public:
Carte(const uint16_t& nr, std::string stema): nr(nr), stema(std::move(stema)) {}
[[nodiscard]] uint16_t getnr() const {
return nr;
}
[[nodiscard]] std::string getstema() const {
return stema;
}
};
class Jucator {
std::string nume;
double suma{};
std::vector<Carte> carti;
public:
explicit Jucator(std::string nume, const double& suma): nume(std::move(nume)), suma(suma) {}
explicit Jucator(std::string nume): nume(std::move(nume)) {} //Dealer
void add(const Carte& carte) {
carti.emplace_back(carte);
}
void win(const double& malai) {
suma += malai;
}
void lose(const double& malai) {
suma -= malai;
}
[[nodiscard]] uint16_t calcPuncte() const {
uint16_t s = 0;
for (const auto& it : carti)
if (it.getnr() > 10)
s += 10;
else
s += it.getnr();
return s;
}
[[nodiscard]] std::string getnume() const {
return nume;
}
[[nodiscard]] double getsuma() const {
return suma;
}
void resetcarti() {
carti.clear();
}
};
class Pachet {
std::vector<Carte> carti;
public:
Pachet() = default;
void add(const Carte& carte) {
carti.emplace_back(carte);
}
[[nodiscard]] Carte getCarte(const uint16_t& poz) const {
return carti[poz];
}
uint16_t check_carti() {
bool cond = false;
for (const auto& it : carti) {
cond = false;
if (it.getnr() >= 2 && it.getnr() <= 14) {
if (it.getstema() == "rosu" || it.getstema() == "negru" || it.getstema() == "romb" || it.getstema() == "trefla")
cond = true;
if (!cond) {
std::cout << "Pachet masluit\n";
return 0;
}
}
else {
std::cout << "Pachet masluit\n";
return 0;
}
}
cond = true;
if (carti.size() == 52) {
for (uint16_t i = 0; i < carti.size() - 1; ++i)
for (uint16_t j = i + 1; j < carti.size(); ++j)
if (carti[i].getnr() == carti[j].getnr() && carti[i].getstema() == carti[j].getstema())
cond = false;
if (cond) {
std::cout << "Pachet OK\n";
return 1;
}
}
else
std::cout << "Pregatit pentru Blackjack\n";
return 1;
}
void shuffle_Pachet(const uint16_t& a, uint16_t& x0, const uint16_t& c, const uint16_t& a1, uint16_t& x01, const uint16_t& c1) {
for (uint16_t i = 0; i < 50; ++i) {
x0 = (a * x0 + c) % carti.size();
x01 = (a1 * x01 + c1) % carti.size();
std::swap(carti[x0], carti[x01]);
}
}
void print() const {
for (const auto& it : carti)
std::cout << it.getnr() << ',' << it.getstema() << '\n';
}
};
int main() {
Pachet Pachet;
std::string command;
std::cin >> command;
if (command == "check_cards") {
uint16_t nr;
char v;
std::string stema;
while (std::cin >> nr >> v >> stema)
Pachet.add(Carte(nr, stema));
Pachet.check_carti();
}
else if (command == "shuffle_cards") {
uint16_t a, c, x0, a1, c1, x01, nr;
std::cin >> a >> c >> x0 >> a1 >> c1 >> x01;
char v;
std::string stema;
while (std::cin >> nr >> v >> stema)
Pachet.add(Carte(nr, stema));
Pachet.shuffle_Pachet(a, x0, c, a1, x01, c1);
Pachet.print();
}
else if (command == "play_game") {
std::vector<Jucator> jucatori;
uint16_t nr_jucatori;
std::cin >> nr_jucatori;
for (uint16_t i = 0; i < nr_jucatori; ++i) {
std::string nume_jucator;
double suma_jucator;
std::cin >> nume_jucator >> suma_jucator;
Jucator newJucator(nume_jucator, suma_jucator);
jucatori.emplace_back(newJucator);
}
for (uint16_t i = 0; i < 4; ++i)
for (uint16_t j = 2; j <= 14; ++j) {
if (j != 11) {
uint16_t nr;
std::string stema;
switch (nr) {
case 0:
stema = "rosu";
break;
case 1:
stema = "negru";
break;
case 2:
stema = "romb";
break;
case 3:
stema = "trefla";
break;
}
nr = j;
Carte newCarte(nr, stema);
Pachet.add(newCarte);
}
}
uint16_t a, c, x0, a1, c1, x01;
Jucator Dealer("Dealer");
while (std::cin >> a) {
std::cin >> c >> x0 >> a1 >> c1 >> x01;
Pachet.shuffle_Pachet(a, x0, c, a1, x01, c1);
uint16_t poz_cur_carte = 0;
bool gata_runda = false;
std::unordered_set<std::string> jucatori_out;
Dealer.resetcarti();
for (auto& it : jucatori) {
it.resetcarti();
for (uint16_t j = 0; j < 2; ++j) {
Carte newCarte = Pachet.getCarte(poz_cur_carte++);
it.add(newCarte);
}
}
for (uint16_t i = 0; i < 2; ++i) {
Carte newCarte = Pachet.getCarte(poz_cur_carte++);
Dealer.add(newCarte);
}
for (auto& it : jucatori)
while (it.calcPuncte() <= 16) {
Carte newCarte = Pachet.getCarte(poz_cur_carte++);
it.add(newCarte);
}
while (Dealer.calcPuncte() <= 16) {
Carte newCarte = Pachet.getCarte(poz_cur_carte++);
Dealer.add(newCarte);
}
if (Dealer.calcPuncte() > 21) {
for (auto& it : jucatori)
if (it.calcPuncte() <= 21)
it.win(10);
else {
it.lose(10);
if (it.getsuma() < 10)
jucatori_out.insert(it.getnume());
}
gata_runda = true;
}
if (!gata_runda)
for (auto& it : jucatori) {
if (it.calcPuncte() > Dealer.calcPuncte())
if (it.calcPuncte() > 21) {
it.lose(10);
if (it.getsuma() < 10)
jucatori_out.insert(it.getnume());
}
else
it.win(10);
else if (it.calcPuncte() < Dealer.calcPuncte()) {
it.lose(10);
if (it.getsuma() < 10)
jucatori_out.insert(it.getnume());
}
}
for (const auto& it : jucatori_out)
for (uint16_t j = 0; j < jucatori.size(); ++j)
if (it == jucatori[j].getnume()) {
jucatori.erase(jucatori.begin() + j);
break;
}
}
for (const auto& it : jucatori)
std::cout << it.getnume() << ": " << it.getsuma() << "\n";
}
return 0;
}