Card Games in C++ with Source Code

C++ operates close to the hardware, manipulates resources easily, eliminates CPU-intensive functions, and is fast. Moreover, it is able to handle the complexity of 3D games and offers Multilayer networking.

These characteristics make C++ an excellent choice for developing gaming systems and game development suites.

Have you ever wondered how game development works in C++? Do you have a coding project or class assessment that involves writing an arcade game in C++?

If the answer to any of these is ‘Yes’, you are precisely at the right place to find what you want. 

I hope you are successfully succeeding in finding out new ways to improve your programming skills in gaming, but before moving to this article, have a look at the gaming engines available for C++ programmers.

In this article, you will learn about different types and modes of card games.

I will explain Cards Games in C++ with Source Code in such easy ways that creating your own card game will be a piece of cake for you after that.

Card Games in C with Source Code - Card Games in C++ with Source Code

Introduction to Card Games

Card games are one of the most ancient games. A card game was first played in the 17th century, so you can say that this game is as old as gold.

A basic card game always consists of a deck of 52 cards. Each card consists of a face and a back. All the cards are of identical shapes and sizes. The front is distinguishable by spades, ace, diamond, and heart shapes on it. But the back of all the cards is the same; hence they are indistinguishable. 

The deck of 52 cards contains 4 suites of 13 cards. The cards are only recognizable from one side, so each participant only knows about the card he is holding.

List of Card Games in C++ with their source codes

Several card games are developed in C++. So now, coming over our primary concern, you will learn about some main Card Games in C++ with Source Codes.

Trick taking game; Hearts

Trick taking card game with source code in C++

Hearts is one of the trick-taking games in which each participant aims to avoid winning the round or tricks that consist of hearts. Queen of the spades is also the focal point to be avoided. The heart game consists of 4 players. 

Three of the players that are player one, player two, and player three are automated by the computer, and the fourth player is the human or the user. The declaration of the game is made after the player with the least points is recognized. Therefore, the player scoring the lowest points is the winner of the game, and the game is over. 

Points allocation

The point allocation is as follows:

  • 5 points are for cards with a face value of less than 10.
  • 10 points are for the rest of the heart cards.
  • 100 points are for the queen of spades and club of the jack.

Technique to play

  • Each player has to deal with thirteen cards when the game begins.
  • The person holding two of the clubs leads to the first trick.
  • The leading suite is the suite in which the first card is played on each hand.
  • The rest of the 3 participants should play each of the cards of the same suite if he has one.
  • The player has to play a card of another suite if he does not have one from the same suite.
  • Players with the leading cards having the highest value will collect the points.
  • The program starts by representing the user’s card when the game begins.
  • In each round, the cards of each player are shown.

Example on Github: Source Code 1 / Source Code 2 / Source Code 3

Solitaire

solitaire card game in CPP(spider, freecell)

Solitaire is one of the games in C++ which have more than 1050 of its versions. It is one of the single-player games that begins with a tableau that is a special card layout.

The user’s objective is to construct the final layout of the cards. From classic games to original games, you can search anywhere on the internet for solitaire games. 

This game consists of a deck of 52 cards like every other card game. Each version of the game includes these cards in a different order, so the user can shift to another if he gets bored with one solitaire game. You can also set the mode of each version of the game according to your will. 

MODES

  • Easy

In the easy version, the user can accomplish the task within the least time as the game’s complexity decreases. This version of the game is mainly for beginners to start with so they can gradually improve their skills and move to higher levels.

  • Moderate

The moderate level is somewhat tricky as there are some skills needed to accomplish the game within the least amount of time so you can make records.

  • Complex

The complex mode of this game requires some excellent skills so you can understand and predict your next move in a way that your cards are arranged in a sequence, and you can win the game in little time.

Moreover, you need to have a brilliant understanding of the game to reach this mode.

/* Source: http://www.cplusplus.com/forum/general/87952/ */

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctime>
#include <windows.h>

using namespace std;

struct Card {
	char *face;
	char *pix;
	char *suit;
	};

typedef struct Card card;

void fillDeck(card *, char *[], char *[], char *[]);
void shuffle(card *, int);
void deal(card *);
void WaitKey();

