forked from AllenDowney/ExercisesInC
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fed338d
commit 6d5598f
Showing
1 changed file
with
76 additions
and
34 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,80 @@ | ||
/* | ||
* Exercise 01 Maalvika Bhat SoftSys2020 | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
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(); | ||
} |