1

SCIT

School of Computing and Information Technology

Faculty of Engineering & Information Sciences

CSCI124 – Applied Programming – July 2017 Session

Assignment 2 (80 marks)

1. Due: TBA

2. Daily penalty: minus 20% per day (maximum 3 days)

3. Please zip all your program files in a folder called “YourName_A2” and submit

via the Moodle system

4. All file names given in this assignment MUST be respected

5. Please write down your names in EVERY file submitted

6. This assignment is worth 12% of the total marks of this subject.

Background

To become familiar with program designs dealing with

– Applications on arrays using pointer arithmetic.

– The use of void* for generic design

– Sorting and searching

– Dynamic allocations

Problem

A deck of cards may be used for playing a variety of card games, with varying elements

of skill and chance, some of which are played for money. No matter what sort of card

games, the basic principle is always the same, shuffle the cards, distribute the cards and

arrange for the cards. These are the objective of this assignment.

The following shows a sample of familiar playing cards used by most of the people:

2

Different countries may have different naming of card values. For example, the French

people name the Jack, Queen and King as Valet, Dame and Roi

respectively.

They call the suits Spades, Hearts, Diamonds and Clubs as Pigues,

Coeurs, Carreaux and Trèfles respectively. If you wish to hear my Singapore

accent of French pronunciation of these 4 words, feel free to see me ?

When you buy a pack of playing cards, you always see that they are arranged in order;

firstly the cards are arranged based on the colors and among the colors, they are arrange

based on the card values (we usually call them the ranks of the cards).

After shuffling the cards, you then distribute the cards to the players. Each player

usually

holds the same number of cards depending on the game. We usually call it a hand (of

cards). When players receive the cards, they usually like to re-arrange the cards in order,

firstly by colors and then by rank.

For example, the bridge game has 4 players, and each player has 13 cards. Here are the

arrangements of cards of the players:

3

Everyone has his (her) way of arranging the cards, this is less important to this

assignment as long as you fix your way of arranging.

Let us now illustrate what you have to do for this Assignment. In order to cater for the

need of various conventions of naming the cards by different countries, we propose the

following structure definition of a playing card:

struct Card

{

Char * color;

RankType rank;

};

A deck of playing cards is an array of structure Card’s and it is not necessary 52 cards,;

for example French Tarot card game has 78 cards!!

Important to note

You should treat the array of Card as Card*. All storages used for the arrays

should be dynamically created; and delete them when they are no longer needed.

When accessing to an element of the array, you should access it via a pointer, i.e. by

dereferencing this pointer. Using the notation, for example cardArray [k] or

*(cardArray + k) accessing to the k

th element of the array cardArray is not

allowed.

The color is one of the following four C-strings (array of characters)

4

“Club” “Diamond” “Heart” “Spade”

and the rank is one of the following enumeration constant pointers:

enum RankType {Two, Three, Four, Five, Six, Seven, Eight,

Nine, Ten, Jack, Queen, King, Ace};

You can alter the order of the enumeration constants in the above definitions but my

illustrations of this assignment will base on the above order.

As can be seen from the above definition of colors, based on the ASCII values

“Club” < “Diamond” < “Heart” < “Spade”
And the order of the constants in RankType is obvious in definition.
A card should be printed together with the color symbols:
? - C
? - D
? - H
? - S
and the ranks should be printed as 2 3 4 5 6 7 8 9 T J Q K A. For example
Spade 10, should be printed as ST.
We have all the definitions and declarations. Let us look at the details of each of the tasks.
Task 1 – Shuffle of cards
A deck of playing cards is stored in an array of Card structures. All the cards are distinct.
A few functions which are important to deal with this task:
- to get a playing card and all the cards should be randomly generated
- to print a playing card
- to insert a playing card into an array
- to print a deck of playing cards
- etc
For example, after we have shuffled the playing cards, we can display them according to
the following format, i.e. the 1st interaction when you execute your program:
5
According to the above interaction, you wish to have 4 hands of 10 cards each. 4 and 10
are input data. Task 2 – Distribute of cards
Depending on the number of players who plays in a card game, we usually try to
distribute the same number of cards to each player. For example, bridge game has 4
players and each player has 13 cards. The cards hold by each player is called a hand.
Based on this definition, you can define
Card ** someHands;
You can interpret someHands is two-dimensional arrays, the 1st dimension (row index)
stands for the number of hands and the 2nd dimension (column index) represents the
number of cards for each hand. Alternatively, you can define an alias using the
typedef definition, for example
typedef Card * Hand;
and use it to declare some hands, for example
Hand *someHands;
Using the deck of cards shuffled in Task 1, we can now distribute them. Let us look at the
interactions and the displays:
6
We will look at some special cases when user enters the wrong information later.
One important function for this task, obviously, is the distribution of card function and
this function should receive as parameter the deck of cards shuffled in Task 1.
Task 3 – Arrange of cards
As can be seen from the distributed hands in Task 2, all the hands are not arranged in
order. Almost all the players when they receive a hand of playing cards, they like to
group the same colors together and among the colors, they also like to arrange them in
rank order; and sometimes they also like to know the number of cards for each color.
How to arrange an array of playing cards in order?
You can assume that you are given an array which is already sorted. You now wish to
insert a playing card in the sorted array. Two alternatives:
- You can search starting from the 1st element of the sorted array and locate the
right position to insert a playing card; or
- You can also search starting from the last element of the sorted array and locate
the right position to insert the playing card.
Initially you can assume that the sorted array consists of only the 1st element of a hand of
playing cards. You continually insert the 2nd element, the 3rd element and so on until the
last element of the array of playing cards.
The following screen shows some of the useful information after the arrangement:
In the above screen, 2 – 4 – 1 – 3 means 2 cards in spades, 4 cards in hearts, 1 card in
diamonds and 3 cards in clubs. The cards are arranged in decreasing order based on the
7
colors; and among the colors, the cards are arranged in decreasing order based on the
ranks.
Finally, the garbage collection!!! All the storages used for this application should be
dynamically allocated. Before proceeding to next step, you should destroyed all storages
used. Here is the interaction and display:
Task 4 – To drive the whole application
You now design a main function to drive the whole application. Let us look at the whole
run of the system: (Replace SP by your own name for the ease of recording your
assignment marks)
As can be seen from the above interactions, the whole dealing system is repeated. The
above screen shows one of the abnormal cases.
8
Another abnormal case:
Final screen captured:
You can see that if you ask for one hand of 52 cards, the deck of playing cards will be
arranged all in order.
IMPORATANT TO NOTES
Put all the function prototypes with some descriptions to the prototypes in a header file
called A2.h; its implementations are in A2.cpp and the main function is stored in the
file called MainA2.cpp.
Here is the summary of all the files you have to submit, zip them in a zip file called
YourName_A2.zip:
(1) A2.h
(2) A2.cpp
(3) MainA2.cpp
(4) A cpp file called YourName_A2.cpp that you copy and paste in order of the
files (1) to (3) for grading and plagiarism testing purpose.
(5) A project file called YourName_A2
9
(6) Make a declaration saying something similar to “You did not pass your
assignment to anyone in the class or copy anyone’s work; and you
are willing to accept whatever penalty given to you and also to all the
related parties involved”