On this page:
Suits, Cards, and Decks
Shuffle Up and Deal
This Means War
6.10

Lab 24: Accumulating Cards

Implement this lab with the Intermediate Student Language with Lambda.

Make sure you follow The Style we use for the {B,I,A}SL{,+} languages in this class.

Choose the initial Head and Hands, and get started!

Suits, Cards, and Decks
; A Suit is one of: "♠" "♥" "♦" "♣".
(define SUITS '("♠" "♥" "♦" "♣"))
; A Value is one of: "A" "K" "Q" "J" "10" "9" "8" "7" "6" "5" "4" "3" "2".
(define VALUES '("A" "K" "Q" "J" "10" "9" "8" "7" "6" "5" "4" "3" "2"))
; A Card is a (string-append Value Suit).
; A Deck is a list of 52 unique cards in any order.

Ex 1: Define a constant DECK which contains the 52 cards in a standard deck. Rather than defining the cards by hand, fold over the constants SUITS and VALUES to build the deck.

Ex 2: Design a function card<?, which returns #true only if the second card is higher than the first card. To keep things simple, we’ll break ties by suit: "♠" > "♥" > "♦" > "♣".

Hint: Create a sorted deck using VALUES and SUITS, then define a helper-function index-of which given an element and a list, returns the index of that element in the list. The card with the higher index (or lower index, depending on the order of your sorted deck) is the higher card.

Shuffle Up and Deal

Swap Head and Hands!

If we want to play some cards, we first need to shuffle the deck.

Ex 3: Define a function shuffle which, given a deck of cards, returns a deck with the same cards as the input but in a random order.

This is a bit tricky, so here are high-level descriptions of two possible implementation strategies:

Ex 4: Define a function deal that given a deck of cards and the number of players n, shuffles and deals the cards to the players by returning a list of n lists of cards.

Example invocations of deal and the resulting lists:

> (deal DECK 1)

(list (list "3♥" "7♠" "A♦" ... 49 more))

> (deal DECK 2)

(list

(list "4♣" "7♦" "2♥" ... 23 more)

(list "3♣" "Q♣" "K♠" ... 23 more)

)

> (deal DECK 3)

(list

(list "9♦" "3♣" "7♥" ... 14 more)

(list "8♥" "J♠" "5♣" ... 14 more)

(list "J♥" "2♦" "5♦" ... 15 more)

)

> (deal DECK 4)

(list

(list "Q♣" "9♠" "K♦" ... 10 more)

(list "Q♦" "J♥" "K♠" ... 10 more)

(list "A♠" "7♣" "5♠" ... 10 more)

(list "9♠" "A♦" "4♣" ... 10 more)

)

This Means War

Swap Head and Hands!

The card game known as War is relatively simple:

Ex 5: Design a function battle that given a list of N cards, returns the index of the highest-valued card.

Ex 6: Design a function war that given the number of players, deals a random hand to each player and plays the game of War until there is a winner.

Hint: These last two exercises are purposely left ambiguous. It is up to you to decide how to implement this problem. There is not one right answer, but some answers are better and simpler than others.