Skip to content

Instantly share code, notes, and snippets.

@ApprenticeGC
Last active September 20, 2017 07:18
Show Gist options
  • Select an option

  • Save ApprenticeGC/a2e13dc7dacca0af7532eb3308efe86e to your computer and use it in GitHub Desktop.

Select an option

Save ApprenticeGC/a2e13dc7dacca0af7532eb3308efe86e to your computer and use it in GitHub Desktop.
Continue to make Crazy Eights game after reading blog.
type Suit = Spades | Hearts | Clubs | Diamonds
type Rank = Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten | Jack | Queen | King | Ace
type Card = Rank * Suit
let deal cards =
match cards with
| head::list -> (Some head, list)
| [] -> (None, [])
type Deck = Card list
let splitList n list =
List.splitAt n list
let convertToList pair =
let a, b = pair
[a; b]
let dovetailShuffle cards =
let count = cards |> List.length
let halfCount = count / 2
let part1, part2 = splitList halfCount cards
List.zip part1 part2 |> List.map (fun x -> x |> convertToList) |> List.concat
let shuffledCards = dovetailShuffle cards
let FisherYatesShuffle (initialList : 'a array) =
let availableFlags = Array.init initialList.Length (fun i -> (i, true))
let rand = new System.Random()
let nextItem nLeft =
let nItem = rand.Next(0, nLeft)
let index =
availableFlags
|> Seq.filter (fun (ndx,f) -> f)
|> Seq.nth nItem
|> fst
availableFlags.[index] <- (index, false)
initialList.[index]
seq {(initialList.Length) .. -1 .. 1}
|> Seq.map (fun i -> nextItem i)
let shuffledCards = newDeck |> List.toArray |> FisherYatesShuffle |> Seq.toList
let newDeck =
[(Two, Spades); (Three, Spades); (Four, Spades); (Five, Spades);
(Six, Spades); (Seven, Spades); (Eight, Spades); (Nine, Spades);
(Ten, Spades); (Jack, Spades); (Queen, Spades); (King, Spades); (Ace, Spades);
(Two, Hearts); (Three, Hearts); (Four, Hearts); (Five, Hearts);
(Six, Hearts); (Seven, Hearts); (Eight, Hearts); (Nine, Hearts);
(Ten, Hearts); (Jack, Hearts); (Queen, Hearts); (King, Hearts); (Ace, Hearts);
(Two, Clubs); (Three, Clubs); (Four, Clubs); (Five, Clubs);
(Six, Clubs); (Seven, Clubs); (Eight, Clubs); (Nine, Clubs);
(Ten, Clubs); (Jack, Clubs); (Queen, Clubs); (King, Clubs); (Ace, Clubs);
(Two, Diamonds); (Three, Diamonds); (Four, Diamonds); (Five, Diamonds);
(Six, Diamonds); (Seven, Diamonds); (Eight, Diamonds); (Nine, Diamonds);
(Ten, Diamonds); (Jack, Diamonds); (Queen, Diamonds); (King, Diamonds); (Ace, Diamonds)]
let suits = [ Spades; Hearts; Clubs; Diamonds ]
let ranks = [ Two; Three; Four; Five; Six; Seven; Eight; Nine; Ten; Jack; Queen; King; Ace ]
let product ranks suits =
seq { for rank in ranks do for suit in suits do yield (rank, suit) } |> Seq.toList
let newDeck = product ranks suits
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment