From e800101d60f81334cf5e37d0412a4ad103207175 Mon Sep 17 00:00:00 2001 From: Wilmer Zwietering Date: Wed, 7 Jun 2017 09:58:32 +0200 Subject: [PATCH] Main menu, customizable deck amount, help and info --- Blackjack/Blackjack.cpp | 95 ++++++++++++++++++++++++++++++++--------- Blackjack/Cards.cpp | 27 ++++++------ Blackjack/blackjack.h | 7 ++- 3 files changed, 91 insertions(+), 38 deletions(-) diff --git a/Blackjack/Blackjack.cpp b/Blackjack/Blackjack.cpp index 3df6cd8..905247e 100644 --- a/Blackjack/Blackjack.cpp +++ b/Blackjack/Blackjack.cpp @@ -4,7 +4,6 @@ #include "stdafx.h" #include "blackjack.h" #include -#include #include // Get player input @@ -41,7 +40,7 @@ void dealCard(int &score, const Card *&cardPtr, int &aces, bool player) { } // Game logic -bool playBlackjack(const std::array &deck) { +bool playBlackjack(const Card deck[]) { // Setup const Card *cardPtr = &deck[0]; int dealerScore = 0; @@ -81,7 +80,7 @@ bool playBlackjack(const std::array &deck) { while (true) { if (dealerScore > 21) { if (dealerAces) { - playerScore -= 10; + dealerScore -= 10; --dealerAces; std::cout << "The dealer his ace is now counted as 1. His score is now " << dealerScore << ".\n"; } @@ -98,31 +97,85 @@ bool playBlackjack(const std::array &deck) { } } + delete[] deck; return (playerScore > dealerScore); } int main() { - do { - bool playerWin = playBlackjack(getDeck(true)); - - if (playerWin) { - std::cout << "Congratulations! You win!!!" << '\n'; - } - else { - std::cout << "Sadly, you lost." << '\n'; - } + std::cout << "Welcome to blackjack!\n"; - std::cout << "Do you want to play another game? (y/n) "; - char choice; + do { + std::cout << "Hit (p) to play, (e) to exit or (h) to get help. "; + char command; do { - std::cin >> choice; - } while (choice != 'y' && choice != 'n'); - - if (choice == 'n') { - break; - } + std::cin >> command; + } while (command != 'p' && command != 'h' && command != 'e' && command != 'i'); + + switch (command) + { + case 'p': + { + std::cout << "How many decks do you want to use? (1-6) "; + int deckCount; + std::cin >> deckCount; + if (deckCount > 6) deckCount = 6; + if (deckCount < 1) deckCount = 1; + do { + bool playerWin = playBlackjack(getDeck(deckCount, true)); + + if (playerWin) { + std::cout << "Congratulations! You win!!!" << '\n'; + } + else { + std::cout << "Sadly, you lost." << '\n'; + } + + std::cout << "Do you want to play another game? (y/n) "; + char choice; + do { + std::cin >> choice; + } while (choice != 'y' && choice != 'n'); + + if (choice == 'n') { + break; + } + } while (true); + break; + } + case 'h': + std::cout << "In blackjack your goal is to obtain a score as high as possible. " + "You gain points by drawing a card. The rank of the card is added to your total score. " + "The ace is valued at 11 points, unless your score is higher than 21, " + "than it is valued at 1 point. When you have more than 21 points, you lose. " + "The highest score wins.\nControls:\nIn game:\n(h) -- hit, get another card.\n(s) -- stand, end turn." + "\nIn the menu:\n(p) -- play the game.\n(h) -- help.\n(i) -- info and license.\n(e) -- exit.\n"; + break; + case 'i': + std::cout << "The project was inspired by http://www.learncpp.com/. " + "The source code can be found at https://github.com/wzwietering/blackjack.\n\n" + "(c) Wilmer Zwietering 2017\n\nPermission is hereby granted, free of charge, to any person obtaining a copy " + "of this software and associated documentation files(the \"Software\"), to deal " + "in the Software without restriction, including without limitation the rights " + "to use, copy, modify, merge, publish, distribute, sublicense, and/or sell " + "copies of the Software, and to permit persons to whom the Software is " + "furnished to do so, subject to the following conditions : \n\n" + + "The above copyright notice and this permission notice shall be included in all " + "copies or substantial portions of the Software. \n\n" + + "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR " + "IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, " + "FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE " + "AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER " + "LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, " + "OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE " + "SOFTWARE.\n"; + break; + case 'e': + return 0; + } } while (true); - return 0; + return 0; } diff --git a/Blackjack/Cards.cpp b/Blackjack/Cards.cpp index 3c98843..64444dd 100644 --- a/Blackjack/Cards.cpp +++ b/Blackjack/Cards.cpp @@ -1,7 +1,6 @@ #include "stdafx.h" #include #include "blackjack.h" -#include #include // Prints the value of a card. @@ -57,30 +56,32 @@ int getRandom(int min, int max) } // Shuffle a deck of cards random -void shuffleDeck(std::array &deck) { - for (Card &card : deck) { - swapCard(card, deck[getRandom(0, 51)]); +void shuffleDeck(Card deck[], int size) { + for (int i = 0; i < size; ++i) { + swapCard(deck[i], deck[getRandom(0, size - 1)]); } } // Returns an ordered deck. The bool is used to return a shuffled deck. -std::array getDeck(bool shuffled) { - std::array deck; - for (int suit = 0; suit < static_cast(Suit::MAX_SUITS); ++suit) { - for (int rank = 0; rank < static_cast(Rank::MAX_RANKS); ++rank) { - deck[suit * static_cast(Rank::MAX_RANKS) + rank] = { static_cast(rank), static_cast(suit) }; +Card* getDeck(int amount, bool shuffled) { + Card *deck = new Card[amount * 52]; + for (int i = 0; i < amount; ++i) { + for (int suit = 0; suit < static_cast(Suit::MAX_SUITS); ++suit) { + for (int rank = 0; rank < static_cast(Rank::MAX_RANKS); ++rank) { + deck[i * 52 + suit * static_cast(Rank::MAX_RANKS) + rank] = { static_cast(rank), static_cast(suit) }; + } } } if (shuffled) { - shuffleDeck(deck); + shuffleDeck(deck, amount * 52); } return deck; } // Print the entire deck -void printDeck(const std::array &deck) { - for (const Card &card : deck) { - printCard(card); +void printDeck(const Card deck[], int size) { + for (int i = 0; i < size; ++i) { + printCard(deck[i]); } std::cout << '\n'; } diff --git a/Blackjack/blackjack.h b/Blackjack/blackjack.h index 2984780..4d1d13c 100644 --- a/Blackjack/blackjack.h +++ b/Blackjack/blackjack.h @@ -1,5 +1,4 @@ #pragma once -#include #ifndef CARDS #define CARDS @@ -36,9 +35,9 @@ struct Card { void printCard(const Card card, bool newline = false); void swapCard(Card &first, Card &second); -void shuffleDeck(std::array &deck); -std::array getDeck(bool shuffled = false); -void printDeck(const std::array &deck); +void shuffleDeck(Card deck[], int size); +Card* getDeck(int amount = 1, bool shuffled = false); +void printDeck(const Card deck[], int size); int getCardValue(const Card &card, const bool &aceEleven = true); #endif // !CARDS