How to Make a Card Game File in C++

by Mike Wallace

The C++ programming language is a compiled language. That means that a text file full of C++ code is converted into a single, executable file during a process known as "compilation." If you are new to C++, you may be interested in a fun project that introduces you to the compilation process and produces a working game. For example, you can create a simple game that draws a card randomly from a deck of cards. This is a project that you can complete in a short amount of time with little to no programming experience.

1

Load the C++ IDE by clicking on its program icon. When it opens, select "File/New/Project" and choose "C++ Project" to create a new C++ project. A blank source code file appears in the text editor portion of the IDE.

2

Write the following four statements at the top of the source code file to import the necessary C++ libraries:

include <cstdio>

include <iostream>

include <cstdlib>

using namespace std;

3

Create two arrays of strings. An array is a container that holds a sequence of objects, and they are perfect for holding the suit and value of a playing card. For a standard deck, minus the Joker cards, you can write the following two string arrays:

string suit[] = {"Diamonds", "Hearts", "Spades", "Clubs"};

string faceValue[] = {"Two", "Three", "Four", "Five",

"Six", "Seven", "Eight", "Nine", "Ace", "King",

"Queen", "Jack"};

4

Write a function that randomly determines the suit and value of a card. Call this function "getCard" and have it output a string. Write the following to create a skeleton of this function:

string getCard()

{}

5

Fill in the function by writing the code necessary to draw a random card. All of the code in Steps 6 through 11 must be placed in-between the curly brackets of the "getCard" function.

6

Declare a string of text that represents a card, which can be done by simply writing the following:

string card;

7

Create two integer variables that create random values. The first variable creates values between 0 and 11, and is perfect for randomly determining the face value of a card. The second variable creates values between 0 and 3, and is suited for determining the suit of a card.

int cardValue = rand() % 12;

int cardSuite = rand() % 4;

8

Add the face value to the string "card" like this:

card += faceValue[cardValue];

9

Place a divider in-between the card value and suit like this:

card += " of ";

10

Add the suit of the card to the string like this:

card += suit[cardSuite];

11

Output the string and end the function by writing the following:

return card;

12

Create a main function. This is the entry point to your program, and it is where it begins execution. The code from the remaining steps must go in-between the curly brackets of the main function, shown below:

int main()

{}

13

Write the following statement to ask the player how many cards he would like to draw:

cout << "How many card would you like to draw?" << endl;

14

Declare an integer variable that stores the number of cards the player wants to draw, but set it to zero for now:

int numberOfCards = 0;

15

Write the following statement to grab the value input from the keyboard and set it equal to the variable "numberOfCards," like this:

cin &gt;&gt; numberOfCards;

16

Create a for loop that repeats the same code as many times as the user has chosen with the "numberOfCards" value. Then call, or invoke, the function "getCard" to generate a random card. You can output some text too, like this:

for(int i = 0; i &lt; numberOfCards; i++)

{ cout &lt;&lt; "You drew a: " &lt;&lt; getCard() &lt;&lt; endl; }

17

Compile the program by pressing the green "Play" button. This creates a single executable file out of all of this source code. This file will automatically be run for you, so you can play your own game. If you type in four, you might get output that looks like this:

You drew a: Seven of Clubs

You drew a: Queen of Diamonds

You drew a: Seven of Diamonds

You drew a: Eight of Spades

Tip

  • check This program draws from an infinite deck of cards, so it is possible to get two or more Jacks of Spades, for example. See if you can change the program so that it only allows one deck of cards.

Items you will need

References

About the Author

Mike Wallace began writing professionally in 2009. He is currently employed as a software engineer who designs, develops and tests software systems. He holds a Bachelor of Science in computer engineering and a Master of Science in electrical and computer engineering from California State University, Chico.

Photo Credits

  • photo_camera Comstock/Comstock/Getty Images