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

BJ 1

This document contains a C program that implements a simple Blackjack game using a graphical interface. It includes functions for creating and shuffling a deck of cards, dealing cards, rendering player and dealer hands, and handling game logic such as hitting, standing, and determining the winner. The game runs in a loop, allowing players to start a new game or quit 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)
39 views4 pages

BJ 1

This document contains a C program that implements a simple Blackjack game using a graphical interface. It includes functions for creating and shuffling a deck of cards, dealing cards, rendering player and dealer hands, and handling game logic such as hitting, standing, and determining the winner. The game runs in a loop, allowing players to start a new game or quit 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 <string.h>
#include <time.h>
#include "gfx.h"

#define WIDTH 800


#define HEIGHT 600
#define CARD_WIDTH 80
#define CARD_HEIGHT 120

typedef struct {
char face[10];
char suit[10];
int value;
} Card;

Card deck[52];
int top = 0; // Tracks the top card in the deck

// Player and dealer hands


Card playerHand[10], dealerHand[10];
int playerCards = 0, dealerCards = 0;

// Function Prototypes
void createDeck();
void shuffleDeck();
Card dealCard();
void drawCard(int x, int y, const char *face, const char *suit);
void renderHand(Card *hand, int numCards, int xStart, int yStart, const char
*label);
int calculateHandValue(Card *hand, int numCards);
void renderTable();
void startNewGame();
void gameOver(const char *message);

int main() {
srand(time(0)); // Seed for random number generation
gfx_open(WIDTH, HEIGHT, "Blackjack 21");

createDeck();
shuffleDeck();

while (1) {
gfx_clear();
renderTable();

char c = gfx_wait();
if (c == 'n') { // Start a new game
startNewGame();
} else if (c == 'q') { // Quit the game
break;
}
}

return 0;
}

void createDeck() {
const char *suits[] = {"Hearts", "Diamonds", "Clubs", "Spades"};
const char *faces[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"Jack", "Queen", "King"};
int values[] = {11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10};

int index = 0;
for (int s = 0; s < 4; s++) {
for (int f = 0; f < 13; f++) {
strcpy(deck[index].suit, suits[s]);
strcpy(deck[index].face, faces[f]);
deck[index].value = values[f];
index++;
}
}
top = 0;
}

void shuffleDeck() {
for (int i = 0; i < 52; i++) {
int j = rand() % 52;
Card temp = deck[i];
deck[i] = deck[j];
deck[j] = temp;
}
}

Card dealCard() {
if (top >= 52) {
createDeck();
shuffleDeck();
}
return deck[top++];
}

void drawCard(int x, int y, const char *face, const char *suit) {


gfx_color(255, 255, 255);
gfx_fillrectangle(x, y, CARD_WIDTH, CARD_HEIGHT);
gfx_color(0, 0, 0);
gfx_text(x + 10, y + 20, face);
gfx_text(x + 10, y + 50, suit);
}

void renderHand(Card *hand, int numCards, int xStart, int yStart, const char
*label) {
gfx_color(255, 255, 255);
gfx_text(xStart, yStart - 20, label);
for (int i = 0; i < numCards; i++) {
drawCard(xStart + (i * (CARD_WIDTH + 10)), yStart, hand[i].face,
hand[i].suit);
}
}

int calculateHandValue(Card *hand, int numCards) {


int total = 0;
int aces = 0;

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


total += hand[i].value;
if (strcmp(hand[i].face, "Ace") == 0) {
aces++;
}
}

while (total > 21 && aces > 0) {


total -= 10;
aces--;
}

return total;
}

void renderTable() {
gfx_color(0, 128, 0);
gfx_fillrectangle(0, 0, WIDTH, HEIGHT);
gfx_color(255, 255, 255);
gfx_text(10, 20, "Press 'N' to Start a New Game or 'Q' to Quit.");
}

void startNewGame() {
playerCards = dealerCards = 0;

playerHand[playerCards++] = dealCard();
playerHand[playerCards++] = dealCard();
dealerHand[dealerCards++] = dealCard();

while (1) {
gfx_clear();
renderTable();

renderHand(playerHand, playerCards, 100, 400, "Player's Hand");


renderHand(dealerHand, dealerCards, 100, 100, "Dealer's Hand");

int playerTotal = calculateHandValue(playerHand, playerCards);


int dealerTotal = calculateHandValue(dealerHand, dealerCards);

gfx_text(10, HEIGHT - 20, "Press 'H' to Hit, 'S' to Stand, 'Q' to Quit.");

char c = gfx_wait();
if (c == 'h') { // Hit
playerHand[playerCards++] = dealCard();
if (calculateHandValue(playerHand, playerCards) > 21) {
gameOver("Bust! You lose.");
break;
}
} else if (c == 's') { // Stand
while (dealerTotal < 17) {
dealerHand[dealerCards++] = dealCard();
dealerTotal = calculateHandValue(dealerHand, dealerCards);
}

if (dealerTotal > 21 || playerTotal > dealerTotal) {


gameOver("You win!");
} else if (playerTotal < dealerTotal) {
gameOver("Dealer wins!");
} else {
gameOver("It's a draw!");
}
break;
} else if (c == 'q') { // Quit
break;
}
}
}

void gameOver(const char *message) {


gfx_clear();
renderTable();
gfx_color(255, 255, 255);
gfx_text(WIDTH / 2 - 50, HEIGHT / 2, message);
gfx_flush();
sleep(2); // Pause for 2 seconds
}

You might also like