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

BJ 15

This document contains C code for a graphical Blackjack game using the gfx library. It defines card and window dimensions, implements game logic including player and dealer actions, and handles user input for gameplay. The game loop manages the display of cards, buttons for 'Hit' and 'Stand', and calculates hand values to determine the winner.

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)
12 views4 pages

BJ 15

This document contains C code for a graphical Blackjack game using the gfx library. It defines card and window dimensions, implements game logic including player and dealer actions, and handles user input for gameplay. The game loop manages the display of cards, buttons for 'Hit' and 'Stand', and calculates hand values to determine the winner.

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 "gfx.

h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

// Card and window dimensions


#define CARD_WIDTH 100
#define CARD_HEIGHT 150
#define WIN_WIDTH 800
#define WIN_HEIGHT 600

// Button dimensions
#define BUTTON_WIDTH 100
#define BUTTON_HEIGHT 40

// Function prototypes
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 draw_card(int x, int y, const char *suit, const char *face);
void draw_button(int x, int y, const char *label);
int is_button_clicked(int bx, int by, int bw, int bh, int mx, int my);
void display_hand_graphics(int x_start, int y, char handSuits[][10], char
handFaces[][10], int numCards, const char *owner);
int calculateHandValue(char handSuits[][10], char handFaces[][10], int
handValues[], int numCards);
void game_loop();

// Main graphical Blackjack function


int main() {
gfx_open(WIN_WIDTH, WIN_HEIGHT, "Blackjack 21 - Graphical");
gfx_clear_color(0, 128, 0); // Green table background
gfx_clear();

game_loop(); // Start the game loop

return 0;
}

// Game loop
void game_loop() {
// Card arrays and variables
char suits[52][10] = {"Hearts", "Diamonds", "Clubs", "Spades"};
char faces[13][10] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"Jack", "Queen", "King"};
int faceValues[13] = {11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10};
char playerSuits[10][10], dealerSuits[10][10];
char playerFaces[10][10], dealerFaces[10][10];
int playerValues[10], dealerValues[10];
int playerCards = 0, dealerCards = 0;

// Initial deal
strcpy(playerSuits[0], suits[0]);
strcpy(playerFaces[0], faces[0]);
playerValues[0] = faceValues[0];
playerCards++;
strcpy(dealerSuits[0], suits[1]);
strcpy(dealerFaces[0], faces[1]);
dealerValues[0] = faceValues[1];
dealerCards++;

// Buttons for gameplay


int hit_x = 50, hit_y = WIN_HEIGHT - 100;
int stand_x = 200, stand_y = WIN_HEIGHT - 100;

while (1) {
gfx_clear(); // Clear screen

// Draw buttons
draw_button(hit_x, hit_y, "Hit");
draw_button(stand_x, stand_y, "Stand");

// Display player and dealer hands


display_hand_graphics(50, 50, playerSuits, playerFaces, playerCards,
"Player");
display_hand_graphics(400, 50, dealerSuits, dealerFaces, dealerCards,
"Dealer");

// Flush graphics
gfx_flush();

// Wait for user input


char c = gfx_wait();
int mx = gfx_xpos();
int my = gfx_ypos();

if (is_button_clicked(hit_x, hit_y, BUTTON_WIDTH, BUTTON_HEIGHT, mx, my)) {


// Add a new card to player
strcpy(playerSuits[playerCards], suits[playerCards]);
strcpy(playerFaces[playerCards], faces[playerCards]);
playerValues[playerCards] = faceValues[playerCards];
playerCards++;

if (calculateHandValue(playerSuits, playerFaces, playerValues,


playerCards) > 21) {
gfx_color(255, 0, 0);
gfx_text("Bust! You lost!", 50, WIN_HEIGHT - 50, medium);
gfx_flush();
sleep(3);
break;
}
} else if (is_button_clicked(stand_x, stand_y, BUTTON_WIDTH, BUTTON_HEIGHT,
mx, my)) {
// Dealer's turn
while (calculateHandValue(dealerSuits, dealerFaces, dealerValues,
dealerCards) < 17) {
strcpy(dealerSuits[dealerCards], suits[dealerCards + 10]);
strcpy(dealerFaces[dealerCards], faces[dealerCards + 10]);
dealerValues[dealerCards] = faceValues[dealerCards + 10];
dealerCards++;
}

// Final results
int playerTotal = calculateHandValue(playerSuits, playerFaces,
playerValues, playerCards);
int dealerTotal = calculateHandValue(dealerSuits, dealerFaces,
dealerValues, dealerCards);

gfx_color(255, 255, 255);


if (playerTotal > dealerTotal || dealerTotal > 21) {
gfx_text("You win!", 50, WIN_HEIGHT - 50, medium);
} else {
gfx_text("Dealer wins!", 50, WIN_HEIGHT - 50, medium);
}

gfx_flush();
sleep(3);
break;
} else if (c == 'q') {
break; // Quit the game
}
}
}

// Draw a card graphically


void draw_card(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_rectangle(x, y, CARD_WIDTH, CARD_HEIGHT);

// Draw the suit in the center


int suit_x = x + CARD_WIDTH / 2;
int suit_y = y + CARD_HEIGHT / 2;
if (strcmp(suit, "Hearts") == 0) draw_heart(suit_x, suit_y, 0.5);
if (strcmp(suit, "Diamonds") == 0) draw_diamond(suit_x, suit_y, 0.5);
if (strcmp(suit, "Clubs") == 0) draw_club(suit_x, suit_y, 0.5);
if (strcmp(suit, "Spades") == 0) draw_spade(suit_x, suit_y, 0.5);

// Draw the face at the top-left


gfx_text((char *)face, x + 10, y + 20, medium);
}

// Draw a button
void draw_button(int x, int y, const char *label) {
gfx_color(200, 200, 200); // Button background
gfx_fillrectangle(x, y, BUTTON_WIDTH, BUTTON_HEIGHT);
gfx_color(0, 0, 0); // Button border and text
gfx_rectangle(x, y, BUTTON_WIDTH, BUTTON_HEIGHT);
gfx_text((char *)label, x + 10, y + 25, small);
}

// Check if a button is clicked


int is_button_clicked(int bx, int by, int bw, int bh, int mx, int my) {
return (mx >= bx && mx <= bx + bw && my >= by && my <= by + bh);
}

// Display a hand graphically


void display_hand_graphics(int x_start, int y, char handSuits[][10], char
handFaces[][10], int numCards, const char *owner) {
gfx_color(255, 255, 255);
gfx_text((char *)owner, x_start, y - 20, medium);

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


draw_card(x_start + i * (CARD_WIDTH + 10), y, handSuits[i], handFaces[i]);
}
}

// Calculate hand value


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

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


total += handValues[i];
if (strcmp(handFaces[i], "Ace") == 0) aceCount++;
}

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


total -= 10;
aceCount--;
}

return total;
}

You might also like