int main()
{
	int shuffled = 1250;
	card deck[52];
	char *face[] = {"Ace","Duece","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};
	char *pix[] = {"\x03","\x05","\x04","\x06"}; // - Added to original program
	char *suit[] = {"Hearts","Clubs","Diamonds","Spades"};
	time_t t;
    srand((unsigned) time(&t)); // Randomize using time
	cout << "\n\t\t*** A deck of cards after " << shuffled << " shuffles ***\n\n";
	fillDeck(deck, face, suit, pix);
	shuffle(deck, shuffled);
	deal(deck);
	printf("\n\n\t");
	WaitKey();
	return 0;
}

void fillDeck(card *wDeck, char *wFace[], char *wSuit[], char *wPix[])
{
	int i;

	for (i = 0; i < 52; i++)
	{
		wDeck[i].face = wFace[i % 13];
		wDeck[i].pix  = wPix[i / 13]; // Added
		wDeck[i].suit = wSuit[i / 13];
	}
}

void shuffle(card *wDeck, int shuffled)
{
	int i, j, x;
	Card temp;
	for (x = 0; x < shuffled ; x++) // A big shuffle
	{
		for (i = 0; i < 52; i++)
		{
			j=rand() % 52;
			temp = wDeck[i];
			wDeck[i] = wDeck[j];
			wDeck[j] = temp;
		}
	}
}
 
void deal(card *wDeck)
{
	int i;
		for (i = 0; i < 51; i++)
		{
			if ( wDeck[i].pix == "\x03"||wDeck[i].pix== "\x04" )
				SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), 4);
			else
				SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), 8);
			printf("    %5s of %-8s (%s) %c", wDeck[i].face, wDeck[i].suit, wDeck[i].pix, (i + 1) % 3 ? '\b' : '\n');
		}
		if ( wDeck[51].pix == "\x03"||wDeck[51].pix== "\x04" )
				SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), 4);
			else
				SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), 8);
	printf("\t\t\t     %5s of %-8s (%s) \n", wDeck[51].face, wDeck[51].suit, wDeck[51].pix);
}

void WaitKey()
{
SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), 8);
cout << "\t\t   Press ENTER to continue...\n\t\t";
while (_kbhit()) _getch(); // Empty the input buffer
_getch(); // Wait for a key
while (_kbhit()) _getch(); // Empty the input buffer (some keys sends two messages)
}

Versions of classic Solitaire

  • FreeCell

It is one of the most common solitaire games. Only a few of the deals in the games are unsolvable. All the cards are dealt face-up from the start of the game. The cards in each game are shuffled randomly by the computer. 

There are four open cells and four open foundations. All cards should be moved to the foundation cells in a sequence to win the game. The player can move the complete or partial tableaus to arrange the cards.

Example on Github: Source Code 1 / Source Code 2 / Source Code 3

  • Spider

The Spider solitaire game was developed in 1949, and the idea for the game’s name was taken from a spider. The 8 legs of a spider refer to the 8 foundation piles to be filled in an order and sequence to win the game.

The cards are arranged from a bigger value card that is king to the smallest card that is one. 

Example on Github: Source Code 1 / Source Code 2 / Source Code 3

  1. Fishing Card games

In fishing card games, the user has to match the card from their hand to the one present on the table in each turn. If he cannot match any card, it stays on the table. One of the fishing card games in C++ is as follows.

Example on Github: Source Code 1 / Source Code 2 / Source Code 3

Go Fish Game

Go Fish Game in CPP wit source code

Go fish game in C++ uses a standard deck of 52 cards that are randomly shuffled. Every card on the deck is labeled with a suit and a rank.

Every player has to deal with the hand of seven cards, and only the player dealing with the cards can see the cards. The remaining cards are stocked aside, and they are faced down. 

Player 1 starts the game as he asks player 2 for his cards. The player should have had a single card of the same rank as player B. The game continues by alternating turns. The players with the most matches cards will win the game.

Example on Github: Source Code 1 / Source Code 2 / Source Code 3Some help with C++ source code

So that’s all for now. I hope this article will enhance your knowledge about card games in C++, and you can furtherly create these games on your own.

If you want to suggest an addition to this article, you are welcome to share it in the comments section. I will be waiting for your feedback.

Scroll to Top