home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / BLAKJAK.PAK / OWLMAIN.CPP < prev    next >
Text File  |  1995-08-29  |  21KB  |  777 lines

  1. //-----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1995 by Borland International
  3. // Owlmain.cpp contains the GUI interface for the blackjack game.
  4. //-----------------------------------------------------------------------------
  5. #include <stdlib.h>
  6. #include "blakjack.h"
  7. #include <owl\point.h>
  8. #include <owl\module.h>
  9.  
  10. #include "mhcd2001.h"
  11. #include "owlmain.h"
  12.  
  13. const char AppName[] = "Blackjack";
  14. const int  TBlackjack::TextLen = 5; // text length.
  15.  
  16.  
  17. DEFINE_RESPONSE_TABLE1(TBlackjack, TDialog)
  18.   EV_COMMAND  (ID_STAND_BTN,    IdStandBtn)   ,
  19.   EV_COMMAND  (ID_HIT_BTN,      IdHitBtn)     ,
  20.   EV_COMMAND  (ID_BANKROLL_BTN, IdBankrollBtn),
  21.   EV_COMMAND  (ID_BET_BTN,      IdBetBtn)     ,
  22.   EV_EN_CHANGE(ID_DISP_BANKROLL_INPUT, CheckBankRollInput),
  23.   EV_EN_CHANGE(ID_DISP_BET_INPUT, CheckBetInput),
  24. END_RESPONSE_TABLE;
  25.  
  26. TBlackjack::TBlackjack(TWindow *pWin)
  27.   :TWindow(pWin),
  28.     TDialog(pWin, AppName),
  29.     Brush(TColor(0, 0, 0))
  30. {
  31.  
  32.     strcpy(prevBet, "100");
  33.     strcpy(prevBankroll, "1000");
  34.  
  35.     // You must new these card objects here.
  36.  
  37.      VBXCardCount = 0;
  38.  
  39.      // The order of "new"-ing the VBX cards is important, other wise
  40.      // the clipping order will become wrong. You do not want the first
  41.      // card which was dealt, to be sitting on top of all other cards, which
  42.      // were dealt later.
  43.  
  44.      for(int l=51; l >= 0; l--)
  45.      {
  46.             // All the card goes on the deck.
  47.             // This is placed on the Dealer side.
  48.             ppVBXCard[l] = new TVbxMhCardDeck(this,
  49.                                                                      500-l,
  50.                                                                      "VbControl",
  51.                                                                      DEALER_VBX_CARD1_X,
  52.                                                                      DEALER_VBX_CARD1_Y,
  53.                                                                      VBX_CARD_WIDTH,
  54.                                                                      VBX_CARD_LENGTH);
  55.             ppVBXCard[l]->SetPropVisible(FALSE);
  56.      }
  57.  
  58.      // Following objects are for the buttons.
  59.      pHitBtn      = new TButton(this, ID_HIT_BTN)       ;
  60.      pBankrollBtn = new TButton(this, ID_BANKROLL_BTN)  ;
  61.      pStandBtn    = new TButton(this, ID_STAND_BTN)     ;
  62.      pBetBtn      = new TButton(this, ID_BET_BTN)       ;
  63.  
  64.      // The following objects are used to display scores
  65.      pIdDispBankroll1 = new TStatic(this, ID_DISP_BANKROLL1); // Bankroll
  66.      pIdDispBet       = new TStatic(this, ID_DISP_BET)      ; // Bet
  67.      pIdDispPPoints   = new TStatic(this, ID_DISP_PPOINTS)  ; // Player points.
  68.      pIdDispDPoints   = new TStatic(this, ID_DISP_DPOINTS)  ; // Dealer points.
  69.  
  70.      // pointer to Edit field used to Input Bankroll amount.
  71.      pEInputBankRoll  = new TEdit(this, ID_DISP_BANKROLL_INPUT, TBlackjack::TextLen);
  72.      pEInputBet       = new TEdit(this, ID_DISP_BET_INPUT,      TBlackjack::TextLen);
  73.  
  74.      // pointer to validater which validates Input Bankroll field.
  75.      pValidBankRoll   = new TFilterValidator("0-9");
  76.      pValidBet        = new TFilterValidator("0-9");
  77.  
  78.      pEInputBankRoll->SetValidator(pValidBankRoll);
  79.      pEInputBet->SetValidator(pValidBet);
  80.  
  81.      pEInputBankRoll->ShowWindow(SW_HIDE);
  82.      pEInputBet->ShowWindow(SW_HIDE);
  83. }
  84. TBlackjack::~TBlackjack()
  85. {
  86.     for (int l=0; l < 52; l++)
  87.         delete ppVBXCard[l];
  88.  
  89.     delete pEInputBankRoll;
  90.     delete pEInputBet;
  91.  
  92.     delete pValidBankRoll;
  93.     delete pValidBet;
  94.  
  95.     delete pHitBtn;
  96.     delete pBankrollBtn;
  97.     delete pStandBtn;
  98.     delete pBetBtn;
  99.  
  100.     delete pIdDispBankroll1;
  101.     delete pIdDispBet;
  102.     delete pIdDispPPoints;
  103.     delete pIdDispDPoints;
  104. }
  105. void
  106. TBlackjack::SetupWindow()
  107. {
  108.     TDialog::SetupWindow();
  109.     // Center the dialog.
  110.     int x = ::GetSystemMetrics(SM_CXSCREEN);
  111.     int y = ::GetSystemMetrics(SM_CYSCREEN);
  112.     MoveWindow(
  113.           (x/2 - GetWindowRect().Width()/2),
  114.           (y < GetWindowRect().Height() ? 0: (y-GetWindowRect().Height())/2),
  115.           GetWindowRect().Width(), GetWindowRect().Height()
  116.       );
  117.  
  118.     // Enables the buttons in the dialog.
  119.     InitBlackjack();
  120.     DisplayCardOnTable();
  121. }
  122.  
  123. BOOL
  124. TBlackjack::EvInitDialog( HWND hWnd )
  125. {
  126.   BOOL rv = TDialog::EvInitDialog( hWnd );
  127.   return rv;
  128. }
  129. void
  130. TBlackjack::InitBlackjack()
  131. {
  132.      pBankrollBtn->EnableWindow(TRUE);
  133.      pHitBtn->EnableWindow(FALSE);
  134.      pStandBtn->EnableWindow(FALSE);
  135.      pBetBtn->EnableWindow(FALSE);
  136. }
  137.  
  138. void
  139. TBlackjack::DisplayCardOnTable()
  140. {
  141.      int k = 5;
  142.      // Display card on dealer's part of the table
  143.      for(int i=0; i < 9; i++,k++)
  144.      {
  145.             ppVBXCard[i]->SetPropLeft(DEALER_VBX_CARD1_X+ (i*VBX_CARD_WIDTH*10));
  146.             ppVBXCard[i]->SetPropTop (DEALER_VBX_CARD1_Y);
  147.  
  148.             ppVBXCard[i]->SetPropSuit(i%4);
  149.             ppVBXCard[i]->SetPropValue(k%52);
  150.  
  151.             ppVBXCard[i]->SetPropVisible(TRUE);
  152.      }
  153.  
  154.      // Display cards on player's part of the table
  155.      int p = 0;
  156.      for(k =13, i=10; i < 19; i++, k--, p++)
  157.      {
  158.             ppVBXCard[i]->SetPropLeft(PLAYER_VBX_CARD1_X + p*VBX_CARD_WIDTH*10);
  159.             ppVBXCard[i]->SetPropTop(PLAYER_VBX_CARD1_Y);
  160.             if((13-k)%2)
  161.             {
  162.                 ppVBXCard[i]->SetPropSuit((13-k)%4);
  163.                 ppVBXCard[i]->SetPropValue(k%52);
  164.             }
  165.             else
  166.             {
  167.                 ppVBXCard[i]->SetPropCardBack(TVbxMhCardDeck::CardBack_10_Castle);
  168.             }
  169.             ppVBXCard[i]->SetPropVisible(TRUE);
  170.      }
  171. }
  172. void
  173. TBlackjack::RemoveAllCardsOnTable()
  174. {
  175.      for(int i=0; i < 52; i++)
  176.      {
  177.             ppVBXCard[i]->SetPropVisible(FALSE);
  178.      }
  179. }
  180.  
  181. //
  182. // This is the callback function for the "BANKROLL" button.
  183. //
  184. void
  185. TBlackjack::IdBankrollBtn()
  186. {
  187.  
  188.      BankRollEnteredFirstTime = 1;
  189.  
  190.     // Enable the input field for bankroll
  191.     pEInputBankRoll->ShowWindow(SW_SHOWNORMAL);
  192.     pEInputBankRoll->Clear();
  193.     pEInputBankRoll->Insert(prevBankroll);
  194.     pEInputBankRoll->SetSelection(0,4);
  195.  
  196.     pBetBtn->SetFocus();
  197.     // Enables "return" key to work on the "Bet" button.
  198.     SetDefaultId(pBetBtn->GetId());
  199. }
  200. //
  201. // This is the callback function for the "BET"/"PLAY" button.
  202. //
  203. void
  204. TBlackjack::IdBetBtn()
  205. {
  206.     char btnText[10];
  207.     pBetBtn->GetWindowText(btnText,8); // It migth be "&Bet" or "Pla&y"
  208.     if(!strcmp("&Bet", btnText))
  209.     {
  210.         ProcessBetButton();
  211.     }
  212.     else
  213.     {
  214.         ProcessPlayButton();
  215.     }
  216.  
  217. }
  218. // When "Bet" button is hit, this function is called.
  219. // The "Bet" button can be clicked under 2 different conditions.
  220. // 1) When a new bankroll amount is entered and
  221. // 2) When a game is finished.
  222. // For case one, the input bankroll amount must be more than 0, then only
  223. // the bet button is activated.
  224. // When the player hits the bet button he is allowed to enter a bet
  225. // amount in the bet input field.
  226. // "Bet" button is toggled to "Play" button.
  227. // By default, the previous bet amount is displayed in the "Bet" input field.
  228. // If the previous bet amount is more than the current bankroll amount,
  229. // it is made equal to bankroll amount and displayed.
  230. void
  231. TBlackjack::ProcessBetButton()
  232. {
  233. char str[TBlackjack::TextLen+6];
  234. int currBankroll;
  235. int bankrollMoreThanZero = 0;
  236.  
  237.         if(BankRollEnteredFirstTime)
  238.         {
  239.             // When new bankroll is entered, it is read from
  240.             // the input field.
  241.             pEInputBankRoll->GetLine(str, TBlackjack::TextLen, 0);
  242.             currBankroll = atoi(str);
  243.  
  244.             if(!currBankroll)
  245.             {
  246.                 bankrollMoreThanZero = 0;
  247.                 currBankroll = 0;
  248.                 strcpy(str, "You are Bankrupt!");
  249.                 pIdDispBet->SetText(str);
  250.             }
  251.             else
  252.             {
  253.                 bankrollMoreThanZero = 1;
  254.                 strcpy(prevBankroll, str);
  255.                 BankRollEnteredFirstTime = 0;
  256.                 (bj.getPlayer(1))->getPocket().setTotal(currBankroll);
  257.             }
  258.         }
  259.         else
  260.         {
  261.             bankrollMoreThanZero = 1;
  262.             currBankroll = (bj.getPlayer(1))->getPocket().getTotal();
  263.             itoa(currBankroll, str, 10);
  264.         }
  265.         if(bankrollMoreThanZero)
  266.         {
  267.             // When bankroll amount is more than 0.
  268.             // Make bet button to play button
  269.             pBetBtn->SetWindowText("Pla&y");
  270.  
  271.             // Clear and activate the Input bet field
  272.             pEInputBet->ShowWindow(SW_SHOWNORMAL);
  273.             pEInputBet->Clear();
  274.  
  275.             // Set focus on play(bet) button
  276.             pBetBtn->SetFocus();
  277.  
  278.             // Enables "return" key to work on the "Bet" button.
  279.             SetDefaultId(pBetBtn->GetId());
  280.  
  281.             // Disable the Bankroll Edit field and Button.
  282.             pEInputBankRoll->ShowWindow(SW_HIDE);
  283.             ::EnableWindow(GetDlgItem(ID_BANKROLL_BTN), FALSE);
  284.  
  285.             // Display the new/changed bankroll
  286.             pIdDispBankroll1->SetText(str);
  287.  
  288.             // If bet is more than bankroll amount, make the bet
  289.             // equal to bankroll, else use the prev bet amount.
  290.             if(atoi(prevBet) > currBankroll)
  291.             {
  292.                 char currBankrollStr[10];
  293.                 itoa(currBankroll, currBankrollStr, 10);
  294.                 pEInputBet->Insert(currBankrollStr);
  295.             }
  296.             else
  297.                 pEInputBet->Insert(prevBet);
  298.         }
  299. }
  300.  
  301. // When the play button is hit this funtion is called.
  302. // "Play" button is toggled to "Bet" button.
  303. // If the deck has less than 26 cards, a new deck is introduced.
  304. // The Old cards are removed from the table
  305. // The input bet amount is captured form the input bet field.
  306. // If the "Bet" amount is more than Bankroll
  307. // amount or 0, a warning is issued, otherwise for a valid bet, the "Hit" and
  308. // "Stand" buttons are enabled but all the other buttons are disabled.
  309. // Since this is a new game, the dealer and the player gets
  310. // 2 cards each. Both the players cards are displayed but
  311. // for the dealer, only one card is displayed.
  312. // If any of the players score 21(with 2 cards), he  wins(Blackjack).
  313.  
  314. void
  315. TBlackjack::ProcessPlayButton()
  316. {
  317.         {
  318.             char buf[TextLen+1] = "\0";
  319.             int money = (bj.getPlayer(1))->getPocket().getTotal();
  320.  
  321.  
  322.             // Check if the total card count on the deck is less than equal
  323.             // to 26, if it is, start with a new deck of cards.
  324.  
  325.             if(bj.getDealer().getDeck().GetTotal() < 26)
  326.             {
  327.                 strcpy(buf, "New Deck");
  328.                 pIdDispBet->SetText(buf);
  329.                 VBXCardCount = 0;
  330.                 delete (&bj.getDealer().getDeck());
  331.                 bj.getDealer().setDeck(new Deck);
  332.             }
  333.  
  334.             RemoveAllCardsOnTable();
  335.  
  336.             // Get the input bet amount
  337.             pEInputBet->GetLine(buf, TBlackjack::TextLen, 0);
  338.             int betMoney = atoi(buf);
  339.             strcpy(prevBet, buf);
  340.  
  341.             // Disable the Bet input
  342.             pEInputBet->ShowWindow(SW_HIDE);
  343.  
  344.             // Toggle "Play" button to "Bet" button.
  345.             pBetBtn->SetWindowText("&Bet");
  346.  
  347.             if(!(bj.getPlayer(1))->Bet(betMoney))
  348.             {
  349.                 //if bet Money > bankroll amount, or 0.
  350.                 strcpy(buf, "Bet > bankroll or 0");
  351.                 pIdDispBet->SetText(buf);
  352.  
  353.                 // Set the points from the previous game to 0
  354.                 pIdDispPPoints->SetText("0");
  355.                 pIdDispDPoints->SetText("0");
  356.             }
  357.             else
  358.             {
  359.                 // Valid bet amount.
  360.                 // Display and set bet amount.
  361.                 pIdDispBet->SetText(buf);
  362.                 (bj.getPlayer(1))->getPocket().setTotal(money);
  363.  
  364.                 // Enable "Stand" and "Hit" buttons
  365.                 pStandBtn->EnableWindow   (TRUE);
  366.                 pHitBtn->EnableWindow     (TRUE);
  367.                 pHitBtn->SetFocus();
  368.  
  369.                 // Enable "return" key to activate the "Hit" button.
  370.                 SetDefaultId(pHitBtn->GetId());
  371.  
  372.                 // Disable "Bet" and "Bankroll" button.
  373.                 pBankrollBtn->EnableWindow(FALSE);
  374.                 pBetBtn->EnableWindow     (FALSE);
  375.  
  376.                 // For a new game get rid of the cards
  377.                 // in dealers hand and players hand.
  378.                 bj.getDealer().flushCards();
  379.                 (*bj.getPlayer(1)).flushCards();
  380.  
  381.                 // Dealer deals 2 cards to the player and displays
  382.                 *(bj.getPlayer(1)) << bj.getDealer();
  383.                 *(bj.getPlayer(1)) << bj.getDealer();
  384.  
  385.                 // Display the dealt cards on players side.
  386.                 *this << *(bj.getPlayer(1));
  387.  
  388.                 // Dealer deals a card to himself and displays.
  389.                 bj.getDealer() << bj.getDealer();
  390.                 *this << bj.getDealer();
  391.  
  392.                 // Dealer deals another card to himself which is not displayed.
  393.                 bj.getDealer() << bj.getDealer();
  394.  
  395.                 // Check for Blackjack condition here.
  396.                 if(bj.IsBlackjack())
  397.                 {
  398.                         gameOverCleanupBusted(bj.whoLost());
  399.                 }
  400.             }
  401.         }
  402. }
  403. //
  404. // This is the callback function for the "HIT" button.
  405. //
  406. void
  407. TBlackjack::IdHitBtn()
  408. {
  409.          *(bj.getPlayer(1)) << bj.getDealer();      //player gets a card.
  410.          int pPoint = (*bj.getPlayer(1)).getPoints();
  411.  
  412.          // Display the players card.
  413.          *this << *(bj.getPlayer(1));
  414.  
  415.          if(pPoint > 21)
  416.          {
  417.               gameOverCleanupBusted(PLAYER);
  418.          }
  419. }
  420. //
  421. // This is the callback function for the "STAND" button.
  422. //
  423. void
  424. TBlackjack::IdStandBtn()
  425. {
  426. int dPoint;
  427. int pPoint;
  428.  
  429.          pPoint = (*bj.getPlayer(1)).getPoints();
  430.          dPoint = (bj.getDealer()).getPoints();
  431.          if(dPoint > pPoint)
  432.          {
  433.                 gameOverCleanupBusted(PLAYER);
  434.          }
  435.          else
  436.          {
  437.              if((dPoint >= 17) && (pPoint >= dPoint))
  438.              {
  439.                 // In this case the Dealer cannot draw any new
  440.                 // cards, game is over, Dealer busted.
  441.                 if(pPoint == dPoint)
  442.                     gameOverCleanupBusted(BOTH);
  443.                 else
  444.                     gameOverCleanupBusted(DEALER);
  445.              }
  446.              else
  447.              {
  448.                  // Dealers algorithm for hit or stand.
  449.                  int done = 0;
  450.                  while((dPoint < 17) && !done)
  451.                  {
  452.                      bj.getDealer() << bj.getDealer();
  453.                      dPoint = (bj.getDealer()).getPoints();
  454.                      if(dPoint > pPoint)
  455.                      {
  456.                         if(dPoint > 21)
  457.                             gameOverCleanupBusted(DEALER);
  458.                         else
  459.                             gameOverCleanupBusted(PLAYER);
  460.                         done = 1;
  461.                      }
  462.                      if(!done && (dPoint == pPoint))
  463.                      {
  464.                         gameOverCleanupBusted(BOTH);
  465.                         done = 1;
  466.                      }
  467.                      if(!done && (dPoint < pPoint) && (dPoint >=17))
  468.                      {
  469.                         gameOverCleanupBusted(DEALER);
  470.                         done = 1;
  471.                      }
  472.  
  473.                  }// end while
  474.  
  475.              }
  476.          }
  477. }
  478.  
  479. // Input parameter is the person who got busted. Valid values are.
  480. // PLAYER 1
  481. // DEALER 2
  482. // BOTH   3
  483. // UNKNOWN 4
  484. void
  485. TBlackjack::gameOverCleanupBusted(int who) throw(const char *)
  486. {
  487. char buf[20];
  488.         if(who == UNKNOWN)
  489.             throw("gameOverCleanup():Input value Unknow.");
  490.  
  491.         // Put up the winning sign.
  492.  
  493.         if(who == DEALER)
  494.         {
  495.             pIdDispBet->SetText("Player Won");
  496.             (*bj.getPlayer(1)).Won();
  497.             int t = (*bj.getPlayer(1)).getPocket().getTotal();
  498.             itoa(t, buf, 10);
  499.             pIdDispBankroll1->SetText(buf);
  500.         }
  501.  
  502.         if(who == PLAYER)
  503.         {
  504.             pIdDispBet->SetText("Dealer Won");
  505.             (*bj.getPlayer(1)).Lost();
  506.             int t = (*bj.getPlayer(1)).getPocket().getTotal();
  507.             itoa(t, buf, 10);
  508.             pIdDispBankroll1->SetText(buf);
  509.         }
  510.  
  511.         if(who == BOTH)
  512.         {
  513.             pIdDispBet->SetText("Draw");
  514.         }
  515.  
  516.         // Display all the Dealers card.
  517.         *this << bj.getDealer();
  518.  
  519.         // Check if the Bank roll is 0,
  520.         // Move focus to the Bankroll button in that case.
  521.  
  522.         if(!(bj.getPlayer(1))->getPocket().getTotal())
  523.         {
  524.             // Bankroll is 0 because you lost,
  525.             // time to put more money in the bankroll.
  526.  
  527.             pIdDispBet->SetText("Enter Bankroll");
  528.  
  529.             pHitBtn->EnableWindow     (FALSE);
  530.             pStandBtn->EnableWindow   (FALSE);
  531.             pBetBtn->EnableWindow     (FALSE);
  532.             pBankrollBtn->EnableWindow(TRUE);
  533.             pBankrollBtn->SetFocus();
  534.             // Enables "return" key to work on the "bankroll" button. 
  535.             SetDefaultId(pBankrollBtn->GetId());
  536.  
  537.         }
  538.         else
  539.         {
  540.  
  541.             // BankRoll is not 0 but it cannot be more
  542.             // than $9999, ie the limit.
  543.             // If it is above it then start a new game.
  544.             if((bj.getPlayer(1))->getPocket().getTotal() < 9999)
  545.             {
  546.                 pBankrollBtn->EnableWindow(FALSE);
  547.                 pHitBtn->EnableWindow     (FALSE);
  548.                 pStandBtn->EnableWindow   (FALSE);
  549.                 pBetBtn->EnableWindow     (TRUE );
  550.                 pBetBtn->SetFocus();
  551.                 // Enables "return" key to work on the "Bet" button.
  552.                 SetDefaultId(pBetBtn->GetId());
  553.  
  554.             }
  555.             else
  556.             {
  557.                 // If bankroll is 9999 or more.
  558.                 pIdDispBet->SetText("Max Bankroll !!!");
  559.  
  560.                 pHitBtn->EnableWindow     (FALSE);
  561.                 pStandBtn->EnableWindow   (FALSE);
  562.                 pBetBtn->EnableWindow     (FALSE);
  563.                 pBankrollBtn->EnableWindow(TRUE);
  564.                 pBankrollBtn->SetFocus();
  565.                 // Enables "return" key to work on the "Bankroll" button.
  566.                 SetDefaultId(pBankrollBtn->GetId());
  567.  
  568.             }
  569.         }
  570. }
  571. void
  572. TBlackjack::CheckBankRollInput()
  573. {
  574.     // Enable bet button if valid input is present.
  575.     if(pEInputBankRoll->GetLineLength(0))
  576.         ::EnableWindow(GetDlgItem(ID_BET_BTN), TRUE);
  577.     else
  578.         ::EnableWindow(GetDlgItem(ID_BET_BTN), FALSE);
  579. }
  580. void
  581. TBlackjack::CheckBetInput()
  582. {
  583.     // Enable bet button if valid input is present.
  584.     if(pEInputBet->GetLineLength(1))
  585.     {
  586.         //It should display "Play" button
  587.         pBetBtn->SetWindowText("Pla&y");
  588.     }
  589.     else
  590.     {
  591.         // Should display the "Bet" button.
  592.         pBetBtn->SetWindowText("&Bet");
  593.     }
  594. }
  595. //------------------------------- operator << () ---------------------------
  596. // This is operator is used to display the cards on the table
  597. // for the dealer.
  598.  
  599. TBlackjack::operator << (Dealer &rhs)
  600. {
  601. int total = rhs.getTotalCards();
  602.   for(int i =0; i < total; i++)
  603.   {
  604.  
  605.       if(rhs.getCards()[i] != NULL)
  606.       {
  607.             if(!(rhs.getCards()[i])->getVBXCard())
  608.             {
  609.                 TVbxMhCardDeck * pCard = ppVBXCard[VBXCardCount];
  610.                 pCard->SetPropSuit(ConvToVBXSuite(rhs.getCards()[i]->getType()));
  611.  
  612.                 // Make the card faceup.
  613.                 pCard->SetPropValue(ConvToVBXNum(rhs.getCards()[i]->getNumber()));
  614.  
  615.                 // Shift by "half the card width" to right.
  616.                 pCard->SetPropLeft(DEALER_VBX_CARD1_X+ (i*VBX_CARD_WIDTH*10));
  617.                 pCard->SetPropTop (DEALER_VBX_CARD1_Y);
  618.                 pCard->SetPropVisible(TRUE);
  619.  
  620.                 // "VBXCardCount", points to the next VBX card which is
  621.                 // available for display.
  622.                 (rhs.getCards()[i])->setVBXCard(VBXCardCount+1);
  623.                 VBXCardCount++;
  624.  
  625.             }
  626.  
  627.       }
  628.   }
  629.  
  630.   // Set the points field of the dealer in TBlackjack dialog.
  631.   char points[10];
  632.   itoa(rhs.getPoints(), points, 10);
  633.   pIdDispDPoints->SetText(points);
  634.   return 1;
  635. }
  636. //------------------------------- operator << () ---------------------------
  637. // This is operator is used to display the cards on the table
  638. // for the player.
  639.  
  640. TBlackjack::operator << (Player &rhs)
  641. {
  642. int total = rhs.getTotalCards();
  643.   for(int i =0; i < total; i++)
  644.   {
  645.       if(rhs.getCards()[i] != NULL)
  646.       {
  647.             if(!(rhs.getCards()[i])->getVBXCard())
  648.             {
  649.                 TVbxMhCardDeck * pCard = ppVBXCard[VBXCardCount];
  650.                 pCard->SetPropSuit(ConvToVBXSuite(rhs.getCards()[i]->getType()));
  651.  
  652.                 // Make the card faceup.
  653.                 pCard->SetPropValue(ConvToVBXNum(rhs.getCards()[i]->getNumber()));
  654.  
  655.                 pCard->SetPropLeft(PLAYER_VBX_CARD1_X + i*VBX_CARD_WIDTH*10);
  656.                 pCard->SetPropTop(PLAYER_VBX_CARD1_Y);
  657.  
  658.                 pCard->SetPropVisible(TRUE);
  659.  
  660.                 // VBXCardCount, keeps the card count on the actual VBX deck.
  661.                 // count 12 menas 0-11 cards are already dealt.
  662.                 // The cards always remain here only while displaying it
  663.                 // displayed from here.
  664.  
  665.                 (rhs.getCards()[i])->setVBXCard(VBXCardCount+1);
  666.                 VBXCardCount++;
  667.  
  668.             }
  669.  
  670.       }
  671.   }
  672.  
  673.   // Set the points field of the dealer in TBlackjack dialog.
  674.   char points[10];
  675.   itoa(rhs.getPoints(), points, 10);
  676.   pIdDispPPoints->SetText(points);
  677.   return 1;
  678. }
  679.  
  680. void
  681. TBlackjackFrame::SetupWindow()
  682. {
  683.   TFrameWindow::SetupWindow();
  684.   BlackjackDialog = new TBlackjack(this);
  685.   SetClientWindow(BlackjackDialog);
  686.  
  687.   // Center the Frame window.
  688.   int x = ::GetSystemMetrics(SM_CXSCREEN);
  689.   int y = ::GetSystemMetrics(SM_CYSCREEN);
  690.   MoveWindow(
  691.                 (x/2 - GetWindowRect().Width()/2),
  692.                 (y < GetWindowRect().Height() ? 0: (y-GetWindowRect().Height())/2),
  693.                 GetWindowRect().Width(), GetWindowRect().Height()
  694.                 );
  695. }
  696. void
  697. MovableDialog::SetupWindow()
  698. {
  699.     TDialog::SetupWindow();
  700.  
  701.     MoveWindow(
  702.             Parent->GetWindowRect().TopLeft().x+topLeftX,
  703.             Parent->GetWindowRect().TopLeft().y+topLeftY,
  704.           GetWindowRect().Width(), GetWindowRect().Height()
  705.       );
  706. }
  707. DEFINE_RESPONSE_TABLE(TBlackjackApp)
  708.   EV_COMMAND(ID_ABOUT, IdAbout),
  709. END_RESPONSE_TABLE;
  710.  
  711. void
  712. TBlackjackApp::IdAbout()
  713. {
  714.     TDialog *pTDlg = new MovableDialog(0,0,MainWindow , AboutBox);
  715.     pTDlg->Execute();
  716. }
  717.  
  718. //
  719. // The card suite runs from 1-4
  720. // The VBX suite are enum-ed they have different values.
  721. //
  722. int
  723. ConvToVBXSuite(int cardSuite)
  724. {
  725.     return((cardSuite-1)+ TVbxMhCardDeck::Suit_0_Clubs);
  726. }
  727.  
  728. //
  729. // The card numbers runs from 0(Ace)-13(Queen), this is for internal
  730. // representation nothing to do with Blackjack card points.
  731. // The VBX card numbers are enum-ed and they have different values.
  732. // it runs from (1-14), 0 is the back of the card.
  733.  
  734. int
  735. ConvToVBXNum(int cardNum)
  736. {
  737.     return (cardNum + TVbxMhCardDeck::Value_1_Ace);
  738. }
  739.  
  740. //----------------------------------------------------------------------------
  741. //
  742. // Create the BlackJack dialog as the application's main window.
  743. //
  744. void
  745. TBlackjackApp::InitMainWindow()
  746. {
  747.   EnableBWCC();
  748.   MainWindow = new TBlackjackFrame(0, "Turbo 21", 0, TRUE);
  749.   MainWindow->SetIcon(this, "BJ_ICO");
  750.   MainWindow->AssignMenu("BJ_MENU");
  751.   MainWindow->Attr.Style &= ~(WS_MAXIMIZEBOX | WS_THICKFRAME|WS_POPUP);
  752. }
  753. TBlackjackApp::~TBlackjackApp()
  754. {
  755.     delete MainWindow;
  756. }
  757. int
  758. OwlMain(int /*argc*/, char* /*argv*/ [])
  759. {
  760.   int retVal;
  761.   TBIVbxLibrary vbxLib;
  762.   try{
  763.         retVal = TBlackjackApp(AppName).Run();
  764.   }
  765.   catch(const char *str)
  766.   {
  767.         ::MessageBox(0, str, "Exception", MB_OK);
  768.   }
  769.   catch(...)
  770.   {
  771.         ::MessageBox(0, "Exception Occured", "Generic Exception", MB_OK);
  772.   }
  773.   return retVal;
  774. }
  775.  
  776.  
  777.