-
-
Save veeableful/b0fd34b39fd82b65236bd0f16155e8c3 to your computer and use it in GitHub Desktop.
Coder Radio Challenge #1
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 <string.h> | |
#define LEN(x) (sizeof(x) / sizeof(x[0])) | |
#define NUM_USER_HANDS (28) | |
#define NUM_DEALER_HANDS (10) | |
#define CHART_SIZE (NUM_USER_HANDS * NUM_DEALER_HANDS) | |
#define H (1) /* Hit */ | |
#define S (2) /* Stand */ | |
#define D (3) /* Double if allowed, otherwise Hit */ | |
#define DS (4) /* Double if allowed, otherwise Stand */ | |
#define SP (5) /* Split */ | |
#define XH (6) /* Surrender if allowed, otherwise Hit */ | |
#define XP (7) /* Surrender if allowed, otherwise Split */ | |
#define XS (8) /* Surrender if allowed, otherwise Stand */ | |
/* Help */ | |
static const char *HELP = | |
"usage: %s [USER HAND] [DEALER HAND]\n" | |
"for example: %s A,5 7\n" | |
"\n"; | |
/* User hands */ | |
static char *user_hands[NUM_USER_HANDS] = { | |
"8", | |
"9", | |
"10", | |
"11", | |
"12", | |
"13", | |
"14", | |
"15", | |
"16", | |
"17", | |
"A,2", | |
"A,3", | |
"A,4", | |
"A,5", | |
"A,6", | |
"A,7", | |
"A,8", | |
"A,9", | |
"2,2", | |
"3,3", | |
"4,4", | |
"5,5", | |
"6,6", | |
"7,7", | |
"8,8", | |
"9,9", | |
"10,10", | |
"A,A" | |
}; | |
/* Dealer hands */ | |
static char *dealer_hands[NUM_DEALER_HANDS] = { | |
"2", | |
"3", | |
"4", | |
"5", | |
"6", | |
"7", | |
"8", | |
"9", | |
"10", | |
"A", | |
}; | |
/* Strategy chart */ | |
static char chart[CHART_SIZE] = { | |
/* 2 3 4 5 6 7 8 9 10 A */ | |
/* 8 */ H, H, H, H, H, H, H, H, H, H, | |
/* 9 */ H, D, D, D, D, H, H, H, H, H, | |
/* 10 */ D, D, D, D, D, D, D, D, H, H, | |
/* 11 */ D, D, D, D, D, D, D, D, D, D, | |
/* 12 */ H, H, S, S, S, H, H, H, H, H, | |
/* 13 */ S, S, S, S, S, H, H, H, H, H, | |
/* 14 */ S, S, S, S, S, H, H, H, H, H, | |
/* 15 */ S, S, S, S, S, H, H, H, XH, XH, | |
/* 16 */ S, S, S, S, S, H, H, XH, XH, XH, | |
/* 17 */ S, S, S, S, S, S, S, S, S, XS, | |
/* A,2 */ H, H, H, D, D, H, H, H, H, H, | |
/* A,3 */ H, H, H, D, D, H, H, H, H, H, | |
/* A,4 */ H, H, D, D, D, H, H, H, H, H, | |
/* A,5 */ H, H, D, D, D, H, H, H, H, H, | |
/* A,6 */ H, D, D, D, D, H, H, H, H, H, | |
/* A,7 */ S, DS, DS, DS, DS, S, S, H, H, H, | |
/* A,8 */ S, S, S, S, S, S, S, S, S, S, | |
/* A,9 */ S, S, S, S, S, S, S, S, S, S, | |
/* 2,2 */ SP, SP, SP, SP, SP, SP, H, H, H, H, | |
/* 3,3 */ SP, SP, SP, SP, SP, SP, H, H, H, H, | |
/* 4,4 */ H, H, H, SP, SP, H, H, H, H, H, | |
/* 5,5 */ D, D, D, D, D, D, D, D, H, H, | |
/* 6,6 */ SP, SP, SP, SP, SP, H, H, H, H, H, | |
/* 7,7 */ SP, SP, SP, SP, SP, SP, H, H, H, H, | |
/* 8,8 */ SP, SP, SP, SP, SP, SP, SP, SP, SP, XP, | |
/* 9,9 */ SP, SP, SP, SP, SP, S, SP, SP, S, S, | |
/* 10,10 */ S, S, S, S, S, S, S, S, S, S, | |
/* A,A */ SP, SP, SP, SP, SP, SP, SP, SP, SP, SP, | |
}; | |
static int find_user_hand(const char *s) | |
{ | |
int i; | |
for (i = 0; i < LEN(user_hands); i++) { | |
if (strcmp(s, user_hands[i]) == 0) | |
return i; | |
} | |
return -1; | |
} | |
static int find_dealer_hand(const char *s) | |
{ | |
int i; | |
for (i = 0; i < LEN(dealer_hands); i++) { | |
if (strcmp(s, dealer_hands[i]) == 0) | |
return i; | |
} | |
return -1; | |
} | |
static const char * suggest_action(int user_hand_index, int dealer_hand_index) | |
{ | |
const char *s; | |
int chart_index; | |
int action_code; | |
/* Check user hand index */ | |
if (user_hand_index < 0 || user_hand_index > NUM_USER_HANDS) { | |
fprintf(stdout, "invalid user hand index: %d\n", user_hand_index); | |
return NULL; | |
} | |
/* Check dealer hand index */ | |
if (dealer_hand_index < 0 || dealer_hand_index > NUM_USER_HANDS) { | |
fprintf(stdout, "invalid dealer hand index: %d\n", dealer_hand_index); | |
return NULL; | |
} | |
/* Determine action code from the chart */ | |
chart_index = dealer_hand_index + user_hand_index * LEN(dealer_hands); | |
action_code = chart[chart_index]; | |
/* Convert action code into human-readable string */ | |
switch (action_code) { | |
case H: | |
s = "Hit"; | |
break; | |
case S: | |
s = "Stand"; | |
break; | |
case D: | |
s = "Double if allowed, otherwise Hit"; | |
break; | |
case DS: | |
s = "Double if allowed, otherwise Stand"; | |
break; | |
case SP: | |
s = "Split"; | |
break; | |
case XH: | |
s = "Surrender if allowed, otherwise Hit"; | |
break; | |
case XP: | |
s = "Surrender if allowed, otherwise Split"; | |
break; | |
case XS: | |
s = "Surrender if allowed, otherwise Stand"; | |
break; | |
default: | |
s = "(unknown action)"; | |
} | |
return s; | |
} | |
int main(int argc, char **argv) | |
{ | |
int user_hand_index; | |
int dealer_hand_index; | |
/* Print help */ | |
if (argc < 3) { | |
fprintf(stdout, HELP, argv[0], argv[0]); | |
return 0; | |
} | |
/* Find user hand */ | |
user_hand_index = find_user_hand(argv[1]); | |
if (user_hand_index < 0) { | |
fprintf(stdout, "invalid user hand\n"); | |
return -1; | |
} | |
fprintf(stdout, "user hand: %s\n", user_hands[user_hand_index]); | |
/* Find dealer hand */ | |
dealer_hand_index = find_dealer_hand(argv[2]); | |
if (dealer_hand_index < 0) { | |
fprintf(stdout, "invalid dealer hand\n"); | |
return -1; | |
} | |
fprintf(stdout, "dealer hand: %s\n", dealer_hands[dealer_hand_index]); | |
/* Print suggested action */ | |
fprintf(stdout, "Suggested action is %s\n", suggest_action(user_hand_index, dealer_hand_index)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment