Skip to content

Instantly share code, notes, and snippets.

@Qata
Created March 3, 2016 22:30
Show Gist options
  • Save Qata/a4141d7f38f08d0e3d69 to your computer and use it in GitHub Desktop.
Save Qata/a4141d7f38f08d0e3d69 to your computer and use it in GitHub Desktop.
import Signal
import Graphics.Element exposing (..)
import Graphics.Input exposing (button)
import Graphics.Collage exposing (collage, toForm)
import List exposing (..)
import List.Extra exposing (..)
import Color
import Text
import Random
import Window
import String
import Task exposing (Task)
type alias Model =
{ seed : Random.Seed
, bingoTiles : List String
, windowWidth : Int
, windowHeight : Int
}
type Action = NoOp
| Shuffle
| UpdateWindowSize (Int, Int)
randomFold : String -> (List (String, Float), Random.Seed) -> (List (String, Float), Random.Seed)
randomFold value (l, s) =
let randomValue = Random.generate randomGenerator s
randomFloat = fst randomValue
seed = snd randomValue
in
((value, randomFloat) :: l, seed)
shuffle : Random.Seed -> List String -> List String
shuffle seed list =
foldr randomFold ([], seed) list
|> fst
|> sortBy snd
|> map fst
generateRandomSeed : Random.Seed -> Random.Seed
generateRandomSeed seed =
snd <| Random.generate randomGenerator seed
randomGenerator : Random.Generator Float
randomGenerator =
Random.float 0 <| toFloat (length initialNumbers)
initialNumbers : List String
initialNumbers =
[ "Always wants to fight"
, "Autistic"
, "Black and purple wardrobe"
, "Choker collar"
, "Communist"
, "Dollmaker games"
, "Furry"
, "Gay"
, "Linguist"
, "Loves the moon"
, "Loves weird animals"
, "Neon Genesis Evangelion"
, "Owns tabletop simulator"
, "Owns thigh high socks"
, "Plays competetive smash"
, "Polyamorous"
, "Programmer"
, "Scene phase"
, "Slime"
, "Tired"
, "Too much salt"
, "Wants to be a robot"
, "Went on /d/"
, "Would date an alien"
, "Kiss kiss fall in love"
]
main =
Signal.map view model
update : Action -> Model -> Model
update action model =
case action of
NoOp ->
model
Shuffle ->
{ model
| bingoTiles = shuffle model.seed model.bingoTiles
, seed = generateRandomSeed model.seed
}
UpdateWindowSize size ->
{ model
| windowWidth = fst size
, windowHeight = snd size
}
model : Signal Model
model =
Signal.foldp
update
{ seed = Random.initialSeed 42
, bingoTiles = initialNumbers
, windowWidth = 0
, windowHeight = 0
}
actions.signal
view : Model -> Element
view model =
let boxWidth = 150
boxHeight = 150
boxSpacing = 10
bingoTiles =
indexedMap (\index string -> ((index % 5, index // 5), string)) (take 25 model.bingoTiles)
|> map (\((x, y), string) -> ((x, y), color Color.lightGrey (container boxWidth boxHeight middle (centered <| Text.height 10 (Text.fromString string)))))
|> map (\((x, y), element) -> container model.windowWidth model.windowHeight (middleAt (relative <| (toFloat x) * 0.08 + 0.34) (relative <| (toFloat y) * 0.14 + 0.2)) element)
-- |> map (\((x, y), element) -> container model.windowWidth model.windowHeight (middleAt (absolute <| x * (boxWidth + boxSpacing)) (absolute <| y * (boxHeight + boxSpacing))) element)
in
collage model.windowWidth model.windowHeight
(map toForm
([ ] ++
bingoTiles ++
[ container model.windowWidth model.windowHeight (midBottomAt (relative 0.5) (absolute 80)) <| button (Signal.message actions.address Shuffle) "Shuffle"
]))
actions : Signal.Mailbox Action
actions =
Signal.mailbox NoOp
port windowSizeUpdate : Signal (Task x ())
port windowSizeUpdate =
Signal.map UpdateWindowSize Window.dimensions
|> Signal.map (Signal.send actions.address)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment