18PE01 ADS Mini Project
18PE01 ADS Mini Project
PROJECT REPORT
Submitted by
Bala Murali Krishnan 20F109
Bhavishya S 20F110
Giritharan K 20F116
z
1
CERTIFICATE
Certified that this project report titled “Deck of cards” is the bonafide work
(20F116), Mohamed Ashraf MA (20F131), who carried out the project under
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
Examiner I Examiner II
z
2
ABSTRACT
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
5 SOURCE CODE 11
6 RESULTS & DISCUSSION 13
7 DESCRIPTION 14
8 CONCLUSION 15
z
4
LIST OF ABBREVIATIONS
z
5
1 PROBLEM DEFINITION
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
RAM: 128 MB
Language: C Programming
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;
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;
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);
}
}
temp = deck[i];
deck[i] = deck[swapper];
deck[swapper] = temp;
}
}
z
12
6 RESULT AND DISCUSSION
Output 1:
z
13
7 DESCRIPTION
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