Last active
August 29, 2015 14:21
-
-
Save paralax/b90662d06e6567d63b4c to your computer and use it in GitHub Desktop.
prototype of texas hold 'em core in Elm
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
| import Basics | |
| import Graphics.Element exposing (..) | |
| import Graphics.Input.Field as Field | |
| import List exposing (..) | |
| import Random exposing (..) | |
| import String | |
| type Face = Two | Three | Four | Five | Six | Seven | Eight | Nine | Ten | Jack | Queen | King | Ace | Joker | |
| type Suit = Diamonds | Hearts | Clubs | Spades | Brand | |
| type alias Card = { suit:Suit, face:Face } | |
| {-- https://gist.github.com/TheSeamau5/07553149ba3d499e6436 --} | |
| shuffle : List a -> List a | |
| shuffle lt = | |
| let | |
| len = length lt | |
| fgen = float 0 1 | |
| lgen = list len fgen | |
| rlist = fst <| generate lgen (initialSeed 31415) | |
| total = sum rlist | |
| nlist = map (\x -> x / total) rlist | |
| tlist = zip lt nlist | |
| flist = sortBy snd tlist | |
| in | |
| fst <| unzip flist | |
| zip : List a -> List b -> List (a,b) | |
| zip l1 l2 = | |
| case l1 of | |
| [] -> [] | |
| x :: xs -> | |
| case l2 of | |
| [] -> [] | |
| y :: ys -> (x,y) :: zip xs ys | |
| makeDeck : List Card | |
| makeDeck = | |
| let | |
| suits = [ Diamonds, Hearts, Clubs, Spades ] |> List.map(\x -> List.repeat 13 x) |> List.concat | |
| cards = [ Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace ] |> List.repeat 4 |> List.concat | |
| deck = zip suits cards |> List.map(\(s,c) -> {suit=s, face=c}) | |
| in | |
| shuffle deck | |
| dealCards : List Card -> (List Card, List Card) | |
| dealCards cards = | |
| (List.take 2 cards, List.drop 2 cards) | |
| dealPlayer : Int -> List Card -> (List List Card, List Card) | |
| dealPlayer n cards = | |
| let | |
| loop n cards acc = | |
| case n of | |
| 0 -> (acc, cards) | |
| _ -> let | |
| (dealt,cards) = dealCards cards | |
| in | |
| loop (n-1) (dealt::acc) cards | |
| in | |
| loop n cards | |
| communityCard : List Card -> (Card, List Card) | |
| communityCard cards = | |
| let | |
| badcard = {suit=Brand, face=Joker} | |
| in | |
| (List.drop 1 cards |> List.head |> Maybe.withDefault badcard, List.drop 2 cards) | |
| stringCard : Card -> String | |
| stringCard card = (card.face |> toString) ++ " of " ++ (card.suit |> toString) | |
| getNumber : String -> Int | |
| getNumber s = String.toInt s |> Result.toMaybe |> Maybe.withDefault 0 | |
| content : Signal.Mailbox Field.Content | |
| content = | |
| Signal.mailbox Field.noContent | |
| main : Signal Element | |
| main = | |
| Signal.map scene content.signal | |
| scene : Field.Content -> Element | |
| scene fieldContent = | |
| let | |
| cards = makeDeck | |
| (mycards,cards') = dealCards | |
| (othercards,cards'') = dealPlayer (getNumber fieldContent.string) cards' | |
| (turn,cards''') = communityCard cards'' | |
| (river,cards'''') = communityCard cards''' | |
| in | |
| flow down | |
| [ Field.field Field.defaultStyle (Signal.message content.address) "Number of players?" fieldContent | |
| , show (fieldContent.string ++ " players") | |
| ] ++ | |
| [ show (List.map (\x -> stringCard x) mycards |> String.join " ")] ++ | |
| List.map (\x -> show (List.map (\c -> stringCard c) |> String.join " ")) othercards | |
| {-- TODO | |
| 1. UI support | |
| 2. testing | |
| --} |
Author
paralax
commented
May 26, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment