Creating a BlackJack game.....

Write your question here.
In curiosity, how could I go about creating a blackjack game with standard house rules without using objects/classes. As I dont seek a quick answer, just a template I can use in order to figure out how to make this game. Ive started very simply by creating a struct in order to figure out what the cards represent

im stuck on objects_classes. TBH, Im completely stuck on what to do next...

any guidance would be greatly appreciated.

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
 #include <iostream>
#include <ctime>
#include <string>
using namespace std; 

struct DefineCard {
	char cardSuit;     // SPADES, DIAMONDS, HEARTS, CLUBS 
	int faceValue;     // ACE, 2, 3, 4, 5, 6, 7, 8, 9, 10, JACK, QUEEN, KING
	int pointsValue   // FACE => 10 POINTS & ACE's => 1 OR 11 (WHICHEVER IS BEST FOR PLAYER/DEALER
	int cardStatus;   // 0 = InPlay , 1 = InDeck, 2 = DiscardPile
}


int main ()
{
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
return 0; 	
}
Last edited on
@ZeroSploit

Here is a small program to get you started. It creates a deck of cards, gives them their values, with Ace as 11, and sets them all to be in deck. I then shuffle the deck, and re-displays them. Hope this gets you a head start.
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Card Deck.cpp : main project file.


#include <stdio.h>
#include <ctime>
#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::cin;
using std::endl;

struct DefineCard
{
	string suit;
	string face;
	int pointsValue;   // FACE => 10 POINTS & ACE's => 1 OR 11 (WHICHEVER IS BEST FOR PLAYER/DEALER
	int cardStatus;   // 0 = InPlay , 1 = InDeck, 2 = DiscardPile
} Deck[53]; // Deck[52] will be used when shuffling deck


int main()
{
	srand((unsigned)time(0));
	int New_Suit = 0;
	string faces[13] = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
	string suits[4] = { "Diamonds", "Clubs", "Hearts", "Spades" };
	string Status[3] = { "In Play", "In Deck", "Discard Pile" };
	int CardValue[13] = { 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10 };
	for (int x = 0; x < 52; x++)
	{

		Deck[x].face = faces[x % 13];
		Deck[x].suit = suits[New_Suit];
		Deck[x].pointsValue = CardValue[x % 13];
		Deck[x].cardStatus = 1;
		if ((x + 1) % 13 == 0)
			New_Suit++;
	}
	cout << "Straight deck of cards.." << endl << endl;
	for (int x = 0; x < 52; x ++)
	{
		//cout << Deck[x].face << " of " << Deck[x].suit << "   " << Deck[x + 1].face << " of " << Deck[x + 1].suit << "  " << Deck[x + 2].face << " of " << Deck[x + 2].suit << "   " << Deck[x + 3].face << " of " << Deck[x + 3].suit << endl;
		cout << Deck[x].face << " of " << Deck[x].suit << " : Value of " << Deck[x].pointsValue << " : " << Status[Deck[x].cardStatus] << endl;
	}

	for (int x = 0; x < 600; x++)
	{
		int a = rand() % 52;
		int b = rand() % 52;
		
		Deck[52].face = Deck[a].face;
		Deck[52].suit = Deck[a].suit;
		Deck[52].pointsValue = Deck[a].pointsValue;
		Deck[52].cardStatus = Deck[a].cardStatus;

		Deck[a].face = Deck[b].face;
		Deck[a].suit = Deck[b].suit;
		Deck[a].pointsValue = Deck[b].pointsValue;
		Deck[a].cardStatus = Deck[b].cardStatus;

		Deck[b].face = Deck[52].face;
		Deck[b].suit = Deck[52].suit;
		Deck[b].pointsValue = Deck[52].pointsValue;
		Deck[b].cardStatus = Deck[52].cardStatus;
	}

	cout << endl << "Shuffled deck of cards.." << endl << endl;

	for (int x = 0; x < 52; x ++)
	{
		//cout << Deck[x].face << " of " << Deck[x].suit << "   " << Deck[x + 1].face << " of " << Deck[x + 1].suit << "   " << Deck[x + 2].face << " of " << Deck[x + 2].suit << "   " << Deck[x + 3].face << " of " << Deck[x + 3].suit << endl;
		cout << Deck[x].face << " of " << Deck[x].suit << " : Value of " << Deck[x].pointsValue << " : " << Status[Deck[x].cardStatus] << endl;
	}

	cout << endl << endl;
	cin >> New_Suit;
}
I understand the array's, but what I cant wrap my head around are lines: 50, 51, and why you set line 48 to x < 600. What's the purpose of int New_Suit = 0 on line 26. I dont plan on using this code precisely verbatim. I want to learn how to do the project (final) for myself. We never went over objects/classes ( we are allowed to use information we learn on our own, but with 7 days to work on this project, and not a clear cut understanding of even functions in it's entirity. I am trying to stick to what I already know). In the struct command at the top, I was easily able to come up with the code that lies within the brackets, but I could not for the life of me figure out what went after that closing bracket. In this case the Deck[53] array. I now understand your logic behind that. Being as what I defined a card to be. My only logical assumption is that it would of naturally been called Deck, which actually follows one of the criteria on for the final grade.
what I cant wrap my head around are lines: 50, 51


they are just random function
http://www.cplusplus.com/reference/cstdlib/rand/

why you set line 48 to x < 600.


clever shuffling in normal words... 600 is awfully lot and awesome :D

int New_Suit = 0

you don't want garbage value to creep in your code...

We never went over objects/classes

you can easily do this stuff without classes...
Last edited on
Thanks for the help, programmer007, in explaining zeroSploit's questions and concerns.
Guys, I do appreciate the help....I love this community and the resources available and the grandiosity of providing assistance. Im a novice (this hold's true), but Im beginning to become obsessed with programming, and all of it's applications. We wont have time this semester to work on objects or classes, and to be honest I really have no idea how to grasp those idea's outside of the classroom, but im sure with knowing the basics I will be able to grasp those concepts.
When I run the code, the output only returns In Deck. I tried setting deck[x]. cardStatus = 1; to deck[x].cardStatus = 0, 1, 2; I got a crash, but the output did tell me In Play. I was wondering if I could create a for loop that cycle's through each selection of InPlay, InDeck, DiscardPile.
@ZeroSploit

Here you go. I also added in showing 4 hands being dealt to players. Then run through all 52 cards, three times, and change the cardStatus each run through.
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Card Deck.cpp : main project file.


#include <stdio.h>
#include <ctime>
#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::cin;
using std::endl;

struct DefineCard
{
	string suit;
	string face;
	int pointsValue;   // FACE => 10 POINTS & ACE's => 1 OR 11 (WHICHEVER IS BEST FOR PLAYER/DEALER
	int cardStatus;   // 0 = InPlay , 1 = InDeck, 2 = DiscardPile
} Deck[53]; // Deck[52] will be used when shuffling deck


int main()
{
	srand((unsigned)time(0));
	int a, b;
	int New_Suit = 0;
	string faces[13] = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
	string suits[4] = { "Diamonds", "Clubs", "Hearts", "Spades" };
	string Status[3] = { "In Play", "In Deck", "Discard Pile" };
	int CardValue[13] = { 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10 };
	for (int x = 0; x < 52; x++)
	{

		Deck[x].face = faces[x % 13];
		Deck[x].suit = suits[New_Suit];
		Deck[x].pointsValue = CardValue[x % 13];
		Deck[x].cardStatus = rand()%3;
		if ((x + 1) % 13 == 0)
			New_Suit++;
	}
	cout << "Straight deck of cards.." << endl << endl;
	for (int x = 0; x < 52; x ++)
	{
		cout << Deck[x].face << " of " << Deck[x].suit << " : Value of " << Deck[x].pointsValue << " : " << Status[Deck[x].cardStatus] << endl;
	}

	for (int x = 0; x < 600; x++)
	{
		do{
			a = rand() % 52;
			b = rand() % 52;
		} while (a == b);
		
		Deck[52].face = Deck[a].face;
		Deck[52].suit = Deck[a].suit;
		Deck[52].pointsValue = Deck[a].pointsValue;
		Deck[52].cardStatus = Deck[a].cardStatus;

		Deck[a].face = Deck[b].face;
		Deck[a].suit = Deck[b].suit;
		Deck[a].pointsValue = Deck[b].pointsValue;
		Deck[a].cardStatus = Deck[b].cardStatus;

		Deck[b].face = Deck[52].face;
		Deck[b].suit = Deck[52].suit;
		Deck[b].pointsValue = Deck[52].pointsValue;
		Deck[b].cardStatus = Deck[52].cardStatus;
	}

	cout << endl << "Shuffled deck of cards.." << endl << endl;

	for (int x = 0; x < 52; x ++)
	{
		cout << Deck[x].face << " of " << Deck[x].suit << " : Value of " << Deck[x].pointsValue << " : " << Status[Deck[x].cardStatus] << endl;
	}

	cout << endl << endl;
	cout << "Enter a number, then press 'Enter', to deal two cards each, to four players." << endl << endl;// Just to use as a pause, for now
	cin >> New_Suit;
	int dealt = 0, value=0;
	for (int x = 0; x < 4; x++)
	{
		cout << "Player " << x + 1 << "'s hand.." << endl;
		for (int y = 0; y < 2; y++)
		{
			Deck[dealt].cardStatus = 0;
			cout << Deck[dealt].face << " of " << Deck[dealt].suit << " : Value is " << Deck[dealt].pointsValue << " : " << Status[Deck[dealt].cardStatus] << endl;
			value += Deck[dealt].pointsValue;
			dealt++;
		}
		cout << "Value of hand so far, equals " << value;
		if (value > 21)
			cout << endl << "BUSTED. Over 21";
		if (value == 21)
			cout << endl << "21. Automatic WIN!!";
		value = 0;
		cout << endl << endl;
	}
	for (int x = 0; x < 3; x++)
	{
		for (int y = 0; y < 52; y++)
		{
			Deck[y].cardStatus = x;
			cout << Deck[y].face << " of " << Deck[y].suit << " : " << Status[Deck[y].cardStatus] << endl;
		}
		cout << ">>----------------------------------------------------<<" << endl;
	}
}
You are very quick to reply, but I was somewhat hoping for explanation of how to do what you just took the time to write out. Im not one of those kinds of people to get every answer, and part of my learning process is learning the material myself. I have been very curious to know how I could take lines 28 through 31 as a pointer to the deck[52] array. How is it so easy for you to just throw out code like that, in which has taken me hours to learn myself. Ive checked a few other websites looking for answers on how to pass pointers in arrays, how to use functions properly (my understanding of functions is vague to say nonetheless- on my understanding of such functions. I do know that when you have a void function that it returns no kind of data, but what if a function with a void data type has say, arguments within the parenthesis. Do those arguments reverse the action of a void function ? For example, say I create a void function that says, void myNumber (int x, float a) would those arguments of int x and float a be passed down to whatever could be below int main () ? Im also interested in learning enum data types, and how they work in array's or functions. I believe Ive seen enum's being tied to objects/classes, but I really have no idea. I really do appreciate you taking time to go over this with me. Ive been cranking out idea's since 7pm last night, and I have to say my mind is rushing faster than ever. I feel like some of the tutorials do not play to other actions. You know in formulaic mathematics, some formula's remain unchanged and unaltered. It seems like in order for me to get a better grasp on the basic's of c++ that I would have to keep practicing. Like sometimes, even though I know how to create a switch I don't always know how to properly make everything flow together, so i get lost on what code to include. Are there any other websites I could visit that kind of further dumb's down the concept's of c++ programming. Have you got any good practice websites ? Any suggestions on where to go after I get an advanced understanding of C++, such as another computer language ? Do you have any suggestions for books on proper programming technique's because I realize alot of advanced users here are not using (using namespace std; but instead use std::......With me continuing my studies in either Electrical and Computer Engineering or simply Computer Science (If I dont decide to do Chemical Engineering). I want to be ahead of the pack when I start getting into more advanced c++ programming concepts/technique's/etiquette.
@ZeroSploit

Sorry, I don't understand pointers myself, so can't help with the lines 28 to 31, though they could have been put into a function, to shorten up main.
But I can try to explain a bit about lines 81 to 108.
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
int dealt = 0, value=0;// Initialize variables to 0
	for (int x = 0; x < 4; x++)// step through the 4 players
	{
		cout << "Player " << x + 1 << "'s hand.." << endl; // Add 1 to x, so it shows 1 to 4, not 0 to 3
		for (int y = 0; y < 2; y++)// Loop for the 2 cards being dealt
		{
			Deck[dealt].cardStatus = 0;// dealt started as 0, so Deck[0] status is 0
// When we increase dealt, each new card status dealt, will become 0 
			cout << Deck[dealt].face << " of " << Deck[dealt].suit << " : Value is " << Deck[dealt].pointsValue << " : " << Status[Deck[dealt].cardStatus] << endl;
			value += Deck[dealt].pointsValue; // Increase value by cards value
			dealt++;// Increase dealt, so we're using next card in deck
		}
		cout << "Value of hand so far, equals " << value;// Display total value of the 
// 2 cards that were dealt
		if (value > 21)
			cout << endl << "BUSTED. Over 21";// display results if over 21
		if (value == 21)
			cout << endl << "21. Automatic WIN!!";// Or 21 exact
		value = 0; // Set value to 0, so we can add up value of next players cards
		cout << endl << endl;
	}

	for (int x = 0; x < 3; x++) // Loop for the 3 types of card status
	{
		for (int y = 0; y < 52; y++) // Loop for the 52 cards in deck
		{
			Deck[y].cardStatus = x; // Make each card in deck to have value of x
// first time thru, value is 0, then 1, lastly, a 2.

			cout << Deck[y].face << " of " << Deck[y].suit << " : " << Status[Deck[y].cardStatus] << endl;// Print out the cardStatus value of array, Status[]
// If Deck[y].cardStatus equals 0, then it will print out Status[0], or "In Play"
// When x=1, Deck[y].cardStatus equals 1, so then it will print out Status[1], or "In Deck"
		}
		cout << ">>----------------------------------------------------<<" << endl;
	}
}


You can PM me, if you like, and we can go over more of your questions, without going overboard in this thread. Just check my name, and you'll find my email address.
Hello guys,
Am new to this community and i would like to know how to download the codes incase i find some interesting...Am also a beginner
@Styles Kelvin

You really shouldn't hi-jack a thread. Just start a new one, with your question or problem.

Anyway, I'll answer you anyway.
1: Hold down the left mouse button and highlight the code in question
2: After releasing the left button, press the right mouse button, and a menu will appear
3: Click 'copy' from the menu
4: Start up your programming language IDE, and give a newly created program, a name
5: Paste the copied program into your IDE
6: Save the program, then compile
@whitenite

My lecture said we were going to write a game in c++ but i didnt know what to do so after seeing that thought i should gather from it...
any other game you might like to show me is welcome...but only the simpliest ones ones cos this all we learnt ...
#struct
#control structures
#Arrays(just started)
#functions(not into detail)

please can you explain why the deck [53] is between the curly braces and the terminate sign of the struct ..

struct DefineCard
{
string suit;
string face;
int pointsValue; // FACE => 10 POINTS & ACE's => 1 OR 11 (WHICHEVER IS BEST FOR PLAYER/DEALER
int cardStatus; // 0 = InPlay , 1 = InDeck, 2 = DiscardPile
} Deck[53]; // Deck[52] will be used when shuffling deck

The srand and rand used as a function but was not declared ie. line 21,35, and 50


Last edited on
Topic archived. No new replies allowed.