Skip to content

Commit

Permalink
Main menu, customizable deck amount, help and info
Browse files Browse the repository at this point in the history
  • Loading branch information
wzwietering committed Jun 7, 2017
1 parent b734e01 commit e800101
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 38 deletions.
95 changes: 74 additions & 21 deletions Blackjack/Blackjack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "stdafx.h"
#include "blackjack.h"
#include <iostream>
#include <array>
#include <string>

// Get player input
Expand Down Expand Up @@ -41,7 +40,7 @@ void dealCard(int &score, const Card *&cardPtr, int &aces, bool player) {
}

// Game logic
bool playBlackjack(const std::array<Card, 52> &deck) {
bool playBlackjack(const Card deck[]) {
// Setup
const Card *cardPtr = &deck[0];
int dealerScore = 0;
Expand Down Expand Up @@ -81,7 +80,7 @@ bool playBlackjack(const std::array<Card, 52> &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";
}
Expand All @@ -98,31 +97,85 @@ bool playBlackjack(const std::array<Card, 52> &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;
}

27 changes: 14 additions & 13 deletions Blackjack/Cards.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "stdafx.h"
#include <iostream>
#include "blackjack.h"
#include <array>
#include <random>

// Prints the value of a card.
Expand Down Expand Up @@ -57,30 +56,32 @@ int getRandom(int min, int max)
}

// Shuffle a deck of cards random
void shuffleDeck(std::array<Card, 52> &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<Card, 52> getDeck(bool shuffled) {
std::array<Card, 52> deck;
for (int suit = 0; suit < static_cast<int>(Suit::MAX_SUITS); ++suit) {
for (int rank = 0; rank < static_cast<int>(Rank::MAX_RANKS); ++rank) {
deck[suit * static_cast<int>(Rank::MAX_RANKS) + rank] = { static_cast<Rank>(rank), static_cast<Suit>(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<int>(Suit::MAX_SUITS); ++suit) {
for (int rank = 0; rank < static_cast<int>(Rank::MAX_RANKS); ++rank) {
deck[i * 52 + suit * static_cast<int>(Rank::MAX_RANKS) + rank] = { static_cast<Rank>(rank), static_cast<Suit>(suit) };
}
}
}
if (shuffled) {
shuffleDeck(deck);
shuffleDeck(deck, amount * 52);
}
return deck;
}

// Print the entire deck
void printDeck(const std::array<Card, 52> &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';
}
Expand Down
7 changes: 3 additions & 4 deletions Blackjack/blackjack.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma once
#include <array>

#ifndef CARDS
#define CARDS
Expand Down Expand Up @@ -36,9 +35,9 @@ struct Card {

void printCard(const Card card, bool newline = false);
void swapCard(Card &first, Card &second);
void shuffleDeck(std::array<Card, 52> &deck);
std::array<Card, 52> getDeck(bool shuffled = false);
void printDeck(const std::array<Card, 52> &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

0 comments on commit e800101

Please sign in to comment.