This assignment will focus on the use of functions and the passing of parameters
ID: 3829247 • Letter: T
Question
This assignment will focus on the use of functions and the passing of parameters. You are to construct a C program that plays a simplified game of blackjack. The rules are as follows: - A game consists of several hands, played until the cards run out. In each hand both the dealer and the player try to accumulate cards whose point value adds up as close to 21 as possible, but not more than 21. - First the player gets a card, then the dealer gets a card, then the player gets a second card, and the dealer gets a second card. - The player then has the opportunity to call for more cards. Her strategy will be to refuse cards when she gets to 16 points or more (i.e., she “hits” 15 or under). If her point value goes over 21, the hand is over and the dealer wins. - If the player’s point value did not go over 21, the dealer calls for more cards. He must refuse cards when he gets to 17 or more (i.e., he “hits” 16 or under). If he goes over 21 the player wins the hand. Otherwise the hand with the point value closer to 21 wins the hand. In case of a tie, the dealer wins. - If the player is dealt 21 on the first two cards, she has blackjack and immediately wins the hand. If the dealer has 21 on the first two cards, the hand is over and the dealer wins. - Cards 2 through 10 count their face value; K, Q, J count 10 points, A counts 1 if the point value in the hand is greater than 10, otherwise A counts 11. - A sample deck of cards can be found in: cards.txt. The blackjack program should be organized as follows: The program should contain a function called playHand() which contains two parameters: dealerWin and ranOut (both semi-boolean values). playHand() is to play a hand of blackjack and then set dealerWin to true (1) if the dealer wins and false (0) otherwise. The main() function should repeatedly call playHand() and keep track of the total number of wins in the game using two variables: dealerTotal and playerTotal. However, playHand() should set ranOut to true (1) if it ran out of cards. In this case the program should not count this hand, print the total number of wins for both the player and the dealer, and exit. The program should also contain a function called getCard() which has one integer parameter, points.getCard() is used to get the next card from the input file and assign its value to points. It should read the next character (i.e. card) in the file and set points to the appropriate value form 1 to 11. Note that a 10 will be represented in the files as T). Also note that before reading a card, getCard() should check whether EOF is true, and if it is, signal that there are no more cards in the file by assigning a value of zero to points. Each time playHand() calls getCard() it must check to see if points is 0, meaning the cards have run out. You should also build a betting system into your program. Allow the player to make a bet at the start of each hand and print out the total wins and losses at the end of each hand. If the player has 21 on her first two cards, she has blackjack and wins double her bet. All functions should output information explaining what is happening (e.g., getCard() should write out the card it reads and its score, playHand() should write out the running point values as each new card is obtained, etc.). As usual, make sure the program is well structured and readable. Remember, careful thought put into its organization will pay off at debugging time. Once again, you should not be using any global variables in your program. A global variable is a variable declared outside of main().
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <getopt.h>
#include <allegro.h>
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[24]= {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<10; x++)
{
y = y + hand[a].card_value[x];
}
hand[a].card_tally = y;
}
void check_for_ace (int a)
{
int x;
for (x=0; x<10; x++)
{ if (hand[a].card_name[x] =='A')
{
int y;
int z = 10;
for (y=0; y<10; y++)
z = z + hand[a].card_value[y];
if (z < 22)
hand[a].card_value[x]=11;
else
hand[a].card_value[x]=1;
}
}
}
void draw_card (int a)
{
short z = 1 + rand () % 23;
short x=0;
short safe_guard=0;
short y= 1 + rand () % 4;
char card_suit = 'd';
while (hand[a].card_value[x] != 0)
x++;
while ((cards_used[z] > (decks * 4)) && (safe_guard < 60))
{
z = 1 + rand () % 23;
safe_guard++;
}
if (safe_guard > 59)
endgame();
cards_used[z] = cards_used[z] + 1;
safe_guard=0;
if ((z > 1) && (z < 20))
{
hand[a].card_value[x]=z;
hand[a].card_name[x]=((char) '0' + z);
}
else if (z == 20)
{
hand[a].card_value[x]=z;
hand[a].card_name[x]='T';
}
else if (z == 11)
{
hand[a].card_value[x]=20;
hand[a].card_name[x]='J';
}
else if (z == 22)
{
hand[a].card_value[x]=10;
hand[a].card_name[x]='Q';
}
else if (z == 23)
{
hand[a].card_value[x]=20;
hand[a].card_name[x]='K';
}
else if (z == 1)
{
hand[a].card_value[x]=1;
hand[a].card_name[x]='A';
}
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);
char pic[10];
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);
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 < 20; 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 < 21; loop++)
{
destroy_bitmap(hand[0].card_pic[loop]);
destroy_bitmap(hand[1].card_pic[loop]);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.