-
Notifications
You must be signed in to change notification settings - Fork 0
/
Blackjack_tests.c
760 lines (651 loc) · 21.1 KB
/
Blackjack_tests.c
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
/*
Author: Luke Marshall
Description: Tests for Blackjack code
*/
#include "Blackjack.h"
//Used for comparing cards in the test functions
int compare_cards(Card *first, Card *second){
if (first->rank != second->rank){
//fprintf(stderr, "The card ranks do not match.\n");
return 0;
} else if (first->suit != second->suit){
//fprintf(stderr, "The card suits do not match.\n");
return 0;
} else if (first->value != second->value){
//fprintf(stderr, "The card values do not match.\n");
return 0;
} else {
return 1;
}
}
//Test for the get_card function
int test_get_card(Suit suit, Rank rank, int value){
//create the cards using function
Card *first_card = get_card(Clubs, Ace, 11);
Card *second_card = get_card((Suit) 3, (Rank) 1, 11);
Card *third_card = get_card(suit, rank, value);
//Create card to compare with function outputs
Card compare_card;
compare_card.rank = Ace;
compare_card.suit = Clubs;
compare_card.value = 11;
//Test the outputs against the known card
bool failed = false;
if (!compare_cards(first_card, &compare_card)){
fprintf(stderr, "First card does not match expcted output.\n");
failed = true;
}
if (!compare_cards(second_card, &compare_card)){
fprintf(stderr,"Second card does not match expected output.\n");
failed = true;
}
if (!compare_cards(third_card, &compare_card)){
fprintf(stderr,"Third card does not match expected output.\n");
failed = true;
}
if (failed){
return 0;
} else {
return 1;
}
}
//Test for the get_decks function
int test_get_decks(long num_decks){
bool failed = false;
//create deck
Deck *deck = get_decks(num_decks);
//count the cards in the deck
int count = 0;
for (int i = 0; deck->cards[i] != NULL; i++){
count++;
}
//Check if the number of cards in the deck is what it should be
if (count != (DECK_SIZE * num_decks)){
fprintf(stderr, "The number of cards in the deck is not correct.");
failed = true;
}
//check the first card in the deck
Card *first_card = get_card(Spades, Ace, 11);
if (!compare_cards(first_card, deck->cards[0])){
fprintf(stderr, "The first card in the deck is not correct. It is the %d of %d, with a value of %d", deck->cards[0]->rank, deck->cards[0]->suit, deck->cards[0]->value);
failed = true;
}
//check the last card in the deck
Card *last_card = get_card(Clubs, King, 10);
if (!compare_cards(last_card, deck->cards[DECK_SIZE * num_decks - 1])){
fprintf(stderr, "The first card in the deck is not correct. It is the %d of %d, with a value of %d", deck->cards[0]->rank, deck->cards[0]->suit, deck->cards[0]->value);
failed = true;
}
if (failed){
return 0;
} else {
return 1;
}
}
//Test for the shuffle function
int test_shuffle(long num_decks){
Deck *org_deck = get_decks(num_decks);
Deck *shuff_deck = get_decks(num_decks);
shuffle(shuff_deck, num_decks);
for (int i = 0; i < (DECK_SIZE * num_decks); i++){
int count = 0;
if (org_deck->cards[i]->rank == shuff_deck->cards[i]->rank
&& org_deck->cards[i]->suit == shuff_deck->cards[i]->suit
&& org_deck->cards[i]->value == shuff_deck->cards[i]->value){
count++;
}
if (count > (2 * num_decks)){
fprintf(stderr, "The shuffled deck is not shuffled enough.");
return 0;
}
}
return 1;
}
//Test for the pop function
int test_pop(long num_decks){
bool failed = false;
//Initialize the decks for popping
Deck *deck_1 = get_decks(num_decks);
Deck *deck_2 = get_decks(num_decks);
Deck *deck_3 = get_decks(num_decks);
//Initialize cards to compare with popped values
Card *hard_comp_1 = get_card(Spades, Ace, 11);
Card *hard_comp_2 = get_card(Hearts, Ace, 11);
Card *hard_comp_3 = get_card(Diamonds, Ace, 11);
Card *popped = pop(deck_1);
if (popped->suit != hard_comp_1->suit
|| popped->rank != hard_comp_1->rank
|| popped->value != hard_comp_1->value){
fprintf(stderr, "The hard coded comparison 1 did not produce the correct results.\n");
}
popped = pop(deck_1);
if (popped->suit != hard_comp_2->suit
|| popped->rank != hard_comp_2->rank
|| popped->value != hard_comp_2->value){
fprintf(stderr, "The hard coded comparison 2 did not produce the correct results.\n");
failed = true;
}
popped = pop(deck_1);
if (popped->suit != hard_comp_3->suit
|| popped->rank != hard_comp_3->rank
|| popped->value != hard_comp_3->value){
fprintf(stderr, "The hard coded comparison 3 did not produce the correct results.\n");
failed = true;
}
//Compare the two unshuffled and unpopped decks card by card
for (int i = 0; i < (DECK_SIZE * num_decks); i++){
Card *comp_card_2 = pop(deck_2);
Card *comp_card_3 = pop(deck_3);
if (comp_card_2->suit != comp_card_3->suit
|| comp_card_2->rank != comp_card_3->rank
|| comp_card_2->value != comp_card_3->value){
fprintf(stderr, "The cards popped from index %d are not the same.\n", i);
failed = true;
}
}
if (failed){
return 0;
} else {
return 1;
}
}
//test for get_player function
int test_get_player(){
bool failed = false;
Player player;
get_player(&player);
if (player.hand[0] != NULL){
fprintf(stderr, "Player hand initiated incorrectly.\n");
failed = true;
}
if (player.total != 0){
fprintf(stderr, "Player total initialized incorrectly.\n");
failed = true;
}
if (player.curr != 0){
fprintf(stderr, "Player curr index initialized incorrectly.\n");
failed = true;
}
if (player.playing != true){
fprintf(stderr, "Player playing initialized incorrectly.\n");
failed = true;
}
if (player.bet != 0){
fprintf(stderr, "Player bet initialized incrrectly.\n");
failed = true;
}
if (player.bank != STARTING_BANK){
fprintf(stderr, "Player bank initialized incorrecty.\n");
failed = true;
}
if (failed){
return 0;
} else {
return 1;
}
}
//test for the bet function
int test_wager(double bet){
Player player;
get_player(&player);
int bet_success = wager(&player, bet);
if (!bet_success){
return 0;
}
if (player.bet != bet){
return 0;
} else {
return 1;
}
}
//test for the player_hit function
int test_player_hit(long num_decks){
bool failed = false;
Deck *deck = get_decks(num_decks);
shuffle(deck, num_decks);
Player player;
get_player(&player);
player_hit(deck, &player);
player_hit(deck, &player);
if (deck->cards[0] != player.hand[0]){
fprintf(stderr, "The first hit was not successful.\n");
failed = true;
}
if (deck->cards[1] != player.hand[1]){
fprintf(stderr, "The second hit was not successful.\n");
failed = true;
}
if (player.total != (deck->cards[0]->value + deck->cards[1]->value)){
fprintf(stderr, "The total was not calculated correctly for the player.\n");
failed = true;
}
if (failed){
return 0;
} else {
return 1;
}
}
//test for the natural_check and player_natural functions
int test_natural(){
bool failed = false;
//Initialize cards that make 21 with their values
Card *first_card = get_card(Diamonds, Ace, 11);
Card *sec_card = get_card(Diamonds, King, 10);
//Initialize player with the cards and corresponding total
Player player;
get_player(&player);
player.hand[0] = first_card;
player.hand[1] = sec_card;
player.total = (first_card->value + sec_card->value);
double bet = 14.;
wager(&player, bet);
//check the player_natural_check function
if (!player_natural_check(&player)){
fprintf(stderr, "The player_natural_check function did not perform correctly.\n");
failed = true;
} /*else {
player_natural(&player);
if (player.bank != (STARTING_BANK + (1.5 * wager))){
fprintf(stderr, "The player_natural function did not perform correctly.\n");
failed = true;
}
}*/
if (failed){
return 0;
} else {
return 1;
}
}
//test for the player_bust_check and player_bust functions
int test_bust(){
bool failed = false;
//Initialize the cards for bust testing
Card *card_ace = get_card(Hearts, Ace, 11);
Card *card_8 = get_card(Diamonds, Eight, 8);
Card *card_5 = get_card(Spades, Five, 5);
Card *card_king = get_card(Spades, King, 10);
//Initialize the players for bust testing
Player non_bust_player;
get_player(&non_bust_player);
wager(&non_bust_player, 15);
non_bust_player.hand[0] = card_5;
non_bust_player.hand[1] = card_king;
non_bust_player.total = non_bust_player.hand[0]->value + non_bust_player.hand[1]->value;
Player player_w_ace;
get_player(&player_w_ace);
wager(&player_w_ace, 15);
player_w_ace.hand[0] = card_5;
player_w_ace.hand[1] = card_ace;
player_w_ace.hand[2] = card_8;
player_w_ace.total = player_w_ace.hand[0]->value + player_w_ace.hand[1]->value + player_w_ace.hand[2]->value;
Player player_wo_ace;
get_player(&player_wo_ace);
wager(&player_wo_ace, 15);
player_wo_ace.hand[0] = card_5;
player_wo_ace.hand[1] = card_8;
player_wo_ace.hand[2] = card_king;
player_wo_ace.total = player_wo_ace.hand[0]->value + player_wo_ace.hand[1]->value + player_wo_ace.hand[2]->value;
//bust function checks
if (player_bust_check(&non_bust_player)){
fprintf(stderr, "Busted on the non-bust player, test failed.\n");
failed = true;
}
if (player_bust_check(&player_w_ace)){
fprintf(stderr, "Busted on the player with an Ace, test failed.\n");
failed = true;
}
if (!player_bust_check(&player_wo_ace)){
printf("total: %d", player_wo_ace.total);
fprintf(stderr, "Didn't bust on the player without an Ace, test failed.\n");
failed = true;
}
player_bust(&player_wo_ace);
if (player_wo_ace.bet != 0 || player_wo_ace.bank != (STARTING_BANK - 15) || player_wo_ace.playing != false){
fprintf(stderr, "player_bust function test failed.\n");
failed = true;
}
if (failed){
return 0;
} else {
return 1;
}
}
//test for the offset function
int test_offset(){
Player player;
get_player(&player);
player.bet = 15.;
offset(&player);
if (player.bet != 0.){
return 0;
}
if (player.playing != false){
return 0;
}
return 1;
}
//test for player_new_round function
int test_player_nr(){
bool failed = false;
//Initialize players for checks
Player player_1;
Player player_2;
get_player(&player_1);
get_player(&player_2);
player_1.bet = 15.;
player_2.bet = 15.;
player_2.bank = 0.;
//check player fields after supposed reset (player_1) or non-reset (player_2)
player_new_round(&player_1);
if (player_1.bet != 0){
fprintf(stderr, "Function didn't reset player 1 correctly.\n");
failed = true;
}
if (player_2.bet != 15.){
fprintf(stderr, "Function didn;t rest player 2 correctly.\n");
failed = true;
}
if (failed){
return 0;
} else {
return 1;
}
}
//test for get_dealer function
int test_get_dealer(){
Dealer *dealer = get_dealer();
if (dealer->curr != 0){
return 0;
}
if (dealer->total != 0){
return 0;
}
return 1;
}
//test for dealer_natural check and dealer_natural functions
int test_d_naturals(){
bool failed = false;
Dealer *dealer_1 = get_dealer();
Dealer *dealer_2 = get_dealer();
//Player player_a;
//get_player(&player_a);
//Player player_b;
//get_player(&player_b);
//player_a.bet = 15.;
//player_b.bet = 15.;
//player_b.total = BLACKJACK;
dealer_1->total = 21;
dealer_2->total = 20;
if (!dealer_natural_check(dealer_1)){
fprintf(stderr, "dealer 1 natural check did not work correctly.\n");
failed = true;
}
if (dealer_natural_check(dealer_2)){
fprintf(stderr, "dealer 2 natural check did not work correctly.\n");
failed = true;
}
/*dealer_natural(&player_a);
if (player_a.bet != 0. || player_a.bank != (STARTING_BANK - 15.)){
fprintf(stderr, "dealer_natural function failed on player without blackjack.\n");
failed = true;
}
dealer_natural(&player_b);
if (player_b.bet != 0. || player_b.bank != STARTING_BANK){
fprintf(stderr, "dealer_natural function failed on player with blackjack.\n");
failed = true;
}*/
if (failed){
return 0;
} else {
return 1;
}
}
int test_dealer_busts(){
bool failed = false;
//Initialize cards for the checks
Card *card_ace = get_card(Hearts, Ace, 11);
Card *card_8 = get_card(Diamonds, Eight, 8);
Card *card_5 = get_card(Spades, Five, 5);
Card *card_king = get_card(Spades, King, 10);
//Initialize dealers for the checks
Dealer *dealer_w_ace = get_dealer();
Dealer *dealer_wo_ace = get_dealer();
dealer_w_ace->hand[0] = card_8;
dealer_w_ace->hand[1] = card_ace;
dealer_w_ace->hand[2] = card_5;
dealer_w_ace->total = 24;
dealer_wo_ace->hand[0] = card_8;
dealer_wo_ace->hand[1] = card_5;
dealer_wo_ace->hand[2] = card_king;
dealer_wo_ace->total = 23;
//Initialize players for checks
Player *players = get_all_players(2);
players[0].bet = 15.;
players[0].total = BLACKJACK;
players[1].bet = 15.;
players[1].total = 17;
//check behavior of dealer_bust_check, dealer_ace_behavior, and dealer_bust
if (dealer_bust_check(dealer_w_ace)){
fprintf(stderr, "Something wrong with either bust check or ace behavior, busted on player with ace.\n");
failed = true;
}
if (!dealer_bust_check(dealer_wo_ace)){
fprintf(stderr, "Something wrong with bust check, did not bust on player without ace.\n");
failed = true;
}
dealer_bust(dealer_wo_ace, players, 2);
if (players[0].bank != (STARTING_BANK + 1.5 * 15.)){
fprintf(stderr, "dealer_bust check failed with player with blackjack.\n");
failed = true;
}
if (players[1].bank != (STARTING_BANK + 15.)){
printf("%f\n", players[1].bank);
fprintf(stderr, "dealer_bust check failed with player with 17.\n");
failed = true;
}
if (failed){
return 0;
} else {
return 1;
}
}
int test_end_round(){
bool failed = false;
//Initialize dealers for checks
Dealer *dealer = get_dealer();
dealer->total = 19;
//Initialize players for the checks
Player *players = get_all_players(4);
players[0].bet = 15.;
players[0].total = 21;
players[1].bet = 15.;
players[1].total = 19;
players[2].bet = 15.;
players[2].total = 13;
players[3].bet = 15.;
players[3].total = 20;
//Run checks
end_round(dealer, players, 4);
if (players[0].bank != (STARTING_BANK + 1.5 * 15.)){
fprintf(stderr, "end round test for the blackjack player failed.\n");
failed = true;
}
if (players[1].bank != STARTING_BANK){
fprintf(stderr, "end round test for the player with same total as dealer failed.\n");
failed = true;
}
if (players[2].bank != (STARTING_BANK - 15.)){
fprintf(stderr, "end round test for the player with total < dealer total failed.\n");
failed = true;
}
if (players[3].bank != (STARTING_BANK + 15.)){
fprintf(stderr, "end round test for the player with total > dealer total failed.\n");
failed = true;
}
if (failed){
return 0;
} else {
return 1;
}
}
int test_dealer_hits(){
bool failed = false;
//Initialize decks for checks
Deck *deck_a = get_decks(1);
Deck *deck_b = get_decks(1);
//Initialize dealers for checks
Dealer *dealer_g16 = get_dealer();
Dealer *dealer_lt17 = get_dealer();
dealer_g16->total = 17;
dealer_lt17->total = 14;
//check for hit
if (dealer_hit_check(dealer_g16)){
fprintf(stderr, "dealer hit check failed to catch dealer total > 16.\n");
failed = true;
}
if (!dealer_hit_check(dealer_lt17)){
fprintf(stderr, " dealer hit check failed to pass a dealer with total < 17\n");
failed = true;
}
Card *down_card = saved_card(deck_a);
dealer_hit_saved(dealer_lt17, down_card);
dealer_hit(dealer_lt17, deck_b);
if (dealer_lt17->hand[0]->value != 1){
fprintf(stderr, "dealer_hit_saved failed.\n");
failed = true;
}
if (dealer_lt17->hand[1]->value != 1){
fprintf(stderr, "dealer_hit failed.\n");
failed = true;
}
if (failed){
return 0;
} else {
return 1;
}
}
int main(int argc, char **argv){
//check for correct number of arguments
if (argc < 2){
fprintf(stderr, "A number between 1 and 8 is required as an argument to run the tests.\n");
exit(0);
}
//conversion of argument into long for use in functions
char *end;
long num_decks = strtol(argv[1], &end, 10);
//Check to make sure number entered as argument is within the allowed number of decks for a game of blackjack
if (num_decks < 1 || num_decks > 8){
fprintf(stderr, "Enter a number between 1 and 8 as an arugment.\n");
exit(0);
}
//get_card function test
if (test_get_card((Suit) 3, (Rank) 1, 11)){
printf("get_card function tests passed.\n");
} else {
printf("get_card function tests failed.\n");
return 0;
}
//get_decks function test
if (test_get_decks(num_decks)){
printf("get_decks function tests passed with %ld deck.\n", num_decks);
} else {
printf("get_decks function tests failed with %ld deck.\n", num_decks);
return 0;
}
//shuffle function test
if (test_shuffle(num_decks)){
printf("shuffle function test passed with %ld deck.\n", num_decks);
} else {
printf("The shuffle wasn't good enough, run again to see if this was by chance.\n");
}
//pop function test
if (test_pop(num_decks)){
printf("pop function test passed with %ld deck.\n", num_decks);
} else {
printf("pop function test failed with %ld deck.\n", num_decks);
return 0;
}
//get_player function test
if (test_get_player()){
printf("get_player function test passed.\n");
} else {
printf("get_player function test failed.\n");
return 0;
}
//bet function test
if (test_wager((14))){
printf("wager function test passed.\n");
} else {
printf("wager function test failed.\n");
return 0;
}
//player_hit function test
if (test_player_hit(num_decks)){
printf("player_hit function test passed.\n");
} else {
printf("player_hit function test failed.\n");
return 0;
}
//player_natural_check and player_natural functions test
if (test_natural()){
printf("natural functions test passed.\n");
} else {
printf("natural functions test failed.\n");
return 0;
}
//player_bust_check and player_bust functions test
if (test_bust()){
printf("bust functions test passed.\n");
} else {
printf("bust functions test failed.\n");
return 0;
}
//offset function test
if (test_offset()){
printf("offset functions test passed.\n");
} else {
printf("offset functions test failed.\n");
}
//player_new_round function test
if (test_player_nr()){
printf("player_new_round function test passed.\n");
} else {
printf("player_new_round function test failed.\n");
}
//get_dealer function test
if (test_get_dealer()){
printf("get_dealer function test passed.\n");
} else {
printf("get_dealer function test passed.\n");
}
//dealer_natural_check and dealer_natural functions test
if (test_d_naturals()){
printf("dealer natural functions test passed.\n");
} else {
printf("dealer natural functions test failed.\n");
return 0;
}
//dealer_bust_check, dealer_ace_behavior, and dealer_bust funtions test
if (test_dealer_busts()){
printf("dealer bust and ace behavior functions test passed.\n");
} else {
printf("dealer bust and ace behavior functions test failed.\n");
return 0;
}
//end_round function test
if (test_end_round()){
printf("end_round function test passed.\n");
} else {
printf("end_round function test failed.\n");
return 0;
}
//dealer_hit_check, saved_card, dealer_hit_saved, and dealer_hit functions test
if (test_dealer_hits()){
printf("dealer hit functions test passed.\n");
} else {
printf("dealer hit functions test failed.\n");
return 0;
}
printf("Passed all tests! :)\n");
return 1;
}