Advertisement
edensheiko

c - game

Sep 22nd, 2022
890
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.92 KB | None | 0 0
  1. //
  2. // Created by edens on 21/09/2022.
  3. //
  4.  
  5. #ifndef EMBEDDED_C_FINAL_PROJECT_BLACK_JACK_GAME__V1_4_0_CARDLIST_H
  6. #define EMBEDDED_C_FINAL_PROJECT_BLACK_JACK_GAME__V1_4_0_CARDLIST_H
  7.  
  8. #include "Card.h"
  9.  
  10.  
  11.  
  12. typedef struct cardlist{
  13.     card_t *head;
  14.     size_t len; // cache
  15. }cardlist_t;
  16.  
  17.  
  18. void init_card_deck(cardlist_t* deck);
  19.  
  20. card_t *get_random_card_from_deck(cardlist_t* deck);
  21.  
  22. void delete_data_node(card_t*);
  23.  
  24. void push_card_to_list(cardlist_t*,card_t *);
  25.  
  26. void print_player_deck(cardlist_t *);
  27.  
  28. void free_init(cardlist_t *);
  29.  
  30. void print_list(cardlist_t *);
  31.  
  32. #endif //EMBEDDED_C_FINAL_PROJECT_BLACK_JACK_GAME__V1_4_0_CARDLIST_H
  33.  
  34.  
  35. //
  36. // Created by edens on 21/09/2022.
  37. //
  38.  
  39. #ifndef EMBEDDED_C_FINAL_PROJECT_BLACK_JACK_GAME__V1_4_0_CARD_H
  40. #define EMBEDDED_C_FINAL_PROJECT_BLACK_JACK_GAME__V1_4_0_CARD_H
  41.  
  42. #include <stdint.h>
  43. #include <stdio.h>
  44. #include <time.h>
  45. #include <stdlib.h>
  46.  
  47. #define  SUITS 4
  48.  
  49. #define  RANKS 13
  50.  
  51. typedef struct card{
  52.     uint8_t data;
  53.     struct card* next;
  54. }card_t;
  55.  
  56. uint8_t Card_get_rank(card_t* card);
  57.  
  58. uint8_t Card_get_suit(card_t* card);
  59.  
  60. void Card_set_rank(card_t* card, uint8_t rank);
  61.  
  62. void Card_set_suit(card_t* card, uint8_t suit);
  63.  
  64.  
  65.  
  66. #endif //EMBEDDED_C_FINAL_PROJECT_BLACK_JACK_GAME__V1_4_0_CARD_H
  67.  
  68.  
  69. //
  70. // Created by edens on 21/09/2022.
  71. //
  72. #include "CardList.h"
  73. #include <stdlib.h>
  74. const char *suits[SUITS] ={"Spades","Hearts","Clubs","Diamonds"};
  75.  
  76. const char *ranks[RANKS+1]={"None","Ace","2","3","4",
  77.                               "5","6","7","8",
  78.                               "9","10","Jack","Queen","King"};
  79.  
  80. void init_card_deck(cardlist_t *deck)
  81. {
  82.     card_t *last_node = NULL;
  83.     card_t *new_node = NULL;
  84.    for (uint8_t suit = 0; suit < SUITS; suit++) {
  85.        for (uint8_t rank = 1; rank < RANKS + 1; rank++) {
  86.            new_node = (card_t *) calloc(1, sizeof(*new_node));
  87.            if (new_node == NULL) {
  88.                perror("dynamic allocation error\n");
  89.                return;
  90.            }
  91.            Card_set_rank(new_node, rank);
  92.            Card_set_suit(new_node, suit);
  93.  
  94.            if (deck->head == NULL) {
  95.                deck->head = new_node;
  96.            } else {
  97.                last_node->next = new_node;
  98.            }
  99.            deck->len++;
  100.            last_node = new_node;
  101.        }
  102.     }
  103. }
  104.  
  105. card_t *get_random_card_from_deck(cardlist_t* list) {
  106.     int index = rand() % list -> len; //52 // if rand 0 its dead
  107.     card_t* tmp = NULL;
  108.     if (index==0)
  109.     {
  110.         tmp = list -> head;
  111.         list->head = list->head->next;
  112.     } else {
  113.         tmp = list->head;
  114.         // arrive to item before requested
  115.         while (index != 1) {
  116.             tmp = tmp->next;
  117.             index--;
  118.         }
  119.         card_t *result = tmp->next;
  120.         tmp->next = result->next;
  121.         tmp = result;
  122.     }
  123.     list -> len--;
  124.     tmp -> next = NULL;
  125.     return tmp;
  126. }
  127.  
  128. void delete_data_node(card_t* ptr)
  129. {
  130.     card_t *tmp = ptr;
  131.     tmp=tmp->next;
  132.     ptr->data=tmp->data;
  133.     ptr->next=tmp->next;
  134.     free(tmp);
  135. }
  136.  
  137. void push_card_to_list(cardlist_t* list,card_t *data) {
  138.     if (list->head==NULL) // case when its empty
  139.     {
  140.         list->head=data;
  141.     } else {
  142.         card_t *ptr = list->head;
  143.         while (ptr->next != NULL) // case when not empty
  144.         {
  145.             ptr=ptr->next;
  146.         }
  147.         ptr->next=data;
  148.     }
  149.     data->next=NULL;
  150.     list->len++;
  151. }
  152.  
  153. void print_player_deck(cardlist_t *list)
  154. {
  155.     printf("your Hand: \n");
  156.     card_t *ptr = list->head;
  157.     while(ptr)
  158.     {
  159.         printf("     %s of %s\n", ranks[Card_get_rank(ptr)], suits[Card_get_suit(ptr)]);
  160.         printf("---------------\n");
  161.         printf(" Location Debug %d\n\n", ptr->data);
  162.         ptr=ptr->next;
  163.     }
  164. }
  165.  
  166. void print_list(cardlist_t *list)
  167. {
  168.     card_t *ptr = list -> head;
  169.     while (ptr)
  170.     {
  171.         printf("%d -> ",ptr->data);
  172.         ptr = ptr->next;
  173.     }
  174.     printf("NULL \n");
  175. }
  176.  
  177. void free_init(cardlist_t *list)
  178. {
  179.     card_t *head = list -> head;
  180.     while(head)
  181.     {
  182.         card_t *tmp = head;
  183.         head=head->next;
  184.         free(tmp);
  185.     }
  186.     list -> head = NULL;
  187. }
  188.  
  189. //
  190. // Created by edens on 21/09/2022.
  191. //
  192. #include "Card.h"
  193. #include <stdlib.h>
  194. #include <stdio.h>
  195.  
  196.  
  197. uint8_t Card_get_rank(card_t* card) {
  198.     return ((card -> data) & (15 /*binary 1111*/ << 2)) >> 2;
  199. }
  200.  
  201. uint8_t Card_get_suit(card_t* card) {
  202.     return (card -> data) & 3 /*binary 11*/;
  203. }
  204.  
  205. void Card_set_rank(card_t* card, uint8_t rank) {
  206.     (card -> data) = (card -> data) | (rank << 2);
  207. }
  208.  
  209. void Card_set_suit(card_t* card, uint8_t suit) {
  210.     (card -> data) = (card -> data) | suit;
  211. }
  212.  
  213. #include "CardList.h"
  214. #include "time.h"
  215. int main(int argc,char** argv)
  216. {
  217.     srand(time(NULL));
  218.     unsigned int amount=0;
  219.     int cash=1000;
  220.     int pot=0;
  221.     unsigned int player_sum;
  222.     unsigned int dealer_sum;
  223.     int player_choice=0;
  224.     cardlist_t deck = {
  225.             .head = NULL,
  226.             .len = 0,
  227.     };
  228.  
  229.     cardlist_t dealer_hand= {
  230.             .head = NULL,
  231.             .len = 0,
  232.     };
  233.  
  234.     cardlist_t player_hand= {
  235.             .head = NULL,
  236.             .len = 0,
  237.     };
  238.  
  239.     init_card_deck(&deck);
  240.     print_list(&deck);
  241.     printf("Embedded C Final Project Black-Jack Game (v1.4.0)\n");
  242.     printf(" \t Created By : Eden Sheiko\n");
  243.  
  244.     while (1) {
  245.         printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
  246.         printf("\t \t New Game \n");
  247.         printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
  248.  
  249.         printf("How much would you like to bet? \n");
  250.         printf("Enter a number in multiples of 10: \n");
  251.         fflush(stdout);
  252.  
  253.         scanf("%d",&amount);
  254.         while (getchar() != '\n');
  255.         if (amount % 10 == 0 )
  256.         {
  257.             card_t *random_card_p_1=get_random_card_from_deck(&deck);
  258.             card_t *random_card_p_2=get_random_card_from_deck(&deck);
  259.             card_t *random_card_d_1=get_random_card_from_deck(&deck);
  260.             card_t *random_card_d_2=get_random_card_from_deck(&deck);
  261.  
  262.             push_card_to_list(&player_hand,random_card_p_1);
  263.             push_card_to_list(&player_hand,random_card_p_2);
  264.             push_card_to_list(&dealer_hand,random_card_d_1);
  265.             push_card_to_list(&dealer_hand,random_card_d_2);
  266.             print_player_deck(&player_hand);
  267.  
  268.             printf("would you like to continue? press 0 for yes or anything else for no\n");
  269.             scanf("%d",&player_choice);
  270.             while (getchar() != '\n');
  271.             fflush(stdout);
  272.             if(player_choice!=0)
  273.             {
  274.                 break;
  275.             }
  276.             if (player_hand.len>0 && dealer_hand.len>0)
  277.             {
  278.                 free_init(&dealer_hand);
  279.                 free_init(&player_hand);
  280.             }
  281.         }
  282.         else
  283.         {
  284.             printf("Error , please Enter a number in multiples of 10:\n");
  285.         }
  286.     }
  287.     free_init(&deck);
  288.     return 0;
  289. }
  290.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement