Created
October 23, 2018 15:57
-
-
Save lincore81/7e65cf95954ed0cac97f29ee4f565c4e to your computer and use it in GitHub Desktop.
My first attempt to create a real thing in haskell (wip)
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
data Suit | |
= Diamonds | |
| Hearts | |
| Clubs | |
| Spades | |
deriving (Eq, Ord, Enum, Show) | |
data Rank = Two | Three | Four | Five | Six | Seven | Eight | |
| Nine | Ten | Jack | Queen | King | Ace | |
deriving (Eq, Ord, Enum) | |
instance Show Rank where | |
show Two = "2" | |
show Three = "3" | |
show Four = "4" | |
show Five = "5" | |
show Six = "6" | |
show Seven = "7" | |
show Eight = "8" | |
show Nine = "9" | |
show Ten = "10" | |
show Jack = "Jack" | |
show Queen = "Queen" | |
show King = "King" | |
show Ace = "Ace" | |
data Card = Card Rank Suit deriving (Eq, Ord) | |
instance Show Card where | |
show (Card r s) = concat [show r, " of ", show s] | |
rankvalue :: Rank -> [Int] | |
rankvalue Ace = [1, 11] | |
rankvalue r | |
| num <= 10 = pure num | |
| otherwise = pure 10 | |
where num = fromEnum r | |
cardvalue :: Card -> [Int] | |
cardvalue (Card r _) = rankvalue r | |
deckvalue :: Deck -> [Int] | |
deckvalue = map cardvalue | |
where | |
n = 2 | |
f card = case cardvalue card of v | |
| len | |
type Seed = Int | |
type Deck = [Card] | |
-- player dealer deck stakes cash | |
data BlackJackState = State Deck Deck Deck Int Int deriving (Show, Ord, Eq) | |
data Player = Player | Dealer | |
data Action | |
= Deal Player Int | |
| RaiseStake Int | |
standardDeck :: Deck | |
standardDeck = [Card r s | | |
r <- enumFromTo Two Ace, | |
s <- [s | s <- enumFromTo Diamonds Spades]] | |
getHand :: Player BlackJackState -> Deck | |
getHand Player (State deck _ _ _ _) = deck | |
getHand Dealer (State _ deck _ _ _) = deck | |
getDeck :: BlackJackState -> Deck | |
getDeck (State _ _ deck _ _) = deck | |
updateHand :: Player -> Deck -> BlackJackState -> BlackJackState | |
updateHand Player deck (State _ b c d e) = State deck b c d e | |
updateHand Dealer deck (State a _ c d e) = State a deck c d e | |
updateDeck :: Deck -> BlackJackState -> BlackJackState | |
updateDeck deck (State a b _ d e) = State a b deck d e | |
deal :: Player -> Int -> BlackJackState -> Maybe BlackJackState | |
deal player n state = case player of | |
Player -> | |
where | |
act :: Action -> BlackJackState -> BlackJackState | |
act |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment