CSC148H - Summer 96

Assignment 3: A Card Trick

Due: Thursday July 18, 6pm.

PART I:


The trick

From the player's point of view, the trick proceeds as follows. The magician lays out 21 cards face up in 3 columns of 7 cards each (see figure 1). After choosing a card, the player indicates which column the card is in. The magician then gathers the cards up and lays them out again in the same pattern but in a different order. Once again the player indicates which column his chosen card is in, and the process is repeated once more. Then, the magician magically presents the card that the user chose.

   figure27
Figure 1: The layout of the cards.

Here's how the trick is done. After the player indicates a column, the magician picks up each of the columns separately and places the chosen column in between the other two columns forming a deck of 21 cards. The cards are then laid out (for the second time) across columns, one card per column at a time (as in solitaire). After the player's second choice, they are picked up in the same manner as before, with the chosen column again sandwiched between the other two columns. Finally, after a third repetition, the chosen card always ends up as the middle card in the 21-card deck. Try it with a real deck of cards.

In this assignment, you will write a program that performs the trick. The computer will act as magician, and the user as player. The purpose of the assignment is to give you experience in using both linked lists and modules. The main work will be to design and implement a Turing module that stores and manipulates the current state of the cards involved in the trick. You will also have to write the main program that performs the trick by making calls to subprograms in the module.

The module

There are obviously many ways to design and implement the module, so, for the sake of consistency (to help the markers), you must adhere to the following specification of the module.

The data structure

There are 21 cards involved in the trick; at any time, they can be organized in one of two ways. Before being laid out, they are organized as a deck (i.e., a list) of cards. When the cards have been laid down for the user to view, they are organized as three lists of cards, one for each column. Notice that the data structure reflects the physical organization of the cards.

You are required to implement each list of cards as a linked list. Therefore, the 21-card deck will be a linked list, and each column will also be a separate linked list. Figure 2 shows the structure of the column layout. Note that The implementation of the list of columns is up to you. The whole implementation should be flexible enough to deal with other configurations, such as 4 columns of 4 cards, with only minor alterations.

   figure42
Figure 2: Structure of the column layout.

We also need a data type for a card. On the web page, we have provided a module called DeckOfCards, which defines and exports the following type:

    type cardType :
        record
            suit : string (1)   % "S", "H", "D", or "C"
            value : string (2)  % "A", "1" - "10", "J", "Q", "K"
        end record
The DeckOfCards module is discussed in greater detail below.

The interface

Several operations are possible on this data structure; the following is a suggested breakdown. If you find a different set of operations to be better, then you may use it if you justify your decision in your report. Of course, you will probably want to further subdivide these operations into subprograms internal to the module.

Initialize
This operation should initialize the data structure.

Set up
This operation should deal 21 cards from a shuffled deck of 52 cards, and put them into the 21-card deck used in the trick. The DeckOfCards module contains subprograms for initializing, shuffling, dealing, and displaying cards from a 52-card deck. The module is documented internally.

Lay out
This operation should lay out the cards from the 21-card deck into the three columns. It should also display the cards on the screen in an appropriate manner. You can use the DisplayCard procedure in the DeckOfCards module to display cards at given coordinates (of type coord) on the screen. The coord type is defined in the Graphics module, which is imported by the DeckOfCards module.

Pick up
This operation should pick up the columns in the specified order and build a new 21-card deck. For example, if the user chose the first column, then the first column should be placed between the second and third columns.

Find card
This operation should return the card in a specified position of the 21-card deck. For example, if given the integer 11, it should return the 11th card.

The main program

The main program should import the module that you have written. At each stage of the trick it should ask the user to specify which column his chosen card is in. When the trick is complete it should tell the user which card he picked. If you want to be fancy, we have provided the Graphics module, which contains a subprogram called MouseLocation that returns the coordinates of the mouse when the button is pressed, so you could have the user select the column by using the mouse, instead of by typing.

The report

You should write a short report that includes the following information. (Be clear, concise, and neat.)

How to approach the program

We suggest that you first look over the two modules that we have provided. Observe their style, and use this style in your assignment. Also note how the DeckOfCards module makes use of the coord type defined in the Graphics module.

To learn how to use the modules, we suggest that you write a short main program that imports the modules, and displays all of the cards in the 52-card deck.

For the module that you are to write, start on paper by designing the data structures, and consider how to make the required operations on them as efficient as possible.

On the web page you will find:

1. The DeckOfCards module (be sure to save it as deck.t).
2. The Graphics module (be sure to save it as graphics.t).

PART II


Analysis

This part is completely separate from the Part I, and requires only a written solution. It is meant to give you experience in analysing the differences and tradeoffs between different implementations of the same same abstract data type.

The problem: The Rhinoceros Party of Canada has hired you to write a program to help in their fund-raising. The program should store a list of members with information about their past contributions. The party membership changes often, but they don't have very many members on average, so you are able to store the entire list of members in memory. (Use of files is necessary only for backup purposes). The main purpose of the program is to generate mailing lists of different groups of members, for example, of members who have donated a lot in the past, or who have not paid fees in the current year.

The following information is stored for each member: surname, given names, street address, city or town name, province, postal code, date of last received membership fee, total donations, and amount of last donation.

You are considering four different implementations:

  1. An array sorted by surname.

  2. An unordered circular doubly linked list.

  3. A linked list of member records, ordered by surname, but also `threaded' by city. A threaded list allows one to have several `threads' running through the same set of records at once. Here, the main thread links the records in order by surname. The second thread (actually, set of threads) links the records by city so that all members who live in the same city are connected. An array of cities contains pointers to the head of each city thread. See figure 3.

       figure96
    Figure 3: A list threaded on surname and city

  4. A linked list of member records, ordered by surname, but this time threaded by postal code, date of last received membership fee, and amount of last received donation. This structure contains exactly four threads, each of which is a linked list ordered by one of the keys. Thus, we could easily traverse the list in order of, say, postal code.

Question: The following operations must be applied to the list of members. Discuss the relative merits of each implementation with respect to the amount of memory used by the structure, and the runtime used by each operation.

What to hand in


About this document ...

This document was generated using the LaTeX2HTML translator Version 96.1 (Feb 5, 1996) Copyright © 1993, 1994, 1995, 1996, Nikos Drakos, Computer Based Learning Unit, University of Leeds.

The command line arguments were:
latex2html -split 0 -no_navigation as3.

The translation was initiated by Philip Edmonds on Tue Jun 25 12:13:57 EDT 1996


Philip Edmonds
Tue Jun 25 12:13:57 EDT 1996