0% found this document useful (0 votes)
203 views15 pages

18PE01 ADS Mini Project

This document is a project report for creating a deck of cards game that can be played by four players. It outlines the problem definition, requirements, data structures used, and algorithms designed for the project. The key functions created include generating cards for each type, shuffling the deck, allocating cards to each player, sorting the cards of each player, and preparing the cards for the game by shuffling, dealing, and determining the first player. The project implements concepts like structures, arrays, functions, and built-in functions in C programming. It could be further developed with a GUI and more features in the future.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
203 views15 pages

18PE01 ADS Mini Project

This document is a project report for creating a deck of cards game that can be played by four players. It outlines the problem definition, requirements, data structures used, and algorithms designed for the project. The key functions created include generating cards for each type, shuffling the deck, allocating cards to each player, sorting the cards of each player, and preparing the cards for the game by shuffling, dealing, and determining the first player. The project implements concepts like structures, arrays, functions, and built-in functions in C programming. It could be further developed with a GUI and more features in the future.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

DECK OF CARDS

PROJECT REPORT
Submitted by
Bala Murali Krishnan 20F109

Bhavishya S 20F110

Giritharan K 20F116

Mohamed Ashraf MA 20F131

18PE01 ADVANCED DATA STRUCTURES


(SEMESTER 4)

KARPAGAM COLLEGE OF ENGINEERING


(Autonomous)
COIMBATORE – 641 032
JULY 2022

z
1
CERTIFICATE
Certified that this project report titled “Deck of cards” is the bonafide work

of Bala Murali Krishnan(20F109), Bhavishya S(20F110), Giritharan K

(20F116), Mohamed Ashraf MA (20F131), who carried out the project under

my supervision. Certified further, that to the best of my knowledge the work

reported herein does not form part of any other project report.

I understand the policy on plagiarism and declare that the project and

publications are my own work, except where specifically acknowledged and has

not been copied from other sources or been previously submitted for award or

assessment.

LAVANYA SUDHAKAR
Faculty In-charge

Submitted for the Project Viva-Voce examination held on

Examiner I Examiner II

z
2
ABSTRACT

This is a small-scale project on Deck of Cards done using data structures in C


programming. Jack decides to create a one-deck game that can be played by four
players. He came up with the rules for dealing cards to four players. There are four
types of cards in the deck. Club (C), Diamond (D), Hearts (H), Spades (S). Each
card has a priority assigned to it. The order of the cards must be C-> D-> H-> S
(low to high). I have created a program that performs the following functions: I
created generate_cards_per_type to generate cards. This method takes a card type as
inputs and returns a list of cards generated for a particular card type. Then shuffle. I
have created a method shuffle_card_deck (cards_list). This method takes a list of
542 cards as inputs and shuffles the list of cards. Please return in the order listed.
The most important part of the game is the user. SOlocate_cards_to_players
(cards_list) to know user information: This method accepts a list of shuffled cards
and returns a dictionary containing the card details of all 4 players. Cards must be
distributed individually to each player in the order shown below. (Player1-> player2-
> player3-> player4). To perform a sort, sort_cards_of_each_player (card_list): This
method takes a list of each player's cards as input and sorts them in ascending order
of priority. Returns a sorted list. The user needs to prepare the cards on the board,
so I created Prepare_cards (). This method creates a deck, shuffles the cards, deals
cards to all four players (player1, player2, player3, player4), and rearranges the
dealt cards and which player is playing the game. Decide if you want to start. Must
return the name of the first player. To implement this we have usedthe concepts of
structure, array , function , use of inbuilt functions.

In future we can develop this project using GUI. With more features like login-
logout, use of sounds and more picturization.

z
3
S.NO. TITLE PAGE NO.
TABLE OF CONTENTS
LIST OF FIGURES
LIST OF ABBREVIATIONS
1 PROBLEM DEFINITION 6

2 REQUIREMENTS SPECIFICATION
2.1 HARDWARE REQUIREMENTS 7
2.2 SOFTWARE REQUIREMENTS 7

3 DATA STRUCTURES USED 8


4 ALGORITHM DESIGN 9
4.1 ALGORITHM

5 SOURCE CODE 11
6 RESULTS & DISCUSSION 13

7 DESCRIPTION 14
8 CONCLUSION 15

z
4
LIST OF ABBREVIATIONS

RAM RANDOM ACCESS MEMORY


GB GIGABYTES

z
5
1 PROBLEM DEFINITION

Mini project “Deck of cards” is also a simple project.

Jack has decided to create a game with a deck of cards which can be played by 4
players. He has now come up with the rules for distributing the cards among the 4
players. The rules are as mentioned below.
1. The deck contains 52 cards. The cards should be equally split among the four
players. 2. There are four types of cards in the deck. They are Clubs ( C )
,Diamonds ( D ), Hearts ( H ) and Spades ( S ).
3. Each card type has a set of 13 cards, numbered from 2,3…10, J, Q, K, A. The
values of the cards J, Q, K and A are 11, 12, 13 and 14 respectively. The card
number '2' has got the lowest priority and the card number 'A' has got the
highest priority.
4. The cards should be equally distributed among the four players.
5. They have to sort their cards before starting the game. The card type 'C' has got
the lowest priority and the card type 'S' has got the highest priority. The order
of the cards should be C -> D -> H -> S.
6. The player who has got the card 'CA' should begin the game.
Write a program to perform the following functionalities.
generate_cards_per_type(card_type): This method takes the card type as the input
and returns the list of cards generated for the specific card type.
Example, for card_type="C", list of cards should be
C2,…C10,CJ,CQ,CK,CA generate_card_deck(): This method returns
the list of 52 cards.
Hint: Use generate_cards_per_type() to generate the cards based on
card_type. Example: C2..CA, D2..DA,H2..HA,S2..SA
shuffle_card_deck(cards_list): This method takes the list of 52 cards as the input
and returns the list of cards in a shuffled order.
Hint: Shuffling can be done by randomly generating two index positions – one from
the first half of the list and other from the second half of the list and then swapping
the cards at those index positions.
allocate_cards_to_players(cards_list): This method accepts the shuffled list of
cards and returns a dictionary which contains the card details of all the four
players. The cards should be allocated one at a time to each player in the sequence
as shown below. (player1->player2 ->player3 ->player4)
The dictionary should have the player name (Example: player1) as the key and the
list of the cards allocated to them as the value.
sort_cards_of_each_player(card_list): This method takes the list of cards of each player
as the input and sort them based on the increasing order of their priority. It returns the
sorted list. prepare_cards(): This method generates the card deck, shuffles the cards,
allocates the cards to all the four players(player1,player2,player3,player4), sorts their
allocated cards and decides which player should start the game. It should return the
name of the first player.

z
6
2 REQUIREMENTS SPECIFICATION

2.1 HARDWARE

REQUIREMENTS

Processor: Core i3/ i5/ i7

RAM: 128 MB

Hard Disk: 20GB

Monitor: 15” Color monitor

Key Board: 104 Keys

2.2 SOFTWARE REQUIREMENTS

Operating System:Windows 7/8.

Language: C Programming

Software: Dev C++

z
7
3 DATA STRUCTURES USED

Array
An array is a data structure for storing more than one data item that has a
similar data type. The items of an array are allocated at adjacent memory locations.
These memory locations are called elements of that array. The total number of
elements in an array is called length.
The details of an array are accessed about its position. This reference is called
index or subscript.
The simplest type of data structures is a linear array, also called one-
dimensional array.
The elements of an array data structures are required to have the same size
and should use the same data representation.
Array are among the oldest and most important data structure and are used in
almost every program. They are also used to implement many other data structure,
such as string and lists.

Structure
Arrays allow to define type of variables that can hold several data items of
the same kind. Similarly structure is another user defined data type available in C
that allows to combine data items of different kinds.Structures are used to
represent a record. Due to that we have used the concept of structure as we have to
maintain a record of deck of cards.

Functions
A function is a block of code which only runs when it is called. You can
pass data, known as parameters, into a function. Functions are used to perform
certain actions, and they are important for reusing code: Define the code once, and
use it many times.

Inbuilt functions
A built-in function is a function that is already available in a programming language,
application, or another tool that can be accessed by end users.This makes optimization
of code and more easier to code.

z
8
4 ALGORITHM DESIGN

BEGIN
struct card{
char *rank;
char suit[MAX];
END;
typedef struct card Card;

char *ranks[MAX_RANKS] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven",


"Eight", "Nine", "Ten", "Jack", "Queen", "King"};

char suits[MAX_SUITS][MAX] = {"Clubs", "Diamonds", "Hearts", "Spades"};

void generate_cards_per_type(Card []);


