Creating a text based card game in C++ (part 1)

in #programming10 years ago


Image source

Hey guys! I just wanted to do a simple walk-through of creating a card game in C++. Text based card games are some of the easiest games to program. They are great for beginner programmers! For this post, I will create the deck and shuffle it. This will be a great setup for implementing many card games that use a 52 card deck. I will code different games later in this series.

Planning out the code

To go about this, I will make 3 files. A header file, a file to hold my functions, and a file for main().

  1. header (cards.h)
    • Create a class called Card
      • This will hold the attributes of one card (face value and suit)
  2. functions (card_functions.cpp)
    • Create several functions
      • Create the deck --> set_deck()
      • Shuffle the deck --> shuffle_deck()
  3. Main (cards_main.cpp)
    • Create an array of 52 Card objects
    • Call set_deck()
    • Call shuffle_deck()
    • print the deck before and after it is shuffled (the program doesn't have any other use at the moment)

Writing the code

Here is what I wrote...

cards.h

#ifndef CARDS_H
#define CARDS_H

class Card
{
    public:
        string face;
        string suit;
};

#endif
  • Every Card object has two string members
    • face --> holds the face value
    • suit --> holds what suit the card is

cards_functions.cpp

#ifndef CARDS_FUNCTIONS_CPP
#define CARDS_FUNCTIONS_CPP

void set_deck(Card* deck)
{
    for(int x = 0; x < 13; x++)// all hearts
    {
        char number = x + 49;
        deck[x].suit = "hearts";
        if(x == 0)
            deck[x].face = "Ace";
        else if(x == 9)
            deck[x].face = "10";
        else if(x == 10)
            deck[x].face = "Jack";
        else if(x == 11)
            deck[x].face = "Queen";
        else if(x == 12)
            deck[x].face = "King";
        else
            deck[x].face = number;
    }
    for(int x = 0; x < 13; x++)// all diamonds
    {
        char number = x + 49;
        deck[x+13].suit = "diamonds";
        if(x == 0)
            deck[x+13].face = "Ace";
        else if(x == 9)
            deck[x+13].face = "10";
        else if(x == 10)
            deck[x+13].face = "Jack";
        else if(x == 11)
            deck[x+13].face = "Queen";
        else if(x == 12)
            deck[x+13].face = "King";
        else
            deck[x+13].face = number;
    }
    for(int x = 0; x < 13; x++)// all clubs
    {
        char number = x + 49;
        deck[x+26].suit = "clubs";
        if(x == 0)
            deck[x+26].face = "Ace";
        else if(x == 9)
            deck[x+26].face = "10";
        else if(x == 10)
            deck[x+26].face = "Jack";
        else if(x == 11)
            deck[x+26].face = "Queen";
        else if(x == 12)
            deck[x+26].face = "King";
        else
            deck[x+26].face = number;
    }
    for(int x = 0; x < 13; x++)// all spades
    {
        char number = x + 49;
        deck[x+39].suit = "spades";
        if(x == 0)
            deck[x+39].face = "Ace";
        else if(x == 9)
            deck[x+39].face = "10";
        else if(x == 10)
            deck[x+39].face = "Jack";
        else if(x == 11)
            deck[x+39].face = "Queen";
        else if(x == 12)
            deck[x+39].face = "King";
        else
            deck[x+39].face = number;
    }
}

void shuffle_deck(Card* deck, int size)
{
    for(int x = 0; x < size; x++)
    {
        int swap = rand() % size;
        Card temp = deck[x];
        deck[x] = deck[swap];
        deck[swap] = temp;
    }
}

#endif
  • set_deck() --> function that sets all values of a given Card array
    • deck --> the Card array
    • Every for loop sets the values of each card for one suit
    • number --> a character to hold the character values of x (only works for 1 - 9)
      • When 49 is added an integer from 0 - 9, this is the ASCII value for that number
      • in other words, this changes the integer value of x into its character equivalent
  • shuffle_deck() --> function that shuffles a Card array given to it
    • deck --> the Card array
    • size --> the size of the Card array (shuffle_deck() will shuffle decks of a different size than 52 also)
    • swap --> a random number between 0 and size-1
      • this will be a random address for the each Card to swap with in the array
    • temp --> a temporary Card object to hold the value of deck[x]
    • swap the values of the two addresses

cards_main.cpp

#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>

using namespace std;

#include "cards.h"
#include "cards_functions.cpp"


int main()
{
    Card deck[52];
    set_deck(deck);

// prints the deck
    for(int x = 0; x < 52; x++)
        cout << deck[x].face << " of " << deck[x].suit << endl;

    for(int x = 0; x < 30; x++)
        cout << "--";
    cout << endl;

    srand (time(NULL));
    shuffle_deck(deck, 52);

// prints the deck
    for(int x = 0; x < 52; x++)
        cout << deck[x].face << " of " << deck[x].suit << endl;

    return 0;
}
  • place all of the includes and "using namespace std;" before the other files are included
  • include cards.h and cards_functions.cpp
  • main()
    • deck --> an array of 52 Card objects
    • call set_deck() and pass it deck
    • print the cards in order
    • call shuffle_deck() and hand in deck (and the size of 52)
    • print the shuffled deck

The output:

[cmw4026@omega cards]$ g++ cards_main.cpp
[cmw4026@omega cards]$ ./a.out
Ace of hearts
2 of hearts
3 of hearts
4 of hearts
5 of hearts
6 of hearts
7 of hearts
8 of hearts
9 of hearts
10 of hearts
Jack of hearts
Queen of hearts
King of hearts
Ace of diamonds
2 of diamonds
3 of diamonds
4 of diamonds
5 of diamonds
6 of diamonds
7 of diamonds
8 of diamonds
9 of diamonds
10 of diamonds
Jack of diamonds
Queen of diamonds
King of diamonds
Ace of clubs
2 of clubs
3 of clubs
4 of clubs
5 of clubs
6 of clubs
7 of clubs
8 of clubs
9 of clubs
10 of clubs
Jack of clubs
Queen of clubs
King of clubs
Ace of spades
2 of spades
3 of spades
4 of spades
5 of spades
6 of spades
7 of spades
8 of spades
9 of spades
10 of spades
Jack of spades
Queen of spades
King of spades
------------------------------------------------------------
7 of diamonds
2 of clubs
7 of clubs
7 of spades
Jack of clubs
8 of hearts
6 of clubs
King of clubs
10 of hearts
7 of hearts
4 of clubs
King of diamonds
3 of spades
10 of diamonds
Jack of spades
5 of diamonds
10 of spades
6 of diamonds
2 of spades
Jack of hearts
8 of diamonds
4 of diamonds
8 of clubs
6 of spades
Queen of spades
Ace of diamonds
5 of hearts
Queen of clubs
4 of spades
Ace of clubs
3 of clubs
2 of hearts
Queen of diamonds
5 of spades
9 of hearts
9 of diamonds
Ace of spades
King of hearts
3 of hearts
8 of spades
6 of hearts
10 of clubs
4 of hearts
Queen of hearts
Ace of hearts
5 of clubs
2 of diamonds
9 of spades
King of spades
3 of diamonds
Jack of diamonds
9 of clubs
[cmw4026@omega cards]$
  • As you can see, the cards were shuffled nicely and very randomly.

That is it for this part. I will be implementing this code for actual card games in the future. Thanks for reading!


Coin Marketplace

STEEM 0.04
TRX 0.33
JST 0.097
BTC 65045.78
ETH 1919.76
USDT 1.00
SBD 0.39