Note: This is an optional challenge, for students who found the Unit 3 Summative a little too easy.
A standard deck of cards is comprised of four suits (H
earts, D
iamonds, Clubs, and Spades) and each suit has the cards 2 through 10, Jack, Queen, King, and Ace.
One way of storing each card is through two-character strings. For example "AH" is the Ace of Hearts and "2D" is the 2 of diamonds, "8S" is the 8 of spades, etc. (It is worth noting that this is an amateur way of storing the data, but it still works!)
Another way is use the ASCII characters for the suits: ♥ ♦ ♣ ♠
This means that a deck of cards can be created using these strings:
deck = ["2D", "2H", "2C", "2S", "3D", "3H", ... , "AC", "AS"]
or
deck = ["2♦", "2♥", "2♣", "2♠", "3♦", "3♥", ... , "A♣", "A♠"]
``
The array deck
would have a length of 52 when complete.
Create the function createDeck()
that returns a complete array of 52 cards, as described above. The deck should be ordered from 2 to ACE and the suit order should be D, H, C, S. The array will be created programatically, not hard-coded.
Create the function shuffle(deck)
. This function receives a deck of cards and shuffles the order of that deck - returning the shuffled deck. Keep in mind that the method you use for shuffling must be random so that it returns a new shuffled order each time.
Cards are used to play games. When you play a game, you deal cards to people. Create the function deal(deck, num)
that deals out cards (or one card). This function returns an array of cards of length num
, having removed those cards from the deck
. To clarify, the arguments to the function are the deck
to deal from and the num
ber of cards to deal. It removes those cards from the deck, returning an array of the cards that were dealt. Even if only one card is being dealt - it is in an array.
Cards can be put into a deck, but only if the deck doesn't already have that card! Create the function insert(deck, card)
that:
- Checks the
deck
to see ifcard
is already in it.- If the card is not already in the deck, it inserts the card at the end. If you want to shuffle it after inserting, that's up to you. Return
true
. - If the card is already in the deck, do nothing and return
false
.
- If the card is not already in the deck, it inserts the card at the end. If you want to shuffle it after inserting, that's up to you. Return
A deck of cards is only useful if it is complete (has all the proper cards). Create the function isComplete(deck)
that receives a deck
of cards and checks to see if it has all the appropriate cards for a complete deck. It will return true
if the deck is complete and false
otherwise.