void allocate_cards_to_players(Card []);
void prepare_cards(const Card[]);
int main()
BEGIN
char newline = '\n';
//declare an array of 52 cards
Card deck[MAX_CARDS] = {"",""};
generate_cards_per_type(deck);
printf("Ordered deck of cards:\n");
prepare_cards(deck);
while('\n' == newline){
printf("\n Shuffling deck ... \n");
allocate_cards_to_players(deck);
prepare_cards(deck);
printf("Allocate cards again?\nIf so, press \"Enter\" key. If not, press any other key.
");
newline = getchar();
END
return 0;
END
void generate_cards_per_type(Card deck[])
BEGIN
int i = 0;
for(i=0;i<MAX_CARDS;i++)
BEGIN
deck[i].rank = ranks[i%MAX_RANKS];
strncpy(deck[i].suit, suits[i/MAX_RANKS], MAX);
END
END

z
9
void allocate_cards_to_players(Card deck[])
BEGIN
int swapper = 0;
int i = 0; //counter
Card temp = {"", ""};
srand(time(NULL));
for(i=0;i<MAX_CARDS;i++)
BEGIN
swapper = rand() % MAX_CARDS;
temp = deck[i];
deck[i] = deck[swapper];
deck[swapper] = temp;
END
END
void prepare_cards(const Card deck[])
BEGIN
int i = 0;
for(i=0;i<MAX_CARDS;i++)
BEGIN
printf("%5s of %-12s", deck[i].rank, deck[i].suit);
if(0==((i+1)%COLS))
BEGIN
printf("\n");
END
END
END

z
10
5 SOURCE CODE

#include<stdio.h>
#include <time.h> //time function
#include <stdlib.h> //random number generator functions
#include <string.h>
#define MAX 9
#define MAX_CARDS 52
#define MAX_RANKS 13
#define MAX_SUITS 4
#define COLS 3

struct card{
char *rank;
char suit[MAX];
};
typedef struct card Card;

char *ranks[MAX_RANKS] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven",


"Eight", "Nine", "Ten", "Jack", "Queen", "King"};

char suits[MAX_SUITS][MAX] = {"Clubs", "Diamonds", "Hearts", "Spades"};

void generate_cards_per_type(Card []);


void allocate_cards_to_players(Card []);
void prepare_cards(const Card[]);

int main(){
char newline = '\n';
//declare an array of 52 cards
Card deck[MAX_CARDS] = {"",""};
generate_cards_per_type(deck);
printf("Ordered deck of cards:\n");
prepare_cards(deck);
while('\n' == newline){
printf("\n Shuffling deck ... \n");
allocate_cards_to_players(deck);
prepare_cards(deck);
printf("Allocate cards again?\nIf so, press \"Enter\" key. If not, press any other key.
");
newline = getchar();
}
return 0;
}

z
11
void generate_cards_per_type(Card deck[]){
int i = 0;
for(i=0;i<MAX_CARDS;i++){
deck[i].rank = ranks[i%MAX_RANKS];
strncpy(deck[i].suit, suits[i/MAX_RANKS], MAX);
}
}

void allocate_cards_to_players(Card deck[]){


int swapper = 0;
int i = 0; //counter
Card temp = {"", ""};
srand(time(NULL));
for(i=0;i<MAX_CARDS;i++){

swapper = rand() % MAX_CARDS;

temp = deck[i];
deck[i] = deck[swapper];
deck[swapper] = temp;
}
}

void prepare_cards(const Card deck[]){


int i = 0;
for(i=0;i<MAX_CARDS;i++){
printf("%5s of %-12s", deck[i].rank, deck[i].suit);
if(0==((i+1)%COLS)){
printf("\n");
}
}
}

z
12
6 RESULT AND DISCUSSION

Output 1:

z
13
7 DESCRIPTION

As we scheduled the details , the cards are allocated.

z
14
8 CONCLUSION

Thus the project for card game is executed successfully using the
method of structures and arrays and has proved beneficial for storing and
retrieving of data. With further improvement and development, the
project will be able to implemented in a large scale.
The project has explored wide range of use cases. Although it requires to
implement the project to test variety of cases with some features such as
restart game , user information the main logic / idea behind the
data organization using structures has been successfully executed and
verified.
As we know that no program can be 100% reliable and efficient, There
are also drawbacks in the system, some are: It cannot perform all
required functions of the professional software, System is not sharply
graphical user interface, It is not a multi-purpose or multi-tasking
program.
On the similar side, the project has various advantages: It is user
friendly software, The software offers 0 duplication that is if 1 record is
saved, duplicate of the created record cannot be made (user info ).
Further enhancements can be made in future by: Users can create card
records, graphical interpretation of the game , scale and movements , For
security purpose , enhanced encryption method can be introduced by creating
user login.
The main objective of reducing paperwork and proper arrangement of
data were successfully demonstrated using C programming language.

z
15

You might also like