Blackjack game

I need to make a blackjack game which matches the real life version. However I feel really overwhelmed and lost. So far I've started the menu, which isn't working as I don't know what condition to put in the while. But even when that is working I don't know where to go from here. I know I need to create the cards and set the deck up which I was going to do using enums but I just feel lost. I want to keep it as basic as I can.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using namespace std;


void Introduction();


int main() {

	void Introduction()
	
	{
		while ((choice != ))
		{//begin WHILE
			cout << "\n Welcome To Blackjack" << endl;
			cout << "\nMenu\n";
			cout << "New Game - Press N \n";
			cout << "About - Press A \n";
			cout << "Help  - Press H \n";
			cout << "Exit - Press X  \n";
			cin >> choice;

			if ((choice == 'N') || (choice == 'n'))
			{
				GameMenu();

			}
			else if ((choice == 'H') || (choice == 'h'))
			{
				cout << " Help: \n";
			}
			else if ((choice == 'A') || (choice == 'a'))
			{
				cout << " About: \n";
			}
			
			else if ((choice == 'X') || (choice == 'x'))
			{
				cout << " You chose to exit the game \n";

			}
			else
			{
				cout << "\n Oops you pressed an invalid choice\n";
			}
		}
Last edited on
Create an object to represent a single card.
Create an instance of that object. Make it represent a single card.
Create 52 of those objects. Make each represent a different card.
Also, you appear to be trying to define a function, Introduction(), inside another function definition, main(). You cannot do this. You cannot define functions inside other functions.
A few starting pointers:

Line 2: You need to include <cstdio> in order to use cout.

Line 9: You can't nest functions.

Line 12: A few problems. 1) choice is not declared,. 2) You have noting to compare choice to. 3) You might want to use a do/while loop rather than a while loop since you don't get the value of choice until line 20.

Lines 14-20: Aren't these lines really your menu and belong in GameMenu()?

Line 30: You don't have a way of exiting the game here.

Are you writing this in C or C++. If you're writing in C++, you might start to think about the objects you will need in your game. A Deck which contains cards, two or more Players, etc.

Ok, I think you need to approach the functionality first, before any titles, menus or fancy features.

First step would be to create a function that creates an array of a set of cards, by combining the suits with the values creating all possibilities. Each possibility is then added to an array (vector)

Something like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <vector>
#include <string>

......

vector<vector<string>> createDeckOfCards() 
{
	vector<string> suits = { "C","D","H","S" }; //Suits in deck: Clubs, Diamonds, Hearts, Spades
	vector<string> values = { "A","2","3","4","5","6","7","8","9","10","J","Q","K" }; //Values for cards in deck
	vector<vector<string>> deckOfCards; // Array to hold deck

	for (int suit = 0; suit < suits.size(); ++suit) //go through all the suits
	{
		for (int value = 0; value < values.size(); ++value) //go through all the values
		{
			deckOfCards.push_back({ values[value],suits[suit] }); //combine suit and values then add to deck of cards array
		}
	}

	return deckOfCards; //return array once finished
}


You're next step would be to produce a function that gets your newly created deck of cards as an input, then shuffles them, returning the now shuffled deck.

Something like this.

1
2
3
4
5
6
7
8
9
10
#include <algorithm>

......


vector<vector<string>> shuffleCards(vector<vector<string>> deckOfCards) 
{
	random_shuffle(begin(deckOfCards), end(deckOfCards)); //shuffle deck of cards
	return deckOfCards; //return deck
}


After these 2 functions, you now have a deck of cards that are shuffled, which you can now assign the player 2 chosen cards.

Since they are shuffled, you can assign them the first item in the deck of cards array, then remove it so it cannot be chosen twice.

Something like this.

1
2
cardsInHand.push_back(deckOfCards[0]); //add first card to players cards in hand
deckOfCards.erase(deckOfCards.begin()); //remove chosen item (first card) from deck of cards 


The rest of the program now relies on if functions, inputs and loops to allow the player to continue until the end of the game, at which they can then continue with a new reshuffled deck of cards or exit the game.

Hope that helps. :)

Can I not use an enum for cards as I find that easier to understand?
Absolutely you can.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
enum Suit  
{   CLUBS,
    SPADES,
    HEARTS,
    DIAMONDS
};

enum Face 
{   Two,
    Three,
    Four, 
    Five, 
    Siz,
    Seven,
    Eight,
    Nine,
    Ten,
    Jack,
    Queen, 
    King, 
    Ace
};

I humbly suggest adding a max to ALL enums you make. It helps in any number of places later on.

enum Face
{ Two,
Three,
Four,
Five,
Siz,
Seven,
Eight,
Nine,
Ten,
Jack,
Queen,
King,
Ace,
Face_max
};

couple of overly simple whys:
...
vector<int> suit(Face_max);
for(int i = 0; i<Face_max; i++)
...

remember that blackjack specifically is an annoying game that tends to have multiple decks mixed together, so its possible to draw 8 copies of the 2 of spades in a row for example. On the flipside, infinite shoe approach isnt a bad game: just randomly generate each and every card on the fly, don't need any decks/shoes/whatevers that way.
if input == hit
total += roll_a_card()
as long as your random algorithm is good, it should not do anything too crazy.

Last edited on
Registered users can post here. Sign in or register to post.