Learning F# – BlackJack

This source code will serve as a reference for common set operations that have been partially implemented for a BlackJack game.
Feedback is welcomed within the comments section as I continue learning this mythical language.

module Core
type Suit = | Spades
            | Clubs
            | Diamonds
            | Hearts
type Face = | Two | Three | Four | Five 
            | Six | Seven | Eight | Nine | Ten
            | Jack | Queen | King | Ace
type Card = {Face:Face; Suit:Suit}
type Deal = | Hand of Card * Card
            | Hit of Card
let private suits = [Spades; Clubs; Diamonds ; Hearts]
let private faces = [Two; Three; Four; Five; Six; Seven; Eight; Nine; Ten;
                     Jack; Queen; King; Ace]
let deck = [for suit in suits do
            for face in faces do
                yield {Face=face; Suit=suit}]
let hitPlayer (deck:Card list) =
    (deck.Head, deck.Tail)
let deal = function
    | card1::card2::remaining -> Some(card1, card2), remaining
    | _ -> None, []
let shuffle xs =
    let swap i j (array : _[]) =
        let tmp = array.[i]
        array.[i] <- array.[j]
        array.[j] <- tmp
    let rnd = System.Random()
    let xArray = Seq.toArray xs
    let n = Array.length xArray
    for i in [0..(n-2)] do
        let j = rnd.Next(i, n-1)
        swap i j xArray
    xArray |> Seq.toList
let hand, deckRemaining = deal (shuffle deck);;
let cardsLeft = Seq.length deckRemaining;;

One Reply to “Learning F# – BlackJack”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: