0% found this document useful (0 votes)
20 views4 pages

BJ 14

This document contains C code for a Blackjack game using a graphical interface. It includes functions to create and shuffle a deck of cards, deal cards to players, calculate hand values, and display the game interface. The main loop allows users to start the game, view help, or exit the application.

Uploaded by

bt21 -
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views4 pages

BJ 14

This document contains C code for a Blackjack game using a graphical interface. It includes functions to create and shuffle a deck of cards, deal cards to players, calculate hand values, and display the game interface. The main loop allows users to start the game, view help, or exit the application.

Uploaded by

bt21 -
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <stdio.

h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "gfx.h" // Include the gfx library

#define WINDOW_WIDTH 800


#define WINDOW_HEIGHT 600
#define NUM_CARDS 52

// Parallel arrays to represent the deck of cards


char suits[NUM_CARDS][10];
char faces[NUM_CARDS][10];
int values[NUM_CARDS];
int top = 0; // Keeps track of the top card in the deck

// Function Prototypes
void createDeck();
void shuffleDeck();
int dealCard(char handSuits[][10], char handFaces[][10], int handValues[], int
*numCards);
int calculateHandValue(char handSuits[][10], char handFaces[][10], int
handValues[], int numCards);
void displayHand(char handSuits[][10], char handFaces[][10], int handValues[], int
numCards, const char *owner);
void drawCard(int x, int y, const char *face, const char *suit, float size);
void draw_heart(int x, int y, float size);
void draw_diamond(int x, int y, float size);
void draw_club(int x, int y, float size);
void draw_spade(int x, int y, float size);
void displayHelp();

// Main function
int main() {
srand(time(0)); // Seed for random number generation

// Initialize gfx library


gfx_open(WINDOW_WIDTH, WINDOW_HEIGHT, "Blackjack Game");

while (1) {
gfx_clear();
gfx_color(255, 255, 255);
gfx_text(50, 50, "Welcome to the Blackjack Game!");
gfx_text(50, 100, "1. Start Game");
gfx_text(50, 140, "2. Help");
gfx_text(50, 180, "3. Exit");

gfx_text(50, 220, "Please select an option (1-3) using your keyboard.");

char input = gfx_wait(); // Wait for user input

if (input == '1') {
gfx_clear();
// Example hand display for demonstration
char handSuits[3][10] = {"Hearts", "Clubs", "Spades"};
char handFaces[3][10] = {"A", "10", "K"};
int handValues[3] = {11, 10, 10};
displayHand(handSuits, handFaces, handValues, 3, "Player");
gfx_text(50, WINDOW_HEIGHT - 50, "Press any key to return to the main
menu.");
gfx_wait();
} else if (input == '2') {
gfx_clear();
displayHelp();
gfx_text(50, WINDOW_HEIGHT - 50, "Press any key to return to the main
menu.");
gfx_wait();
} else if (input == '3') {
gfx_close();
printf("Thank you for playing! Goodbye!\n");
exit(0);
} else {
gfx_text(50, 300, "Invalid input. Please select a valid option (1-
3).");
}
}

return 0;
}

// Function to create a deck of cards


void createDeck() {
const char *suitsList[] = {"Hearts", "Diamonds", "Clubs", "Spades"};
const char *facesList[] = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"J", "Q", "K"};
int faceValues[] = {11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10};

int index = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 13; j++) {
strcpy(suits[index], suitsList[i]);
strcpy(faces[index], facesList[j]);
values[index] = faceValues[j];
index++;
}
}
}

// Function to shuffle the deck


void shuffleDeck() {
for (int i = 0; i < NUM_CARDS; i++) {
int j = rand() % NUM_CARDS;
char tempSuit[10];
char tempFace[10];
int tempValue;

// Swap suits
strcpy(tempSuit, suits[i]);
strcpy(suits[i], suits[j]);
strcpy(suits[j], tempSuit);

// Swap faces
strcpy(tempFace, faces[i]);
strcpy(faces[i], faces[j]);
strcpy(faces[j], tempFace);

// Swap values
tempValue = values[i];
values[i] = values[j];
values[j] = tempValue;
}
}

// Function to deal a card


int dealCard(char handSuits[][10], char handFaces[][10], int handValues[], int
*numCards) {
if (top >= NUM_CARDS) {
printf("No more cards left in the deck!\n");
return 0;
}
strcpy(handSuits[*numCards], suits[top]);
strcpy(handFaces[*numCards], faces[top]);
handValues[*numCards] = values[top];
(*numCards)++;
top++;
return 1;
}

// Function to calculate the total value of a hand


int calculateHandValue(char handSuits[][10], char handFaces[][10], int
handValues[], int numCards) {
int total = 0;
int aces = 0;

for (int i = 0; i < numCards; i++) {


total += handValues[i];
if (strcmp(handFaces[i], "A") == 0) {
aces++;
}
}

// Adjust for Aces if total exceeds 21


while (total > 21 && aces > 0) {
total -= 10;
aces--;
}

return total;
}

// Function to display the hand graphically


void displayHand(char handSuits[][10], char handFaces[][10], int handValues[], int
numCards, const char *owner) {
int x = 50, y = 100; // Starting position for the first card
float size = 1.0;

for (int i = 0; i < numCards; i++) {


drawCard(x + i * 150, y, handFaces[i], handSuits[i], size);
}

int total = calculateHandValue(handSuits, handFaces, handValues, numCards);


printf("%s's total value: %d\n", owner, total); // Console output for
debugging
}

// Function to draw a card


void drawCard(int x, int y, const char *face, const char *suit, float size) {
// Draw card border
gfx_color(255, 255, 255);
gfx_fillrectangle(x - 50 * size, y - 70 * size, 100 * size, 140 * size);
gfx_color(0, 0, 0);
gfx_rectangle(x - 50 * size, y - 70 * size, 100 * size, 140 * size);

// Draw the face value


gfx_color(0, 0, 0);
gfx_text(x - 45 * size, y - 60 * size, face);

// Draw the suit


if (strcmp(suit, "Hearts") == 0) {
draw_heart(x, y, size);
} else if (strcmp(suit, "Diamonds") == 0) {
draw_diamond(x, y, size);
} else if (strcmp(suit, "Clubs") == 0) {
draw_club(x, y, size);
} else if (strcmp(suit, "Spades") == 0) {
draw_spade(x, y, size);
}
}

// Function to display help


void displayHelp() {
gfx_color(255, 255, 255);
gfx_text(50, 50, "Blackjack Game Help:");
gfx_text(50, 100, "1. Press '1' to start the game.");
gfx_text(50, 140, "2. Cards are displayed graphically.");
gfx_text(50, 180, "3. The objective is to get a total value closest to 21.");
gfx_text(50, 220, "4. Press '3' at any time to exit.");
}

// Functions to draw suits


void draw_heart(int x, int y, float size) { ... }
void draw_diamond(int x, int y, float size) { ... }
void draw_club(int x, int y, float size) { ... }
void draw_spade(int x, int y, float size) { ... }

You might also like