Advertisement
Guest User

main.cpp

a guest
Oct 7th, 2019
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <array>
  3.  
  4. #include "card.h"
  5.  
  6. Result playABlackjack(std::array<Card, 52> &deck);
  7. void countPlayerScore(int *playerScore, Card &card);
  8. void printScore(int playerScore, int dealerScore);
  9.  
  10. int main()
  11. {
  12.     using namespace std;
  13.  
  14.     srand(static_cast<unsigned int>(time(nullptr)));
  15.  
  16.     array<Card, 52> deck;
  17.  
  18.     for (int i = 0; i < static_cast<int>(Suit::MAX_SUIT); ++i) {
  19.         for (int j = 0; j < 13; ++j) {
  20.             Card card { static_cast<Dignity>(j), static_cast<Suit>(i) };
  21.             deck[static_cast<unsigned long long>(i * 13 + j)] = card;
  22.         }
  23.     }
  24.  
  25.     while (true) {
  26.         shuffleDeck(deck);
  27.  
  28.         Result winner = playABlackjack(deck);
  29.  
  30.         if (winner == Result::PLAYER_WINS) {
  31.             cout << "Player wins!" << endl;
  32.         }
  33.         else if (winner == Result::PLAYER_LOSE) {
  34.             cout << "Player lose" << endl;
  35.         }
  36.         else {
  37.             cout << "No one wins" << endl;
  38.         }
  39.  
  40.         bool playAgain { false };
  41.  
  42.         while (true) {
  43.             cout << "Do you wanna play again? (y/n): ";
  44.             char yesNo;
  45.             cin >> yesNo;
  46.  
  47.             if (cin.fail()) {
  48.                 cin.clear();
  49.                 cin.ignore(32676, '\n');
  50.  
  51.                 continue;
  52.             }
  53.  
  54.             if (yesNo == 'y' || yesNo == 'n') {
  55.                 playAgain = yesNo == 'y';
  56.  
  57.                 break;
  58.             }
  59.         }
  60.  
  61.         cout << endl;
  62.  
  63.         if (playAgain) {
  64.             continue;
  65.         }
  66.         else {
  67.             break;
  68.         }
  69.     }
  70.  
  71.     return 0;
  72. }
  73.  
  74. Result playABlackjack(std::array<Card, 52> &deck)
  75. {
  76.     using namespace std;
  77.  
  78.     Card *cardPtr = &deck[0];
  79.     int dealerScore { 0 };
  80.     int playerScore { 0 };
  81.  
  82.     cout << "Let's play a Blackjack.\n";
  83.  
  84.     dealerScore += getCardValue(*cardPtr);
  85.  
  86.     playerScore += getCardValue(*(++cardPtr));
  87.  
  88.     countPlayerScore(&playerScore, *(++cardPtr));
  89.     printScore(playerScore, dealerScore);
  90.  
  91.     while (true) {
  92.         cout << "Enter 'hit' or 'stand': ";
  93.         string hitOrStand;
  94.         cin >> hitOrStand;
  95.  
  96.         if (cin.fail()) {
  97.             cin.clear();
  98.             cin.ignore(32676, '\n');
  99.  
  100.             continue;
  101.         }
  102.  
  103.         if (hitOrStand == "hit") {
  104.             countPlayerScore(&playerScore, *(++cardPtr));
  105.  
  106.             cout << "Player score: " << playerScore << endl;
  107.  
  108.             if (playerScore > 21) {
  109.                 return Result::PLAYER_LOSE;
  110.             }
  111.         }
  112.  
  113.         if (hitOrStand == "stand") {
  114.             break;
  115.         }
  116.     }
  117.  
  118.     if (playerScore > 21) {
  119.         printScore(playerScore, dealerScore);
  120.  
  121.         return Result::PLAYER_LOSE;
  122.     }
  123.  
  124.     while (true) {
  125.         if (dealerScore >= 17) {
  126.             break;
  127.         }
  128.  
  129.         dealerScore += getCardValue(*(++cardPtr));
  130.     }
  131.  
  132.     if (dealerScore > 21) {
  133.         printScore(playerScore, dealerScore);
  134.  
  135.         return Result::PLAYER_WINS;
  136.     }
  137.     else if (playerScore > dealerScore) {
  138.         printScore(playerScore, dealerScore);
  139.  
  140.         return Result::PLAYER_WINS;
  141.     }
  142.     else if (playerScore < dealerScore) {
  143.         printScore(playerScore, dealerScore);
  144.  
  145.         return Result::PLAYER_LOSE;
  146.     }
  147.     else {
  148.         printScore(playerScore, dealerScore);
  149.  
  150.         return Result::NO_ONE_WINS;
  151.     }
  152. }
  153.  
  154. void countPlayerScore(int *playerScore, Card &card)
  155. {
  156.     int cardValue = getCardValue(card);
  157.  
  158.     if (cardValue == 11 && *playerScore >= 11) {
  159.         *playerScore += 1;
  160.     }
  161.     else {
  162.         *playerScore += cardValue;
  163.     }
  164. }
  165.  
  166. void printScore(int playerScore, int dealerScore)
  167. {
  168.     std::cout << "\nDealer score: " << dealerScore << "\nPlayer score: " << playerScore << "\n\n";
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement