Created
November 4, 2014 13:46
-
-
Save jponc/b22b4171738ee61b0e31 to your computer and use it in GitHub Desktop.
BlackJack Game for my youngest brother lol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#define SUIT_MAX_VAL 4 | |
#define NUM_MAX_VAL 13 | |
#define BLACK_JACK_NUM 21 | |
char deck[4][13]; | |
int randSuit() { | |
srand(time(NULL)); | |
return rand() % SUIT_MAX_VAL; | |
} | |
int randCardNum() { | |
srand(time(NULL)); | |
return rand() % NUM_MAX_VAL; | |
} | |
int cardInvalid(int suit, int cardNum) { | |
if (deck[suit][cardNum] == 'x') { | |
return 1; | |
} else { | |
deck[suit][cardNum] = 'x'; | |
return 0; | |
} | |
} | |
const char * parseSuit(suitNum) { | |
const char *suitStr; | |
switch(suitNum) { | |
case 0: suitStr = "Diamond"; break; | |
case 1: suitStr = "Spades"; break; | |
case 2: suitStr = "Clover"; break; | |
case 3: suitStr = "Kamote"; break; | |
} | |
return suitStr; | |
} | |
int appendRandomCard(int total) { | |
int suit, cardNum; | |
do { | |
suit = randSuit(); | |
cardNum = randCardNum(); | |
} while(cardInvalid(suit, cardNum)); | |
cardNum++; | |
printf("Card: %d of %s\n", cardNum, parseSuit(suit)); | |
return cardNum; | |
} | |
int initializeCards(int total) { | |
for (int x=0; x<2; x++) { | |
total += appendRandomCard(total); | |
} | |
return total; | |
} | |
int chooseAgain() { | |
char answer; | |
printf("Pick a Card? [Y/N]"); | |
if (scanf(" %c", &answer) == 1 && answer == 'Y') { | |
return 1; | |
} else { | |
return 0; | |
} | |
} | |
int cardExceeded(int total) { | |
if (total > BLACK_JACK_NUM) { | |
return 1; | |
} else { | |
return 0; | |
} | |
} | |
int userTurn(int total) { | |
while (!cardExceeded(total) && chooseAgain()) { | |
total += appendRandomCard(total); | |
printf("\nYou now have a total of %d\n\n", total); | |
}; | |
return total; | |
} | |
int dealerTurn(int dealerTotal, int userTotal) { | |
while(!cardExceeded(dealerTotal) && (dealerTotal < userTotal)){ | |
dealerTotal += appendRandomCard(dealerTotal); | |
} | |
return dealerTotal; | |
} | |
int main(void) { | |
int userTotal = 0, dealerTotal = 0; | |
printf("----- Initializing --------\n"); | |
printf("User selects...\n"); | |
userTotal = initializeCards(userTotal); | |
printf("\nDealer selects...\n"); | |
dealerTotal = initializeCards(dealerTotal); | |
printf("\n\n========= User's Turn =============\n"); | |
printf("Current User Total: %d\n", userTotal); | |
userTotal = userTurn(userTotal); | |
if (userTotal <= BLACK_JACK_NUM) { | |
printf("\n\n\n\n======== Dealer's Turn ==========\n"); | |
printf("Current Dealer Total: %d\n", dealerTotal); | |
dealerTotal = dealerTurn(dealerTotal, userTotal); | |
} | |
if ((dealerTotal > userTotal) && !(dealerTotal > BLACK_JACK_NUM)) { | |
printf("\nDealer won!"); | |
} else { | |
printf("\nUser won!"); | |
} | |
printf("\n\nFinal score:\nUser = %d\nDealer = %d\n\n", userTotal, dealerTotal); | |
printf("Thank you for playing Black Jack by Albert Ponce\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment