Skip to content

Commit

Permalink
Actual submission
Browse files Browse the repository at this point in the history
  • Loading branch information
maalvikabhat committed Feb 5, 2020
1 parent fed338d commit 6d5598f
Showing 1 changed file with 76 additions and 34 deletions.
110 changes: 76 additions & 34 deletions exercises/ex01/cards.c
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();
}

0 comments on commit 6d5598f

Please sign in to comment.