Created
April 19, 2014 00:10
-
-
Save kesne/11069250 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
/* play.c */ | |
#include "mp2.h" | |
/***************************************************************** shuffle *****************************************************************************************/ | |
void shuffle(int cards[][12], int num_cards[], int drawnDeck[]) | |
{ | |
int i , j ; | |
/* Your code goes here. */ | |
num_cards[0] = 0; | |
num_cards[1] = 0; | |
for(i = 0; i < 2; i++){ | |
for(j = 0; j < 12; j++){ | |
cards[i][j] = 0; | |
} | |
} | |
for(i = 0; i < 52; i++){ | |
drawnDeck[i] = 0; | |
} | |
} | |
/***************************************************************** draw_card *****************************************************************************************/ | |
void draw_card(int deck[], int cards[][12], int num_cards[], int computer_or_player, int drawnDeck[]) | |
{ | |
int new_card; | |
/* flag variables , 0 means false , 1 means true */ | |
int card_already_selected = 0; | |
int card_added = 0; | |
/* Your code goes here. */ | |
while(!card_added){ | |
new_card = rand() % 52; | |
card_already_selected = drawnDeck[new_card]; | |
if(!card_already_selected){ | |
cards[computer_or_player][num_cards[computer_or_player]] = deck[new_card]; | |
num_cards[computer_or_player] += 1; | |
card_added = 1; | |
drawnDeck[new_card] = 1; | |
} | |
} | |
} | |
/***************************************************************** compute_scores *****************************************************************************************/ | |
void compute_scores(int scores[], int cards[][12], int num_cards[], int computer_or_player) | |
{ | |
/* compute the two lowest scores */ | |
/* note that there may be more than one score due to aces */ | |
/* if there are three or more possible scores then only two may be under 22 points */ | |
/* Here are the possible values:*/ | |
/* 1 Ace: 1 or 11 */ | |
/* 2 Aces: 2 or 12 (22 is too high) */ | |
/* 3 Aces: 3 or 13 (23 and 33 are too high) */ | |
/* 4 Aces: 4 or 14 (24 and 34 and 44 are too high) */ | |
/* go through the cards and total the non-ace values */ | |
/* count the number of aces and use the above value pairs to add to the non-ace total */ | |
int i; | |
int score1 = 0; | |
int score2 = 0; | |
int ace_count = 0; | |
int non_ace_total = 0; | |
int card_type; | |
/* Your code goes here. */ | |
for(i = 0; i < num_cards[computer_or_player]; i++){ | |
card_type = cards[computer_or_player][i] % 100; | |
switch(card_type){ | |
case 14: | |
++ace_count; | |
case 13: | |
case 12: | |
case 11: | |
case 10: | |
non_ace_total += 10; | |
case 9: | |
case 8: | |
case 7: | |
case 6: | |
case 5: | |
case 5: | |
case 3: | |
case 2: | |
non_ace_total += card_type; | |
} | |
} | |
switch(ace_count){ | |
case 4: | |
score1 = non_ace_total + 4; | |
score2 = non_ace_total + 14; | |
scores[computer_or_player] = best_score(score1, score2); | |
case 3: | |
score1 = non_ace_total + 3; | |
score2 = non_ace_total + 13; | |
scores[computer_or_player] = best_score(score1, score2); | |
case 2: | |
score1 = non_ace_total + 2; | |
score2 = non_ace_total + 12; | |
scores[computer_or_player] = best_score(score1, score2); | |
case 1: | |
score1 = non_ace_total + 1; | |
score2 = non_ace_total + 11; | |
scores[computer_or_player] = best_score(score1, score2); | |
case 0: | |
scores[computer_or_player] = non_ace_total; | |
} | |
if(computer_or_player == 1 && score1 !== 0){ | |
int greater = score1 > score2 ? score1 : score2; | |
int lesser = score1 > score2 ? score2 : score1; | |
if(greater < 22){ | |
printf("You have an optional score of %i.\n", lesser); | |
} | |
} | |
} | |
/***************************************************************** best_score *****************************************************************************************/ | |
int best_score(int score1, int score2) | |
{ | |
/* Your code goes here. */ | |
if(score1 < 22 && score2 < 22){ | |
if(score1 > score2){ | |
return score1; | |
}else{ | |
return score2; | |
} | |
}else if(score1 < 22){ | |
return score1; | |
}else if(score2 < 22){ | |
return score2; | |
}else{ | |
if(score1 > score2){ | |
return score2; | |
}else{ | |
return score1; | |
} | |
} | |
} | |
/***************************************************************** players_turn *****************************************************************************************/ | |
int players_turn(int deck[], int cards[][12], int num_cards[], int scores[], int points[], int drawnDeck[]) | |
{ | |
/* initialize flag variables for each hand */ | |
int hand_over = 0; /* hand_over = 1 if players score >= 22 */ | |
int player_done = 0; | |
char hit_or_stay[132]; | |
/* Your code goes here. */ | |
do { | |
do{ | |
printf("\nHit(h) or Stay(s)? "); | |
scanf("%c", &hit_or_stay); | |
if(hit_or_stay[1]){ | |
printf("You must enter a single character, either 'h' or 's'\n"); | |
} | |
}while((strlen(hit_or_stay) != 1) || ( (hit_or_stay[0] != 'h') && (hit_or_stay[0] != 'H') && (hit_or_stay[0] != 's') && (hit_or_stay[0] != 'S') )); | |
switch(hit_or_stay[0]){ | |
case 'h': | |
case 'H': | |
draw_card(deck, cards, num_cards, 1, drawnDeck); | |
display_cards(cards, num_cards, 1, 0); | |
display_cards(cards, num_cards, 0, 1); | |
compute_scores(scores, cards, num_cards, 1); | |
display_scores(scores, 1); | |
if(scores[0] > 21){ | |
hand_over = 1; | |
--points[0]; | |
} | |
case 's': | |
case 'S': | |
player_done = 1; | |
} | |
}while(!hand_over && !player_done); | |
return hand_over; | |
} | |
/***************************************************************** computers_turn *****************************************************************************************/ | |
void computers_turn(int deck[], int cards[][12], int num_cards[], int scores[], int points[], int drawnDeck[]) | |
{ | |
int hand_over = 0; | |
/* Your code goes here. */ | |
} | |
/***************************************************************** request_another_game *****************************************************************************************/ | |
int request_another_game(void) | |
{ | |
char YN[132]; | |
/* logical variable, game_over */ | |
int game_over = 0; | |
/* Your code goes here. */ | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment