-
Notifications
You must be signed in to change notification settings - Fork 0
/
black.c
90 lines (62 loc) · 2 KB
/
black.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
#include "black.h"
enum outcomes {NO_WINNER, PLY_WINS, BLACKJACK, CPU_WINS, TIE};
void assignPoints(struct player_data *player) {
if (player->card_value == 11)
player->aces++;
if ( (player->aces > 0) && ( (player->total + player->card_value) > 21) ) {
player->total += player->card_value;
player->total = player->total - 10; /* Ace now counts as 1 */
player->aces--;
}
else
player->total += player->card_value;
}
void findWinner (struct black *table) {
enum outcomes winner;
if (table->check_stand == 1) {
if (table->player[CPU].total == table->player[PLY].total) /* Tie */
winner = TIE;
else if (table->player[CPU].total <= 21) {
if (table->player[PLY].total > table->player[CPU].total) /* Player wins */
winner = PLY_WINS;
else
winner = CPU_WINS;
}
else /* Dealer wins player->total > 21 */
winner = CPU_WINS;
}
else {
if ( (table->player[PLY].total == 21) && (table->player[CPU].total == 21) ) /* A tie */
winner = TIE;
else if (table->player[PLY].total > 21) /* If the player exceeds a sum of 21 "busts", loses, even if the dealer also exceeds 21 */
winner = CPU_WINS;
else if (table->player[PLY].total == 21)
winner = PLY_WINS;
else if (table->player[CPU].total == 21) /* Dealer wins */
winner = CPU_WINS;
else if (table->player[CPU].total > 21)
winner = PLY_WINS;
else
winner = NO_WINNER; /* no winner found */
}
if (winner != NO_WINNER) {
if (winner == BLACKJACK) {
table->credit += (table->bet * 3)/2;
endHand("Congratulation you win: ", table);
}
else if (winner == PLY_WINS) {
table->credit += table->bet;
endHand("Congratulation you win: ", table);
}
else if (winner == CPU_WINS) {
table->credit -= table->bet;
endHand("Dealer wins ", table);
}
else
endHand("No one wins", table);
if (table->credit <= 0) {
table->credit = 50;
endHand("Dealer wins. Sorry you bankrupted", table);
}
}
}