Programming Languages

G22.2110 Spring 1998

 

Project #3

(Due April 20, 1998)

 

 

This project focuses on experimenting with the Ada 95 language. Please answer all questions separately. Question (4) is considered optional, but will result in extra-credit if answered correctly.

 

  1. Ada composite data types:
  2.  

    Produce a package defining data types to represent individual playing cards and also packs of cards.

     

    Define data types to represent the suit and value of a playing card. Cards have four suits (Clubs, Diamonds, Hearts, and Spades) and 13 cards in each suit (Ace, 2 to 10, Jack, Queen, and King).

     

    Each card should be a record containing a suit and a value. A pack should contain an array of up to 52 cards (try to avoid the magic number 52 if you can!) along with the number of cards in the pack.

     

    Provide subprograms to initialize a pack (so that it contains a complete set of four suits of 13 cards each), to deal a card (by removing the first card from the pack), to replace a card (by adding it to the end of the pack), and to shuffle a pack (by interchanging each card with another randomly selected card from the same pack).

     

    To implement the shuffle subprogram, you can generate random values of a discrete type X by instantiating the package Ada.Numerics.Discrete_Random for type X):

     

    Package Random X is

    New Ada.Numerics.Discrete_Random (X);

    Gen : Random_X.Generator; -- a random-value generator

     

    You will need a with clause for Ada.Numerics.Discrete_Random. The random-value generator Gen can be initialized ready for use by calling the procedure Reset(Gen); you can then generate random values by calling the function Random(Gen), which will produce a new random value of type X from the generator Gen each time you call it.

     

    Write a simple test program to check that the package works correctly.

     

  3. Ada Exceptions:
  4.  

    Modify the playing card package from (1) to define an exception which will be raised if you try to deal a card from an empty pack or replace a card in a full pack. Use the package to implement a simple card game called " Follow the Leader", in which a human player and the computer player are dealt ten cards each. The purpose of the game is to get rid of all the cards in your hand. The first player lays down a card, and each player in turn has to play a card which matches either the suit or the value of the previously played card (e.g., the Jack of Clubs could be followed by any Jack or any Club). If a player has no card that can be used to follow on, an extra card must be taken from the pack. If the pack becomes empty, the cards that were previously played (except the last one) must be returned to the pack. The first player to play out a hand and have no cards left is the winner.

     

    Write a simple test program to check that the package works correctly.

     

  5. Ada Private Types:

 

Modify the playing card package from (2) to use private types for cards and packs of cards. A card should have a Boolean component called Valid with a default value of False to indicate whether a card variable contains a valid card or not, and a pack should have a Boolean component called Initialized with a default value of False to show whether it has been initialized. All operations on a pack of cards should check the Initialized member and initialize the pack if necessary so that it contains a full set of 52 cards. Note that cards and packs should be limited private types so that it isnít possible to duplicate them by assignment.

 

Provide a Move operation which moves a valid card into an invalid variable, leaving the destination valid and the original card invalid, so that it is impossible to create duplicate cards. It should be an error to move a card into another card which is marked as valid, since this would allow existing cards to be destroyed. You will also need functions to access the suit and value of a card.

 

Write a simple test program to check that the package works correctly.

 

(4) Ada Controlled Types:

 

Modify the playing card package from (3) to make a pack of cards a controlled type. Declaring a pack of cards should create a shuffled set of all 52 cards automatically.

 

Write a simple test program to check that the package works correctly.