Last active
September 20, 2017 07:18
-
-
Save ApprenticeGC/a2e13dc7dacca0af7532eb3308efe86e to your computer and use it in GitHub Desktop.
Continue to make Crazy Eights game after reading blog.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let deal cards = | |
| match cards with | |
| | head::list -> (Some head, list) | |
| | [] -> (None, []) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Deck = Card list |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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