Skip to content

Instantly share code, notes, and snippets.

@paralax
Last active August 29, 2015 14:21
Show Gist options
  • Save paralax/b90662d06e6567d63b4c to your computer and use it in GitHub Desktop.
Save paralax/b90662d06e6567d63b4c to your computer and use it in GitHub Desktop.
prototype of texas hold 'em core in Elm
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
--}
@paralax
Copy link
Author

paralax commented May 26, 2015

Error in 216easy.elm:

Type mismatch between the following types between lines 92 and 97:

        appendable

        Graphics.Element.Element

    Looks like you want something appendable, but the only Strings, Lists,
and Text can be appended with the (++) operator.
    It is related to the following expression:

        (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))

Type mismatch between the following types on line 97, column 82 to 92:

        List List

        List

    It is related to the following expression:

        othercards

Type mismatch between the following types on line 97, column 31 to 79:

        List Main.Card -> List String

        List String

    It is related to the following expression:

        (List.map (\c -> stringCard c)) |> (String.join " ")

Type mismatch between the following types on line 87, column 28 to 37:

        List Main.Card -> (List Main.Card, List Main.Card)

        (a, b)

    It is related to the following expression:

        dealCards

Type mismatch between the following types between lines 51 and 60:

        Main.Card

        List Main.Card

    It is related to the following expression:

        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

Type mismatch between the following types between lines 53 and 59:

        Main.Card

        List Main.Card

    It is related to the following expression:

        case n of
          0 -> (acc,cards)
          _ -> let (dealt,cards) = dealCards cards
               in loop (n - 1) (dealt :: acc) cards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment