You must make the following: 1. Do not show the dealer hole card until after the
ID: 3697473 • Letter: Y
Question
You must make the following:
1. Do not show the dealer hole card until after the player stands, and do not show the dealer score until after the player stands.
2. After the player stands, playout the dealer according to the rules <= 16 hit, >= 17 stand.
3. Fix the scoring of aces
4. Add the ability for the player to double down.
I have this so far, I could be totally off.
// Blackjack.c : Defines the entry point for the console application.
//
#include <stdio.h>
#include <Windows.h>
#include <stdlib.h>
#include <tchar.h>
#define CLUBS 0
#define DIAMONDS 1
#define HEARTS 2
#define SPADES 3
enum{ TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE };
int Deck[52];
int CurrentDealCard;
int Dealer[20], Player[20];
int DealerCards, PlayerCards;
int DealerScore, PlayerScore;
int PlayerStand, PlayerDoubled;
void DoShuffle()
{
int i, nextcard;
int Used[52];
/* Here we clear out Used array to zeros, which indicates no
values have been used. */
for (i = 0; i < 52; i++) Used[i] = 0;
/* Loop through the deck of cards, there are 52 values */
for (i = 0; i < 52; i++)
{
/* Here we used a do-while. If the card has already been used,
we need to keep generating random numbers until we find a card
that has not been used. */
do
{
nextcard = rand() % 52; /* Value from 0 to 51 */
} while (Used[nextcard] == 1); /* This is our check */
/* Here we set to 1 so that we remember that this card has been used */
Used[nextcard] = 1;
/* Finally, put the card in the deck. */
Deck[i] = nextcard;
}
}
void DrawCard(int rank, int suit)
{
switch (rank)
{
case TWO:
printf("Two ");
break;
case THREE:
printf("Three ");
break;
case FOUR:
printf("Four ");
break;
case FIVE:
printf("Five ");
break;
case SIX:
printf("Six ");
break;
case SEVEN:
printf("Seven ");
break;
case EIGHT:
printf("Eight ");
break;
case NINE:
printf("Nine ");
break;
case TEN:
printf("Ten ");
break;
case JACK:
printf("Jack ");
break;
case QUEEN:
printf("Queen ");
break;
case KING:
printf("King ");
break;
case ACE:
printf("Ace ");
break;
}
switch (suit)
{
case CLUBS:
printf("Clubs");
break;
case DIAMONDS:
printf("Diamonds");
break;
case HEARTS:
printf("Hearts");
break;
case SPADES:
printf("Spades");
break;
}
}
void DisplayShuffledDeck()
{
int i, suit, rank;
for (i = 0; i < 52; i++)
{
suit = Deck[i] / 13;
rank = Deck[i] % 13;
DrawCard(rank, suit);
printf(" ");
}
}
void DealCards()
{
PlayerCards = DealerCards = CurrentDealCard = 0;
Player[PlayerCards++] = Deck[CurrentDealCard++];
Dealer[DealerCards++] = Deck[CurrentDealCard++];
Player[PlayerCards++] = Deck[CurrentDealCard++];
Dealer[DealerCards++] = Deck[CurrentDealCard++];
}
void DisplayDealtCards()
{
int i;
printf(" Dealer: ");
for (i = 0; i < DealerCards; i++){
/*Check if it is the dealer hold*/
if(i==1){
/*Only show dealer hold if the player stand*/
if(PlayerStand==1){
DrawCard(Dealer[i] % 13, Dealer[i] / 13), printf( " " );
}
}else{
DrawCard(Dealer[i] % 13, Dealer[i] / 13), printf( " " );
}
}
/*Show the Dealer score only if Player Stand*/
if(PlayerStand==2){
printf("Dealer Score is %d Player: ", DealerScore);
}
for (i = 0; i < PlayerCards; i++) DrawCard(Player[i] % 13, Player[i] / 13), printf(" ");
if(PlayerDoubled==1){
printf("Player has Doubled Down");
}
printf("Player Score is %d ", PlayerScore);
}
void DisplayDoubleDownChoice(){
printf("Would you like to Double Down? ");
printf("1. Yes 2. No ");
}
void DisplayChoice()
{
printf("What would you like to do? ");
printf("1. Hit 2. Stand ");
}
int GetChoice(int min, int max)
{
int choice;
do
{
printf("Make a choice from %d to %d:", min, max);
scanf("%d", &choice);
} while (choice < min || choice > max);
return(choice);
}
void CalculateScores()
{
int i, rank, suit;
int aces=0;
PlayerScore = 0;
for (i = 0; i < PlayerCards; i++)
{
rank = Player[i] % 13;
suit = Player[i] / 13;
if (rank >= TWO && rank <= TEN) PlayerScore += ( rank + 2 );
else if (rank >= JACK && rank <= KING) PlayerScore += 10;
else{
PlayerScore += 11;
aces++;
}
/*As we have stored the number of aces, we use them to get as near as possible to 21.
We have added all aces as 11. If we bust, we start substracting 10 until we run out of aces
*/
while(PlayerScore>21 && aces>0){
PlayerScore-=10;
aces--;
}
}
aces=0;
DealerScore = 0;
for (i = 0; i < DealerCards; i++)
{
rank = Dealer[i] % 13;
suit = Dealer[i] / 13;
if (rank >= TWO && rank <= TEN) DealerScore += (rank + 2);
else if (rank >= JACK && rank <= KING) DealerScore += 10;
else {
DealerScore += 11;
aces++;
}
/*As we have stored the number of aces, we use them to get as near as possible to 21.
We have added all aces as 11. If we bust, we start substracting 10 until we run out of aces
*/
while(DealerScore>21 && aces>0){
DealerScore-=10;
aces--;
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int choice;
int doubleDown;
srand(GetTickCount());
DoShuffle();
DealCards();
CalculateScores();
DisplayDealtCards();
DisplayDoubleDownChoice();
PlayerDoubled = GetChoice(1,2);
DisplayChoice();
choice = GetChoice(1,2);
/*Check if player double down*/
if(doubleDown==1){
/*Keep Asking until the Player Stand or Bust*/
while (choice == 1 && PlayerScore<21)
{
Player[PlayerCards++] = Deck[CurrentDealCard++];
CalculateScores();
DisplayDealtCards();
}
return 0;
}
PlayerStand=1;
/*At this point the Palyer has ended, now play the Dealer*/
if(DealerScore < 17){
Dealer[DealerCards++] = Deck[CurrentDealCard++];
CalculateScores();
DisplayDealtCards();
}
Explanation / Answer
#include #include #include #include #include #include struct cards { int card_value[10]; char card_name[10]; char card_suit[10]; int card_tally; BITMAP *card_pic[10]; }; struct cards hand[2]; short decks=1; short cards_used[14]= {0}; int player_cash = 500; void endgame() { if (player_cash < 1) alert("You lost it all big guy! Better luck next time!", NULL, NULL, "&Ok", NULL, 'o', 'k'); else if (player_cash < 501) alert("Game Over: Not enough cards to continue", "In the end, you didn't win a dime but at least", "you still have the shirt on your back", "&Ok", NULL, 'o', 'k'); else { char cash[100]; snprintf(cash, sizeof(cash), "You are leaving with $%d", player_cash); alert("Amazing! You beat the house!", cash, NULL, "&Ok", NULL, 'o', 'k'); } exit(EXIT_SUCCESS); } void tally (int a) { int x=0, y=0; for (x=0; x 1) && (z < 10)) { hand[a].card_value[x]=z; hand[a].card_name[x]=((char) '0' + z); } else if (z == 10) { hand[a].card_value[x]=z; hand[a].card_name[x]='T'; } else if (z == 11) { hand[a].card_value[x]=10; hand[a].card_name[x]='J'; } else if (z == 12) { hand[a].card_value[x]=10; hand[a].card_name[x]='Q'; } else if (z == 13) { hand[a].card_value[x]=10; hand[a].card_name[x]='K'; } else if (z == 1) { /*Function 'check_for_ace' deals with this more properly*/ hand[a].card_value[x]=1; hand[a].card_name[x]='A'; } /*Assign Suits Randomly*/ if (y == 1) card_suit='c'; if (y == 2) card_suit='d'; if (y == 3) card_suit='h'; if (y == 4) card_suit='s'; check_for_ace(a); /*Link the Picture*/ char pic[20]; snprintf(pic, sizeof(pic), "card/%c%c.bmp", hand[a].card_name[x], card_suit); hand[a].card_pic[x]=load_bmp(pic, NULL); tally(a); } void display_hands () { int x; int y=10; clear_bitmap(screen); /*Dealer hand*/ for (x=0; hand[0].card_name[x]!=0; x++) { blit(hand[0].card_pic[x], screen, 0,0,y,10,73,97); y=y+75; } /*Player Hand, displayed on bottom of screen*/ y=10; for (x=0; hand[1].card_name[x]!=0; x++) { blit(hand[1].card_pic[x], screen, 0,0,y,300,73,97); y=y+75; } textprintf_ex(screen, font, 335, 2, makecol(0, 0, 0), makecol(248, 248, 230), " "); textprintf_ex(screen, font, 335, 10, makecol(0, 0, 0), makecol(248, 248, 230), " Cash "); textprintf_ex(screen, font, 335, 18, makecol(0, 0, 0), makecol(248, 248, 230), " %d ", player_cash); textprintf_ex(screen, font, 335, 26, makecol(0, 0, 0), makecol(248, 248, 230), " "); } void dealer_turn() { while (hand[0].card_tally < 17) { draw_card(0); display_hands(); } if (hand[0].card_tally > 21) { hand[0].card_tally = 0; alert("Dealer Busts!", NULL, NULL, "&Ok", NULL, 'o', 'k'); } } void player_turn() { int action=1; while (action != 2 && hand[1].card_tally < 21) { action=alert("What will you do?", NULL, NULL, "&Hit", "&Stand", 'h', 's'); if (action == 1) draw_card(1); display_hands(); tally(1); } if (hand[1].card_tally > 21) alert("Player Busts!", NULL, NULL, "&Ok", NULL, 'o', 'k'); } int main(int argc, char **argv) { allegro_init(); install_keyboard(); install_mouse(); set_color_depth(16); set_gfx_mode(GFX_AUTODETECT_WINDOWED, 400,400,0,0); argc -= optind; argv += optind; if (argv[0]) decks = atoi (argv[0]); srand(time(NULL)); int x=0; while (x != -1) { display_hands(); int bet = 50; int alert_val = alert3("Please place your bet", NULL, NULL, "&50", "&100", "15&0", '5', '1', '0'); bet = alert_val * 50; player_cash=player_cash - bet; display_hands(); draw_card(0); draw_card(1); draw_card(1); display_hands(); player_turn(); if (hand[1].card_tally < 22) { dealer_turn(); display_hands(); } if ((hand[0].card_tally > hand[1].card_tally) || (hand[0].card_tally == hand[1].card_tally || hand[1].card_tally > 21)) { alert("Dealer wins!", NULL, NULL, "&Ok", NULL, 'o', 'k'); } else { alert("Player wins!", NULL, NULL, "&Ok", NULL, 'o', 'k'); player_cash = player_cash + (bet * 2); display_hands(); } if (player_cash < 1) endgame(); int i; for (i=0; i < 2; i++) { for (x=0; x < 10; x++) { hand[i].card_name[x] = 0; hand[i].card_suit[x] = 0; hand[i].card_value[x] = 0; } hand[i].card_tally = 0; } if (alert("Continue or Quit?", NULL, NULL, "&Continue", "&Quit", 'c', 'q') == 2) exit(EXIT_SUCCESS); } int loop=0; for (loop=0; loop < 11; loop++) { destroy_bitmap(hand[0].card_pic[loop]); destroy_bitmap(hand[1].card_pic[loop]); } return 0; } END_OF_MAIN();Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.