Introduction to Functional Programming – Lab 2: “BlackJack” | TDA555 / DIT440, LP1 2015 |
Home | Schedule | Labs | Exercises | Exam | About | FAQ | Fire | Forum | TimeEdit | Links |
Introduction to Functional Programming – Lab 2: “BlackJack” | TDA555 / DIT440, LP1 2015 |
Home | Schedule | Labs | Exercises | Exam | About | FAQ | Fire | Forum | TimeEdit | Links |
Some notes:
Good luck!
In this lab assignment, you will write an implementation of (a simple variant of) the game Black Jack.1 By doing so you will learn to define recursive functions and QuickCheck properties. Since you have not really learned how to do input/output yet, we provide you with a wrapper which takes care of those things for you.
Later in the course, you will learn how to write graphical user interfaces, and if you want to, you can then write such an interface for this program. To cater for this possibility, try to write your program in such a way that you can understand it in a month’s time.
The lab assignment is divided into 2 parts:
size
function) and parts of Task B (implement the functions value
, gameOver
, and winner
).There are also some extra assignments mentioned after Task H. These are not obligatory, but you will learn more when you do them!
This lab assignment has 3 deadlines:
The game you will implement is a simple variant of Black Jack. This section describes the rules that you should implement. We expect your implementation to follow these rules.
There are two players, the “guest” and the bank. First the guest plays. She (he) can draw as many cards as she wants, as long as the total value does not exceed 21. When the guest has decided to stop, or gone bust (score over 21), the bank plays. The bank draws cards until its score is 16 or higher, and then it stops.
The value of a hand (the score) is the sum of the values of the cards. The values are as follows:
The winner is the player with the highest score that does not exceed 21. If the players end up with the same score, then the bank wins. The bank also wins if both players go bust.
For this lab, you need the two files Cards.hs and Wrapper.hs. You do not need to understand anything about the wrapper, but you have to understand most of Cards
. That module contains some definitions from the lectures; data types and Arbitrary
instances for playing cards. See the slides here and here. Read through the file so that you know which types you are supposed to use.
We can define an example hand, consisting of the cards 2 of hearts and jack of spades, as follows:
hand2 = Add (Card (Numeric 2) Hearts)
(Add (Card Jack Spades) Empty)
Cards.hs contains a function that computes the number of cards in a hand:
size :: Num a => Hand -> a
size Empty = 0
size (Add card hand) = 1 + size hand
In order to understand this function, take some time to work with it before you continue. You can for instance execute size hand2
by hand, on paper. The result should be 2, right?
Task A. Execute the expression
Write this sequence of equations as comments in a file called |
To help you we have included a couple of QuickCheck properties below. Your functions must satisfy these properties. If they do not, then you know that something is wrong. Testing helps you find bugs that you might otherwise have missed. Note that you also have to write some properties yourself, as indicated below.
The purpose of writing properties is three-fold:
So, to take maximum advantage of the properties, write them before you write the corresponding functions, or at the same time.
The code has to be documented. See the Cards.hs and Wrapper.hs modules to get an idea about what kind of documentation is expected of you. Try to follow these guidelines:
Similar arguments apply to documentation as for properties; write the documentation when you write your functions, not afterwards.
Write all code in a file called BlackJack.hs
. To make everything work, add the following lines at the top of the file:
module BlackJack where
import Cards
import Wrapper
This tells the Haskell system that the module is called BlackJack
, and that you want to use the functions and data types defined in Cards
and Wrapper
. Download Cards.hs and Wrapper.hs and store them in the same directory as BlackJack.hs
, but do not modify the files.
As usual, when using QuickCheck you need to first import it. However, it turns out that recent versions of QuickCheck define a function shuffle
whose name clashes with the function you are supposed to define in task G. To avoid this problem, you can import QuickCheck as follows:
import Test.QuickCheck hiding (shuffle)
If the above import statement gives an error, you probably have an older version of QuickCheck. Just remove hiding (shuffle)
in that case.
When working on the functions below, it is a good idea to make two definitions,
aCard1 :: Card
aCard1 = ... -- define your favorite card here
aCard2 :: Card
aCard2 = ... -- define another card here
so that you can use these cards to test your functions. Also, make a definition,
aHand :: Hand
aHand = ... -- a Hand with two Cards, aCard1 and aCard2
to test your functions that work on hands.
Task B. Implement the following functions.
|
The remaining tasks belong to Part II of the lab.
Task C. Given two hands,
(Note that a function name with only symbols indicates an infix operator. It is used just like This function must satisfy the following QuickCheck properties. The function should be associative:
Furthermore the size of the combined hand should be the sum of the sizes of the two individual hands:
The implementation of this property is not given here, you have to write it yourselves. |
Task D. You also need to define a function that returns a full deck of cards:
You could do this by listing all 52 cards, like we did with two cards above. However, that is very tedious. Instead, do it like this: Write a function which given a suit returns a hand consisting of all the cards in that suit. Then combine the 13-card hands for the four different suits into one hand using |
Task E. Given a deck and a hand, draw one card from the deck and put on the hand. Return both the deck and the hand (in that order):
If the deck is empty, report an error using
|
Hint: To return two values a
and b
as a pair, use the syntax (a, b)
. This syntax is also used for pattern matching on pairs:
first :: (a, b) -> a
first (x, y) = x
Task F. Given a deck, play for the bank according to the rules above (starting with an empty hand), and return the bank’s final hand:
To write this function you will probably need to introduce a helper function that takes two hands as input, the deck and the bank’s hand. To draw a card from the deck you can use
You can read more about |
Task G. Make sure you have read about random numbers before starting this task. Given an
Note: See above about how to import QuickCheck without getting name clashes with the If you want a (small) challenge, try to implement One way to shuffle the cards would be to pick an arbitrary card from the deck and put it on top of the deck, and then repeat that many times. However, how many times should one repeat? If one repeats 52 times, then the probability that the last card is never picked is about 36%. This means that the last card is often the same, which of course is not good. A better idea is to pick an arbitrary card and put it in a new deck, then pick another card and put it on top of the new deck, and so on. Then we know that we have a perfectly shuffled deck in 52 steps (given that the random number generator is perfect, which it is not). Note that for both approaches we need a function that removes the n-th card from a deck. The function
For this we need the helper function
(Here we’re using The above property does not guarantee that the size of the deck is preserved by
|
You have barely touched upon input/output in the lectures, so we have to provide you with a wrapper that takes care of those things. All you have to do is to write the functions above, package them together (as explained below), and then call the wrapper with the package as an argument.
Task H. To “package up” these functions, write the following code:
(Note that To run the program, define
in your source file, load the file in GHCi, and run |
Happy playing!
The following is completely optional, but if you want to do more, there are many possibilities:
Most of the ideas above require that you program input/output (I/O) yourself. You have seen how to do simple I/O in the lectures. Use the Wrapper.hs module as a starting point.
Note that doing something extra will not directly affect your grade, but can of course be beneficial in the long run. Take care to do the compulsory part above before attempting something more advanced, though.
Write your answers/solutions in one file, called BlackJack.hs
. For each assignment, use Haskell comments to indicate what part of the file contains the answer to the assignment. For answers in natural language, you can use Swedish or English; write these answers also as Haskell comments.
Before you submit your code, clean it up! Remember, submitting clean code is Really Important, and simply the polite thing to do. After you feel you are done, spend some time on cleaning your code; make it simpler, remove unneccessary things, etc. We will reject your solution if it is not clean. Clean code:
Please do not submit the modules Cards.hs
and Wrapper.hs
(unless an extra assignment required you to make changes to them).
When you are done, please submit it using the Fire system.
Good luck!
This lab is based on material by Nils Anders Danielsson, Koen Claessen, Hampus Ram, Andreas Farre, Sebastian Sylvan and Mary Bergman.↩