CSC 101 Programming Assignment 3
Preparation for Card Game Programming
The purpose of this assignment is to write functions that will be used in programming assignments 4 and 5, where you will write a program to play a card game. Writing the program for assignment 3 requires that you understand the following concepts:
The card game to be played will use a standard deck of 52 cards, where each card has a face value and suit. The face value is one of "Ace", "King", "Queen", "Jack", or a number between 2 and 10. The suit is one of "Spades", "Hearts", "Diamonds", or "Clubs".
The specification of this assignment covers two areas:
External Representation of Cards
A human player is going to communicate with the computer when the card game is played. To do so, the human views and input card values on the terminal. The following external representation is used for card input and output:
The face value of the card is represented as follows:
The suit of the card is represented as follows:
Face
ValueExternal
RepresentationAce A King K Queen Q Jack J 10 through 2 the number itself
For example, the Ace of Spades is input and output as the string "AS". The two of clubs is "2C".
Suit
NameExternal
RepresentationSpades S Hearts H Diamonds D Clubs C
Card-Related Functions
For this assignment, you are implementing the following three functions:
The program you are writing for this assignment consists of four separate files:
Your primary job for this assignment is to implement all of the functions in cards.c. This file is 100% your responsibility.
You will use the cards.h file largely as it is provided. The only modifications you need to make to this file are if you define any new functions in cards.c that don't have a prototype in provided in the original cards.h. If you do this, all you need to do is add prototypes to cards.h for your new functions. Whether or not you define new functions is up to you, but it will likely help with your overall program design.
You use both cards_test.h and cards_test.c 100% as is. Note in particular that you do not write your own main function for this assignment. It's provided for you in cards_test.c.
You compile your finished program like this:
gcc -g -ansi -pedantic -Wall -Werror -o cards_test cards.c card_test.c
The card_test.c program will call the functions you write and print the results to stdout. The output of a successful test run is in the file named test_output . This is a "flow blown" test run of a properly implemented set of functions. As described below under "Testing Details" it is strongly recommended that you write your own simpler versions of cards_test.c, that you run before you do the full blown tests.
The Cards and Deck
One of the key design issues for a card game program is to determine an appropriate data representation for the deck of cards. The basic data representation needs to be an array of some form. There are a number of possibilities, the most straightforward of which is an array of strings, where each array element is the external representation of a card. This is the representation that is defined in the provided cards.h, and therefore the one you'll use for this programming assignment.
For both decks and hands of cards, you need to represent how cards are removed. Two approaches are marking and an integer position variable. Marking involves putting some empty value in the deck, in the place previously occupied by a card. The position-variable approach involves decrementing an integer variable each time a card is removed from the deck.
In this assignment, you will be using the the position-variable representation. This means that the functions above that refer to "a" deck actually refer to two parameters, one for the deck itself and the other for the integer position that's the top of the deck.
The Shuffling Algorithm
To shuffle a deck of cards you use the math library function named rand. The algorithm goes like this:
To help you get started, there is an example of an algorithm similar to this in
the file
rand_example.c
Testing Details
You are given a testing program in the files named cards_test.h and cards_test.c. These files define a main function, which calls the testing functions, which in turn call your card functions.
Since you are not implementing an actual game-playing program for this assignment, the purpose of the main function is to perform testing. It has the following basic structure:
int main() { test_shuffle(...); /* Test the deck shuffling function */ test_deal_two_hands(...); /* Test the dealing of hands */ test_compare_two_cards(...); /* Test the comparison of cards */ }
As noted above, the cards_test.c file is the "flow blown" testing program that you can run when you think you have implemented all of your functions correctly. Before you run the full blown tests, it is strongly recommended that you write your own simpler tests. An incremental development and testing strategy goes like this:
and run mini_test to see how it worksgcc -g -ansi -pedantic -Wall -Werror cards.c compare_cards_mini_test.c -o mini_test
compare_two_cards: 30/100
deal_two_hands: 35/100
shuffle: 35/100
NO collaboration is allowed on this assignment. Everyone must do their own individual work.
Hand in on unix1 with the command
handin gfisher 101_prog3 cards.c cards.hIf you do not modify cards.h, you don't need to hand it in.