0% found this document useful (0 votes)
15 views

bj7

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 of cards, dealing cards to players, and displaying game instructions and hands. The main menu allows users to start a new game, view instructions, or exit the program.

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)
15 views

bj7

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 of cards, dealing cards to players, and displaying game instructions and hands. The main menu allows users to start a new game, view instructions, or exit the program.

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 <unistd.h> // For sleep()
#include "gfx.h"

#define NUM_CARDS 52
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#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 checkDeckStatus();
void displayInstructions();
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 startNewGame();
void mainMenu();

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(0, 128, 128);
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", 0, 200, 0);
drawButton(WINDOW_WIDTH / 2 - 75, 250, "Instructions", 0, 0, 200);
drawButton(WINDOW_WIDTH / 2 - 75, 350, "Exit", 200, 0, 0);
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(0, 128, 128);
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(dealerSuits, dealerFaces, dealerValues, &dealerCards);

// Draw initial hands


displayHand(100, 200, playerSuits, playerFaces, playerValues, playerCards,
"Player");
displayHand(400, 200, dealerSuits, dealerFaces, dealerValues, dealerCards,
"Dealer");

// Draw buttons
drawButton(100, 500, "Hit", 0, 200, 0);
drawButton(300, 500, "Stand", 200, 0, 0);

gfx_flush();
}

void createDeck() {
const char *suitList[] = {"Diamonds", "Hearts", "Spades", "Clubs"};
const char *faceList[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"Jack", "Queen", "King"};
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;
}

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, 1);

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


gfx_color(0, 0, 255);
gfx_fillrectangle(x + i * 60, y, 50, 70);
gfx_color(255, 255, 255);
gfx_text(handFaces[i], x + i * 60 + 10, y + 30, 1);
gfx_text(handSuits[i], x + i * 60 + 10, y + 50, 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