Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6389271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:22:58+00:00 2026-05-25T03:22:58+00:00

What is the general alogirthm for a BlackJack game ? I’m writing one in

  • 0

What is the general alogirthm for a BlackJack game ?
I’m writing one in c++ and end up having WAY too many if statements which ruin the whole thing.

The project is a win32 GUI application, and Im posting the message loop as well as the part of the program that checks the game’s status
Posting the complete code will make it humongous, so here are the links to all the files:
Full Source Code

Message Loop

LRESULT CALLBACK WndProc( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam ){
  switch(Msg)
  {
  case WM_PAINT:
  {
      PAINTSTRUCT PS;
      HDC hDC = BeginPaint(hWnd, &PS);
      You.Draw(hDC);
      Dealer.Draw(hDC);
      EndPaint(hWnd, &PS);
      if(Bet.Enabled){
          Bet.GetFocus();
          Bet.Select(0,Bet.Length());
      }
  }
  return 0;
  case WM_CTLCOLORSTATIC:
  {
      SetBkMode((HDC)wParam, TRANSPARENT);
  }
  return (INT_PTR)(HBRUSH)GetStockObject(NULL_BRUSH);
  case WM_CREATE:
  {
      //Create edit control
      Bet.Create(hWnd, 10, 550, 100, 25, "0");
      //Create labels
      char Buffer[30];
      sprintf(Buffer, "%d", You.HandValue());
      BetLabel.Create(hWnd, 10, 500, 100, 40, "Enter Bet Amount");
      GameControls.Create(hWnd, 10, 375, 100, 40, "Game Controls");
      PlayerLabels.Create(hWnd, 10, 10, 100, 20, "You:");
      PlayerLabels.Create(hWnd, 10, 150, 100, 20, "Dealer:");
      HandValueLabel.Create(hWnd, (int)You.x[You.cards] + 85, (int)You.y[You.cards] + 25, 100, 20, Buffer);
      YourMoney.Create(hWnd, 125, 500, 100, 40, "Your money: ");
      sprintf(Buffer, "%d", You.money);
      MoneyValue.Create(hWnd, 125, 525, 100, 40, Buffer);
      //Create buttons
      Ok.Create(hWnd, 10, 600, 100, 50, "Ok");
      Hit.Create(hWnd, 10, 425, 100, 50, "Hit");
      Stand.Create(hWnd, 120, 425, 100, 50, "Stand");
      //Select Text
      Bet.Select(0,3);
  }
  return 0;
  case WM_CLOSE:
    exit(0);
    break;
  case WM_COMMAND:
  {
      switch(HIWORD(wParam))
      {
      case BN_CLICKED:
      {
          switch(LOWORD(wParam))
          {
          case ID_OK:
          {
              //Place bet
              int bet = 0;
              bet = StringToNumber(Bet.Text());
              You.money -= bet;
              char Buffer[30];
              sprintf(Buffer, "%d", You.money);
              MoneyValue.SetText(Buffer);
              //Update the window
              InvalidateRect(hWnd, 0, TRUE);
              You.Bet = YES;
              Ok.Disable();
              Bet.Disable();
              You.DealCard();
              You.DealCard();
              Dealer.DealCard();
              Dealer.DealCard();
              You.win = false;
              You.bust = false;
              You.playing = YES;
              Dealer.win = NO;
              Dealer.bust = NO;
              Dealer.playing = YES;
          }
          break;
          case ID_HIT:
          {
             if(You.HandValue() < 21 && You.playing && !You.bust){
                 //Deal a card to the player, if he hasn't won or lost
                 You.DealCard();
             }
             if((Dealer.HandValue() <= 17 || Dealer.HandValue() < You.HandValue()) && Dealer.playing)
                       {
                         Dealer.DealCard();
                       }
            //Update Hand Value
            char Buffer[30];
            sprintf(Buffer, "%d", You.HandValue());
            HandValueLabel.SetText(Buffer);
            InvalidateRect(hWnd,0,TRUE);
            MoveWindow(HandValueLabel.Handle, (int)You.x[You.cards] + 80, (int)You.y[You.cards] + 25, 100, 20, TRUE);
            RedrawWindow(hWnd, NULL, NULL, RDW_INVALIDATE | RDW_INTERNALPAINT);
          }
          break;
          case ID_STAND:
              if(Hit.Enabled){
                  //Don't deal more cards
                  Hit.Disable();
                  You.playing = false;
              }
              if((Dealer.HandValue() <= 17 || Dealer.HandValue() < You.HandValue()) && Dealer.playing){
                  //Deal a card to the dealer
                  Dealer.DealCard();
              }
          }

StatusChecker

void ProcessStatus(HWND hWnd){
     if(Dealer.HandValue() > 17){
          if(Dealer.HandValue() > You.HandValue() && Dealer.playing){
              Dealer.playing = false;
            }
          else if(!You.playing && Dealer.HandValue() <= You.HandValue() && Dealer.playing){
              Dealer.DealCard();
            }
        }
      else if(Dealer.HandValue() <= 17 && Dealer.HandValue() <= You.HandValue() && !You.playing && Dealer.playing){
          Dealer.DealCard();
        }
      if(EvaluateStatus() != 0){
          status = OVER;
        }
     if(EvaluateStatus() == -1){
         switch(MessageBoxA(hWnd, "You Lost! \n Keep Playing ?", "You Lost!", MB_YESNO | MB_ICONQUESTION))
         {
         case IDYES:
             Reset();
             break;
         case IDNO:
             exit(0);
             break;
         }
     }
     else if(EvaluateStatus() == 1){
         switch(MessageBoxA(hWnd, "You Won! \n Keep Playing ?", "You Won!", MB_YESNO | MB_ICONQUESTION))
         {
         case IDYES:
             Reset();
             break;
         case IDNO:
             exit(0);
             break;
         }
     }
    }
int EvaluateStatus()
{
  if(You.HandValue() > 21 && You.playing)
    {
      You.bust = true;
      Dealer.win = true;
      You.playing = NO;
      return -1;
    }
  if(You.HandValue() == 21)
    {
      You.win = true;
      return 1;
    }
  if(Dealer.HandValue() > 21)
    {
      Dealer.bust = true;
      You.win = true;
      return 1;
    }
  if(Dealer.HandValue() == 21)
    {
      Dealer.win = true;
      return -1;
    }
  if(!You.playing && Dealer.HandValue() > You.HandValue() &&!Dealer.bust)
    {
      Dealer.win = true;
      return -1;
    }
  if(You.HandValue() > Dealer.HandValue() && (!You.playing && (!Dealer.playing || Dealer.bust)))
    {
      You.win = true;
      return 1;
    }
  if(You.HandValue() == Dealer.HandValue() && (!You.playing || !Dealer.playing))
    {
      You.win = true;
      return 1;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T03:22:59+00:00Added an answer on May 25, 2026 at 3:22 am

    As a first step: I probably wouldn’t mix game logic and your GUI logic.. separate them into different modules.

    And if you only have two players, you only need a bool to decide which one won. You don’t need for both of the players Bust and Won variables.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

General javascript question here, which would also be good to know how(if possible) to
General question about java servlets and the best way to handle requests. If I
I'm writing a general purpose function for feeding query string straight into a sproc.
I am having a general problem finding a good algorithm for generating each possible
Is there a general algorithm that address playback speed of a log file having
I am looking a general algorithm that would convert from one (arbitrary) numerical base
I have a counting algorithm for which I am trying to get a general
I am having some trouble getting general files to read into a program that
I have a MasterProcess which is a specialization of a general process concept and
General wisdom is when you remove a component from the stage you also need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.