Advertisement
edensheiko

Embedded C Final Project Black-Jack Game (v1.4.0) Created By : Eden Sheiko

Sep 25th, 2022
1,728
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 16.49 KB | Gaming | 0 0
  1. //
  2. // Created by Eden on 23/09/2022.
  3. //
  4.  
  5. #include "Card.h"
  6.  
  7.  
  8.  
  9. uint8_t Card_get_rank(card_t* card) {
  10.     return ((card -> data) & (15 /*binary 1111*/ << 2)) >> 2;
  11. }
  12.  
  13. uint8_t Card_get_suit(card_t* card) {
  14.     return (card -> data) & 3 /*binary 11*/;
  15. }
  16.  
  17. void Card_set_rank(card_t* card, uint8_t rank) {
  18.     (card -> data) = (card -> data) | (rank << 2);
  19. }
  20.  
  21. void Card_set_suit(card_t* card, uint8_t suit) {
  22.     (card -> data) = (card -> data) | suit;
  23. }
  24.  
  25. void print_new_game()
  26. {
  27.     printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
  28.     printf("\t \t New Game \n");
  29.     printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
  30. }
  31.  
  32. void print_game_over()
  33. {
  34.     printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
  35.     printf("\t \t Game Over!!! \n");
  36.     printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
  37. }
  38.  
  39. void print_game_name()
  40. {
  41.     printf("Embedded C Final Project Black-Jack Game (v1.4.0)\n");
  42.     printf(" \t Created By : Eden Sheiko\n");
  43. }
  44.  
  45. void print_bust()
  46. {
  47.     printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
  48.     printf("\t \t BUST!!! \n");
  49.     printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
  50. }
  51.  
  52. void print_you_lose()
  53. {
  54.     printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
  55.     printf("\t \t YOU LOSE!!! \n");
  56.     printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
  57. }
  58.  
  59. void print_dealer_bust()
  60. {
  61.     printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
  62.     printf("\t \t Dealer Bust!!! \n");
  63.     printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
  64. }
  65.  
  66.  
  67. void print_draw()
  68. {
  69.     printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
  70.     printf("\t \t DRAW!!! \n");
  71.     printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
  72. }
  73.  
  74. void print_dealer_wins()
  75. {
  76.     printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
  77.     printf("\t \t Dealer wins!!! \n");
  78.     printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
  79. }
  80.  
  81. void print_player_wins()
  82. {
  83.     printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
  84.     printf("\t \t Player wins!!! \n");
  85.     printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
  86. }
  87.  
  88. //
  89.  
  90.  
  91. //
  92. // Created by Eden on 23/09/2022.
  93. //
  94.  
  95. #ifndef WSL_CARD_H
  96. #define WSL_CARD_H
  97.  
  98. #include <stdint.h>
  99. #include <stdio.h>
  100. #include <time.h>
  101. #include <stdlib.h>
  102. #include <stdbool.h>
  103. #include <string.h>
  104.  
  105. #define  SUITS 4
  106.  
  107. #define  RANKS 13
  108.  
  109. typedef struct card{
  110.     uint8_t data;
  111.     struct card* next;
  112. }card_t;
  113.  
  114. uint8_t Card_get_rank(card_t* card);
  115.  
  116. uint8_t Card_get_suit(card_t* card);
  117.  
  118. void Card_set_rank(card_t* card, uint8_t rank);
  119.  
  120. void Card_set_suit(card_t* card, uint8_t suit);
  121.  
  122.  
  123. #endif //WSL_CARD_H
  124. ///
  125. //
  126. // Created by Eden on 23/09/2022.
  127. //
  128.  
  129. #include "CardList.h"
  130. #include <stdlib.h>
  131. const char *suits[SUITS] ={"Spades","Hearts","Clubs","Diamonds"};
  132.  
  133. const char *ranks[RANKS+1]={"None","Ace","2","3","4",
  134.                             "5","6","7","8",
  135.                             "9","10","Jack","Queen","King"};
  136.  
  137.  
  138.  
  139. void init_card_deck(cardlist_t *deck)
  140. {
  141.     card_t *last_node = NULL;
  142.     card_t *new_node = NULL;
  143.     for (uint8_t suit = 0; suit < SUITS; suit++) {
  144.         for (uint8_t rank = 1; rank < RANKS + 1; rank++) {
  145.             new_node = (card_t *) calloc(1, sizeof(*new_node));
  146.             if (new_node == NULL) {
  147.                 perror("dynamic allocation error\n");
  148.                 return;
  149.             }
  150.             Card_set_rank(new_node, rank);
  151.             Card_set_suit(new_node, suit);
  152.  
  153.             if (deck->head == NULL) {
  154.                 deck->head = new_node;
  155.             } else {
  156.                 last_node->next = new_node;
  157.             }
  158.             deck->len++;
  159.             last_node = new_node;
  160.         }
  161.     }
  162. }
  163.  
  164. card_t *get_random_card_from_deck(cardlist_t* list) {
  165.     int index = rand() % list -> len; //52 // if rand 0 its dead
  166.     card_t* tmp = NULL;
  167.     if (index==0)
  168.     {
  169.         tmp = list -> head;
  170.         list->head = list->head->next;
  171.     } else {
  172.         tmp = list->head;
  173.         // arrive to item before requested
  174.         while (index != 1) {
  175.             tmp = tmp->next;
  176.             index--;
  177.         }
  178.         card_t *result = tmp->next;
  179.         tmp->next = result->next;
  180.         tmp = result;
  181.     }
  182.     list -> len--;
  183.     tmp -> next = NULL;
  184.     return tmp;
  185. }
  186.  
  187. void delete_data_node(card_t* ptr)
  188. {
  189.     card_t *tmp = ptr;
  190.     tmp=tmp->next;
  191.     ptr->data=tmp->data;
  192.     ptr->next=tmp->next;
  193.     free(tmp);
  194. }
  195.  
  196. void push_card_to_list(cardlist_t* list,card_t *data) {
  197.     if (list->head==NULL) // case when its empty
  198.     {
  199.         list->head=data;
  200.     } else {
  201.         card_t *ptr = list->head;
  202.         while (ptr->next != NULL) // case when not empty
  203.         {
  204.             ptr=ptr->next;
  205.         }
  206.         ptr->next=data;
  207.     }
  208.     data->next=NULL;
  209.     list->len++;
  210. }
  211.  
  212. void print_player_deck(cardlist_t *list)
  213. {
  214.     printf("Player hand:\n");
  215.     card_t *ptr = list->head;
  216.     while(ptr)
  217.     {
  218.         printf("     %s of %s\n", ranks[Card_get_rank(ptr)], suits[Card_get_suit(ptr)]);
  219.         //printf("---------------\n");
  220.         //printf(" Location Debug %d\n\n", ptr->data);
  221.         ptr=ptr->next;
  222.     }
  223. }
  224.  
  225. void print_dealer_deck(cardlist_t *list)
  226. {
  227.     printf("Dealer hand:\n");
  228.     card_t *ptr = list->head;
  229.     while(ptr)
  230.     {
  231.         printf("     %s of %s\n", ranks[Card_get_rank(ptr)], suits[Card_get_suit(ptr)]);
  232.         //printf("---------------\n");
  233.         //printf(" Location Debug %d\n\n", ptr->data);
  234.         ptr=ptr->next;
  235.     }
  236. }
  237. void print_dealer_deck_half(cardlist_t *list)
  238. {
  239.     printf("Dealer hand:\n");
  240.     card_t *ptr = list->head;
  241.     while(1)
  242.     {
  243.         printf("     %s of %s\n", ranks[Card_get_rank(ptr)], suits[Card_get_suit(ptr)]);
  244.         //printf("---------------\n");
  245.         //printf(" Location Debug %d\n\n", ptr->data);
  246.         break;
  247.     }
  248. }
  249.  
  250. void print_list(cardlist_t *list)
  251. {
  252.     card_t *ptr = list -> head;
  253.     while (ptr)
  254.     {
  255.         printf("%d -> ",ptr->data);
  256.         ptr = ptr->next;
  257.     }
  258.     printf("NULL \n");
  259. }
  260.  
  261. void free_init(cardlist_t *list)
  262. {
  263.     card_t *head = list -> head;
  264.     while(head)
  265.     {
  266.         card_t *tmp = head;
  267.         head=head->next;
  268.         free(tmp);
  269.     }
  270.     list -> head = NULL;
  271. }
  272.  
  273. bool is_black_jack_player(cardlist_t *list)
  274. {
  275.     int sum=0;
  276.     card_t *ptr = list->head;
  277.  
  278.     while(ptr)
  279.     {
  280.         if(Card_get_rank(ptr)==11 ||Card_get_rank(ptr)==12||Card_get_rank(ptr)==13)
  281.         {
  282.             uint8_t tmp = 10;
  283.             sum=sum+tmp;
  284.             ptr=ptr->next;
  285.             if(sum==21)
  286.             {
  287.                 printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
  288.                 printf("\t \t “Black Jack! \n");
  289.                 printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
  290.                 return 1;
  291.             }
  292.         }
  293.         else{
  294.             if (Card_get_rank(ptr) == 1)
  295.             {
  296.                 sum+=10;
  297.                 if(sum>21)
  298.                 {
  299.                     sum-=10;
  300.                 }
  301.             }
  302.             sum=sum+Card_get_rank(ptr);
  303.             ptr=ptr->next;
  304.             if(sum==21)
  305.             {
  306.                 printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
  307.                 printf("\t \t “Black Jack! \n");
  308.                 printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
  309.                 return 1;
  310.             }
  311.         }
  312.  
  313.     }
  314.     return 0;
  315. }
  316.  
  317. bool is_bust(cardlist_t *list)
  318. {
  319.     int sum=0;
  320.     card_t *ptr = list->head;
  321.  
  322.     while(ptr)
  323.     {
  324.         if(Card_get_rank(ptr)==11 ||Card_get_rank(ptr)==12||Card_get_rank(ptr)==13)
  325.         {
  326.             uint8_t tmp = 10;
  327.  
  328.             sum=sum+tmp;
  329.             ptr=ptr->next;
  330.             if(sum>21)
  331.             {
  332.                return 1;
  333.             }
  334.         }
  335.         else{
  336.             if (Card_get_rank(ptr) == 1)
  337.             {
  338.                 sum+=10;
  339.                 if(sum>21)
  340.                 {
  341.                     sum-=10;
  342.                 }
  343.             }
  344.             sum=sum+Card_get_rank(ptr);
  345.             ptr=ptr->next;
  346.             if(sum>21)
  347.             {
  348.  
  349.                 return 1;
  350.             }
  351.         }
  352.  
  353.     }
  354.     return 0;
  355. }
  356.  
  357. int return_sum(cardlist_t *list) // bad
  358. {
  359.     int sum=0;
  360.     card_t *ptr = list->head;
  361.  
  362.     while(ptr)
  363.     {
  364.         if(Card_get_rank(ptr)==11 ||Card_get_rank(ptr)==12||Card_get_rank(ptr)==13)
  365.         {
  366.             uint8_t tmp = 10;
  367.  
  368.             sum=sum+tmp;
  369.             ptr=ptr->next;
  370.  
  371.         }
  372.         else{
  373.             if (Card_get_rank(ptr) == 1)
  374.             {
  375.                 sum+=10;
  376.                 if(sum>21)
  377.                 {
  378.                     sum-=10;
  379.                 }
  380.             }
  381.             sum=sum+Card_get_rank(ptr);
  382.             ptr=ptr->next;
  383.  
  384.         }
  385.  
  386.     }
  387.     return sum;
  388. }
  389.  
  390. //
  391.  
  392.  
  393. //
  394. // Created by Eden on 23/09/2022.
  395. //
  396.  
  397. #ifndef WSL_CARDLIST_H
  398. #define WSL_CARDLIST_H
  399.  
  400. #include "Card.h"
  401.  
  402.  
  403.  
  404. typedef struct cardlist{
  405.     card_t *head;
  406.     size_t len; // cache
  407. }cardlist_t;
  408.  
  409.  
  410. void init_card_deck(cardlist_t* deck);
  411.  
  412. card_t *get_random_card_from_deck(cardlist_t* deck);
  413.  
  414. void delete_data_node(card_t*);
  415.  
  416. void push_card_to_list(cardlist_t*,card_t *);
  417.  
  418. void print_player_deck(cardlist_t *);
  419.  
  420. void print_dealer_deck(cardlist_t *);
  421.  
  422. void print_dealer_deck_half(cardlist_t *);
  423.  
  424. void free_init(cardlist_t *);
  425.  
  426. void print_list(cardlist_t *);
  427.  
  428. bool is_black_jack_player(cardlist_t *);
  429.  
  430. bool is_bust(cardlist_t *);
  431.  
  432. int return_sum(cardlist_t *);
  433.  
  434. void print_status_print(int, int);
  435.  
  436. void print_new_game();
  437.  
  438. void print_game_over();
  439.  
  440. void print_game_name();
  441.  
  442. void print_bust();
  443.  
  444. void print_you_lose();
  445.  
  446. void print_dealer_bust();
  447.  
  448. void print_draw();
  449.  
  450. void print_dealer_wins();
  451.  
  452. void print_player_wins();
  453.  
  454. void betting(unsigned int,unsigned int*,int,int*);
  455.  
  456.  
  457. void Lost_bet(unsigned int,unsigned int);
  458.  
  459.  
  460. #endif //WSL_CARDLIST_H
  461.  
  462.  
  463. ///
  464. #include "CardList.h"
  465. #include "time.h"
  466.  
  467.  
  468. void Reset_cards(cardlist_t *,cardlist_t *,cardlist_t *list);
  469.  
  470.  
  471. int main(int argc,char** argv)
  472. {
  473.     print_game_name();
  474.     srand(time(NULL));
  475.  
  476.     unsigned int amount=0;
  477.     int cash=1000;
  478.     int pot=0;
  479.  
  480.     unsigned int player_sum=0;
  481.     unsigned int dealer_sum=0;
  482.  
  483.     int h_s=0;
  484.  
  485.     int player_choice=0;
  486.     cardlist_t deck = {
  487.             .head = NULL,
  488.             .len = 0,
  489.     };
  490.  
  491.     cardlist_t dealer_hand= {
  492.             .head = NULL,
  493.             .len = 0,
  494.     };
  495.  
  496.     cardlist_t player_hand= {
  497.             .head = NULL,
  498.             .len = 0,
  499.     };
  500.  
  501.     init_card_deck(&deck);
  502.     //print_list(&deck);
  503.     while (1) {
  504.  
  505.         print_new_game();
  506.         betting(cash,&cash,pot,&pot);
  507.  
  508.         card_t *random_card_p_1=get_random_card_from_deck(&deck);
  509.         card_t *random_card_p_2=get_random_card_from_deck(&deck);
  510.         card_t *random_card_d_1=get_random_card_from_deck(&deck);
  511.         card_t *random_card_d_2=get_random_card_from_deck(&deck);
  512.  
  513.         push_card_to_list(&player_hand,random_card_p_1);
  514.         push_card_to_list(&player_hand,random_card_p_2);
  515.         push_card_to_list(&dealer_hand,random_card_d_1);
  516.         push_card_to_list(&dealer_hand,random_card_d_2);
  517.  
  518.  
  519.         print_player_deck(&player_hand);
  520.         print_dealer_deck_half(&dealer_hand);
  521.  
  522.             bool is_black_jack = is_black_jack_player(&player_hand);
  523.             if(is_black_jack==1)
  524.             {
  525.                 cash=cash+(pot*2.5);
  526.                 pot=0;
  527.                 //Reset_cards()
  528.             }
  529.             // hit or stand
  530.             printf("Do you wanna hit(press 1) or stand press(press 0) \n?");
  531.             scanf("%d",&h_s);
  532.             fflush(stdin);
  533.             fflush(stdout);
  534.         if (h_s==0) {
  535.  
  536.         }
  537.         else if(h_s==1) {
  538.  
  539.             while (1) {
  540.                 int tmp;
  541.                 printf("to draw card press everything-yes 0-no\n");
  542.                 scanf("%d",&tmp);
  543.                 if(tmp==0)
  544.                 {
  545.                     break;
  546.                 }
  547.                 else
  548.                 {
  549.                     card_t *random_card_p_3 = get_random_card_from_deck(&deck);
  550.                     push_card_to_list(&player_hand, random_card_p_3);
  551.                     bool bust = is_bust(&player_hand);
  552.                     if (bust == 1) {
  553.                         Lost_bet(cash, pot);
  554.                         break;
  555.                     }
  556.                     print_player_deck(&player_hand);
  557.                 }
  558.  
  559.             }
  560.         }
  561.         print_player_deck(&player_hand);
  562.         print_dealer_deck(&dealer_hand);
  563.  
  564. // dealer draw
  565.  
  566.  
  567.         player_sum = return_sum(&player_hand);
  568.         dealer_sum = return_sum(&dealer_hand);
  569.         printf("before update \n");
  570.         printf("player score- >%d\n",player_sum);
  571.         printf("dealer score- >%d\n",dealer_sum);
  572.  
  573.         while(dealer_sum<17)
  574.         {
  575.             card_t *random_card_d_3 = get_random_card_from_deck(&deck);
  576.             push_card_to_list(&dealer_hand, random_card_d_3);
  577.             dealer_sum = return_sum(&dealer_hand);
  578.         }
  579.         printf("after update --------\n");
  580.         printf("player score- >%d\n",player_sum);
  581.         printf("dealer score- >%d\n",dealer_sum);
  582.  
  583.  
  584.             if (dealer_sum > player_sum)
  585.             {
  586.                 print_you_lose();
  587.                 pot=0;
  588.             }
  589.             else if (dealer_sum > 21)
  590.             {
  591.                 print_dealer_bust();
  592.                 cash += (pot * 2);
  593.                 pot = 0;
  594.                 //rest cards
  595.             }
  596.             else if (dealer_sum <= 21 && dealer_sum == player_sum)
  597.             {
  598.                 print_draw();
  599.                 //REST cards
  600.             }
  601.             else if (dealer_sum <= 21 && dealer_sum > player_sum)
  602.             {
  603.                 print_dealer_wins();
  604.                 pot = 0;
  605.                 //rest cards
  606.             }
  607.             else if (dealer_sum <= 21 && dealer_sum < player_sum)
  608.             {
  609.                 print_player_wins();
  610.                 //rest cards
  611.                 cash = cash + (pot * 2);
  612.                 pot = 0;
  613.             }
  614.  
  615.  
  616.  
  617.             // rest cards;
  618.             //Reset_cards(&deck,&dealer_hand,&player_hand);
  619.  
  620.             printf("would you like to continue? press 0 for yes or anything else for no\n");
  621.             scanf("%d",&player_choice);
  622.             while (getchar() != '\n');
  623.             fflush(stdout);
  624.             if (pot==0 && cash < 10)
  625.             {
  626.                 print_game_over();
  627.                 free_init(&dealer_hand);
  628.                 free_init(&player_hand);
  629.                 break;
  630.  
  631.             }
  632.             if(player_choice!=0)
  633.             {
  634.                 free_init(&dealer_hand);
  635.                 free_init(&player_hand);
  636.                 print_game_over();
  637.                 break;
  638.             }
  639.             if (player_hand.len>0 && dealer_hand.len>0)
  640.             {
  641.                 free_init(&dealer_hand);
  642.                 free_init(&player_hand);
  643.             }
  644.         }
  645.     free_init(&deck);
  646.     return 0;
  647.     }
  648.  
  649.  
  650. void print_status_print(int c, int p)
  651. {
  652.     printf("===========================================\n");
  653.     printf("\t\t Cash:%d ,Pot:%d \n",c,p);
  654.     printf("===========================================\n");
  655. }
  656.  
  657. void betting(unsigned int cash,unsigned int *cash_return,int pot,int *pot_return)
  658. {
  659.     unsigned int money;
  660.     print_status_print(cash,pot);
  661.     printf("How much would you like to bet? \n");
  662.     printf("Enter a number in multiples of 10: \n");
  663.     fflush(stdout);
  664.     if(pot==0 && cash <10)
  665.     {
  666.         print_game_over();
  667.         return;
  668.     }
  669.     while(1)
  670.     {
  671.         scanf("%d",&money);
  672.         while (getchar() != '\n');
  673.         if (money % 10 == 0 && money <= cash)
  674.         {
  675.             cash -= money;
  676.             pot += money;
  677.             *pot_return=pot;
  678.             *cash_return=cash;
  679.             //todo: call to Initial Deal: func
  680.             break;
  681.         }
  682.         else
  683.         {
  684.             printf("Error , please Enter a number in multiples of 10:\n");
  685.  
  686.         }
  687.     }
  688. }
  689.  
  690. void Lost_bet(unsigned int cash,unsigned int pot)
  691. {
  692.     pot=0;
  693.     print_bust();
  694. }
  695.  
  696.  
  697. void Reset_cards(cardlist_t *deck,cardlist_t *dealer_hand,cardlist_t *player_hand)
  698. {
  699.     free_init(deck);
  700.     init_card_deck(deck);
  701.     free_init(dealer_hand);
  702.     free_init(player_hand);
  703. }
  704.  
  705. ///
  706. cmake_minimum_required(VERSION 3.23)
  707. project(WSL C)
  708.  
  709. set(CMAKE_C_STANDARD 99)
  710.  
  711. add_executable(WSL main.c Card.h Card.c CardList.h CardList.c)
  712.  
  713.  
Tags: c-game bj
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement