0% found this document useful (0 votes)
36 views5 pages

BJ 9

This document is a C program that implements a graphical Blackjack game using the GFX library. It includes functions for creating and shuffling a deck, dealing cards, calculating hand values, and displaying the game interface with buttons for user interaction. The program features a main menu, game instructions, and handles player actions such as hitting or standing.

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)
36 views5 pages

BJ 9

This document is a C program that implements a graphical Blackjack game using the GFX library. It includes functions for creating and shuffling a deck, dealing cards, calculating hand values, and displaying the game interface with buttons for user interaction. The program features a main menu, game instructions, and handles player actions such as hitting or standing.

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/ 5

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h> // For sleep()
#include "gfx.h"

#define NUM_CARDS 52
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define CARD_WIDTH 70
#define CARD_HEIGHT 100
#define BUTTON_WIDTH 150
#define BUTTON_HEIGHT 50

// Parallel arrays for the deck


char suits[NUM_CARDS][10];
char faces[NUM_CARDS][10];
int values[NUM_CARDS];
int top = 0;

// 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(int x, int y, char handSuits[][10], char handFaces[][10], int
handValues[], int numCards, const char *owner);
void drawButton(int x, int y, const char *label, int r, int g, int b);
int isButtonClicked(int buttonX, int buttonY, int buttonWidth, int buttonHeight,
int mouseX, int mouseY);
void drawCard(int x, int y, const char *suit, const char *face);
void startNewGame();
void mainMenu();
void displayInstructions();

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

// Open the graphics window


gfx_open(WINDOW_WIDTH, WINDOW_HEIGHT, "Graphical Blackjack");
mainMenu();
return 0;
}

void mainMenu() {
while (1) {
gfx_clear_color(255, 182, 193); // Light pink background
gfx_clear();

// Title
gfx_color(255, 255, 255);
gfx_text((char*)"BLACKJACK 21", WINDOW_WIDTH / 2 - 100, 50, 2);

// Menu buttons
drawButton(WINDOW_WIDTH / 2 - 75, 150, "Start Game", 255, 192, 203);
drawButton(WINDOW_WIDTH / 2 - 75, 250, "Instructions", 255, 105, 180);
drawButton(WINDOW_WIDTH / 2 - 75, 350, "Exit", 255, 20, 147);

gfx_flush();

while (1) {
char event = gfx_wait();
int x = gfx_xpos(), y = gfx_ypos();

if (event == 1) { // Mouse click


if (isButtonClicked(WINDOW_WIDTH / 2 - 75, 150, BUTTON_WIDTH,
BUTTON_HEIGHT, x, y)) {
startNewGame();
}
if (isButtonClicked(WINDOW_WIDTH / 2 - 75, 250, BUTTON_WIDTH,
BUTTON_HEIGHT, x, y)) {
displayInstructions();
}
if (isButtonClicked(WINDOW_WIDTH / 2 - 75, 350, BUTTON_WIDTH,
BUTTON_HEIGHT, x, y)) {
gfx_clear();
gfx_text((char*)"Goodbye!", WINDOW_WIDTH / 2 - 50, 300, 1);
gfx_flush();
sleep(2);
exit(0);
}
}
}
}
}

void startNewGame() {
gfx_clear_color(255, 182, 193); // Light pink background
gfx_clear();

char playerSuits[NUM_CARDS][10], dealerSuits[NUM_CARDS][10];


char playerFaces[NUM_CARDS][10], dealerFaces[NUM_CARDS][10];
int playerValues[NUM_CARDS], dealerValues[NUM_CARDS];
int playerCards = 0, dealerCards = 0;

dealCard(playerSuits, playerFaces, playerValues, &playerCards);


dealCard(playerSuits, playerFaces, playerValues, &playerCards);
dealCard(dealerSuits, dealerFaces, dealerValues, &dealerCards);
dealCard(dealerSuits, dealerFaces, dealerValues, &dealerCards);

while (1) {
gfx_clear();

// Display hands
displayHand(100, 150, playerSuits, playerFaces, playerValues, playerCards,
"Player");
displayHand(400, 150, dealerSuits, dealerFaces, dealerValues, dealerCards,
"Dealer");

// Draw Hit and Stand buttons


drawButton(100, 500, "Hit", 255, 192, 203);
drawButton(300, 500, "Stand", 255, 105, 180);

gfx_flush();
char event = gfx_wait();
int x = gfx_xpos(), y = gfx_ypos();

if (event == 1) { // Mouse click


if (isButtonClicked(100, 500, BUTTON_WIDTH, BUTTON_HEIGHT, x, y)) { //
Hit
dealCard(playerSuits, playerFaces, playerValues, &playerCards);
if (calculateHandValue(playerSuits, playerFaces, playerValues,
playerCards) > 21) {
gfx_color(255, 0, 0);
gfx_text((char*)"Player Busted!", 300, 100, 2);
gfx_flush();
sleep(2);
return;
}
}
if (isButtonClicked(300, 500, BUTTON_WIDTH, BUTTON_HEIGHT, x, y)) { //
Stand
while (calculateHandValue(dealerSuits, dealerFaces, dealerValues,
dealerCards) < 17) {
dealCard(dealerSuits, dealerFaces, dealerValues, &dealerCards);
}
return;
}
}
}
}

void createDeck() {
const char *suitList[] = {"♥", "♦", "♠", "♣"};
const char *faceList[] = {"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 s = 0; s < 4; s++) {
for (int f = 0; f < 13; f++) {
strcpy(suits[index], suitList[s]);
strcpy(faces[index], faceList[f]);
values[index] = faceValues[f];
index++;
}
}
top = 0;
}

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 -1;
}

strcpy(handSuits[*numCards], suits[top]);
strcpy(handFaces[*numCards], faces[top]);
handValues[*numCards] = values[top];
top++;
(*numCards)++;
return 0;
}

int calculateHandValue(char handSuits[][10], char handFaces[][10], int


handValues[], int numCards) {
int total = 0;
int aceCount = 0;

// Sum the values of all cards in the hand


for (int i = 0; i < numCards; i++) {
total += handValues[i];
if (strcmp(handFaces[i], "Ace") == 0) {
aceCount++;
}
}

// Adjust for Aces: if total > 21 and there are Aces, treat them as 1 instead
of 11
while (total > 21 && aceCount > 0) {
total -= 10;
aceCount--;
}
return total;
}

void drawButton(int x, int y, const char *label, int r, int g, int b) {


gfx_color(r, g, b);
gfx_fillrectangle(x, y, BUTTON_WIDTH, BUTTON_HEIGHT);
gfx_color(255, 255, 255);
gfx_text((char*)label, x + 10, y + 30, 1);
}

int isButtonClicked(int buttonX, int buttonY, int buttonWidth, int buttonHeight,


int mouseX, int mouseY) {
return (mouseX >= buttonX && mouseX <= buttonX + buttonWidth &&
mouseY >= buttonY && mouseY <= buttonY + buttonHeight);
}

void displayHand(int x, int y, char handSuits[][10], char handFaces[][10], int


handValues[], int numCards, const char *owner) {
gfx_color(255, 255, 255);
gfx_text((char*)owner, x, y - 20, 2);

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


drawCard(x + i * (CARD_WIDTH + 10), y, handSuits[i], handFaces[i]);
}
}

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


gfx_color(255, 255, 255);
gfx_fillrectangle(x, y, CARD_WIDTH, CARD_HEIGHT);
gfx_color(0, 0, 0);
gfx_text((char*)face, x + 10, y + 30, 1);
gfx_text((char*)suit, x + 10, y + 60, 1);
}

void displayInstructions() {
gfx_clear();
gfx_color(255, 255, 255);
gfx_text((char*)"Instructions:", 50, 50, 2);
gfx_text((char*)"1. Goal: Get as close to 21 as possible without exceeding.",
50, 100, 1);
gfx_text((char*)"2. Hit: Draw another card. Stand: End your turn.", 50, 150,
1);
gfx_text((char*)"3. Dealer draws until 17 or higher.", 50, 200, 1);
gfx_flush();
sleep(5);
}

You might also like