BJ 9
BJ 9
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
// 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
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();
void startNewGame() {
gfx_clear_color(255, 182, 193); // Light pink background
gfx_clear();
while (1) {
gfx_clear();
// Display hands
displayHand(100, 150, playerSuits, playerFaces, playerValues, playerCards,
"Player");
displayHand(400, 150, dealerSuits, dealerFaces, dealerValues, dealerCards,
"Dealer");
gfx_flush();
char event = gfx_wait();
int x = gfx_xpos(), y = gfx_ypos();
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;
}
strcpy(handSuits[*numCards], suits[top]);
strcpy(handFaces[*numCards], faces[top]);
handValues[*numCards] = values[top];
top++;
(*numCards)++;
return 0;
}
// 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 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);
}