-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomework6.cpp
612 lines (579 loc) · 17.8 KB
/
homework6.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
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
#include "card.hpp"
#include <bitset>
#include <codecvt>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <locale>
#include <sstream>
#include <string>
#include <vector>
//this is literally just a more stable version of my homework 5. which i think is the whole point here?
//anyways, still very unoptimized, -O2 and -march=haswell gets it down to 10,993 instructions. so my i7-4800mq should be able to do everything in less than a second anyways.
//this is ABOLSUTLEY NOT NT or DOS compatible. im relying on alot of POSIX-specific stuff here. NT likes to have all its characters as utf-16, which i refuse to impliment.
//playing cards can be rendered as they are just utf-8 characters, and xterm-256color has full utf-8 support. however, in utf-32 the table is layed out in a very convienent way for us to just do math.
//so, we use utf-32 for when we want to do math on cards, and utf-8 is just for rendering them. we'll have to convert between these formats quite a bit.
//in the last assignment, you said i did things outside the scope of this class. and i agree. maybe S&T should just accept my transfer credits for my C++ classes and i wont have to find ways to make these assignments interesting.
// i would love to know how many other students actually render the cards. tbh the hex math and binary manipulation is the only reason i actually cared about this assignment.
// cards are objects
// decks are vectors of card objects
// hands are just really small decks
double bal = 0;
double wager = 0;
void updatePlayerBalance(double change) { bal += change; }
void DisplayOutCome(int status) {
if (status) {
cout << "Balance update: +$" << wager << endl;
} else {
cout << "Balance update: -$" << wager << endl;
}
cout << "Your new balance is $";
if (status) {
cout << bal + wager;
} else {
cout << bal - wager;
}
cout << endl;
}
void displayGameSummary(int win, int loss, int tie, int initbal, int wb,
int lb) {
cout << "=== Game Summary === " << endl;
cout << "Total Rounds Played: " << win + loss + tie + wb + lb << endl;
cout << "Total Wins (Regular): " << win << endl;
cout << "Total Wins (Blackjack): " << wb << endl;
cout << "Total Losses (Regular): " << loss << endl;
cout << "Total Losses (Blackjack): " << lb << endl;
cout << "Total Ties (Regular): " << tie << endl;
cout << "Final Balance: $" << fixed << setprecision(2) << bal << endl;
cout << "Net Gain: $" << fixed << setprecision(2) << bal - initbal << endl;
}
using namespace std;
int findCard(vector<card> inpt, card src) {
int rtn;
for (unsigned int i = 0; i < inpt.size(); i++) {
if (inpt.at(i).fullname == src.fullname) {
rtn = i;
}
}
return rtn;
}
int getHandSum(vector<card> inpt) {
int sum = 0;
for (unsigned int i = 0; i < inpt.size(); i++) {
sum += inpt.at(i).worth;
}
return sum;
}
int isBlackJack(vector<card> inpt) { // check to see if we got a blackjack.
// requirement of assignment.
int sum = 0;
for (unsigned int i = 0; i < inpt.size(); i++) {
sum += inpt.at(i).worth;
}
if (sum - 21) {
return 0;
} else {
return 1;
}
return 0;
}
void printHand(vector<card> inpt) {
/*for (unsigned int i=0; i < inpt.size();i++){
cout<<inpt.at(i).render << " "<< inpt.at(i).fullname<<endl;
}*/
// for the upgrade to hw6, we are told to display in the order of:
// Diamonds,Hearts,Clubs, then Spades.
for (unsigned int i = 0; i < inpt.size(); i++) {
if (inpt.at(i).suit == "Diamonds") {
cout << inpt.at(i).render << " " << inpt.at(i).fullname << endl;
}
}
for (unsigned int i = 0; i < inpt.size(); i++) {
if (inpt.at(i).suit == "Hearts") {
cout << inpt.at(i).render << " " << inpt.at(i).fullname << endl;
}
}
for (unsigned int i = 0; i < inpt.size(); i++) {
if (inpt.at(i).suit == "Clubs") {
cout << inpt.at(i).render << " " << inpt.at(i).fullname << endl;
}
}
for (unsigned int i = 0; i < inpt.size(); i++) {
if (inpt.at(i).suit == "Spades") {
cout << inpt.at(i).render << " " << inpt.at(i).fullname << endl;
}
}
for (unsigned int i = 0; i < inpt.size(); i++) {
if (inpt.at(i).suit == "Mysteries") {
cout << inpt.at(i).render << " " << inpt.at(i).fullname << endl;
}
}
}
unsigned int GetCardNum(
string inptcard) { // this function turns our renderable utf8 card into a
// mathable utf-32 integer of its unicode point code.
unsigned int fist, secnt, therd, phorf;
stringstream e;
e.clear();
fist = inptcard[0] - 0xffffff00;
secnt = inptcard[1] - 0xffffff00;
therd = inptcard[2] - 0xffffff00;
phorf = inptcard[3] - 0xffffff00;
e << bitset<3>(fist) << "" << bitset<6>(secnt) << "" << bitset<6>(therd) << ""
<< bitset<6>(phorf);
string outpt = e.str();
return stoll(outpt, nullptr, 2);
}
string GetCardName(string inpt) { // this function gets us the name of a card
unsigned int crd = GetCardNum(inpt);
string nme = "error";
stringstream hexcvtr;
switch (crd % 0x10) {
case 0x0:
if (crd == 0x1F0A0) {
nme = "Hidden Card";
}
break;
case 0x1:
nme = "Ace";
break;
case 0xB:
nme = "Jack";
break;
case 0xC:
nme = "Knight";
break;
case 0xD:
nme = "Queen";
break;
case 0xE:
nme = "King";
break;
case 0xF:
nme = "Joker";
break;
default:
hexcvtr << dec << (crd % 0x10);
nme = hexcvtr.str();
break;
};
// nme=to_string(crd);
return nme;
}
int IsCardValid(string inpt) { // do we use this card in blackjack?
int chkr = 0;
unsigned int crd = GetCardNum(inpt);
switch (crd % 0x10) {
case 0x0:
if (crd == 0x1F0A0) { // card back
chkr = 1;
break;
}
chkr = 0; // no "zero" cards
break;
case 0xF:
chkr = 0;
break; // no jokers
case 0xC:
chkr = 0;
break; // no knights
default:
chkr = 1;
break;
};
return chkr;
}
string GetCardSuit(string inpt) { // this gets us the suit of any given card
string nme;
unsigned int crd = GetCardNum(inpt);
switch ((crd % 0x100) / 0x10) {
case 0x1:
nme = "Ace";
break;
case 0xA:
if (crd == 0x1F0A0) {
nme = "Mysteries";
break;
}
nme = "Spades";
break;
case 0xB:
nme = "Hearts";
break;
case 0xC:
nme = "Diamonds";
break;
case 0xD:
nme = "Clubs";
break;
default:
nme = "error";
break;
};
return nme;
}
int GetCardWorth(string inpt) { // how much is this card worth in blackjack?
int nme = 0;
unsigned int crd = GetCardNum(inpt);
stringstream hexcvtr;
switch (crd % 0x10) {
case 0x0:
if (crd == 0x1F0A0) {
nme = 0;
}
break;
case 0x1:
nme = 1;
break;
case 0xB:
nme = 10;
break;
case 0xC:
nme = 10;
break;
case 0xD:
nme = 10;
break;
case 0xE:
nme = 10;
break;
case 0xF:
nme = 0;
break;
default:
hexcvtr << dec << (crd % 0x10);
nme = stoi(hexcvtr.str());
break;
};
return nme;
}
card generateRandomCard(
vector<card> inpt) { // generate a random card from a given deck.
// requirement of the assignment
return inpt.at(rand() % inpt.size());
}
card generateRandomCard(
vector<card> inpt,
const int probability) { // generate a random card from a given deck, with
// chance of guarenteeing a 10-score card.
// requirement of the assignment
wstring_convert<codecvt_utf8<char32_t>, char32_t> utf32_to_utf8;
if ((rand() % 99) <= probability) {
// lets generate a vector of only 10 cards.
vector<card> genvcdeck;
string blankcard = "\U0001f0a0";
card genrat;
stringstream genss;
for (int i = 0; i <= 63; i++) {
if (IsCardValid(utf32_to_utf8.to_bytes(GetCardNum(blankcard) + i))) {
genrat.render = utf32_to_utf8.to_bytes(GetCardNum(blankcard) + i);
genrat.name =
GetCardName(utf32_to_utf8.to_bytes(GetCardNum(blankcard) + i));
genrat.suit =
GetCardSuit(utf32_to_utf8.to_bytes(GetCardNum(blankcard) + i));
genss.str("");
genss << GetCardName(utf32_to_utf8.to_bytes(GetCardNum(blankcard) + i))
<< " of "
<< GetCardSuit(utf32_to_utf8.to_bytes(GetCardNum(blankcard) + i));
genrat.fullname = genss.str();
genrat.worth =
GetCardWorth(utf32_to_utf8.to_bytes(GetCardNum(blankcard) + i));
if (genrat.worth == 10) {
genvcdeck.push_back(genrat);
}
}
}
return genvcdeck.at(rand() % genvcdeck.size());
}
return inpt.at(rand() % inpt.size());
}
int PlayBlackJack() {
wstring_convert<codecvt_utf8<char32_t>, char32_t>
utf32_to_utf8; // make an object "utf32_to_utf8 to convert my integers
// into renderable characters
string blankcard = "\U0001f0a0";
// okay, now that we have ways to get data about cards, lets create our card
// objects. we're gonna steal the setup of our rendering test (from hw5) to make our
// table's deck.
vector<card> tabledeck;
card genrat;
stringstream genss;
for (int i = 0; i <= 63; i++) {
if (IsCardValid(utf32_to_utf8.to_bytes(GetCardNum(blankcard) + i))) {
genrat.render = utf32_to_utf8.to_bytes(GetCardNum(blankcard) + i);
genrat.name =
GetCardName(utf32_to_utf8.to_bytes(GetCardNum(blankcard) + i));
genrat.suit =
GetCardSuit(utf32_to_utf8.to_bytes(GetCardNum(blankcard) + i));
genss.str("");
genss << GetCardName(utf32_to_utf8.to_bytes(GetCardNum(blankcard) + i))
<< " of "
<< GetCardSuit(utf32_to_utf8.to_bytes(GetCardNum(blankcard) + i));
genrat.fullname = genss.str();
genrat.worth =
GetCardWorth(utf32_to_utf8.to_bytes(GetCardNum(blankcard) + i));
tabledeck.push_back(genrat);
}
}
// now that our table's deck is made, we can just interract with cards by
// interracting with the vector.
// time to play blackjack!
vector<card> playerhand;
vector<card> dealerhand;
card flipcard;
flipcard = tabledeck.at(0);
tabledeck.erase(tabledeck.begin());
card holdcard;
int choice = 5;
cout << dec << fixed << setprecision(2) << "Current balance: $" << bal
<< endl;
;
cout << "How much money would you like to wager? ";
cin >> fixed >> setprecision(2) >> wager;
while ((bal - wager) <= 0) {
cout << "no, try again. ";
cin >> wager;
}
// time to deal!
cout << endl;
dealerhand.push_back(generateRandomCard(tabledeck));
tabledeck.erase(tabledeck.begin() + findCard(tabledeck, dealerhand.back()));
dealerhand.push_back(generateRandomCard(tabledeck));
tabledeck.erase(tabledeck.begin() + findCard(tabledeck, dealerhand.back()));
holdcard = dealerhand.back();
dealerhand.back() = flipcard;
// now the dealer has 2 cards.
playerhand.push_back(generateRandomCard(tabledeck, 15));
tabledeck.erase(tabledeck.begin() + findCard(tabledeck, playerhand.back()));
playerhand.push_back(generateRandomCard(tabledeck, 15));
tabledeck.erase(tabledeck.begin() + findCard(tabledeck, playerhand.back()));
// now the player has 2 cards.
choice = 0;
system("clear");
cout << dec << fixed << setprecision(2) << "Current balance: $" << bal
<< endl;
cout << "Player's hand: " << endl;
printHand(playerhand);
cout << endl;
cout << "Dealer's hand: " << endl;
printHand(dealerhand);
cout << endl;
// check for blackjack
dealerhand.back() = holdcard;
if (isBlackJack(playerhand) && !(isBlackJack(dealerhand))) {
cout << "Congratulations! You hit Blackjack!" << endl;
return 2;
}
if (isBlackJack(dealerhand) && !(isBlackJack(playerhand))) {
cout << "Too bad, Dealer has Blackjack!" << endl;
return -2;
}
if (isBlackJack(playerhand) && (isBlackJack(dealerhand))) {
cout << "Its a tie!" << endl;
return 69420;
}
dealerhand.back() = flipcard;
// checked for blackjacks
cout << endl;
// player's turn
while (!(choice)) {
cout << endl;
cout << "make a choice:" << endl
<< "0: hit" << endl
<< "1: double down" << endl
<< "3: stand" << endl
<< "*: exit program." << endl;
if (!(cin >> choice)) {
return (30);
cin.clear();
cout << endl;
cout << "error";
cout << endl;
cin.ignore();
}
if (!(choice)) {
playerhand.push_back(generateRandomCard(tabledeck));
cout << "you received a: " << playerhand.back().render << " "
<< playerhand.back().fullname << endl;
tabledeck.erase(tabledeck.begin() +
findCard(tabledeck, playerhand.back()));
// cout<<"Player's hand: "<<endl;
// printHand(playerhand);
if (isBlackJack(playerhand) && !(isBlackJack(dealerhand))) {
cout << endl;
cout << "player's final hand:" << endl;
printHand(playerhand);
cout << endl << "dealers final hand:" << endl;
printHand(dealerhand);
cout << "Congratulations! You hit Blackjack!" << endl;
return 2;
}
if (getHandSum(playerhand) > 21) {
cout << endl;
cout << "player's final hand:" << endl;
printHand(playerhand);
cout << endl << "dealers final hand:" << endl;
printHand(dealerhand);
cout << "you are bust. you lose!" << endl;
return -1;
}
}
if (choice == 1) {
wager *= 2;
playerhand.push_back(generateRandomCard(tabledeck));
tabledeck.erase(tabledeck.begin() +
findCard(tabledeck, playerhand.back()));
cout << "you received a: " << playerhand.back().render << " "
<< playerhand.back().fullname << endl;
choice = 6;
if (isBlackJack(playerhand) && !(isBlackJack(dealerhand))) {
cout << endl;
cout << "player's final hand:" << endl;
printHand(playerhand);
cout << endl << "dealers final hand:" << endl;
printHand(dealerhand);
cout << "Congratulations! You hit Blackjack!" << endl;
return 2;
}
if (getHandSum(playerhand) > 21) {
cout << endl;
cout << "player's final hand:" << endl;
printHand(playerhand);
cout << endl << "dealers final hand:" << endl;
printHand(dealerhand);
cout << "you are bust. you lose!" << endl;
return -1;
}
break;
}
} // end of player turn
// begin dealer's turn
dealerhand.back() = holdcard;
cout << "Dealer's hole card was a " << dealerhand.back().render << " "
<< dealerhand.back().fullname << endl;
// check for blackjack
if (isBlackJack(playerhand) && !(isBlackJack(dealerhand))) {
cout << "Congratulations! You hit Blackjack!" << endl;
return 2;
}
if (isBlackJack(dealerhand) && !(isBlackJack(playerhand))) {
cout << "Too bad, Dealer has Blackjack!" << endl;
return -2;
}
if (isBlackJack(playerhand) && (isBlackJack(dealerhand))) {
cout << "Its a tie!" << endl;
return 69420;
}
// dealerhand.back()=flipcard;
// checked for blackjacks
if (getHandSum(dealerhand) <= 16) {
dealerhand.push_back(generateRandomCard(tabledeck));
cout << "dealer received a: " << dealerhand.back().render << " "
<< dealerhand.back().fullname << endl;
tabledeck.erase(tabledeck.begin() + findCard(tabledeck, dealerhand.back()));
}
if (getHandSum(dealerhand) > 21) {
cout << endl;
cout << "player's final hand:" << endl;
printHand(playerhand);
cout << endl << "dealers final hand:" << endl;
printHand(dealerhand);
cout << "dealer is bust. you win!" << endl;
return 1;
}
if (isBlackJack(dealerhand) && !(isBlackJack(playerhand))) {
cout << endl;
cout << "player's final hand:" << endl;
printHand(playerhand);
cout << endl << "dealers final hand:" << endl;
printHand(dealerhand);
cout << "Too bad, Dealer has Blackjack!" << endl;
return -2;
} // end of dealers turn
// cout<<"\U0001f0a0"<<endl;
if (getHandSum(playerhand) > getHandSum(dealerhand)) {
cout << endl;
cout << "player's final hand:" << endl;
printHand(playerhand);
cout << endl << "dealers final hand:" << endl;
printHand(dealerhand);
cout << "you beat the dealer!" << endl;
return 1;
}
if (getHandSum(playerhand) < getHandSum(dealerhand)) {
cout << endl;
cout << "player's final hand:" << endl;
printHand(playerhand);
cout << endl << "dealers final hand:" << endl;
printHand(dealerhand);
cout << "the dealer beat you!" << endl;
return -1;
}
if (isBlackJack(playerhand) && (isBlackJack(dealerhand))) {
cout << "Its a tie!" << endl;
return 69420;
}
if (getHandSum(playerhand) == getHandSum(dealerhand)) {
cout << endl;
cout << "player's final hand:" << endl;
printHand(playerhand);
cout << endl << "dealers final hand:" << endl;
printHand(dealerhand);
// cout<<"its a tie!"<<endl;
return 69420;
}
/*cout<<endl;
cout<<"player's final hand:"<<endl;
printHand(playerhand);
cout<<endl<<"dealers final hand:"<<endl;
printHand(dealerhand);*/
return 0;
}
int main() {
//srand((int)time(0));
srand(1570);
//why are we not supposed to use actual randomness?
bal = rand() % (900) + 100;
double initbal = bal;
int wins = 0;
int loss = 0;
int lb = 0;
int wb = 0;
int ties = 0;
while (bal > 0) {
cout << endl;
switch (PlayBlackJack()) {
case 1:
DisplayOutCome(1);
wins++;
updatePlayerBalance(wager);
break;
case 2:
DisplayOutCome(1);
wb++;
updatePlayerBalance(wager);
break;
case 69420:
cout << "its a Tie!" << endl;
ties++;
break;
case -1:
DisplayOutCome(0);
loss++;
updatePlayerBalance(wager * -1);
break;
case -2:
DisplayOutCome(0);
updatePlayerBalance(wager * -1);
lb++;
break;
case 30:
displayGameSummary(wins, loss, ties, initbal, wb, lb);
exit(1);
break;
};
}
displayGameSummary(wins, loss, ties, initbal, wb, lb);
return 0;
}