Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Created by Eden on 23/09/2022.
- //
- #include "Card.h"
- uint8_t Card_get_rank(card_t* card) {
- return ((card -> data) & (15 /*binary 1111*/ << 2)) >> 2;
- }
- uint8_t Card_get_suit(card_t* card) {
- return (card -> data) & 3 /*binary 11*/;
- }
- void Card_set_rank(card_t* card, uint8_t rank) {
- (card -> data) = (card -> data) | (rank << 2);
- }
- void Card_set_suit(card_t* card, uint8_t suit) {
- (card -> data) = (card -> data) | suit;
- }
- void print_new_game()
- {
- printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
- printf("\t \t New Game \n");
- printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
- }
- void print_game_over()
- {
- printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
- printf("\t \t Game Over!!! \n");
- printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
- }
- void print_game_name()
- {
- printf("Embedded C Final Project Black-Jack Game (v1.4.0)\n");
- printf(" \t Created By : Eden Sheiko\n");
- }
- void print_bust()
- {
- printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
- printf("\t \t BUST!!! \n");
- printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
- }
- void print_you_lose()
- {
- printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
- printf("\t \t YOU LOSE!!! \n");
- printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
- }
- void print_dealer_bust()
- {
- printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
- printf("\t \t Dealer Bust!!! \n");
- printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
- }
- void print_draw()
- {
- printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
- printf("\t \t DRAW!!! \n");
- printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
- }
- void print_dealer_wins()
- {
- printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
- printf("\t \t Dealer wins!!! \n");
- printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
- }
- void print_player_wins()
- {
- printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
- printf("\t \t Player wins!!! \n");
- printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
- }
- //
- //
- // Created by Eden on 23/09/2022.
- //
- #ifndef WSL_CARD_H
- #define WSL_CARD_H
- #include <stdint.h>
- #include <stdio.h>
- #include <time.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <string.h>
- #define SUITS 4
- #define RANKS 13
- typedef struct card{
- uint8_t data;
- struct card* next;
- }card_t;
- uint8_t Card_get_rank(card_t* card);
- uint8_t Card_get_suit(card_t* card);
- void Card_set_rank(card_t* card, uint8_t rank);
- void Card_set_suit(card_t* card, uint8_t suit);
- #endif //WSL_CARD_H
- ///
- //
- // Created by Eden on 23/09/2022.
- //
- #include "CardList.h"
- #include <stdlib.h>
- const char *suits[SUITS] ={"Spades","Hearts","Clubs","Diamonds"};
- const char *ranks[RANKS+1]={"None","Ace","2","3","4",
- "5","6","7","8",
- "9","10","Jack","Queen","King"};
- void init_card_deck(cardlist_t *deck)
- {
- card_t *last_node = NULL;
- card_t *new_node = NULL;
- for (uint8_t suit = 0; suit < SUITS; suit++) {
- for (uint8_t rank = 1; rank < RANKS + 1; rank++) {
- new_node = (card_t *) calloc(1, sizeof(*new_node));
- if (new_node == NULL) {
- perror("dynamic allocation error\n");
- return;
- }
- Card_set_rank(new_node, rank);
- Card_set_suit(new_node, suit);
- if (deck->head == NULL) {
- deck->head = new_node;
- } else {
- last_node->next = new_node;
- }
- deck->len++;
- last_node = new_node;
- }
- }
- }
- card_t *get_random_card_from_deck(cardlist_t* list) {
- int index = rand() % list -> len; //52 // if rand 0 its dead
- card_t* tmp = NULL;
- if (index==0)
- {
- tmp = list -> head;
- list->head = list->head->next;
- } else {
- tmp = list->head;
- // arrive to item before requested
- while (index != 1) {
- tmp = tmp->next;
- index--;
- }
- card_t *result = tmp->next;
- tmp->next = result->next;
- tmp = result;
- }
- list -> len--;
- tmp -> next = NULL;
- return tmp;
- }
- void delete_data_node(card_t* ptr)
- {
- card_t *tmp = ptr;
- tmp=tmp->next;
- ptr->data=tmp->data;
- ptr->next=tmp->next;
- free(tmp);
- }
- void push_card_to_list(cardlist_t* list,card_t *data) {
- if (list->head==NULL) // case when its empty
- {
- list->head=data;
- } else {
- card_t *ptr = list->head;
- while (ptr->next != NULL) // case when not empty
- {
- ptr=ptr->next;
- }
- ptr->next=data;
- }
- data->next=NULL;
- list->len++;
- }
- void print_player_deck(cardlist_t *list)
- {
- printf("Player hand:\n");
- card_t *ptr = list->head;
- while(ptr)
- {
- printf(" %s of %s\n", ranks[Card_get_rank(ptr)], suits[Card_get_suit(ptr)]);
- //printf("---------------\n");
- //printf(" Location Debug %d\n\n", ptr->data);
- ptr=ptr->next;
- }
- }
- void print_dealer_deck(cardlist_t *list)
- {
- printf("Dealer hand:\n");
- card_t *ptr = list->head;
- while(ptr)
- {
- printf(" %s of %s\n", ranks[Card_get_rank(ptr)], suits[Card_get_suit(ptr)]);
- //printf("---------------\n");
- //printf(" Location Debug %d\n\n", ptr->data);
- ptr=ptr->next;
- }
- }
- void print_dealer_deck_half(cardlist_t *list)
- {
- printf("Dealer hand:\n");
- card_t *ptr = list->head;
- while(1)
- {
- printf(" %s of %s\n", ranks[Card_get_rank(ptr)], suits[Card_get_suit(ptr)]);
- //printf("---------------\n");
- //printf(" Location Debug %d\n\n", ptr->data);
- break;
- }
- }
- void print_list(cardlist_t *list)
- {
- card_t *ptr = list -> head;
- while (ptr)
- {
- printf("%d -> ",ptr->data);
- ptr = ptr->next;
- }
- printf("NULL \n");
- }
- void free_init(cardlist_t *list)
- {
- card_t *head = list -> head;
- while(head)
- {
- card_t *tmp = head;
- head=head->next;
- free(tmp);
- }
- list -> head = NULL;
- }
- bool is_black_jack_player(cardlist_t *list)
- {
- int sum=0;
- card_t *ptr = list->head;
- while(ptr)
- {
- if(Card_get_rank(ptr)==11 ||Card_get_rank(ptr)==12||Card_get_rank(ptr)==13)
- {
- uint8_t tmp = 10;
- sum=sum+tmp;
- ptr=ptr->next;
- if(sum==21)
- {
- printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
- printf("\t \t “Black Jack! \n");
- printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
- return 1;
- }
- }
- else{
- if (Card_get_rank(ptr) == 1)
- {
- sum+=10;
- if(sum>21)
- {
- sum-=10;
- }
- }
- sum=sum+Card_get_rank(ptr);
- ptr=ptr->next;
- if(sum==21)
- {
- printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
- printf("\t \t “Black Jack! \n");
- printf("+++++++++++++++++++++++++++++++++++++++++++++++++\n");
- return 1;
- }
- }
- }
- return 0;
- }
- bool is_bust(cardlist_t *list)
- {
- int sum=0;
- card_t *ptr = list->head;
- while(ptr)
- {
- if(Card_get_rank(ptr)==11 ||Card_get_rank(ptr)==12||Card_get_rank(ptr)==13)
- {
- uint8_t tmp = 10;
- sum=sum+tmp;
- ptr=ptr->next;
- if(sum>21)
- {
- return 1;
- }
- }
- else{
- if (Card_get_rank(ptr) == 1)
- {
- sum+=10;
- if(sum>21)
- {
- sum-=10;
- }
- }
- sum=sum+Card_get_rank(ptr);
- ptr=ptr->next;
- if(sum>21)
- {
- return 1;
- }
- }
- }
- return 0;
- }
- int return_sum(cardlist_t *list) // bad
- {
- int sum=0;
- card_t *ptr = list->head;
- while(ptr)
- {
- if(Card_get_rank(ptr)==11 ||Card_get_rank(ptr)==12||Card_get_rank(ptr)==13)
- {
- uint8_t tmp = 10;
- sum=sum+tmp;
- ptr=ptr->next;
- }
- else{
- if (Card_get_rank(ptr) == 1)
- {
- sum+=10;
- if(sum>21)
- {
- sum-=10;
- }
- }
- sum=sum+Card_get_rank(ptr);
- ptr=ptr->next;
- }
- }
- return sum;
- }
- //
- //
- // Created by Eden on 23/09/2022.
- //
- #ifndef WSL_CARDLIST_H
- #define WSL_CARDLIST_H
- #include "Card.h"
- typedef struct cardlist{
- card_t *head;
- size_t len; // cache
- }cardlist_t;
- void init_card_deck(cardlist_t* deck);
- card_t *get_random_card_from_deck(cardlist_t* deck);
- void delete_data_node(card_t*);
- void push_card_to_list(cardlist_t*,card_t *);
- void print_player_deck(cardlist_t *);
- void print_dealer_deck(cardlist_t *);
- void print_dealer_deck_half(cardlist_t *);
- void free_init(cardlist_t *);
- void print_list(cardlist_t *);
- bool is_black_jack_player(cardlist_t *);
- bool is_bust(cardlist_t *);
- int return_sum(cardlist_t *);
- void print_status_print(int, int);
- void print_new_game();
- void print_game_over();
- void print_game_name();
- void print_bust();
- void print_you_lose();
- void print_dealer_bust();
- void print_draw();
- void print_dealer_wins();
- void print_player_wins();
- void betting(unsigned int,unsigned int*,int,int*);
- void Lost_bet(unsigned int,unsigned int);
- #endif //WSL_CARDLIST_H
- ///
- #include "CardList.h"
- #include "time.h"
- void Reset_cards(cardlist_t *,cardlist_t *,cardlist_t *list);
- int main(int argc,char** argv)
- {
- print_game_name();
- srand(time(NULL));
- unsigned int amount=0;
- int cash=1000;
- int pot=0;
- unsigned int player_sum=0;
- unsigned int dealer_sum=0;
- int h_s=0;
- int player_choice=0;
- cardlist_t deck = {
- .head = NULL,
- .len = 0,
- };
- cardlist_t dealer_hand= {
- .head = NULL,
- .len = 0,
- };
- cardlist_t player_hand= {
- .head = NULL,
- .len = 0,
- };
- init_card_deck(&deck);
- //print_list(&deck);
- while (1) {
- print_new_game();
- betting(cash,&cash,pot,&pot);
- card_t *random_card_p_1=get_random_card_from_deck(&deck);
- card_t *random_card_p_2=get_random_card_from_deck(&deck);
- card_t *random_card_d_1=get_random_card_from_deck(&deck);
- card_t *random_card_d_2=get_random_card_from_deck(&deck);
- push_card_to_list(&player_hand,random_card_p_1);
- push_card_to_list(&player_hand,random_card_p_2);
- push_card_to_list(&dealer_hand,random_card_d_1);
- push_card_to_list(&dealer_hand,random_card_d_2);
- print_player_deck(&player_hand);
- print_dealer_deck_half(&dealer_hand);
- bool is_black_jack = is_black_jack_player(&player_hand);
- if(is_black_jack==1)
- {
- cash=cash+(pot*2.5);
- pot=0;
- //Reset_cards()
- }
- // hit or stand
- printf("Do you wanna hit(press 1) or stand press(press 0) \n?");
- scanf("%d",&h_s);
- fflush(stdin);
- fflush(stdout);
- if (h_s==0) {
- }
- else if(h_s==1) {
- while (1) {
- int tmp;
- printf("to draw card press everything-yes 0-no\n");
- scanf("%d",&tmp);
- if(tmp==0)
- {
- break;
- }
- else
- {
- card_t *random_card_p_3 = get_random_card_from_deck(&deck);
- push_card_to_list(&player_hand, random_card_p_3);
- bool bust = is_bust(&player_hand);
- if (bust == 1) {
- Lost_bet(cash, pot);
- break;
- }
- print_player_deck(&player_hand);
- }
- }
- }
- print_player_deck(&player_hand);
- print_dealer_deck(&dealer_hand);
- // dealer draw
- player_sum = return_sum(&player_hand);
- dealer_sum = return_sum(&dealer_hand);
- printf("before update \n");
- printf("player score- >%d\n",player_sum);
- printf("dealer score- >%d\n",dealer_sum);
- while(dealer_sum<17)
- {
- card_t *random_card_d_3 = get_random_card_from_deck(&deck);
- push_card_to_list(&dealer_hand, random_card_d_3);
- dealer_sum = return_sum(&dealer_hand);
- }
- printf("after update --------\n");
- printf("player score- >%d\n",player_sum);
- printf("dealer score- >%d\n",dealer_sum);
- if (dealer_sum > player_sum)
- {
- print_you_lose();
- pot=0;
- }
- else if (dealer_sum > 21)
- {
- print_dealer_bust();
- cash += (pot * 2);
- pot = 0;
- //rest cards
- }
- else if (dealer_sum <= 21 && dealer_sum == player_sum)
- {
- print_draw();
- //REST cards
- }
- else if (dealer_sum <= 21 && dealer_sum > player_sum)
- {
- print_dealer_wins();
- pot = 0;
- //rest cards
- }
- else if (dealer_sum <= 21 && dealer_sum < player_sum)
- {
- print_player_wins();
- //rest cards
- cash = cash + (pot * 2);
- pot = 0;
- }
- // rest cards;
- //Reset_cards(&deck,&dealer_hand,&player_hand);
- printf("would you like to continue? press 0 for yes or anything else for no\n");
- scanf("%d",&player_choice);
- while (getchar() != '\n');
- fflush(stdout);
- if (pot==0 && cash < 10)
- {
- print_game_over();
- free_init(&dealer_hand);
- free_init(&player_hand);
- break;
- }
- if(player_choice!=0)
- {
- free_init(&dealer_hand);
- free_init(&player_hand);
- print_game_over();
- break;
- }
- if (player_hand.len>0 && dealer_hand.len>0)
- {
- free_init(&dealer_hand);
- free_init(&player_hand);
- }
- }
- free_init(&deck);
- return 0;
- }
- void print_status_print(int c, int p)
- {
- printf("===========================================\n");
- printf("\t\t Cash:%d ,Pot:%d \n",c,p);
- printf("===========================================\n");
- }
- void betting(unsigned int cash,unsigned int *cash_return,int pot,int *pot_return)
- {
- unsigned int money;
- print_status_print(cash,pot);
- printf("How much would you like to bet? \n");
- printf("Enter a number in multiples of 10: \n");
- fflush(stdout);
- if(pot==0 && cash <10)
- {
- print_game_over();
- return;
- }
- while(1)
- {
- scanf("%d",&money);
- while (getchar() != '\n');
- if (money % 10 == 0 && money <= cash)
- {
- cash -= money;
- pot += money;
- *pot_return=pot;
- *cash_return=cash;
- //todo: call to Initial Deal: func
- break;
- }
- else
- {
- printf("Error , please Enter a number in multiples of 10:\n");
- }
- }
- }
- void Lost_bet(unsigned int cash,unsigned int pot)
- {
- pot=0;
- print_bust();
- }
- void Reset_cards(cardlist_t *deck,cardlist_t *dealer_hand,cardlist_t *player_hand)
- {
- free_init(deck);
- init_card_deck(deck);
- free_init(dealer_hand);
- free_init(player_hand);
- }
- ///
- cmake_minimum_required(VERSION 3.23)
- project(WSL C)
- set(CMAKE_C_STANDARD 99)
- add_executable(WSL main.c Card.h Card.c CardList.h CardList.c)
Advertisement
Comments
-
- works , there are no mem leaks chacked by valgrind
- need to do add Rest cards option.
Add Comment
Please, Sign In to add comment
Advertisement