Created
May 5, 2016 13:04
-
-
Save adamchalmers/586787fee3320c4d55ea560aa12e7fb0 to your computer and use it in GitHub Desktop.
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
module Cards where | |
import Random | |
-- DATA | |
type alias Card = {face: Face, suit: Suit} | |
type Suit = Clubs | Spades | Diamonds | Hearts | |
type Face = Ace | Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten | Jack | Queen | King | |
type alias Deck = List Card | |
intToFace : Int -> Face | |
intToFace n = | |
case n of | |
0 -> Ace | |
1 -> Two | |
2 -> Three | |
3 -> Four | |
4 -> Five | |
5 -> Six | |
6 -> Seven | |
7 -> Eight | |
8 -> Nine | |
9 -> Ten | |
10 -> Jack | |
11 -> Queen | |
_ -> King | |
intToSuit : Int -> Suit | |
intToSuit n = | |
case n of | |
0 -> Clubs | |
1 -> Spades | |
2 -> Diamonds | |
_ -> Hearts | |
genFace : Random.Generator Face | |
genFace = Random.map intToFace (Random.int 0 12) | |
genSuit : Random.Generator Suit | |
genSuit = Random.map intToSuit (Random.int 0 3) | |
-- CARD OPERATIONS | |
genCard : Random.Generator Card | |
genCard = Random.map2 (\f s -> {face = f, suit = s}) genFace genSuit | |
intToCard : Int -> Card | |
intToCard n = | |
let | |
q = n // 13 | |
r = n `rem` 13 | |
in | |
{face = intToFace r, suit = intToSuit q} | |
-- DECK OPERATIONS | |
sortedDeck : Deck | |
sortedDeck = List.map intToCard [0..51] | |
-- without returns the input list without its ith element. | |
without : Int -> List a -> List a | |
without i arr = | |
let before = List.take i arr | |
after = List.drop (i+1) arr | |
in | |
before ++ after | |
-- rndFrom removes a random card from the deck | |
rndFrom : Deck -> Random.Seed -> (Deck, Random.Seed) | |
rndFrom deck seed = | |
let | |
n = (List.length deck) - 1 | |
(i, s0) = Random.generate (Random.int 0 n) seed | |
in | |
(without i deck, s0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment