From 6d5598fc86c4dedb59569e2401955f748949c150 Mon Sep 17 00:00:00 2001 From: Maalvika Bhat Date: Wed, 5 Feb 2020 18:07:03 -0500 Subject: [PATCH] Actual submission --- exercises/ex01/cards.c | 110 ++++++++++++++++++++++++++++------------- 1 file changed, 76 insertions(+), 34 deletions(-) diff --git a/exercises/ex01/cards.c b/exercises/ex01/cards.c index 4e41deb2..4806a077 100644 --- a/exercises/ex01/cards.c +++ b/exercises/ex01/cards.c @@ -1,38 +1,80 @@ +/* + * Exercise 01 Maalvika Bhat SoftSys2020 + */ + #include #include -int main() -{ - char card_name[3]; - int count = 0; - while (card_name[0] != 'x'){ - puts("Enter the card_name: "); - scanf("%2s", card_name); - int val = 0; - switch(card_name[0]) { - case 'K': - case 'Q': - case 'J': - val = 10; - break; - case 'A': - val = 11; - break; - case 'X': - continue; - default: - val = atoi(card_name); - if ((val < 1) || (val > 10)){ - puts("Don't understand value"); - continue; - } - } - if ((val > 2) && (val < 7)) { - count ++; - } else if (val == 10) { - count--; - } - printf("Current count: %i\n", count); - } - return 0; +/* + * Prompts the user for the name of the card that has been played. + * + * prompt: Message to display + * card_Name: User-input result + */ +void getCardName(char *prompt, char *card_name) { + puts(prompt); + scanf("%2s", card_name); +} + +/* + * Returns the value by which the running total should increase. + * + * val: The number value of the card that has been played + */ +int getIncrement(int val) { + if ((val > 2) && (val < 7)) { + return 1; + } else if (val == 10) { + return -1; + } else { + return 0; + } +} + +/* + * Prints the total of the cards played so far. + * + * total: The current total + */ +void printTotal(int current) { + printf("Current count: %i\n", current); +} + +/* + * Simulates a game of blackjack. + */ +int play() { + char card_name[3]; + int count = 0; + int val = 0; + do { + getCardName("Enter the card_name: ", card_name); + switch(card_name[0]) { + case 'K': + case 'Q': + case 'J': + val = 10; + break; + case 'A': + val = 11; + break; + case 'X': + continue; + default: + val = atoi(card_name); + if ((val < 1) || (val > 10)) { + puts("I don't understand that value!"); + continue; + } + } + printTotal(count + getIncrement(val)); + } while (card_name[0] != 'X'); + return 0; +} + +/* + * The main function. + */ +int main() { + return play(); }