#include #include #include #include #include #include #include "cards2.h" #include #include HWND hBtn; //HANDLE TO "DEAL" BUTTON HWND hBtn2; //HANDLE TO "HIT" BUTTON HWND hBtn4; //HANDLE TO "STAND" BUTTON HWND hBetBtn1; //HANDLE TO "+" BUTTON HWND hBetBtn2; //HANDLE TO BET EDIT BOX HWND hBetBtn3; //HANDLE TO "-" BUTTON HWND hBtnReset; //HANDLE TO "RESET BUTTON" HWND hFore; LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM); void deck_init(void); void deck_shuffle(void); int add_to_score(int car, int x); // PLAYER IS TRUE, DEALER IS FALSE (VALUE OF X) HMENU hMenu; HINSTANCE hInst; struct scards { int num; //CARD NUMBER int x; //CARD X POSITION int y; //CARD Y POSITION int fd; //DRAW FACED-DOWN? }; typedef struct scards tcards; tcards cards[52]; int first = 1; /********************* BEGIN OF COPY AREA *********************************/ int c = 0; //CARD NUMBER REFERENCE FOR ARRAY int x = 100; int y = 100; int numcards = 0; // NUMBER OF CARDS USED (FOR DRAWING THE SCREEN) int next = 0; // NEXT CARD TO BE DRAWN int dNextx = 100; // DEALER'S NEXT X POSITION int dNexty = 100; // DEALER'S NEXT Y POSITION int pNextx = 100; // PLAYER'S NEXT X POSITION int pNexty = 250; // PLAYER'S NEXT Y POSITION int pBusted = 0; // IS THE PLAYER BUSTED? int pStand = 0; // DID THE PLAYER STAND? int dStand = 0; int pScore = 0; // PLAYER'S SCORE int dScore = 0; // DEALER'S SCORE int drawtext = 0; // VAR TO CONTROL TEXTOUT int draw_your_bet = 0; // VAR TO CONTROL " YOUR BET: " int show_d_score = 0; // SHOW DEALER SCORE? int pBJ = 0; // DOES THE PLAYER HAVE A BLACKJACK? int pWins = 0; // DID THE PLAYER WIN? int GameOver = 0; // IS THE GAME OVER? int z = 0; // ANOTHER TEMP COUNTER int pNumAces = 0; // NUMBER OF ACES THAT THE PLAYER HAS int dNumAces = 0; // NUMBER OF ACES THAT THE DEALER HAS int iEdit = 0; // COUNTER FOR EDIT ARRAY int whowon = 0; // WHO WON MESSAGE int doIt; int DealPushed = 0; /********************** END OF COPY AREA ***************************/ int money = 50; int f; static int iSelect = IDM_OPTIONS_CARDSTYLE1; eggx; eggy; int i; // TEMP COUNT VARIABLE (CARD DRAWING) time_t t; // TIME VARIABLE TO SEED RANDOM NUMBER GENERATOR int temp; // TEMP PLACEHOLDER int temp2; char szBuffer[40]; // BUFFER FOR TEXT OUT struct { char *text; }stEdit[] = { "5", "10", "15", "20", "25" };// BUFFER FOR THE EDIT TEXT int deck[52]; // THE CARDS HWND hwnd; char szWinName[] = "MyWin"; // Name of the window class int APIENTRY WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR lpszArgs, int nWinMode) { MSG msg; WNDCLASSEX wcl; srand((unsigned) time(&t)); // Define a window class.*********************************************** wcl.cbSize = sizeof (wcl); wcl.style = CS_HREDRAW | CS_VREDRAW; // default style wcl.lpfnWndProc = WindowFunc; // window function wcl.cbClsExtra = 0; // no extra wcl.cbWndExtra = 0; // information needed wcl.hInstance = hThisInst; // handle to this instance wcl.hIcon = LoadIcon(hThisInst, "IDI_ICON1"); // icon style wcl.hCursor = LoadCursor(NULL, IDC_ARROW); //cursor style wcl.hbrBackground = (HBRUSH) CreateSolidBrush(RGB(0,128,0)); wcl.lpszMenuName = szWinName; // menu wcl.lpszClassName = szWinName; // window class name wcl.hIconSm = LoadIcon(hThisInst, szWinName); // icon style //********************************************************************* // Register the window class. if(!RegisterClassEx (&wcl)) return 0; // Now that a window class has been registered, a window can be created. hwnd = CreateWindow( szWinName, // name of the window class "Black Jack: v1", // title WS_OVERLAPPEDWINDOW, // window style - normal 100, // x coordinate 50, // y coordinate 600, // width 450, // height HWND_DESKTOP, // no parent window NULL, // menu hThisInst, // handle of this instance of the program NULL); // Display the window ShowWindow(hwnd, nWinMode); UpdateWindow(hwnd); // INITIALIZE AND SHUFFLE THE CARDS ehCardInit(); deck_init(); deck_shuffle(); deck_shuffle(); // Create the message loop while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); // allow use of keyboard DispatchMessage(&msg); // return control to Windows } // TERMINATE THE CARDS32.DLL LIBRARY ehCardTerm(); return msg.wParam; } // This function is called by Windows 95 and is passed // messages from the message queue. LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; HDC hdc; static int cxClient, cyClient; static HICON hIcon; int b, ba; hFore = FindWindow(32770, "Oh Look!"); EndDialog(hFore, 0); SendDlgItemMessage(hFore, IDOK, WM_COMMAND, IDOK, 0); first++; switch(message) { case WM_CREATE: { hIcon = LoadIcon (hInst, szWinName); // CREATE THE "DEAL" BUTTON hBtn = CreateWindow( "BUTTON", "Deal", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 375,300, 50, 26, hwnd, (HMENU) 101, hInst, NULL); //*************** BET INPUT ******************************** // MAKE A "+" BUTTON hBetBtn1 = CreateWindow( "BUTTON", "+", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 350,100, 20, 20, hwnd, (HMENU) 201, hInst, NULL); // MAKE A EDIT BOX hBetBtn2 = CreateWindow( "EDIT", "5", WS_CHILD | WS_VISIBLE | ES_CENTER, 370,100, 40, 20, hwnd, (HMENU) 202, hInst, NULL); // MAKE A "-" BUTTON hBetBtn3 = CreateWindow( "BUTTON", "-", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 410,100, 20, 20, hwnd, (HMENU) 203, hInst, NULL); //*********************************************************** return 0; } case WM_SIZE: { // X AND Y SIZE OF THE SCREEN cxClient = LOWORD (lParam); cyClient = HIWORD (lParam); return 0; } case WM_LBUTTONDOWN: eggx = LOWORD (lParam); //b eggy = HIWORD (lParam); //ba for (b = 0; b < 6; b++) { for (ba = 0; ba < 6; ba++) { if ((eggx == b) && (eggy == ba)) { MessageBox (hwnd, "CONGRATULATIONS!!! You have found the EASTER EGG!!! Pretty dumb, huh?", "Black Jack: v1", MB_OK); } } } return 0; case WM_PAINT: { // InvalidateRect(hwnd, NULL, TRUE); hdc = BeginPaint (hwnd, &ps); SetBkColor(hdc, RGB(0,128,0)); SetTextColor(hdc, RGB(255,255,255)); // WRITE THE PLAYERS MONEY if (!GameOver) { TextOut (hdc, 350, 67, szBuffer, sprintf(szBuffer, "Money Left: $%d", money)); } if ((GameOver) && (pWins == 1)) { money = money + atoi(stEdit[iEdit].text); TextOut (hdc, 350, 67, szBuffer, sprintf(szBuffer, "Money Left: $%d", money)); GameOver = 0; } if (pWins == 2) { TextOut (hdc, 350, 67, szBuffer, sprintf(szBuffer, "Money Left: $%d", money)); GameOver = 0; } else { if ((GameOver) && (!pWins)) { money = money - atoi(stEdit[iEdit].text); TextOut (hdc, 350, 67, szBuffer, sprintf(szBuffer, "Money Left: $%d", money)); GameOver = 0; } } // WRITE THE TEXT TO THE SCREEN if (draw_your_bet) { // IF BEFORE "DEAL" BUTTON IS PUSHED... TextOut (hdc, 350, 82, szBuffer, sprintf(szBuffer, "Your bet:")); } if (drawtext) { // IF AFTER "DEAL" BUTTON IS PUSHED... TextOut (hdc, 100, 82, szBuffer, sprintf(szBuffer, "Dealer's hand:")); TextOut (hdc, 100, 232, szBuffer, sprintf(szBuffer, "Your hand: %d", pScore)); TextOut (hdc, 350, 82, szBuffer, sprintf(szBuffer, "Your bet: $%s", stEdit[iEdit].text)); if (show_d_score == 1) { TextOut (hdc, 100, 82, szBuffer, sprintf(szBuffer, "Dealer's hand: %d", dScore)); } if (pBusted == 1) { TextOut (hdc, 100, 232, szBuffer, sprintf(szBuffer, "Your hand: BUSTED! ! !")); } if (pBJ == 1) { TextOut (hdc, 100, 232, szBuffer, sprintf(szBuffer, "Your hand: B L A C K J A C K ! ! !")); } } // DRAW ALL CARDS, PREVIOUS AND NEW for (i = 0; i < numcards; i++) { ehCardDraw(hdc, cards[i].x, cards[i].y, cards[i].num, cards[i].fd, 0); } EndPaint(hwnd, &ps); switch (whowon) { case 0: break; case 1: TextOut (hdc, 100, cyClient-30, szBuffer, sprintf(szBuffer, "I T' S A P U S H ! ! !")); break; case 2: TextOut (hdc, 100, cyClient-30, szBuffer, sprintf(szBuffer, "Y O U W I N ! ! !")); break; case 3: TextOut (hdc, 100, cyClient-30, szBuffer, sprintf(szBuffer, "Y O U L O S E ! ! !")); break; } return 0; } case WM_COMMAND: { hMenu = GetMenu(hwnd); // IF THE DEAL BUTTON IS PUSHED.... switch (LOWORD (wParam)) { case IDM_FILE_EXIT: PostQuitMessage(0); break; case IDM_FILE_NEWGAME: /********************* BEGIN OF COPY AREA *********************************/ c = 0; //CARD NUMBER REFERENCE FOR ARRAY x = 100; y = 100; numcards = 0; // NUMBER OF CARDS USED (FOR DRAWING THE SCREEN) next = 0; // NEXT CARD TO BE DRAWN dNextx = 100; // DEALER'S NEXT X POSITION dNexty = 100; // DEALER'S NEXT Y POSITION pNextx = 100; // PLAYER'S NEXT X POSITION pNexty = 250; // PLAYER'S NEXT Y POSITION pBusted = 0; // IS THE PLAYER BUSTED? pStand = 0; // DID THE PLAYER STAND? pScore = 0; // PLAYER'S SCORE dScore = 0; // DEALER'S SCORE dStand = 0; drawtext = 0; // VAR TO CONTROL TEXTOUT draw_your_bet = 0; // VAR TO CONTROL " YOUR BET: " show_d_score = 0; // SHOW DEALER SCORE? pBJ = 0; // DOES THE PLAYER HAVE A BLACKJACK? pWins = 0; // DID THE PLAYER WIN? GameOver = 0; // IS THE GAME OVER? z = 0; // ANOTHER TEMP COUNTER iEdit = 0; // COUNTER FOR EDIT ARRAY pNumAces = 0; // NUMBER OF ACES THAT THE PLAYER HAS dNumAces = 0; // NUMBER OF ACES THAT THE DEALER HAS money = 50; whowon = 0; /********************** END OF COPY AREA ***************************/ if (DealPushed) { DestroyWindow(hBtn2); DestroyWindow(hBtn4); DestroyWindow(hBtnReset); } else { DestroyWindow(hBtn); DestroyWindow(hBetBtn1); DestroyWindow(hBetBtn2); DestroyWindow(hBetBtn3); } srand((unsigned) time(&t)); deck_init(); deck_shuffle(); deck_shuffle(); DealPushed = 0; InvalidateRect(hwnd, NULL, TRUE); SendMessage(hwnd, WM_CREATE, 0, 0); break; //*******************END OF "NEW GAME"************************** case IDM_OPTIONS_CARDSTYLE1: CheckMenuItem(hMenu, iSelect, MF_UNCHECKED); iSelect = LOWORD (wParam); CheckMenuItem(hMenu, iSelect, MF_CHECKED); ehSetDefBack (54); InvalidateRect(hwnd, NULL, FALSE); break; case IDM_OPTIONS_CARDSTYLE2: CheckMenuItem(hMenu, iSelect, MF_UNCHECKED); iSelect = LOWORD (wParam); CheckMenuItem(hMenu, iSelect, MF_CHECKED); ehSetDefBack (55); InvalidateRect(hwnd, NULL, FALSE); break; case IDM_OPTIONS_CARDSTYLE3: CheckMenuItem(hMenu, iSelect, MF_UNCHECKED); iSelect = LOWORD (wParam); CheckMenuItem(hMenu, iSelect, MF_CHECKED); ehSetDefBack (56); InvalidateRect(hwnd, NULL, FALSE); break; case IDM_OPTIONS_CARDSTYLE4: CheckMenuItem(hMenu, iSelect, MF_UNCHECKED); iSelect = LOWORD (wParam); CheckMenuItem(hMenu, iSelect, MF_CHECKED); ehSetDefBack (57); InvalidateRect(hwnd, NULL, FALSE); break; case IDM_HELP_ABOUT: MessageBox (hwnd, "Black Jack: v1 By Dusty Davidson", "Black Jack: v1", MB_OK); break; /************************** DEAL BUTTON *****************************/ case 101: DealPushed = 1; //DRAW TEXT drawtext = 1; draw_your_bet = 1; // GET RID OF THE "DEAL" BUTTONs DestroyWindow(hBtn); DestroyWindow(hBetBtn1); DestroyWindow(hBetBtn2); DestroyWindow(hBetBtn3); // MAKE A "HIT" BUTTON hBtn2 = CreateWindow( "BUTTON", "Hit", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 375,300, 50, 26, hwnd, (HMENU) 104, hInst, NULL); //MAKE A "STAND" BUTTON hBtn4 = CreateWindow( "BUTTON", "Stand", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 425,300, 50, 26, hwnd, (HMENU) 103, hInst, NULL); doIt = 1; //DEALER'S FIRST CARD cards[c].num = deck[next]; cards[c].fd = 1; cards[c].x = dNextx; cards[c].y = dNexty; dScore = dScore + (add_to_score(cards[c].num, 0)); c++; next++; numcards++; //DEALER'S SECOND CARD dNextx = dNextx + 72; cards[c].num = deck[next]; cards[c].fd = 0; cards[c].x = dNextx; cards[c].y = dNexty; dScore = dScore + (add_to_score(cards[c].num, 0)); c++; next++; numcards++; if (dScore >= 17) { dStand = 1; } //PLAYER'S FIRST CARD cards[c].num = deck[next]; cards[c].fd = 0; cards[c].x = pNextx; cards[c].y = pNexty; pScore = pScore + add_to_score(cards[c].num, 1); c++; next++; numcards++; pNextx = pNextx + 16; //PLAYER'S SECOND CARD cards[c].num = deck[next]; cards[c].fd = 0; cards[c].x = pNextx; cards[c].y = pNexty; pScore = pScore + (add_to_score(cards[c].num, 1)); c++; next++; numcards++; if (pScore == 21) { pBJ = 1; InvalidateRect(hwnd, NULL, FALSE); UpdateWindow(hwnd); DestroyWindow(hBtn2); DestroyWindow(hBtn4); SendMessage(hwnd, WM_COMMAND, 103, 0); } break; /************************** HIT BUTTON *****************************/ case 104: if ((!pBusted)&& (!pStand)) { //PLAYER'S THIRD CARD pNextx = pNextx + 16; cards[c].num = deck[next]; cards[c].fd = 0; cards[c].x = pNextx; cards[c].y = pNexty; pScore = pScore + (add_to_score(cards[c].num,1)); if (pScore > 21) { if (pNumAces == 0) { pBusted = 1; InvalidateRect(hwnd, NULL, FALSE); UpdateWindow(hwnd); DestroyWindow(hBtn2); DestroyWindow(hBtn4); SendMessage(hwnd, WM_COMMAND, 103, 0); } else { pScore = pScore - (pNumAces * 10); temp2 = pNumAces; for (temp = 0; temp < temp2; temp++) { pNumAces--; } } } c++; next++; numcards++; } break; /************************** STAND BUTTON *****************************/ case 103: // STAND cards[0].fd = 0; show_d_score = 1; if (pScore <= 21) { pStand = 1; } if ((pStand)&&(!dStand)) { do { //DEALER'S THIRD, AND MORE CARDS dNextx = dNextx + 72; cards[c].num = deck[next]; cards[c].fd = 0; cards[c].x = dNextx; cards[c].y = dNexty; dScore = dScore + (add_to_score(cards[c].num, 0)); c++; next++; numcards++; if (((dScore > pScore)||(dScore >= 17)) && (dNumAces > 0)) { dScore = dScore - (dNumAces * 10); temp2 = dNumAces; for (temp = 0; temp < temp2; temp++) { dNumAces--; } } } while (dScore < 17); } DestroyWindow(hBtn2); DestroyWindow(hBtn4); if ((pScore > dScore) && (!pBusted)) { GameOver = 1; pWins = 1; whowon = 2; InvalidateRect(hwnd, NULL, FALSE); UpdateWindow(hwnd); } else { if (dScore > 21) { GameOver = 1; pWins = 1; whowon=2; InvalidateRect(hwnd, NULL, FALSE); UpdateWindow(hwnd); } else { if (pScore == dScore) { GameOver = 1; whowon = 1; pWins = 2; InvalidateRect(hwnd, NULL, FALSE); UpdateWindow(hwnd); } else { GameOver = 1; whowon = 3; InvalidateRect(hwnd, NULL, FALSE); UpdateWindow(hwnd); } } } // DISPLAY THE PLAY AGAIN BUTTON if (!GameOver) { // CREATE THE "START OVER BUTTON hBtnReset = CreateWindow( "BUTTON", "Play Again", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 20,20, 75, 26, hwnd, (HMENU) 301, hInst, NULL); } break; /************************** "+" BUTTON *****************************/ case 201: if (iEdit == 4) { MessageBox (hwnd, "Hey rich guy! You can only bet $25!!!", "Black Jack: v1", MB_ICONEXCLAMATION | MB_OK); } else { iEdit++; SetWindowText(hBetBtn2, stEdit[iEdit].text); } break; /************************** "-" BUTTON *****************************/ case 203: if (iEdit == 0) { MessageBox (hwnd, "You have to bet something!!!", "Black Jack: v1", MB_ICONEXCLAMATION | MB_OK); } else { iEdit--; SetWindowText(hBetBtn2, stEdit[iEdit].text); } break; /************************** START OVER BUTTON *****************************/ case 301: /********************* BEGIN OF COPY AREA *********************************/ c = 0; //CARD NUMBER REFERENCE FOR ARRAY x = 100; y = 100; numcards = 0; // NUMBER OF CARDS USED (FOR DRAWING THE SCREEN) next = 0; // NEXT CARD TO BE DRAWN dNextx = 100; // DEALER'S NEXT X POSITION dNexty = 100; // DEALER'S NEXT Y POSITION pNextx = 100; // PLAYER'S NEXT X POSITION pNexty = 250; // PLAYER'S NEXT Y POSITION pBusted = 0; // IS THE PLAYER BUSTED? pStand = 0; // DID THE PLAYER STAND? pScore = 0; // PLAYER'S SCORE dScore = 0; // DEALER'S SCORE dStand = 0; drawtext = 0; // VAR TO CONTROL TEXTOUT draw_your_bet = 0; // VAR TO CONTROL " YOUR BET: " show_d_score = 0; // SHOW DEALER SCORE? pBJ = 0; // DOES THE PLAYER HAVE A BLACKJACK? pWins = 0; // DID THE PLAYER WIN? GameOver = 0; // IS THE GAME OVER? z = 0; // ANOTHER TEMP COUNTER iEdit = 0; // COUNTER FOR EDIT ARRAY pNumAces = 0; // NUMBER OF ACES THAT THE PLAYER HAS dNumAces = 0; // NUMBER OF ACES THAT THE DEALER HAS whowon = 0; /********************** END OF COPY AREA ***************************/ if (DealPushed) { DestroyWindow(hBtn2); DestroyWindow(hBtn4); DestroyWindow(hBtnReset); } else { DestroyWindow(hBtn); DestroyWindow(hBetBtn1); DestroyWindow(hBetBtn2); DestroyWindow(hBetBtn3); } srand((unsigned) time(&t)); deck_init(); deck_shuffle(); deck_shuffle(); DealPushed = 0; InvalidateRect(hwnd, NULL, TRUE); SendMessage(hwnd, WM_CREATE, 0, 0); break; } // DRAW THE SCREEN InvalidateRect(hwnd, NULL, FALSE); UpdateWindow(hwnd); return 0; } case WM_DESTROY: // terminate the program PostQuitMessage(0); break; default: // Let Windows 95 process any messages not specified in the // preceding switch statement return DefWindowProc(hwnd, message, wParam, lParam); } return 0; } /**************INITIALIZE THE DECK****************************************/ void deck_init(void) { int i; for (i=0; i<52; i++) { deck[i]=i; } return; } /*************SHUFFLE THE DECK***************************************/ void deck_shuffle(void) { int i; int cv; int cpos; for (i=0; i<52; i++) { cpos = rand() % 52; cv= deck[i]; deck[i] = deck[cpos]; deck[cpos] = cv; } return; } /*********************************************************************/ /**************************************************************************/ int add_to_score(int car, int x) { int s; if ((car == 0) || (car == 1) || (car == 2) || (car == 3)) { // ACES if (x) { //PLAYER pNumAces++; if (pScore + 11 > 21) { pNumAces--; s = 1; } else { s = 11; } } else { dNumAces++; if (dScore + 11 > 21) { dNumAces--; s = 1; } else { s = 11; } } } else { if ((car>=36)&&(car<=51)) { // KINGS, QUEENS, JACKS, AND TENS s = 10; } ////////////////////////////////////////////////////////////////////// else { if ((car >= 4)&&(car <= 7)) { // TWO'S s = 2; } else { if ((car >= 8)&&(car <= 11)) { // THREE'S s = 3; } else { if ((car >= 12)&&(car <= 15)) { // FOUR'S s = 4; } else { if ((car >= 16)&&(car <= 19)) { // FIVE'S s = 5; } else { if ((car >= 20)&&(car <= 23)) { // SIX'S s = 6; } else { if ((car >= 24)&&(car <= 27)) { // SEVENS'S s = 7; } else { if ((car >= 28)&&(car <= 31)) { // EIGHT'S s = 8; } else { if ((car >= 32)&&(car <= 35)) { // NINES'S s = 9; } else { MessageBox (hwnd, "Uh oh!!! Error!!! Please let me know at: dustyd@probe.net", "Black Jack: v1", MB_OK); } } } } } } } } } } ///////////////////////////////////////////////////////////////////////////////// return s; } /*************************************************************************/