Created
May 8, 2019 21:24
-
-
Save dtanner/b3cf09c59b0fd7e1389f635d817facd6 to your computer and use it in GitHub Desktop.
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
#include <stdio.h> | |
#include <math.h> | |
#include <string.h> | |
#define NUMCARDS 9 | |
#define PRODUCTION 1 /** 0=debug-mode | 1=production mode **/ | |
int getCards(int *); | |
void displayCards(int *, int); | |
int getSuit(int codedNumber); | |
int getCardValue(int codedNumber); | |
int calculateHandValue(int *, int); | |
int simpleSumCards(int *, int numCards); | |
int hasThreeOrMoreNonNumberCards(int *, int numCards); | |
int isPrime(unsigned int number); | |
int nextHighestPowerOfTwo(int value); | |
int main() { | |
int numCards; | |
printf("\n Welcome to Up-To-9-Cards Game!"); | |
printf("\n================================\n"); | |
#if PRODUCTION == 1 | |
/** User Interface - Production mode **/ | |
int cards[NUMCARDS]; | |
numCards = getCards(&cards[0]); | |
#endif // PRODUCTION | |
#if PRODUCTION == 0 | |
/** Test Code - Debug mode **/ | |
printf("\nRunning program in Debug mode\n"); | |
// int cards[] = {4,5,6,7,40,41,42,43,51}; //simpleValue = 50 | |
// int cards[] = {22, 8, 16, 45, 2}; // example 1 - 22 points | |
// int cards[] = {47,9,40,48}; // example 2 - 1232 points | |
// int cards[] = {24}; // example 3 - 15 points | |
int cards[] = {50, 49, 2}; // example 4 - 1003 points | |
numCards = sizeof(cards) / sizeof(int); | |
#endif // PRODUCTION | |
displayCards(&cards[0], numCards); | |
printf("\n\nDone!\n"); | |
return 0; | |
} | |
int getSuit(int codedNumber) { | |
return codedNumber % 4; | |
} | |
int getCardValue(int codedNumber) { | |
return (codedNumber / 4) + 1; | |
} | |
int getCards(int arr[]) { | |
int numCards = 0; | |
int input; | |
while (numCards <= 8) { | |
printf("Please enter a card or enter -1 to end: "); | |
scanf("%d", &input); | |
if (input <= 51 && input >= 0) { | |
arr[numCards] = input; | |
} else if (input == -1) { | |
return numCards; | |
} else { | |
printf("Not a valid card entry\n"); | |
continue; | |
} | |
numCards++; | |
} | |
return numCards; | |
} | |
void displayCards(int arr[], int size) { | |
printf("Your hand is:\n"); | |
for (int i = 0; i < size; i++) { | |
int cardValue = getCardValue(arr[i]); | |
int suitNum = getSuit(arr[i]); | |
char suit[10]; | |
if (suitNum == 0) { | |
strncpy(suit, "Clubs", 10); | |
} else if (suitNum == 1) { | |
strncpy(suit, "Spades", 10); | |
} else if (suitNum == 2) { | |
strncpy(suit, "Diamonds", 10); | |
} else { | |
strncpy(suit, "Hearts", 10); | |
} | |
printf(" %d of %s\n", cardValue, suit); | |
} | |
printf("\n"); | |
int handValue = calculateHandValue(arr, size); | |
printf("The Total value of your hand is:\n%d", handValue); | |
} | |
int calculateHandValue(int arr[], int size) { | |
int baseScore = simpleSumCards(arr, size); | |
int totalValue = baseScore; | |
if (hasThreeOrMoreNonNumberCards(arr, size)) { | |
totalValue = totalValue * totalValue + 7; | |
} | |
if (isPrime(totalValue)) { | |
totalValue = totalValue + nextHighestPowerOfTwo(totalValue); | |
} | |
return totalValue; | |
} | |
int nextHighestPowerOfTwo(int value) { | |
return pow(2, ceil(log2(value))); | |
} | |
int simpleSumCards(int arr[], int numCards) { | |
int sum = 0; | |
for (int i = 0; i < numCards; i++) { | |
sum = sum + getCardValue(arr[i]) - getSuit(arr[i]); | |
} | |
return sum; | |
} | |
int hasThreeOrMoreNonNumberCards(int arr[], int numCards) { | |
int count = 0; | |
for (int i = 0; i < numCards; i++) { | |
if (getCardValue(arr[i]) >= 11 || getCardValue(arr[i]) == 1) { | |
count++; | |
} | |
} | |
return count >= 3; | |
} | |
int isPrime(unsigned int number) { | |
unsigned int i; | |
for (i = 2; i * i <= number / 2; i++) { | |
if (number % i == 0) return 0; | |
} | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment