LRESULT CALLBACK HitButtonProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) { if (msg == WM_LBUTTONDOWN) { PlaySound(L"sound-flipcard.wav", NULL, SND_FILENAME | SND_ASYNC); GameEngine* gameEngine = GameEngine::getInstance(); Table* table = gameEngine->getTable(); Hand* playerHand = table->getPlayerHand(); playerHand->dealCard(false); // Check for bust. if (playerHand->isBusted()) { table->setState(TABLE_STATE_FINISHED); updateTextarea(hStaticTableMiddleMessage, "Busted. Dealer Wins."); } RedrawWindow(gameEngine->getHWnd(), NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW); } return CallWindowProc(oldHitButtonProc, hwnd, msg, wp, lp); }
LRESULT CALLBACK StandButtonProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) { if (msg == WM_LBUTTONDOWN) { GameEngine* gameEngine = GameEngine::getInstance(); User* user = gameEngine->getUser(); Table* table = gameEngine->getTable(); table->setState(TABLE_STATE_STANDING); Hand* dealerHand = table->getDealerHand(); Hand* playerHand = table->getPlayerHand(); // // Flip over the first card. // dealerHand->GetCards()->at(0)->setFacedown(false); PlaySound(L"sound-flipcard.wav", NULL, SND_FILENAME | SND_ASYNC); RedrawWindow(gameEngine->getHWnd(), NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW); Sleep(500); // // See if we need additional cards. // while (true) { std::vector<int>* vals = dealerHand->GetValues(); bool done = false; for (int i = 0; i < vals->size(); i++) { // Dealer must stop taking cards // if he has a value of 17 or higher. if (vals->at(i) >= 17) { done = true; break; } } if (done) { break; } PlaySound(L"sound-flipcard.wav", NULL, SND_FILENAME | SND_ASYNC); dealerHand->dealCard(false); RedrawWindow(gameEngine->getHWnd(), NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW); Sleep(500); } // // Determine winner. Update balance amounts. // // table->setState(TABLE_STATE_READY); std::vector<int>* dealerValues = dealerHand->GetValues(); std::vector<int>* playerValues = playerHand->GetValues(); int dealerFinal = 0; int playerFinal = 0; for (int i = 0; i < dealerValues->size(); i++) { if (dealerValues->at(i) > dealerFinal && dealerValues->at(i) < 22) { dealerFinal = dealerValues->at(i); } } for (int i = 0; i < playerValues->size(); i++) { if (playerValues->at(i) > playerFinal && playerValues->at(i) < 22) { playerFinal = playerValues->at(i); } } table->setState(TABLE_STATE_FINISHED); // If values are same, this is a push. if (dealerFinal == playerFinal) { updateTextarea(hStaticTableMiddleMessage, "Push"); // Return player's bet money. int bet = playerHand->getBetAmount(); user->setBalance(user->getBalance() + bet); } else if (dealerFinal < playerFinal) { // Player wins, return bet and winning. updateTextarea(hStaticTableMiddleMessage, "Player Wins!"); int bet = playerHand->getBetAmount(); user->setBalance(user->getBalance() + (bet*2) ); } else if (dealerFinal > playerFinal) { // No need to update cash. Has already been deducted // at time of bet. Update the middle message area. updateTextarea(hStaticTableMiddleMessage, "Dealer Wins."); } RedrawWindow(gameEngine->getHWnd(), NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW); } return CallWindowProc(oldStandButtonProc, hwnd, msg, wp, lp); }
LRESULT CALLBACK DealButtonProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) { if (msg == WM_LBUTTONDOWN) { TCHAR buff[64]; GetWindowText(hTextboxBetAmount, buff, 20); int bet = _ttoi(buff); /* for (int i = 0; buff[i] != NULL; i++) { if (! std::isdigit(buff[i]) ) { MessageBox( GameEngine::getInstance()->getHWnd(), L"Invalid bet amount.", L"Error", NULL); return 0; } } */ // int bet = atoi(buff); Table* table = GameEngine::getInstance()->getTable(); User* user = GameEngine::getInstance()->getUser(); // Can player bet this much? if (bet > user->getBalance()) { MessageBox( GameEngine::getInstance()->getHWnd(), L"Insufficient funds available to place this bet.", L"Error", NULL); return 0; } if (bet == 0) { MessageBox( GameEngine::getInstance()->getHWnd(), L"Must bet more than $0.", L"Error", NULL); return 0; } Hand* dealerHand = new Hand(); Hand* playerHand = new Hand(); playerHand->setBetAmount(bet); user->setBalance(user->getBalance() - bet); dealerHand->dealCard(true); dealerHand->dealCard(false); playerHand->dealCard(false); playerHand->dealCard(false); table->setDealerHand(dealerHand); table->setPlayerHand(playerHand); GameEngine::getInstance()->setState(GameEngine::STATE_PLAYING); PlaySound(L"sound-chips.wav", NULL, SND_FILENAME | SND_ASYNC); // // Check if we have blackjack, if so player // wins right away. Otherwise move to substate // "playing" // if (playerHand->isBlackjack()) { table->setState(TABLE_STATE_FINISHED); updateTextarea(hStaticTableMiddleMessage, "Blackjack! Player Wins."); // Play YAY sound PlaySound(L"sound-yay.wav", NULL, SND_FILENAME | SND_ASYNC); // Update user balance. user->setBalance(user->getBalance() + (bet*2)); } else { table->setState(TABLE_STATE_PLAYING); } // // Force redraw of window, which should now render the new // card data. // // https://msdn.microsoft.com/en-us/library/dd162911%28VS.85%29.aspx // http://stackoverflow.com/questions/2325894/difference-between-invalidaterect-and-redrawwindow // RedrawWindow(GameEngine::getInstance()->getHWnd(), NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW); return 0; } return CallWindowProc(oldDealButtonProc, hwnd, msg, wp, lp); }