Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <ctype.h>
- #define ACE 0
- #define CARDS 13
- #define SUITS 4
- #define MAX_CARDS 5
- #define SCREENLINECOUNT 25
- #define BUF_NAME 15
- #define NO_WINNER 0
- struct table {
- int cards[MAX_CARDS];
- char name[BUF_NAME]; /* Holds the user's first name. */
- int points[MAX_CARDS]; /* Points for each hand */
- int total; /* total points */
- int hand; /* number of cards dealt */
- int aces; /* N° of aces: These variable are used to attribute values 1/11 to Aces*/
- int credit, bet;
- };
- void draw_cards(struct table *dealer, struct table *player);
- void print_cards(struct table *dealer, struct table *player, char check_winner);
- void init(struct table *dealer, struct table *player);
- int findWinner(struct table *dealer, struct table *player, char check_stand);
- void assign_points(struct table *dealer, struct table *player, char ans);
- void bet (struct table *player);
- int main()
- {
- srand((unsigned)time(NULL));
- struct table player = {0};
- struct table dealer = {0};
- player.credit = 50;
- char check_winner; /* to check the presence of a winner */
- char check_stand; /* to check wheter user entered stand */
- check_stand = check_winner = 0;
- char ans = 'M'; /* For Hit/S answers */
- for(;;) {
- if (player.hand == 0) {
- init(&dealer, &player);
- draw_cards(&dealer, &player);
- assign_points(&dealer, &player, 'H');
- assign_points(&dealer, &player, 'H');
- }
- print_cards(&dealer, &player, check_winner);
- check_winner = findWinner(&dealer, &player, check_stand);
- if (check_winner != NO_WINNER) {
- if (check_winner == 4) /* Game Over */
- break;
- else {
- check_stand = 0;
- check_winner = 0;
- ans = 'M';
- continue;
- }
- }
- else {
- printf("Hit or stand (H/S)? ");
- do {
- ans = toupper(getchar());
- if (ans == 'H' || ans == 'S')
- break;
- else
- continue;
- } while(1);
- if (ans == 'S' && dealer.total >= 17)
- check_stand = 1;
- assign_points(&dealer, &player, ans);
- }
- } /* end of for(;;) */
- return 0;
- }
- void draw_cards(struct table *dealer, struct table *player) {
- int card_pick, suit_pick;
- int i = 0;
- int drawn_card[SUITS][CARDS] = {0};
- while (i < MAX_CARDS) { /* draw dealer's cards */
- suit_pick = rand() % SUITS;
- card_pick = rand() % CARDS;
- if (drawn_card[suit_pick][card_pick] == 0) {
- dealer->cards[i] = suit_pick * CARDS + card_pick;
- drawn_card[suit_pick][card_pick] = 1;
- if (card_pick == ACE) { /* Here points are defined but not still assigned */
- dealer->points[i] = 11;
- dealer->aces++;
- }
- else if (card_pick >= 9)
- dealer->points[i] = 10;
- else
- dealer->points[i] = card_pick + 1;
- i++;
- }
- }
- i = 0;
- while (i < MAX_CARDS) { /* draw player's cards */
- card_pick = rand() % CARDS;
- suit_pick = rand() % SUITS;
- if (drawn_card[suit_pick][card_pick] == 0) {
- player->cards[i] = suit_pick * CARDS + card_pick;
- drawn_card[suit_pick][card_pick] = 1;
- if (card_pick == ACE) { /* Here points are defined but not still assigned */
- player->points[i] = 11;
- player->aces++;
- }
- else if (card_pick >= 9)
- player->points[i] = 10;
- else
- player->points[i] = card_pick + 1;
- i++;
- }
- }
- return;
- }
- void assign_points(struct table *dealer, struct table *player, char ans) {
- if (dealer->total < 17) { /* Assing dealer's points: Dealer has to stand at 17 */
- if (dealer->aces > 0 && dealer->total > 21) {
- dealer->total = dealer->total - 10;
- dealer->aces--;
- }
- else
- dealer->total += dealer->points[dealer->hand];
- dealer->hand++;
- }
- if (ans == 'H') { /* Assign player's points */
- if (player->aces > 0 && player->total > 21) {
- player->total = player->total - 10;
- player->aces--;
- }
- else
- player->total += player->points[player->hand];
- player->hand++;
- }
- }
- void print_cards(struct table *dealer, struct table *player, char check_winner) {
- const char *deck[SUITS][CARDS] = {{"🂡","🂢","🂣","🂤","🂥","🂦","🂧","🂨","🂩","🂪","🂫","🂭","🂮"},
- {"🂱","🂲","🂳","🂴","🂵","🂶","🂷","🂸","🂹","🂺","🂻","🂽","🂾"},
- {"🃁","🃂","🃃","🃄","🃅","🃆","🃇","🃈","🃉","🃊","🃋","🃍","🃎"},
- {"🃑","🃒","🃓","🃔","🃕","🃖","🃗","🃘","🃙","🃚","🃛","🃝","🃞"}};
- int i;
- int suit_pick, card_pick;
- printf("Dealer's cards:\n\n");
- if (check_winner != 0) {
- suit_pick = dealer->cards[0] / CARDS;
- card_pick = dealer->cards[0] % CARDS;
- printf("%s ", deck[suit_pick][card_pick]);
- }
- else
- printf("%s ", "🂠");
- for (i = 0; i < dealer->hand; i++) { /* print dealer's cards */
- if (i == 0)
- continue;
- else {
- suit_pick = dealer->cards[i] / CARDS;
- card_pick = dealer->cards[i] % CARDS;
- printf("%s ", deck[suit_pick][card_pick]);
- }
- }
- printf("\n\n");
- printf("Your cards:\n\n");
- for (i = 0; i < player->hand; i++) { /* print player's cards */
- suit_pick = player->cards[i] / CARDS;
- card_pick = player->cards[i] % CARDS;
- printf("%s ", deck[suit_pick][card_pick]);
- }
- while (i < 17) {
- printf("\n"); /* Clears screen by printing a number of blank lines. */
- i++;
- }
- }
- void init(struct table *dealer, struct table *player) {
- player->total = 0;
- dealer->total = 0;
- printf("\n\nStep right up to the Blackjack tables\n\n");
- if (player->name[0] == 0) {
- printf("\nWhat is your first name? ");
- do {
- fgets(player->name, BUF_NAME, stdin);
- if(!isprint(player->name[0])) {
- printf("Please enter a valid name\n");
- continue;
- }
- else
- break;
- } while (1);
- printf("Ok, %sGet ready for casino action * Blackjack pays 3 to 2 * You're credit is 50$ * Press enter to start\n\n", player->name);
- bet(player);
- }
- }
- int findWinner (struct table *dealer, struct table *player, char check_stand) {
- char winner;
- if (check_stand == 1) {
- if (dealer->total == player->total) /* Tie */
- winner = 5;
- else if ( (player->total <= 21) && (player->total > 17) ) /* Player wins */
- winner = 2;
- else /* Dealer wins */
- winner = 3;
- }
- else {
- if (player->total > 21) /* If the player exceeds a sum of 21 "busts", loses, even if the dealer also exceeds 21 */
- winner = 3;
- else if ( (player->total == 21) && (dealer->total == 21) ) /* A tie */
- winner = 5;
- else if ( (player->total == 21) && (dealer->total != 21) ) /* Player wins with Blackjack */
- winner = 1;
- else if ( (player->total <= 21) && (dealer->total > 21) ) /* Player wins */
- winner = 2;
- else if ( (dealer->total == 21) && (player->total != 21) ) /* Dealer wins */
- winner = 3;
- else
- winner = NO_WINNER; /* no winner found */
- }
- if (winner != NO_WINNER) {
- print_cards(dealer, player, winner);
- player->hand = dealer->hand = 0; /* zero number of cards dealt */
- player->aces = dealer->aces = 0; /* zero number of aces */
- if (winner == 1) {
- printf("Congratulation you win!\n");
- player->credit += (player->bet * 3)/2;
- }
- else if (winner == 2) {
- printf("Congratulation you win!\n");
- player->credit += player->bet;
- }
- else if (winner == 3) {
- printf("Sorry, Dealer wins\n");
- player->credit -= player->bet;
- }
- else
- printf("No one wins\n");
- printf("credit: %d$\n", player->credit);
- if (player->credit <= 0) {
- printf("Sorry you bankrupted\n");
- return 4; /* Game Over */
- }
- else {
- char ans;
- printf("Play again (Y/N)?\n");
- do {
- ans = toupper(getchar());
- if(ans == 'Y') {
- bet(player);
- return 1;
- }
- else if (ans == 'N') {
- printf("Goodbye!\n");
- return 4; /* Game Over */
- }
- else
- continue;
- } while(1);
- }
- }
- print_cards(dealer, player, winner);
- return 0;
- }
- void bet (struct table *player) {
- printf("What's your bet?\n");
- while ( (scanf("%d", &player->bet) != 1) || player->bet > player->credit)
- {
- while (getchar() != '\n');
- printf ("Enter a valid sum, your credit is %d$:\n", player->credit);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement